Standard.js 18 KB

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