-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator2.js
More file actions
146 lines (129 loc) · 5.46 KB
/
calculator2.js
File metadata and controls
146 lines (129 loc) · 5.46 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
$(document).ready(function(){
var Calculator = {
operCodes: [42, 43, 45, 47, 37],
numCodes: [48, 49, 50, 51, 52, 53, 54, 55, 56, 57],
numButtons: $('span.button-cell'),
clrButton: $('span.clear'),
eqlButton: $('span.equals'),
eqDiv: $('.equation'),
pstDiv: $('.past'),
rsltDiv: $('.results'),
calcBod: $('.calc-body'),
dispDiv: $('.display'),
calBodMod: {'padding':'0', 'margin':'0'},
dispMod: {'padding': '0', 'margin': '15px'},
smTxtMod: {'padding': '0', 'margin': '0 15px', 'font-size': '1em', 'line-height': '100%', 'height': '25%'},
eqMod: {'font-size': '1.5em', 'vertical-align': 'middle', 'height': '50%', 'padding': '0'},
buttonMod: {'padding': '0'},
stndButton: {'line-height': '100px', 'font-size': '24px', 'vertical-align': 'middle', 'height': '100px'},
numClick: function() {
this.numButtons.click((function(event){
var element = event.currentTarget;
this.eqDiv.append($(element).html());
this.rsltDiv.html(this.calculate(this.eqDiv.html()));
}).bind(this));
},
equalsFunc: function() {
if (this.rsltDiv.html() == undefined){
eq = this.eqDiv.html();
this.eqDiv.html(this.calculate(eq));
} else {
this.pstDiv.html(this.eqDiv.html());
this.eqDiv.html(this.rsltDiv.html());
}
},
clearFunc: function() {
this.eqDiv.html('');
this.rsltDiv.html('');
},
fullClear: function() {
this.clearFunc();
this.pstDiv.html('');
},
typeDisp: function() {
$(document).keypress((function(event) {
if(this.operCodes.indexOf(event.which) != -1) {
this.eqDiv.append(String.fromCharCode(event.which));
} else if (this.numCodes.indexOf(event.which) != -1) {
this.eqDiv.append(String.fromCharCode(event.which));
this.rsltDiv.html(this.calculate(this.eqDiv.text()));
} else if (event.which == 99) {
this.clearFunc();
} else if (event.which == 67) {
this.fullClear();
} else if (event.which == 13) {
this.equalsFunc();
}
}).bind(this));
},
chkInput: function(input) {
for (var i = 0; i < input.length; i++) {
if (input[0].match(/[\/\+\*\%]/)) {
return
} else if ((input[i].match(/[\/\+\-\*\%]/)) && (input[i + 1].match(/[\/\+\-\*\%]/))) {
return
}
}
},
calculate: function(input) {
this.chkInput(input);
lastInput = input.length - 1;
if (input[lastInput].match(/\d/)) {
answer = eval(input);
}
return answer;
},
calcInit: function() {
this.numClick();
this.typeDisp();
this.eqlButton.click(this.equalsFunc.bind(this));
this.clrButton.click(this.clearFunc.bind(this));
this.clrButton.dblclick(this.fullClear.bind(this));
this.resizeDim();
this.chkSetDims();
},
calcElmChg: function(target, modObj, parentH, numRow) {
var cellH = (parentH / numRow) + 'px' // calculates cell-height in pixels
var fntSz = cellH / 2 + 'px' // Calculates font-size as 50% of cell height
target.height(cellH); // Sets whatever target element height to cell-height
target.css(modObj); // Sets the modification object styling to target element
target.css({'line-height': "#{cellH}", 'font-size': "#{fntSz}", 'vertical-align': 'middle'}); // Sets font-sizes and aligns them in the middle.
},
chkSetDims: function() {
if ($(window).height() < 800) {
winHeight = $(window).height();
this.calcBod.height(winHeight + 'px');
this.calcBod.css(this.calBodMod);
// buttons
this.calcElmChg(this.numButtons, this.buttonMod, winHeight, 6);
this.calcElmChg(this.clrButton, this.buttonMod, winHeight, 6);
this.calcElmChg(this.eqlButton, this.buttonMod, winHeight, 6);
// display portion
dispDivHeight = this.dispDiv.height((winHeight / 6));
this.dispDiv.height(dispDivHeight + 'px');
this.calcElmChg(this.eqDiv, this.eqMod, dispDivHeight, 2);
this.calcElmChg(this.rsltDiv, this.smTxtMod, dispDivHeight, 4);
this.calcElmChg(this.pstDiv, this.smTxtMod, dispDivHeight, 4);
if (winHeight < 300) {
this.rsltDiv.css('display', 'none');
this.pstDiv.css('display', 'none');
}
} else if ($(window).height() >= 800) {
this.calcBod.css({'height': '600px', 'width': '400px'});
this.numButtons.css(this.stndButton);
this.clrButton.css(this.stndButton);
this.eqlButton.css({'font-size': '24px', 'width': '100%'});
this.dispDiv.css('height', '100px');
this.eqDiv.css({'height': '50px', 'font-size': '30px'});
this.pstDiv.css({'height': '25px', 'font-size': '16px'});
this.rsltDiv.css({'height': '25px', 'font-size': '16px'});
}
},
resizeDim: function() {
$(window).resize((function(event) {
this.chkSetDims();
}).bind(this));
}
};
Calculator.calcInit();
});