FuncGen.js 6.2 KB

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