-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator_view.js
125 lines (108 loc) · 3.54 KB
/
calculator_view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var CalculatorView = Backbone.View.extend({
initialize: function(options) {
this.calculator = options.calculator;
this.enteredNumber = undefined;
this.render();
_.bindAll(this, "renderStack", "renderMemoryIndicator", "keyDown", "keyUp");
this.calculator.bind("change:stack", this.renderStack);
this.calculator.bind("change:memory", this.renderMemoryIndicator);
$(document).keydown(this.keyDown).keyup(this.keyUp);
},
events: {
"click button" : "buttonPressed"
},
render: function() {
this.renderStack();
this.renderMemoryIndicator();
},
renderStack: function() {
var values = this.calculator.stackValues();
// show at least 4 rows
while (values.length < 4) values.unshift("");
var html = _.map(values, function(value) {
return "<li>"+value+"</li>";
}).join("\n");
this.$("#stack-list").html(html);
var $stack = this.$("#stack");
$stack.attr({scrollTop: $stack.attr("scrollHeight") });
},
renderMemoryIndicator: function() {
// check if memory is set or not
if (this.calculator.isMemoryEmpty()) {
this.$("#memory-indicator").hide();
} else {
this.$("#memory-indicator").show();
}
},
renderEnteredNumber: function() {
this.$("#stack-list li:last").html(this.enteredNumber || "0");
},
buttonPressed: function(e) {
var $target = $(e.target),
name = ($target.attr("name"));
this.buttonEvent(name);
},
buttonEvent: function(name) {
// digit or decimal point
if (name.match(/\d|\./)) {
this.numberEntered(name);
} else if (name == 'clear') {
this.clear();
} else {
this.operation(name);
}
},
keyCodeToButton: {
48: "0", 49: "1", 50: "2", 51: "3", 52: "4", 53: "5", 54: "6", 55: "7", 56: "8", 57: "9", 190: ".",
96: "0", 97: "1", 98: "2", 99: "3", 100: "4", 101: "5", 102: "6", 103: "7", 104: "8", 105: "9", 110: ".",
13: "enter", 27: "clear", 8: "drop", 46: "drop",
107: "add", 109: "subtract", 189: "subtract", 106: "multiply", 111: "divide", 191: "divide"
},
shiftKeyCodeToButton: {
107: "add", 187: "add", 56: "multiply"
},
keyDown: function(e) {
var keyCode = e.originalEvent.keyCode,
name = this._shiftKey ? this.shiftKeyCodeToButton[keyCode] : this.keyCodeToButton[keyCode];
if (keyCode == 16) {
this._shiftKey = true;
} else if (name) {
this.buttonEvent(name);
e.preventDefault();
}
},
keyUp: function(e) {
if (e.originalEvent.keyCode == 16) this._shiftKey = false;
},
numberEntered: function(number) {
if (typeof this.enteredNumber === "undefined") {
if (this._enterOnNewNumber) {
this.calculator.enter();
this._enterOnNewNumber = false;
}
this.enteredNumber = "";
}
// if decimal point already entered then do nothing
if (number == '.' && this.enteredNumber.indexOf('.') >= 0) return;
this.enteredNumber += number;
this.renderEnteredNumber();
},
clear: function() {
this.enteredNumber = undefined;
this._enterOnNewNumber = false;
this.calculator.accumulator(0);
},
operation: function(name) {
// set accumulator to entered number
if (this.enteredNumber) {
this.calculator.accumulator(parseFloat(this.enteredNumber));
this.enteredNumber = undefined;
}
this.calculator[name]();
if (_.include(['add','subtract','multiply','divide','negate'], name)) {
this._enterOnNewNumber = true;
} else if (_.include(['swap','rollDown','rollUp','drop'], name)) {
this._enterOnNewNumber = false;
}
}
});