Codeeditor.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. MWF.xApplication.process.Xform.Codeeditor = MWF.APPCodeeditor = new Class({
  3. Extends: MWF.APP$Module,
  4. options: {
  5. "moduleEvents": ["save", "change", "blur", "postLoadEditor", "destroy", "queryLoad", "load", "postLoad", "afterLoad", "maxSize", "returnSize"]
  6. },
  7. initialize: function(node, json, form, options){
  8. this.node = $(node);
  9. this.node.store("module", this);
  10. this.json = json;
  11. this.form = form;
  12. this.field = true;
  13. this.fieldModuleLoaded = false;
  14. },
  15. load: function(){
  16. this._loadModuleEvents();
  17. if (this.fireEvent("queryLoad")){
  18. this._queryLoaded();
  19. this._loadUserInterface();
  20. this._loadStyles();
  21. this._afterLoaded();
  22. this.fireEvent("postLoad");
  23. this.fireEvent("load");
  24. }
  25. },
  26. _loadUserInterface: function(){
  27. this.node.empty();
  28. if (this.isReadonly()){
  29. var value = this._getBusinessData();
  30. this.highlighting(value);
  31. this.node.setStyles({
  32. "padding": "0.5em 0.6em",
  33. "background-color": "#f7f7f7",
  34. "overflow": "auto",
  35. "font-size": "0.875em",
  36. "border-radius": "var(--oo-default-radius)"
  37. })
  38. }else{
  39. this.loadCodeeditor();
  40. }
  41. },
  42. highlighting: function(value){
  43. var contentType = this.json.mode ? 'text/'+this.json.mode : 'text/javascript';
  44. this.preNode = new Element('pre').inject(this.node);
  45. this.preNode.set('data-lang', contentType);
  46. this.preNode.set('text', value || ' ');
  47. o2.require("o2.widget.monaco", function(){
  48. o2.widget.monaco.load(function(){
  49. monaco.editor.colorizeElement(this.preNode, {});
  50. }.bind(this));
  51. }.bind(this));
  52. },
  53. loadCodeeditor: function(){
  54. MWF.require("MWF.widget.ScriptArea", function(){
  55. this.editor = new MWF.widget.ScriptArea(this.node, {
  56. "title": this.json.title || "",
  57. "isbind": false,
  58. "mode": this.json.mode || "javascript",
  59. "maxObj": this.form.node,
  60. "maxPosition": "absolute",
  61. "onChange": function(){
  62. this._setBusinessData(this.getData());
  63. this.fireEvent('change');
  64. }.bind(this),
  65. "onSave": function(){
  66. this._setBusinessData(this.getData());
  67. this.fireEvent('save');
  68. }.bind(this),
  69. "onQueryLoad": function(){
  70. this.fireEvent('queryLoad');
  71. }.bind(this),
  72. "onPostLoad": function(){
  73. this.fireEvent('postLoad');
  74. this.fireEvent('load');
  75. this.fireEvent('afterLoad');
  76. }.bind(this),
  77. "onMaxSize": function(){
  78. this.fireEvent('maxSize');
  79. }.bind(this),
  80. "onReturnSize": function(){
  81. this.fireEvent('returnSize');
  82. }.bind(this),
  83. "onBlur": function(){
  84. this.fireEvent('blur');
  85. }.bind(this),
  86. "onPostLoadEditor": function(){
  87. this.fireEvent('postLoadEditor');
  88. }.bind(this),
  89. "onDestroy": function(){
  90. this.fireEvent('destroy');
  91. }.bind(this),
  92. "style": this.json.style || "v10"
  93. });
  94. this.editor.load({code: this._getBusinessData()});
  95. }.bind(this));
  96. },
  97. _loadEvents: function(editorConfig){
  98. Object.each(this.json.events, function(e, key){
  99. if (e.code){
  100. this.editor.on(key, function(event){
  101. return this.form.Macro.fire(e.code, this, event);
  102. }.bind(this), this);
  103. }
  104. }.bind(this));
  105. },
  106. _loadValue: function(){
  107. var data = this._getBusinessData();
  108. },
  109. resetData: function(){
  110. this.setData(this._getBusinessData());
  111. },
  112. isEmpty : function(){
  113. return !this.getData().trim();
  114. },
  115. getData: function(){
  116. return this.editor ? this.editor.getData() : this._getBusinessData();
  117. },
  118. setData: function(data){
  119. this._setBusinessData(data);
  120. if (this.editor) this.editor.setData(data);
  121. },
  122. destroy: function(){
  123. if( this.editor )this.editor.destroy();
  124. },
  125. notValidationMode: function(text){
  126. if (!this.isNotValidationMode){
  127. this.isNotValidationMode = true;
  128. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  129. this.node.setStyle("border", "1px solid red");
  130. this.errNode = this.createErrorNode(text).inject(this.node, "after");
  131. this.showNotValidationMode(this.node);
  132. if (!this.errNode.isIntoView()) this.errNode.scrollIntoView(false);
  133. }
  134. },
  135. showNotValidationMode: function(node){
  136. var p = node.getParent("div");
  137. if (p){
  138. if (p.get("MWFtype") == "tab$Content"){
  139. if (p.getParent("div").getStyle("display")=="none"){
  140. var contentAreaNode = p.getParent("div").getParent("div");
  141. var tabAreaNode = contentAreaNode.getPrevious("div");
  142. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  143. var tabNode = tabAreaNode.getLast().getFirst().getChildren()[idx];
  144. tabNode.click();
  145. p = tabAreaNode.getParent("div");
  146. }
  147. }
  148. this.showNotValidationMode(p);
  149. }
  150. },
  151. validationMode: function(){
  152. if (this.isNotValidationMode){
  153. this.isNotValidationMode = false;
  154. this.node.setStyles(this.node.retrieve("borderStyle"));
  155. if (this.errNode){
  156. this.errNode.destroy();
  157. this.errNode = null;
  158. }
  159. }
  160. },
  161. validationConfigItem: function(routeName, data){
  162. var flag = (data.status=="all") ? true: (routeName == data.decision);
  163. if (flag){
  164. var n = this.getData();
  165. var v = (data.valueType=="value") ? n : n.length;
  166. switch (data.operateor){
  167. case "isnull":
  168. if (!v){
  169. this.notValidationMode(data.prompt);
  170. return false;
  171. }
  172. break;
  173. case "notnull":
  174. if (v){
  175. this.notValidationMode(data.prompt);
  176. return false;
  177. }
  178. break;
  179. case "gt":
  180. if (v>data.value){
  181. this.notValidationMode(data.prompt);
  182. return false;
  183. }
  184. break;
  185. case "lt":
  186. if (v<data.value){
  187. this.notValidationMode(data.prompt);
  188. return false;
  189. }
  190. break;
  191. case "equal":
  192. if (v==data.value){
  193. this.notValidationMode(data.prompt);
  194. return false;
  195. }
  196. break;
  197. case "neq":
  198. if (v!=data.value){
  199. this.notValidationMode(data.prompt);
  200. return false;
  201. }
  202. break;
  203. case "contain":
  204. if (v.indexOf(data.value)!=-1){
  205. this.notValidationMode(data.prompt);
  206. return false;
  207. }
  208. break;
  209. case "notcontain":
  210. if (v.indexOf(data.value)==-1){
  211. this.notValidationMode(data.prompt);
  212. return false;
  213. }
  214. break;
  215. }
  216. }
  217. return true;
  218. },
  219. validationConfig: function(routeName, opinion){
  220. if (this.json.validationConfig){
  221. if (this.json.validationConfig.length){
  222. for (var i=0; i<this.json.validationConfig.length; i++) {
  223. var data = this.json.validationConfig[i];
  224. if (!this.validationConfigItem(routeName, data)) return false;
  225. }
  226. }
  227. return true;
  228. }
  229. return true;
  230. },
  231. validation: function(routeName, opinion){
  232. if (!this.validationConfig(routeName, opinion)) return false;
  233. if (!this.json.validation) return true;
  234. if (!this.json.validation.code) return true;
  235. this.currentRouteName = routeName;
  236. var flag = this.form.Macro.exec(this.json.validation.code, this);
  237. this.currentRouteName = "";
  238. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  239. if (flag.toString()!="true"){
  240. this.notValidationMode(flag);
  241. return false;
  242. }
  243. return true;
  244. }
  245. });