This repository was archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 865
/
Copy pathinit-calculator.js
243 lines (220 loc) · 7.67 KB
/
init-calculator.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
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
/* TODO(csilvers): fix these lint errors (http://eslint.org/docs/rules): */
/* eslint-disable no-var */
/* To fix, remove an entry above, run ka-lint, and fix errors. */
/*global Calculator, Exercises, _, i18n, icu*/
Calculator.init = function() {
var ansChars = ["+", "-", "/", "*", "^", " "];
var calculator = $(".calculator");
var history = calculator.children(".history");
var output = $("#calc-output-content");
var inputRow = history.children(".calc-row.input");
var input = inputRow.children("input");
var buttons = calculator.find("a");
var previousInstrs = [];
var currentInstrIndex = -1;
var ans = 0;
var prevAnswer;
var containsAns = false;
var separator = icu.getDecimalFormatSymbols().decimal_separator;
var formatInputHistory = function(text) {
return text.replace(/pi/g, "\u03c0") + " =";
};
var appendDiv = function(div) {
output.append(div);
output.scrollTop(output[0].scrollHeight);
};
var insertPrevAnswer = function() {
var outdiv;
if (prevAnswer !== undefined) {
outdiv = $("<div>").addClass("output").text(prevAnswer);
prevAnswer = undefined;
appendDiv(outdiv);
}
};
var evaluate = function() {
var instr = input.val();
var indiv;
var output;
var outstr;
var isError = false;
var newInputVal = instr;
if ($.trim(instr) !== "") {
previousInstrs.unshift(instr);
indiv = $("<div>").addClass("input-history")
.text(formatInputHistory(instr));
try {
if (separator !== ".") {
// i18nize the input numbers' decimal point
instr = instr.split(separator).join(".");
}
output = ans = Calculator.calculate(instr, ans);
if (typeof output === "number") {
outstr = Math.round(output * 1000000000) / 1000000000;
if (separator !== ".") {
// i18nize the output number's decimal point
outstr = ("" + outstr).replace(".", separator);
}
} else {
outstr = output;
}
newInputVal = outstr;
} catch (e) {
if (e instanceof Calculator.CalculatorError) {
outstr = e.message;
newInputVal = instr;
isError = true;
containsAns = false;
input.css({
backgroundColor: "#ffcccc",
});
return;
} else {
throw e;
}
}
insertPrevAnswer();
appendDiv(indiv);
prevAnswer = outstr;
// errors should appear immediately
if (isError) {
insertPrevAnswer();
}
}
containsAns = true;
currentInstrIndex = -1;
input.val(newInputVal);
};
var selected = function(text) {
return "<span class='selected-anglemode'>" + text + "</span>";
};
var unselected = function(text) {
return "<span class='unselected-anglemode'>" + text + "</span>";
};
var updateAngleMode = function() {
// I18N: "DEGrees" calculator button (3 chars or less)
var deg = i18n._("DEG");
// I18N: "RADians" calculator button (3 chars or less)
var rad = i18n._("RAD");
if (Calculator.settings.angleMode === "DEG") {
$(".calculator-angle-mode").html(unselected(rad) +
"<br>" +
selected(deg));
} else {
$(".calculator-angle-mode").html(selected(rad) +
"<br>" +
unselected(deg));
}
};
// backspace etc isn't caught by keypress...
var BACKSPACE = 8;
var LEFT = 37;
var RIGHT = 39;
var UP = 38;
var DOWN = 40;
var keysToCancel = [LEFT, RIGHT];
input.on("keydown", function(e) {
if (_.contains(keysToCancel, e.keyCode)) {
containsAns = false;
}
if (e.which === BACKSPACE) {
if (containsAns) {
input.val("");
insertPrevAnswer();
return false;
}
}
if (e.which === UP) {
insertPrevAnswer();
currentInstrIndex += 1;
if (currentInstrIndex >= previousInstrs.length) {
currentInstrIndex = previousInstrs.length - 1;
}
input.val(previousInstrs[currentInstrIndex]);
return false;
}
if (e.which === DOWN) {
insertPrevAnswer();
currentInstrIndex -= 1;
if (currentInstrIndex < -1) {
currentInstrIndex = -1;
}
input.val(previousInstrs[currentInstrIndex] || ans);
return false;
}
});
var insertText = function(inputtedChar) {
var shouldOverwriteAns = !_.contains(ansChars, inputtedChar) &&
containsAns;
insertPrevAnswer();
containsAns = false;
if (shouldOverwriteAns) {
input.val("");
}
input.css({
backgroundColor: "white",
});
};
history.on("click", function(e) {
input.focus();
});
// The enter handler needs to bind to keypress to prevent the
// surrounding form submit... (http://stackoverflow.com/a/587575)
var ENTER = 13;
var EQUALS = 61;
input.on("keypress", function(e) {
if (e.which === ENTER || e.which === EQUALS) {
evaluate();
return false;
}
insertText(String.fromCharCode(e.charCode));
});
input.on("click", function(e) {
containsAns = false;
});
buttons.on("click", function() {
var jel = $(this);
var behavior = jel.data("behavior");
if (behavior != null) {
if (behavior === "bs") {
var val = input.val();
input.val(val.slice(0, val.length - 1));
} else if (behavior === "clear") {
input.val("");
ans = undefined;
prevAnswer = undefined;
previousInstrs = [];
currentInstrIndex = -1;
containsAns = false;
output.empty();
} else if (behavior === "angle-mode") {
Calculator.settings.angleMode =
Calculator.settings.angleMode === "DEG" ?
"RAD" : "DEG";
if (typeof window.localStorage !== "undefined") {
var userID = window.KA && window.KA.userId;
window.localStorage["calculator_settings:" +
userID] = JSON.stringify(
Calculator.settings);
}
updateAngleMode();
} else if (behavior === "evaluate") {
evaluate();
}
} else {
var text = jel.data("text") || jel.text();
insertText(text);
input.val(input.val() + text);
}
input.focus();
return false;
});
updateAngleMode();
if (typeof Exercises !== "undefined") {
$(Exercises).on("gotoNextProblem", function() {
input.val("");
output.children().not(inputRow).remove();
});
}
// i18nize the decimal point button
$(".calculator-decimal").html(separator);
};