Standard.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.calc.Standard"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.calc.Standard"] = true;
  8. dojo.provide("dojox.calc.Standard");
  9. dojo.require("dijit._Templated");
  10. dojo.require("dojox.math._base");
  11. dojo.require("dijit.dijit");
  12. dojo.require("dijit.Menu");
  13. dojo.require("dijit.form.DropDownButton");
  14. dojo.require("dijit.TooltipDialog");
  15. dojo.require("dijit.form.TextBox");
  16. dojo.require("dijit.form.Button");
  17. dojo.require("dojox.calc._Executor");
  18. dojo.experimental("dojox.calc.Standard");
  19. dojo.declare(
  20. "dojox.calc.Standard",
  21. [dijit._Widget, dijit._Templated],
  22. {
  23. // summary:
  24. // The dialog layout for a standard 4 function/algebraic calculator
  25. //
  26. templateString: dojo.cache("dojox.calc", "templates/Standard.html", "<div class=\"dijitReset dijitInline dojoxCalc\"\n><table class=\"dijitReset dijitInline dojoxCalcLayout\" dojoAttachPoint=\"calcTable\" rules=\"none\" cellspacing=0 cellpadding=0 border=0>\n\t<tr\n\t\t><td colspan=\"4\" class=\"dojoxCalcInputContainer\"\n\t\t\t><input dojoType=\"dijit.form.TextBox\" dojoAttachEvent=\"onBlur:onBlur,onKeyPress:onKeyPress\" dojoAttachPoint='textboxWidget'\n\t\t/></td\n\t></tr>\n\t<tr>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"seven\" label=\"7\" value='7' dojoAttachEvent='onClick:insertText' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"eight\" label=\"8\" value='8' dojoAttachEvent='onClick:insertText' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"nine\" label=\"9\" value='9' dojoAttachEvent='onClick:insertText' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"divide\" label=\"/\" value='/' dojoAttachEvent='onClick:insertOperator' />\n\t\t</td>\n\t</tr>\n\t<tr>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"four\" label=\"4\" value='4' dojoAttachEvent='onClick:insertText' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"five\" label=\"5\" value='5' dojoAttachEvent='onClick:insertText' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"six\" label=\"6\" value='6' dojoAttachEvent='onClick:insertText' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"multiply\" label=\"*\" value='*' dojoAttachEvent='onClick:insertOperator' />\n\t\t</td>\n\t</tr>\n\t<tr>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"one\" label=\"1\" value='1' dojoAttachEvent='onClick:insertText' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"two\" label=\"2\" value='2' dojoAttachEvent='onClick:insertText' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"three\" label=\"3\" value='3' dojoAttachEvent='onClick:insertText' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"add\" label=\"+\" value='+' dojoAttachEvent='onClick:insertOperator' />\n\t\t</td>\n\t</tr>\n\t<tr>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"decimal\" label=\".\" value='.' dojoAttachEvent='onClick:insertText' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"zero\" label=\"0\" value='0' dojoAttachEvent='onClick:insertText' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"equals\" label=\"x=y\" value='=' dojoAttachEvent='onClick:insertText' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcMinusButtonContainer\">\n\t\t\t<span dojoType=\"dijit.form.ComboButton\" dojoAttachPoint=\"subtract\" label='-' value='-' dojoAttachEvent='onClick:insertOperator'>\n\n\t\t\t\t<div dojoType=\"dijit.Menu\" style=\"display:none;\">\n\t\t\t\t\t<div dojoType=\"dijit.MenuItem\" dojoAttachEvent=\"onClick:insertMinus\">\n\t\t\t\t\t\t(-)\n\t\t\t\t\t</div>\n\t\t\t\t</div>\n\t\t\t</span>\n\t\t</td>\n\t</tr>\n\t<tr>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"clear\" label=\"Clear\" dojoAttachEvent='onClick:clearText' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"sqrt\" label=\"&#x221A;\" value=\"&#x221A;\" dojoAttachEvent='onClick:insertText' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"power\" label=\"^\" value=\"^\" dojoAttachEvent='onClick:insertOperator' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"comma\" label=\",\" value=',' dojoAttachEvent='onClick:insertText' />\n\t\t</td>\n\t</tr>\n\t<tr>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"AnsButton\" label=\"Ans\" value=\"Ans\" dojoAttachEvent='onClick:insertText' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"LeftParenButton\" label=\"(\" value=\"(\" dojoAttachEvent='onClick:insertText' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"RightParenButton\" label=\")\" value=\")\" dojoAttachEvent='onClick:insertText' />\n\t\t</td>\n\t\t<td class=\"dojoxCalcButtonContainer\">\n\t\t\t<button dojoType=\"dijit.form.Button\" dojoAttachPoint=\"enter\" label=\"Enter\" dojoAttachEvent='onClick:parseTextbox' />\n\t\t</td>\n\t</tr>\n</table>\n<span dojoAttachPoint=\"executor\" dojoType=\"dojox.calc._Executor\" dojoAttachEvent=\"onLoad:executorLoaded\"></span>\n</div>\n"),
  27. readStore:null,
  28. writeStore:null,
  29. functions: [],
  30. widgetsInTemplate:true,
  31. executorLoaded: function(){
  32. // summary
  33. // load in the stores after executor is loaded (the stores need executor to be loaded because it parses them)
  34. dojo.addOnLoad(dojo.hitch(this, function(){
  35. this.loadStore(this.readStore, true);
  36. this.loadStore(this.writeStore);
  37. }));
  38. },
  39. saveFunction: function(name, args, body){
  40. // summary
  41. // make the function with executor
  42. this.functions[name] = this.executor.normalizedFunction(name, args, body);
  43. this.functions[name].args = args;
  44. this.functions[name].body = body;
  45. },
  46. loadStore: function(store, isReadOnly){
  47. // summary
  48. // load an entire store, and make it publicly editable/viewable based on isReadOnly
  49. function saveFunctions(items){
  50. for(var i = 0; i < items.length; i++){
  51. this.saveFunction(items[i].name[0], items[i].args[0], items[i].body[0]);
  52. }
  53. }
  54. function saveReadOnlyFunctions(items){
  55. // make the function
  56. for(var i = 0; i < items.length; i++){
  57. this.executor.normalizedFunction(items[i].name[0], items[i].args[0], items[i].body[0]);
  58. }
  59. }
  60. if(store==null){
  61. return;
  62. }
  63. if(isReadOnly){
  64. store.fetch({
  65. onComplete: dojo.hitch(this, saveReadOnlyFunctions),
  66. onError: function(text){console.error(text)}
  67. });
  68. }else{
  69. store.fetch({
  70. onComplete: dojo.hitch(this, saveFunctions),
  71. onError: function(text){console.error(text)}
  72. });
  73. }
  74. },
  75. parseTextbox: function(){
  76. // summary
  77. // parse the contents of the textboxWidget and display the answer somewhere (depending on the layout)
  78. var text = this.textboxWidget.textbox.value;
  79. if(text == "" && this.commandList.length > 0){
  80. this.setTextboxValue(this.textboxWidget, this.commandList[this.commandList.length-1]);
  81. text = this.textboxWidget.textbox.value;
  82. }
  83. if(text!=""){
  84. var ans = this.executor.eval(text);
  85. if((typeof ans == "number" && isNaN(ans))){
  86. if(this.commandList.length == 0 || this.commandList[this.commandList.length - 1] != text){
  87. this.commandList.push(text);
  88. }
  89. this.print(text, false);
  90. this.print("Not a Number", true);
  91. }else if(((typeof ans == "object" && "length" in ans) || typeof ans != "object") && typeof ans != "function" && ans != null){
  92. this.executor.eval("Ans="+ans);
  93. // add it to the command list as well
  94. if(this.commandList.length == 0 || this.commandList[this.commandList.length - 1] != text){
  95. this.commandList.push(text);
  96. }
  97. this.print(text, false);
  98. this.print(ans, true);
  99. }
  100. this.commandIndex = this.commandList.length-1;
  101. //this.displayBox.textbox.scrollTop=this.displayBox.textbox.scrollHeight;
  102. if(this.hasDisplay){
  103. this.displayBox.scrollTop=this.displayBox.scrollHeight;
  104. }
  105. //this.clearText();
  106. //this.textboxWidget.focus();
  107. dijit.selectInputText(this.textboxWidget.textbox);
  108. }else{
  109. this.textboxWidget.focus();
  110. }
  111. },
  112. cycleCommands: function(count, node, event){
  113. // summary
  114. // cycle through the commands that the user has entered
  115. // it does not wrap around
  116. if(count == -1 || this.commandList.length==0){
  117. return;
  118. }
  119. var keyNum = event.charOrCode;
  120. //up arrow
  121. if(keyNum == dojo.keys.UP_ARROW){
  122. this.cycleCommandUp();
  123. }else if(keyNum == dojo.keys.DOWN_ARROW){
  124. this.cycleCommandDown();
  125. }
  126. },
  127. cycleCommandUp: function(){
  128. // summary
  129. // cycle up through the list of commands the user has entered already
  130. if(this.commandIndex-1<0){
  131. this.commandIndex=0;
  132. }else{
  133. this.commandIndex--;
  134. }
  135. this.setTextboxValue(this.textboxWidget, this.commandList[this.commandIndex]);
  136. },
  137. cycleCommandDown: function(){
  138. // summary
  139. // cycle down through the list of commands the user has entered already
  140. if(this.commandIndex+1>=this.commandList.length){
  141. this.commandIndex=this.commandList.length;
  142. this.setTextboxValue(this.textboxWidget, "");
  143. }else{
  144. this.commandIndex++;
  145. this.setTextboxValue(this.textboxWidget, this.commandList[this.commandIndex]);
  146. }
  147. },
  148. onBlur: function(){
  149. // summary
  150. // 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
  151. if(dojo.isIE){
  152. var tr = dojo.doc.selection.createRange().duplicate();
  153. var selectedText = tr.text || '';
  154. var ntr = this.textboxWidget.textbox.createTextRange();
  155. tr.move("character",0);
  156. ntr.move("character",0);
  157. try{
  158. ntr.setEndPoint("EndToEnd", tr);
  159. this.textboxWidget.textbox.selectionEnd = (this.textboxWidget.textbox.selectionStart = String(ntr.text).replace(/\r/g,"").length) + selectedText.length;
  160. }catch(e){}
  161. }
  162. },
  163. onKeyPress: function(event){
  164. // summary
  165. // handle key input for Enter and operators
  166. if(event.charOrCode == dojo.keys.ENTER){
  167. this.parseTextbox();
  168. // stop form submissions
  169. dojo.stopEvent(event);
  170. }else if(event.charOrCode == '!' || event.charOrCode == '^' || event.charOrCode == '*' || event.charOrCode == '/' || event.charOrCode == '-' || event.charOrCode == '+'){
  171. if(dojo.isIE){
  172. var tr = dojo.doc.selection.createRange().duplicate();
  173. var selectedText = tr.text || '';
  174. var ntr = this.textboxWidget.textbox.createTextRange();
  175. tr.move("character",0);
  176. ntr.move("character",0);
  177. try{
  178. ntr.setEndPoint("EndToEnd", tr);
  179. this.textboxWidget.textbox.selectionEnd = (this.textboxWidget.textbox.selectionStart = String(ntr.text).replace(/\r/g,"").length) + selectedText.length;
  180. }catch(e){}
  181. }
  182. if(this.textboxWidget.get("value")==""){
  183. this.setTextboxValue(this.textboxWidget, "Ans");
  184. }else if(this.putInAnsIfTextboxIsHighlighted(this.textboxWidget.textbox, event.charOrCode)){
  185. this.setTextboxValue(this.textboxWidget, "Ans");//this.insertText("Ans");
  186. // move the cursor to the end of "Ans"
  187. dijit.selectInputText(this.textboxWidget.textbox, this.textboxWidget.textbox.value.length, this.textboxWidget.textbox.value.length);
  188. }
  189. }
  190. },
  191. insertMinus: function(){
  192. // summary
  193. // insert a minus sign when they press (-) in the combo button
  194. this.insertText('-');
  195. },
  196. print: function(text, isRight){
  197. // summary
  198. // print the answer (typically) to the display or the input box
  199. var t = "<span style='display:block;";
  200. if(isRight){
  201. t += "text-align:right;'>";
  202. }else{
  203. t += "text-align:left;'>";
  204. }
  205. t += text+"<br></span>";
  206. if(this.hasDisplay){
  207. this.displayBox.innerHTML += t;
  208. }else{// if there is not a display box, put the answer in the input box
  209. this.setTextboxValue(this.textboxWidget, text);
  210. }
  211. //this.setTextboxValue(this.displayBox, this.displayBox.get('value')+'\n'+text);
  212. },
  213. setTextboxValue: function(widget, val){
  214. // summary
  215. // set a widget's value
  216. widget.set('value', val);
  217. },
  218. putInAnsIfTextboxIsHighlighted: function(node){
  219. // summary
  220. // try seeing if the textbox is highlighted completely so you know if Ans should be put in for an operator like +
  221. //console.log("Entered "+node.selectionStart + " "+ node.selectionEnd);
  222. if(typeof node.selectionStart == "number"){ // not-IE
  223. if(node.selectionStart==0 && node.selectionEnd == node.value.length){
  224. //node.value = "Ans";
  225. //dijit.selectInputText(node, node.value.length, node.value.length);
  226. return true;
  227. }
  228. }else if(document.selection){ // IE
  229. //console.log("Entered 2");
  230. var range = document.selection.createRange();
  231. //console.log("Range: "+range.text +" Node: "+node.value);
  232. if(node.value == range.text){
  233. //this.insertText("Ans");
  234. return true;
  235. }
  236. }
  237. return false;
  238. },
  239. clearText: function(){
  240. // summary
  241. // this clears the input box if it has content, but if it does not it clears the display
  242. if(this.hasDisplay && this.textboxWidget.get('value')==""){
  243. this.displayBox.innerHTML = "";//this.setTextboxValue(this.displayBox, "");
  244. }else{
  245. this.setTextboxValue(this.textboxWidget, "");
  246. }
  247. this.textboxWidget.focus();
  248. },
  249. /*insertMinusSign: function(){
  250. //
  251. var v = this.subtract.get('label');
  252. if(v != '(-)' && this.putInAnsIfTextboxIsHighlighted(this.textboxWidget.textbox)){
  253. this.insertText("Ans-");
  254. return;
  255. }
  256. this.insertText('-');
  257. },*/
  258. insertOperator: function(newText){
  259. // summary
  260. // insert an operator with a button
  261. if(typeof newText == "object"){
  262. newText = newText = dijit.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){//(node, newText){
  270. // summary
  271. // insert text to the textboxWidget node
  272. setTimeout(dojo.hitch(this, function(){
  273. var node = this.textboxWidget.textbox;
  274. if(node.value==""){
  275. node.selectionStart = 0;
  276. node.selectionEnd = 0;
  277. }
  278. if(typeof newText == "object"){
  279. newText = newText = dijit.getEnclosingWidget(newText["target"]).value;
  280. }
  281. var value = node.value.replace(/\r/g,'');
  282. if(typeof node.selectionStart == "number"){ // not-IE
  283. var pos = node.selectionStart;
  284. var cr = 0;
  285. if(navigator.userAgent.indexOf("Opera") != -1){ // if(dojo.isOpera){
  286. cr = (node.value.substring(0,pos).match(/\r/g) || []).length;
  287. }
  288. node.value = value.substring(0, node.selectionStart-cr) + newText + value.substring(node.selectionEnd-cr);
  289. node.focus();
  290. pos += newText.length;
  291. //node.setSelectionRange(pos, pos);
  292. dijit.selectInputText(this.textboxWidget.textbox, pos, pos);
  293. }else if(document.selection){ // IE
  294. if(this.handle){
  295. clearTimeout(this.handle);
  296. this.handle = null;
  297. }
  298. node.focus();
  299. this.handle = setTimeout(function(){
  300. var range = document.selection.createRange();
  301. range.text = newText;
  302. // show cursor
  303. range.select();
  304. this.handle = null;
  305. }, 0);
  306. }
  307. }), 0);
  308. },
  309. hasDisplay: false,
  310. postCreate: function(){
  311. // summary
  312. // run startup, see if there is an upper display box, etc
  313. this.handle = null;
  314. this.commandList = [];
  315. this.commandIndex = 0;
  316. if(this.displayBox){
  317. this.hasDisplay = true;
  318. }
  319. if(this.toFracButton && !dojox.calc.toFrac){
  320. dojo.style(this.toFracButton.domNode, { visibility: "hidden" });
  321. }
  322. if(this.functionMakerButton && !dojox.calc.FuncGen){
  323. dojo.style(this.functionMakerButton.domNode, { visibility: "hidden" });
  324. }
  325. if(this.grapherMakerButton && !dojox.calc.Grapher){
  326. dojo.style(this.grapherMakerButton.domNode, { visibility: "hidden" });
  327. }
  328. this._connects.push(dijit.typematic.addKeyListener(this.textboxWidget.textbox,
  329. {
  330. charOrCode:dojo.keys.UP_ARROW,
  331. shiftKey:false,
  332. metaKey:false,
  333. ctrlKey:false // ALT is optional since its unspecified
  334. },
  335. this, this.cycleCommands, 200, 200));
  336. this._connects.push(dijit.typematic.addKeyListener(this.textboxWidget.textbox,
  337. {
  338. charOrCode:dojo.keys.DOWN_ARROW,
  339. shiftKey:false,
  340. metaKey:false,
  341. ctrlKey:false // ALT is optional since its unspecified
  342. },
  343. this, this.cycleCommands, 200, 200));
  344. //onClick="this.insertText(document.getElementById('textbox'), '\u221A')"
  345. //this.sqrt.set("onClick", dojo.hitch(this, "insertText", this.textboxWidget, '\u221A'));
  346. //this.pi.set("onClick", dojo.hitch(this, "insertText", this.textboxWidget, '\u03C0'));
  347. this.startup()
  348. }
  349. });
  350. }