FuncGen.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. require({cache:{
  2. 'url:dojox/calc/templates/FuncGen.html':"<div style=\"border:1px solid black;\">\n\t<select data-dojo-type=\"dijit.form.ComboBox\" placeholder=\"functionName\" data-dojo-attach-point='combo' style=\"width:45%;\" class=\"dojoxCalcFuncGenNameBox\" data-dojo-attach-event='onChange:onSelect'></select>\n\t<input data-dojo-type=\"dijit.form.TextBox\" placeholder=\"arguments\" class=\"dojoxCalcFuncGenTextBox\" style=\"width:50%;\" data-dojo-attach-point='args' />\n\t<BR>\n\t<TEXTAREA data-dojo-type=\"dijit.form.SimpleTextarea\" placeholder=\"function body\" class=\"dojoxCalcFuncGenTextArea\" style=\"text-align:left;width:95%;\" rows=10 data-dojo-attach-point='textarea' value=\"\" data-dojo-attach-event='onClick:readyStatus'></TEXTAREA>\n\t<BR>\n\t<input data-dojo-type=\"dijit.form.Button\" class=\"dojoxCalcFuncGenSave\" data-dojo-attach-point='saveButton' label=\"Save\" data-dojo-attach-event='onClick:onSaved' />\n\t<input data-dojo-type=\"dijit.form.Button\" class=\"dojoxCalcFuncGenReset\" data-dojo-attach-point='resetButton' label=\"Reset\" data-dojo-attach-event='onClick:onReset' />\n\t<input data-dojo-type=\"dijit.form.Button\" class=\"dojoxCalcFuncGenClear\" data-dojo-attach-point='clearButton' label=\"Clear\" data-dojo-attach-event='onClick:onClear' />\n\t<input data-dojo-type=\"dijit.form.Button\" class=\"dojoxCalcFuncGenClose\" data-dojo-attach-point='closeButton' label=\"Close\" />\n\t<BR><BR>\n\t<input data-dojo-type=\"dijit.form.Button\" class=\"dojoxCalcFuncGenDelete\" data-dojo-attach-point='deleteButton' label=\"Delete\" data-dojo-attach-event='onClick:onDelete' />\n\t<BR>\n\t<input data-dojo-type=\"dijit.form.TextBox\" style=\"width:45%;\" data-dojo-attach-point='status' class=\"dojoxCalcFuncGenStatusTextBox\" readonly value=\"Ready\" />\n</div>\n"}});
  3. define("dojox/calc/FuncGen", [
  4. "dojo/_base/declare",
  5. "dojo/_base/lang",
  6. "dojo/dom-style",
  7. "dijit/_WidgetBase",
  8. "dijit/_WidgetsInTemplateMixin",
  9. "dijit/_TemplatedMixin",
  10. "dojox/math/_base",
  11. "dijit/registry",
  12. "dojo/text!./templates/FuncGen.html",
  13. "dojox/calc/_Executor",
  14. "dijit/form/ComboBox", // template
  15. "dijit/form/SimpleTextarea", // template
  16. "dijit/form/Button", // template
  17. "dijit/form/TextBox" // template
  18. ], function(declare, lang, domStyle, WidgetBase, WidgetsInTemplateMixin, TemplatedMixin, math, registry, template, calc){
  19. /*=====
  20. WidgetBase = dijit._WidgetBase;
  21. WidgetsInTemplateMixin = dijit._WidgetsInTemplateMixin;
  22. TemplatedMixin = dijit._TemplatedMixin;
  23. =====*/
  24. var FuncGen = declare(
  25. "dojox.calc.FuncGen",
  26. [WidgetBase, TemplatedMixin, WidgetsInTemplateMixin],
  27. {
  28. // summary:
  29. // The dialog layout for making functions
  30. //
  31. templateString: template,
  32. onSelect: function(){
  33. // summary
  34. // if they select something in the name combobox, then change the body and arguments to correspond to the function they selected
  35. this.reset();
  36. },
  37. onClear: function(){
  38. // summary
  39. // the clear button in the template calls this
  40. // clear the name, arguments, and body if the user says yes
  41. var answer = confirm("Do you want to clear the name, argument, and body text?");
  42. if(answer){
  43. this.clear();
  44. }
  45. },
  46. saveFunction: function(name, args, body){
  47. // override me
  48. },
  49. onSaved: function(){
  50. // this on save needs to be overriden if you want Executor parsing support
  51. //console.log("Save was pressed");
  52. },
  53. clear: function(){
  54. // summary
  55. // clear the name, arguments, and body
  56. this.textarea.set("value", "");
  57. this.args.set("value", "");
  58. this.combo.set("value", "");
  59. },
  60. reset: function(){
  61. // summary
  62. // set the arguments and body to match a function selected if it exists in the function list
  63. if(this.combo.get("value") in this.functions){
  64. this.textarea.set("value", this.functions[this.combo.get("value")].body);
  65. this.args.set("value", this.functions[this.combo.get("value")].args);
  66. }
  67. },
  68. onReset: function(){
  69. // summary
  70. // (Reset button on click event) reset the arguments and body to their previously saved state if the user says yes
  71. //console.log("Reset was pressed");
  72. if(this.combo.get("value") in this.functions){
  73. var answer = confirm("Do you want to reset this function?");
  74. if(answer){
  75. this.reset();
  76. this.status.set("value", "The function has been reset to its last save point.");
  77. }
  78. }
  79. },
  80. deleteThing: function(item){
  81. // summary
  82. // delete an item in the writestore
  83. if(this.writeStore.isItem(item)){
  84. // delete it
  85. //console.log("Found item "+item);
  86. this.writeStore.deleteItem(item);
  87. this.writeStore.save();
  88. }else{
  89. //console.log("Unable to locate the item");
  90. }
  91. },
  92. deleteFunction: function(name){
  93. // override me
  94. },
  95. onDelete: function(){
  96. // summary
  97. // (Delete button on click event) delete a function if the user clicks yes
  98. //console.log("Delete was pressed");
  99. var name;
  100. if((name = this.combo.get("value")) in this.functions){
  101. var answer = confirm("Do you want to delete this function?");
  102. if(answer){
  103. var item = this.combo.item;
  104. //this.writeStore.fetchItemByIdentity({identity:name, onItem: this.deleteThing, onError:null});
  105. this.writeStore.deleteItem(item);
  106. this.writeStore.save();
  107. this.deleteFunction(name);
  108. delete this.functions[name];
  109. this.clear();
  110. }
  111. }else{
  112. this.status.set("value", "Function cannot be deleted, it isn't saved.");
  113. }
  114. },
  115. readyStatus: function(){
  116. // summary
  117. // set the status in the template to ready
  118. this.status.set("value", "Ready");
  119. },
  120. writeStore:null, //the user can save functions to the writestore
  121. readStore:null, // users cannot edit the read store contents, but they can use them
  122. functions:null, // use the names to get to the function
  123. /*postCreate: function(){
  124. this.functions = []; // use the names to get to the function
  125. this.writeStore = new dojo.data.ItemFileWriteStore({data: {identifier: 'name', items:[]}});
  126. this.combo.set("store", this.writeStore);
  127. },*/
  128. startup: function(){
  129. // summary
  130. // make sure the parent has a close button if it needs to be able to close
  131. // link the write store too
  132. this.combo.set("store", this.writeStore);
  133. this.inherited(arguments);// this is super class startup
  134. // close is only valid if the parent is a widget with a close function
  135. var parent = registry.getEnclosingWidget(this.domNode.parentNode);
  136. if(parent && typeof parent.close == "function"){
  137. this.closeButton.set("onClick", lang.hitch(parent, 'close'));
  138. }else{
  139. domStyle.set(this.closeButton.domNode, { display: "none" }); // hide the button
  140. }
  141. }
  142. });
  143. return lang.mixin(calc, { FuncGen: FuncGen });
  144. });