forked from dojo/dojox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStandard.js
357 lines (336 loc) · 11.8 KB
/
Standard.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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
define([
"dojo/_base/declare",
"dojo/_base/lang",
"dojo/_base/sniff",
"dojo/_base/window",
"dojo/_base/event",
"dojo/dom-style",
"dojo/ready",
"dojo/keys",
"dijit/registry",
"dijit/typematic",
"dijit/_WidgetBase",
"dijit/_WidgetsInTemplateMixin",
"dijit/_TemplatedMixin",
"dijit/form/_TextBoxMixin",
"dojox/math/_base",
"dijit/TooltipDialog",
"dojo/text!./templates/Standard.html",
"dojox/calc/_Executor", // template
"dijit/Menu", // template
"dijit/MenuItem", // template
"dijit/form/ComboButton", // template
"dijit/form/Button", // template
"dijit/form/TextBox" // template
], function(declare, lang, has, win, event, domStyle, ready, keys, registry, typematic, WidgetBase, WidgetsInTemplateMixin, TemplatedMixin, _TextBoxMixin, math, TooltipDialog, template, calc){
return declare(
"dojox.calc.Standard",
[WidgetBase, TemplatedMixin, WidgetsInTemplateMixin],
{
// summary:
// The dialog layout for a standard 4 function/algebraic calculator
templateString: template,
readStore:null,
writeStore:null,
functions: [],
executorLoaded: function(){
// summary:
// load in the stores after executor is loaded (the stores need executor to be loaded because it parses them)
ready(lang.hitch(this, function(){
this.loadStore(this.readStore, true);
this.loadStore(this.writeStore);
}));
},
saveFunction: function(name, args, body){
// summary:
// make the function with executor
this.functions[name] = this.executor.normalizedFunction(name, args, body);
this.functions[name].args = args;
this.functions[name].body = body;
},
loadStore: function(store, isReadOnly){
// summary:
// load an entire store, and make it publicly editable/viewable based on isReadOnly
if(!store){
return;
}
store.query({}).forEach(lang.hitch(this, function(item){
lang.hitch(this, isReadOnly ? this.executor.normalizedFunction : this.saveFunction)(item.name, item.args, item.body);
}));
},
parseTextbox: function(){
// summary:
// parse the contents of the textboxWidget and display the answer somewhere (depending on the layout)
var text = this.textboxWidget.textbox.value;
if(text == "" && this.commandList.length > 0){
this.setTextboxValue(this.textboxWidget, this.commandList[this.commandList.length-1]);
text = this.textboxWidget.textbox.value;
}
if(text!=""){
var ans = this.executor.eval(text);
if((typeof ans == "number" && isNaN(ans))){
if(this.commandList.length == 0 || this.commandList[this.commandList.length - 1] != text){
this.commandList.push(text);
}
this.print(text, false);
this.print("Not a Number", true);
}else if(((typeof ans == "object" && "length" in ans) || typeof ans != "object") && typeof ans != "function" && ans != null){
this.executor.eval("Ans="+ans);
// add it to the command list as well
if(this.commandList.length == 0 || this.commandList[this.commandList.length - 1] != text){
this.commandList.push(text);
}
this.print(text, false);
this.print(ans, true);
}
this.commandIndex = this.commandList.length-1;
//this.displayBox.textbox.scrollTop=this.displayBox.textbox.scrollHeight;
if(this.hasDisplay){
this.displayBox.scrollTop=this.displayBox.scrollHeight;
}
//this.clearText();
//this.textboxWidget.focus();
_TextBoxMixin.selectInputText(this.textboxWidget.textbox);
}else{
this.textboxWidget.focus();
}
},
cycleCommands: function(count, node, event){
// summary:
// Cycle through the commands that the user has entered.
// It does not wrap around.
if(count == -1 || this.commandList.length==0){
return;
}
var keyNum = event.charOrCode;
//up arrow
if(keyNum == keys.UP_ARROW){
this.cycleCommandUp();
}else if(keyNum == keys.DOWN_ARROW){
this.cycleCommandDown();
}
},
cycleCommandUp: function(){
// summary:
// cycle up through the list of commands the user has entered already
if(this.commandIndex-1<0){
this.commandIndex=0;
}else{
this.commandIndex--;
}
this.setTextboxValue(this.textboxWidget, this.commandList[this.commandIndex]);
},
cycleCommandDown: function(){
// summary:
// cycle down through the list of commands the user has entered already
if(this.commandIndex+1>=this.commandList.length){
this.commandIndex=this.commandList.length;
this.setTextboxValue(this.textboxWidget, "");
}else{
this.commandIndex++;
this.setTextboxValue(this.textboxWidget, this.commandList[this.commandIndex]);
}
},
onBlur: function(){
// summary:
// IE is lacking in function when it comes to the text boxes, so here, make it work like other browsers do by forcing a node.selectionStart and End onto it
if(has('ie')){
var tr = win.doc.selection.createRange().duplicate();
var selectedText = tr.text || '';
var ntr = this.textboxWidget.textbox.createTextRange();
tr.move("character",0);
ntr.move("character",0);
try{
ntr.setEndPoint("EndToEnd", tr);
this.textboxWidget.textbox.selectionEnd = (this.textboxWidget.textbox.selectionStart = String(ntr.text).replace(/\r/g,"").length) + selectedText.length;
}catch(e){}
}
},
onKeyPress: function(e){
// summary:
// handle key input for Enter and operators
if(e.charOrCode == keys.ENTER){
this.parseTextbox();
// stop form submissions
event.stop(e);
}else if(e.charOrCode == '!' || e.charOrCode == '^' || e.charOrCode == '*' || e.charOrCode == '/' || e.charOrCode == '-' || e.charOrCode == '+'){
if(has('ie')){
var tr = win.doc.selection.createRange().duplicate();
var selectedText = tr.text || '';
var ntr = this.textboxWidget.textbox.createTextRange();
tr.move("character",0);
ntr.move("character",0);
try{
ntr.setEndPoint("EndToEnd", tr);
this.textboxWidget.textbox.selectionEnd = (this.textboxWidget.textbox.selectionStart = String(ntr.text).replace(/\r/g,"").length) + selectedText.length;
}catch(e){}
}
if(this.textboxWidget.get("value")==""){
this.setTextboxValue(this.textboxWidget, "Ans");
}else if(this.putInAnsIfTextboxIsHighlighted(this.textboxWidget.textbox, event.charOrCode)){
this.setTextboxValue(this.textboxWidget, "Ans");//this.insertText("Ans");
// move the cursor to the end of "Ans"
_TextBoxMixin.selectInputText(this.textboxWidget.textbox, this.textboxWidget.textbox.value.length, this.textboxWidget.textbox.value.length);
}
}
},
insertMinus: function(){
// summary:
// insert a minus sign when they press (-) in the combo button
this.insertText('-');
},
print: function(text, isRight){
// summary:
// print the answer (typically) to the display or the input box
var t = "<span style='display:block;";
if(isRight){
t += "text-align:right;'>";
}else{
t += "text-align:left;'>";
}
t += text+"<br></span>";
if(this.hasDisplay){
this.displayBox.innerHTML += t;
}else{// if there is not a display box, put the answer in the input box
this.setTextboxValue(this.textboxWidget, text);
}
//this.setTextboxValue(this.displayBox, this.displayBox.get('value')+'\n'+text);
},
setTextboxValue: function(widget, val){
// summary:
// set a widget's value
widget.set('value', val);
},
putInAnsIfTextboxIsHighlighted: function(node){
// summary:
// try seeing if the textbox is highlighted completely so you know if Ans should be put in for an operator like +
//console.log("Entered "+node.selectionStart + " "+ node.selectionEnd);
if(typeof node.selectionStart == "number"){ // not-IE
if(node.selectionStart==0 && node.selectionEnd == node.value.length){
//node.value = "Ans";
//dijit.selectInputText(node, node.value.length, node.value.length);
return true;
}
}else if(document.selection){ // IE
//console.log("Entered 2");
var range = document.selection.createRange();
//console.log("Range: "+range.text +" Node: "+node.value);
if(node.value == range.text){
//this.insertText("Ans");
return true;
}
}
return false;
},
clearText: function(){
// summary:
// this clears the input box if it has content, but if it does not it clears the display
if(this.hasDisplay && this.textboxWidget.get('value')==""){
this.displayBox.innerHTML = "";//this.setTextboxValue(this.displayBox, "");
}else{
this.setTextboxValue(this.textboxWidget, "");
}
this.textboxWidget.focus();
},
/*insertMinusSign: function(){
//
var v = this.subtract.get('label');
if(v != '(-)' && this.putInAnsIfTextboxIsHighlighted(this.textboxWidget.textbox)){
this.insertText("Ans-");
return;
}
this.insertText('-');
},*/
insertOperator: function(newText){
// summary:
// insert an operator with a button
if(typeof newText == "object"){
newText = newText = registry.getEnclosingWidget(newText["target"]).value;
}
if(this.textboxWidget.get("value") == "" || this.putInAnsIfTextboxIsHighlighted(this.textboxWidget.textbox)){
newText = "Ans"+newText;
}
this.insertText(newText);
},
insertText: function(newText){//(node, newText){
// summary:
// insert text to the textboxWidget node
setTimeout(lang.hitch(this, function(){
var node = this.textboxWidget.textbox;
if(node.value==""){
node.selectionStart = 0;
node.selectionEnd = 0;
}
if(typeof newText == "object"){
newText = newText = registry.getEnclosingWidget(newText["target"]).value;
}
var value = node.value.replace(/\r/g,'');
if(typeof node.selectionStart == "number"){ // not-IE
var pos = node.selectionStart;
var cr = 0;
if(has('opera')){
cr = (node.value.substring(0,pos).match(/\r/g) || []).length;
}
node.value = value.substring(0, node.selectionStart-cr) + newText + value.substring(node.selectionEnd-cr);
node.focus();
pos += newText.length;
//node.setSelectionRange(pos, pos);
_TextBoxMixin.selectInputText(this.textboxWidget.textbox, pos, pos);
}else if(document.selection){ // IE
if(this.handle){
clearTimeout(this.handle);
this.handle = null;
}
node.focus();
this.handle = setTimeout(function(){
var range = document.selection.createRange();
range.text = newText;
// show cursor
range.select();
this.handle = null;
}, 0);
}
}), 0);
},
hasDisplay: false,
postCreate: function(){
// summary:
// run startup, see if there is an upper display box, etc
this.handle = null;
this.commandList = [];
this.commandIndex = 0;
if(this.displayBox){
this.hasDisplay = true;
}
if(this.toFracButton && !calc.toFrac){
domStyle.set(this.toFracButton.domNode, { visibility: "hidden" });
}
if(this.functionMakerButton && !calc.FuncGen){
domStyle.set(this.functionMakerButton.domNode, { visibility: "hidden" });
}
if(this.grapherMakerButton && !calc.Grapher){
domStyle.set(this.grapherMakerButton.domNode, { visibility: "hidden" });
}
this._connects.push(typematic.addKeyListener(this.textboxWidget.textbox,
{
charOrCode:keys.UP_ARROW,
shiftKey:false,
metaKey:false,
ctrlKey:false // ALT is optional since its unspecified
},
this, this.cycleCommands, 200, 200));
this._connects.push(typematic.addKeyListener(this.textboxWidget.textbox,
{
charOrCode:keys.DOWN_ARROW,
shiftKey:false,
metaKey:false,
ctrlKey:false // ALT is optional since its unspecified
},
this, this.cycleCommands, 200, 200));
//onClick="this.insertText(document.getElementById('textbox'), '\u221A')"
//this.sqrt.set("onClick", lang.hitch(this, "insertText", this.textboxWidget, '\u221A'));
//this.pi.set("onClick", lang.hitch(this, "insertText", this.textboxWidget, '\u03C0'));
this.startup()
}
});
});