| 1 |
define([ |
| 2 |
"dojo/_base/declare", |
| 3 |
"dojo/_base/lang", |
| 4 |
"dojo/_base/sniff", |
| 5 |
"dojo/_base/window", |
| 6 |
"dojo/_base/event", |
| 7 |
"dojo/dom-style", |
| 8 |
"dojo/ready", |
| 9 |
"dojo/keys", |
| 10 |
"dijit/registry", |
| 11 |
"dijit/typematic", |
| 12 |
"dijit/_WidgetBase", |
| 13 |
"dijit/_WidgetsInTemplateMixin", |
| 14 |
"dijit/_TemplatedMixin", |
| 15 |
"dijit/form/_TextBoxMixin", |
| 16 |
"dojox/math/_base", |
| 17 |
"dijit/TooltipDialog", |
| 18 |
"dojo/text!./templates/Standard.html", |
| 19 |
"dojox/calc/_Executor", |
| 20 |
"dijit/Menu", |
| 21 |
"dijit/MenuItem", |
| 22 |
"dijit/form/ComboButton", |
| 23 |
"dijit/form/Button", |
| 24 |
"dijit/form/TextBox" |
| 25 |
], function(declare, lang, has, win, event, domStyle, ready, keys, registry, typematic, WidgetBase, WidgetsInTemplateMixin, TemplatedMixin, _TextBoxMixin, math, TooltipDialog, template, calc){ |
| 26 |
|
| 27 |
return declare( |
| 28 |
"dojox.calc.Standard", |
| 29 |
[WidgetBase, TemplatedMixin, WidgetsInTemplateMixin], |
| 30 |
{ |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
templateString: template, |
| 35 |
|
| 36 |
readStore:null, |
| 37 |
writeStore:null, |
| 38 |
functions: [], |
| 39 |
|
| 40 |
executorLoaded: function(){ |
| 41 |
|
| 42 |
|
| 43 |
ready(lang.hitch(this, function(){ |
| 44 |
this.loadStore(this.readStore, true); |
| 45 |
this.loadStore(this.writeStore); |
| 46 |
})); |
| 47 |
}, |
| 48 |
|
| 49 |
saveFunction: function(name, args, body){ |
| 50 |
|
| 51 |
|
| 52 |
this.functions[name] = this.executor.normalizedFunction(name, args, body); |
| 53 |
this.functions[name].args = args; |
| 54 |
this.functions[name].body = body; |
| 55 |
}, |
| 56 |
|
| 57 |
loadStore: function(store, isReadOnly){ |
| 58 |
|
| 59 |
|
| 60 |
if(!store){ |
| 61 |
return; |
| 62 |
} |
| 63 |
store.query({}).forEach(lang.hitch(this, function(item){ |
| 64 |
lang.hitch(this, isReadOnly ? this.executor.normalizedFunction : this.saveFunction)(item.name, item.args, item.body); |
| 65 |
})); |
| 66 |
}, |
| 67 |
|
| 68 |
parseTextbox: function(){ |
| 69 |
|
| 70 |
|
| 71 |
var text = this.textboxWidget.textbox.value; |
| 72 |
if(text == "" && this.commandList.length > 0){ |
| 73 |
this.setTextboxValue(this.textboxWidget, this.commandList[this.commandList.length-1]); |
| 74 |
text = this.textboxWidget.textbox.value; |
| 75 |
} |
| 76 |
if(text!=""){ |
| 77 |
var ans = this.executor.eval(text); |
| 78 |
|
| 79 |
if((typeof ans == "number" && isNaN(ans))){ |
| 80 |
if(this.commandList.length == 0 || this.commandList[this.commandList.length - 1] != text){ |
| 81 |
this.commandList.push(text); |
| 82 |
} |
| 83 |
this.print(text, false); |
| 84 |
this.print("Not a Number", true); |
| 85 |
}else if(((typeof ans == "object" && "length" in ans) || typeof ans != "object") && typeof ans != "function" && ans != null){ |
| 86 |
this.executor.eval("Ans="+ans); |
| 87 |
|
| 88 |
if(this.commandList.length == 0 || this.commandList[this.commandList.length - 1] != text){ |
| 89 |
this.commandList.push(text); |
| 90 |
} |
| 91 |
this.print(text, false); |
| 92 |
this.print(ans, true); |
| 93 |
} |
| 94 |
this.commandIndex = this.commandList.length-1; |
| 95 |
|
| 96 |
if(this.hasDisplay){ |
| 97 |
this.displayBox.scrollTop=this.displayBox.scrollHeight; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
_TextBoxMixin.selectInputText(this.textboxWidget.textbox); |
| 102 |
|
| 103 |
}else{ |
| 104 |
this.textboxWidget.focus(); |
| 105 |
} |
| 106 |
}, |
| 107 |
cycleCommands: function(count, node, event){ |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
if(count == -1 || this.commandList.length==0){ |
| 112 |
return; |
| 113 |
} |
| 114 |
var keyNum = event.charOrCode; |
| 115 |
|
| 116 |
if(keyNum == keys.UP_ARROW){ |
| 117 |
this.cycleCommandUp(); |
| 118 |
}else if(keyNum == keys.DOWN_ARROW){ |
| 119 |
this.cycleCommandDown(); |
| 120 |
} |
| 121 |
}, |
| 122 |
cycleCommandUp: function(){ |
| 123 |
|
| 124 |
|
| 125 |
if(this.commandIndex-1<0){ |
| 126 |
this.commandIndex=0; |
| 127 |
}else{ |
| 128 |
this.commandIndex--; |
| 129 |
} |
| 130 |
this.setTextboxValue(this.textboxWidget, this.commandList[this.commandIndex]); |
| 131 |
}, |
| 132 |
cycleCommandDown: function(){ |
| 133 |
|
| 134 |
|
| 135 |
if(this.commandIndex+1>=this.commandList.length){ |
| 136 |
this.commandIndex=this.commandList.length; |
| 137 |
this.setTextboxValue(this.textboxWidget, ""); |
| 138 |
}else{ |
| 139 |
this.commandIndex++; |
| 140 |
this.setTextboxValue(this.textboxWidget, this.commandList[this.commandIndex]); |
| 141 |
} |
| 142 |
|
| 143 |
}, |
| 144 |
onBlur: function(){ |
| 145 |
|
| 146 |
|
| 147 |
if(has('ie')){ |
| 148 |
var tr = win.doc.selection.createRange().duplicate(); |
| 149 |
var selectedText = tr.text || ''; |
| 150 |
var ntr = this.textboxWidget.textbox.createTextRange(); |
| 151 |
tr.move("character",0); |
| 152 |
ntr.move("character",0); |
| 153 |
try{ |
| 154 |
ntr.setEndPoint("EndToEnd", tr); |
| 155 |
this.textboxWidget.textbox.selectionEnd = (this.textboxWidget.textbox.selectionStart = String(ntr.text).replace(/\r/g,"").length) + selectedText.length; |
| 156 |
|
| 157 |
}catch(e){} |
| 158 |
} |
| 159 |
}, |
| 160 |
onKeyPress: function(e){ |
| 161 |
|
| 162 |
|
| 163 |
if(e.charOrCode == keys.ENTER){ |
| 164 |
this.parseTextbox(); |
| 165 |
|
| 166 |
event.stop(e); |
| 167 |
}else if(e.charOrCode == '!' || e.charOrCode == '^' || e.charOrCode == '*' || e.charOrCode == '/' || e.charOrCode == '-' || e.charOrCode == '+'){ |
| 168 |
if(has('ie')){ |
| 169 |
var tr = win.doc.selection.createRange().duplicate(); |
| 170 |
var selectedText = tr.text || ''; |
| 171 |
var ntr = this.textboxWidget.textbox.createTextRange(); |
| 172 |
tr.move("character",0); |
| 173 |
ntr.move("character",0); |
| 174 |
try{ |
| 175 |
ntr.setEndPoint("EndToEnd", tr); |
| 176 |
this.textboxWidget.textbox.selectionEnd = (this.textboxWidget.textbox.selectionStart = String(ntr.text).replace(/\r/g,"").length) + selectedText.length; |
| 177 |
|
| 178 |
}catch(e){} |
| 179 |
} |
| 180 |
|
| 181 |
if(this.textboxWidget.get("value")==""){ |
| 182 |
this.setTextboxValue(this.textboxWidget, "Ans"); |
| 183 |
}else if(this.putInAnsIfTextboxIsHighlighted(this.textboxWidget.textbox, event.charOrCode)){ |
| 184 |
this.setTextboxValue(this.textboxWidget, "Ans"); |
| 185 |
|
| 186 |
_TextBoxMixin.selectInputText(this.textboxWidget.textbox, this.textboxWidget.textbox.value.length, this.textboxWidget.textbox.value.length); |
| 187 |
} |
| 188 |
} |
| 189 |
}, |
| 190 |
insertMinus: function(){ |
| 191 |
|
| 192 |
|
| 193 |
this.insertText('-'); |
| 194 |
}, |
| 195 |
print: function(text, isRight){ |
| 196 |
|
| 197 |
|
| 198 |
var t = "<span style='display:block;"; |
| 199 |
if(isRight){ |
| 200 |
t += "text-align:right;'>"; |
| 201 |
}else{ |
| 202 |
t += "text-align:left;'>"; |
| 203 |
} |
| 204 |
t += text+"<br></span>"; |
| 205 |
if(this.hasDisplay){ |
| 206 |
this.displayBox.innerHTML += t; |
| 207 |
}else{ |
| 208 |
this.setTextboxValue(this.textboxWidget, text); |
| 209 |
} |
| 210 |
|
| 211 |
}, |
| 212 |
setTextboxValue: function(widget, val){ |
| 213 |
|
| 214 |
|
| 215 |
widget.set('value', val); |
| 216 |
}, |
| 217 |
putInAnsIfTextboxIsHighlighted: function(node){ |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
if(typeof node.selectionStart == "number"){ |
| 223 |
if(node.selectionStart==0 && node.selectionEnd == node.value.length){ |
| 224 |
|
| 225 |
|
| 226 |
return true; |
| 227 |
} |
| 228 |
}else if(document.selection){ |
| 229 |
|
| 230 |
var range = document.selection.createRange(); |
| 231 |
|
| 232 |
if(node.value == range.text){ |
| 233 |
|
| 234 |
return true; |
| 235 |
} |
| 236 |
} |
| 237 |
return false; |
| 238 |
}, |
| 239 |
clearText: function(){ |
| 240 |
|
| 241 |
|
| 242 |
if(this.hasDisplay && this.textboxWidget.get('value')==""){ |
| 243 |
this.displayBox.innerHTML = ""; |
| 244 |
}else{ |
| 245 |
this.setTextboxValue(this.textboxWidget, ""); |
| 246 |
} |
| 247 |
this.textboxWidget.focus(); |
| 248 |
}, |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
insertOperator: function(newText){ |
| 259 |
|
| 260 |
|
| 261 |
if(typeof newText == "object"){ |
| 262 |
newText = newText = registry.getEnclosingWidget(newText["target"]).value; |
| 263 |
} |
| 264 |
if(this.textboxWidget.get("value") == "" || this.putInAnsIfTextboxIsHighlighted(this.textboxWidget.textbox)){ |
| 265 |
newText = "Ans"+newText; |
| 266 |
} |
| 267 |
this.insertText(newText); |
| 268 |
}, |
| 269 |
insertText: function(newText){ |
| 270 |
|
| 271 |
|
| 272 |
setTimeout(lang.hitch(this, function(){ |
| 273 |
|
| 274 |
var node = this.textboxWidget.textbox; |
| 275 |
if(node.value==""){ |
| 276 |
node.selectionStart = 0; |
| 277 |
node.selectionEnd = 0; |
| 278 |
} |
| 279 |
if(typeof newText == "object"){ |
| 280 |
newText = newText = registry.getEnclosingWidget(newText["target"]).value; |
| 281 |
} |
| 282 |
|
| 283 |
var value = node.value.replace(/\r/g,''); |
| 284 |
if(typeof node.selectionStart == "number"){ |
| 285 |
var pos = node.selectionStart; |
| 286 |
var cr = 0; |
| 287 |
if(has('opera')){ |
| 288 |
cr = (node.value.substring(0,pos).match(/\r/g) || []).length; |
| 289 |
} |
| 290 |
node.value = value.substring(0, node.selectionStart-cr) + newText + value.substring(node.selectionEnd-cr); |
| 291 |
node.focus(); |
| 292 |
pos += newText.length; |
| 293 |
|
| 294 |
_TextBoxMixin.selectInputText(this.textboxWidget.textbox, pos, pos); |
| 295 |
}else if(document.selection){ |
| 296 |
if(this.handle){ |
| 297 |
clearTimeout(this.handle); |
| 298 |
this.handle = null; |
| 299 |
} |
| 300 |
node.focus(); |
| 301 |
this.handle = setTimeout(function(){ |
| 302 |
var range = document.selection.createRange(); |
| 303 |
range.text = newText; |
| 304 |
|
| 305 |
range.select(); |
| 306 |
this.handle = null; |
| 307 |
}, 0); |
| 308 |
|
| 309 |
} |
| 310 |
}), 0); |
| 311 |
}, |
| 312 |
hasDisplay: false, |
| 313 |
postCreate: function(){ |
| 314 |
|
| 315 |
|
| 316 |
this.handle = null; |
| 317 |
this.commandList = []; |
| 318 |
this.commandIndex = 0; |
| 319 |
|
| 320 |
if(this.displayBox){ |
| 321 |
this.hasDisplay = true; |
| 322 |
} |
| 323 |
if(this.toFracButton && !calc.toFrac){ |
| 324 |
domStyle.set(this.toFracButton.domNode, { visibility: "hidden" }); |
| 325 |
} |
| 326 |
if(this.functionMakerButton && !calc.FuncGen){ |
| 327 |
domStyle.set(this.functionMakerButton.domNode, { visibility: "hidden" }); |
| 328 |
} |
| 329 |
if(this.grapherMakerButton && !calc.Grapher){ |
| 330 |
domStyle.set(this.grapherMakerButton.domNode, { visibility: "hidden" }); |
| 331 |
} |
| 332 |
this._connects.push(typematic.addKeyListener(this.textboxWidget.textbox, |
| 333 |
{ |
| 334 |
charOrCode:keys.UP_ARROW, |
| 335 |
shiftKey:false, |
| 336 |
metaKey:false, |
| 337 |
ctrlKey:false |
| 338 |
}, |
| 339 |
this, this.cycleCommands, 200, 200)); |
| 340 |
this._connects.push(typematic.addKeyListener(this.textboxWidget.textbox, |
| 341 |
{ |
| 342 |
charOrCode:keys.DOWN_ARROW, |
| 343 |
shiftKey:false, |
| 344 |
metaKey:false, |
| 345 |
ctrlKey:false |
| 346 |
}, |
| 347 |
this, this.cycleCommands, 200, 200)); |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
this.startup() |
| 354 |
} |
| 355 |
}); |
| 356 |
|
| 357 |
}); |