-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalculator.html
More file actions
324 lines (277 loc) · 7.65 KB
/
calculator.html
File metadata and controls
324 lines (277 loc) · 7.65 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<!-- http://thecodeplayer.com/walkthrough/javascript-css3-calculator -->
<html>
<head>
<style>
/* Basic reset */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
/* Better text styling */
font: bold 14px Arial, sans-serif;
}
/* Finally adding some IE9 fallbacks for gradients to finish things up */
/* A nice BG gradient */
html {
height: 100%;
background: white;
background: radial-gradient(circle, #fff 20%, #ccc);
background-size: cover;
}
/* Using box shadows to create 3D effects */
#calculator {
width: 325px;
height: auto;
margin: 100px auto;
padding: 20px 20px 9px;
background: #9dd2ea;
background: linear-gradient(#9dd2ea, #8bceec);
border-radius: 3px;
box-shadow: 0px 4px #009de4, 0px 10px 15px rgba(0, 0, 0, 0.2);
}
/* Top portion */
.top span.clear {
float: left;
}
/* Inset shadow on the screen to create indent */
.top .screen {
height: 40px;
width: 212px;
float: right;
padding: 0 10px;
background: rgba(0, 0, 0, 0.2);
border-radius: 3px;
box-shadow: inset 0px 4px rgba(0, 0, 0, 0.2);
/* Typography */
font-size: 17px;
line-height: 40px;
color: white;
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
text-align: right;
letter-spacing: 1px;
}
/* Clear floats */
.keys, .top {overflow: hidden;}
/* Applying same to the keys */
.keys span, .top span.clear {
float: left;
position: relative;
top: 0;
cursor: pointer;
width: 66px;
height: 36px;
background: white;
border-radius: 3px;
box-shadow: 0px 4px rgba(0, 0, 0, 0.2);
margin: 0 7px 11px 0;
color: #888;
line-height: 36px;
text-align: center;
/* prevent selection of text inside keys */
user-select: none;
/* Smoothing out hover and active states using css3 transitions */
transition: all 0.2s ease;
}
/* Remove right margins from operator keys */
/* style different type of keys (operators/evaluate/clear) differently */
.keys span.operator {
background: #FFF0F5;
margin-right: 0;
}
.keys span.eval {
background: #f1ff92;
box-shadow: 0px 4px #9da853;
color: #888e5f;
}
.top span.clear {
background: #ff9fa8;
box-shadow: 0px 4px #ff7c87;
color: white;
}
/* Some hover effects */
.keys span:hover {
background: #9c89f6;
box-shadow: 0px 4px #6b54d3;
color: white;
}
.keys span.eval:hover {
background: #abb850;
box-shadow: 0px 4px #717a33;
color: #ffffff;
}
.top span.clear:hover {
background: #f68991;
box-shadow: 0px 4px #d3545d;
color: white;
}
/* Simulating "pressed" effect on active state of the keys by removing the box-shadow and moving the keys down a bit */
.keys span:active {
box-shadow: 0px 0px #6b54d3;
top: 4px;
}
.keys span.eval:active {
box-shadow: 0px 0px #717a33;
top: 4px;
}
.top span.clear:active {
top: 4px;
box-shadow: 0px 0px #d3545d;
}
.keys span.disabled {
pointer-events: none;
box-shadow: 0px 4px #6b54d3 !important;
opacity: 0.5;
}
</style>
<script
src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous"></script>
</head>
<body>
<div id="calculator">
<!-- Screen and clear key -->
<div class="top">
<span class="clear">C</span>
<div class="screen"></div>
</div>
<div class="keys">
<!-- operators and other keys -->
<span>7</span>
<span>8</span>
<span>9</span>
<span class="operator">+</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span class="operator">-</span>
<span>1</span>
<span>2</span>
<span>3</span>
<span class="operator">÷</span>
<span>0</span>
<span>.</span>
<span class="eval">=</span>
<span class="operator">x</span>
</div>
</div>
<script>
// Get all the keys from document
var keys = document.querySelectorAll('#calculator span');
var operators = ['+', '-', 'x', '÷'];
var decimalAdded = false;
// Add onclick event to all the keys and perform operations
for(var i = 0; i < keys.length; i++) {
keys[i].onclick = function(e) {
// Get the input and button values
var input = document.querySelector('.screen');
var inputVal = input.innerHTML;
var btnVal = this.innerHTML;
var params = inputVal.split(/[\+\-\÷\x]+/); // split along math operators
var action = operatorText(getOperator(inputVal));
// Now, just append the key values (btnValue) to the input string and finally use ajax to get the result from the server
// If clear key is pressed, erase everything
if(btnVal == 'C') {
input.innerHTML = '';
}
// if we have no value, ignore operator press
else if(inputVal.length == 0 && containsAny(btnVal, operators)){
input.innerHTML = '';
}
// if we have no operator, and evaluation is required, return current val
else if(!containsAny(inputVal, operators) && btnVal == '='){
input.innerHTML = inputVal;
}
// limit length
else if(inputVal.length >= 16 && (btnVal != '=') && (!containsAny(btnVal, operators))){
alert("value too long")
}
// if its time to evaluate, we have a complete equation or '=' is pressed
else if(btnVal == '=' ||
(containsAny(btnVal, operators) && containsAny(inputVal, operators))){
$(".keys span").addClass('disabled'); // disable key pad
input.innerHTML = '...'
var a = params[0];
var b = params[1];
if(containsAny(inputVal.charAt(0), ['-', '+'])){ // handle negative numbers
a = inputVal.charAt(0) + params[1];
b = params[2];
}
$.ajax({
url: "/api/v1/"+action+"?a="+a+"&b="+b,
success: function(data) {
//called when successful
$(".keys span").removeClass('disabled'); // re-enable keypad
if(data.status == 200){
input.innerHTML = data.result
if(btnVal != "="){
// if any other key is pressed, just append it
input.innerHTML += btnVal;
}
} else {
alert(data.message);
input.innerHTML = '';
}
},
error: function(e) {
// called when there is an error
$(".keys span").removeClass('disabled'); // re-enable keypad
console.log(e);
alert((e.responseJSON && e.responseJSON.message ? e.responseJSON.message : "something went wrong"));
input.innerHTML = '';
}
});
}
// if any other key is pressed, just append it
else {
input.innerHTML += btnVal;
}
// prevent page jumps
e.preventDefault();
}
}
// ---------- Helper functions ----------
// check if string contains any value in substring array
function containsAny(string, substringArray){
for (var i = 0; i != substringArray.length; i++) {
var substring = substringArray[i];
if (string.indexOf(substring) != - 1) {
return true;
}
}
return false;
}
// get operator
function getOperator(string){
for (var i = 0; i != operators.length; i++) {
var operator = operators[i];
if (string.indexOf(operator) != - 1) {
return operator;
}
}
return false;
}
// get operator as text
function operatorText(operator){
switch (operator) {
case "+":
return "add";
break;
case "-":
return "subtract";
break;
case "x":
return "multiply";
break;
case "÷":
return "divide";
break;
default:
return "";
}
}
</script>
<!-- PrefixFree -->
<script src="http://thecodeplayer.com/uploads/js/prefixfree-1.0.7.js" type="text/javascript" type="text/javascript"></script>
</body>
</html>