Htmleditor.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. MWF.xApplication.process.Xform.Htmleditor = MWF.APPHtmleditor = new Class({
  3. Extends: MWF.APP$Module,
  4. options: {
  5. "moduleEvents": ["load", "postLoad", "afterLoad"]
  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. },
  14. load: function(){
  15. if (this.fireEvent("queryLoad")){
  16. this._queryLoaded();
  17. this._loadUserInterface();
  18. this._loadStyles();
  19. //this._loadEvents();
  20. this._afterLoaded();
  21. this.fireEvent("postLoad");
  22. this.fireEvent("load");
  23. }
  24. },
  25. _loadUserInterface: function(){
  26. this.node.empty();
  27. if (this.readonly){
  28. this.node.set("html", this._getBusinessData());
  29. this.node.setStyles({
  30. "-webkit-user-select": "text",
  31. "-moz-user-select": "text"
  32. });
  33. }else{
  34. var config = Object.clone(this.json.editorProperties);
  35. if (this.json.config){
  36. if (this.json.config.code){
  37. var obj = MWF.Macro.exec(this.json.config.code, this);
  38. Object.each(obj, function(v, k){
  39. config[k] = v;
  40. });
  41. }
  42. }
  43. this.loadCkeditor(config);
  44. }
  45. // this._loadValue();
  46. },
  47. loadCkeditor: function(config){
  48. COMMON.AjaxModule.loadDom("ckeditor", function(){
  49. var editorDiv = new Element("div").inject(this.node);
  50. var htmlData = this._getBusinessData();
  51. if (htmlData){
  52. editorDiv.set("html", htmlData);
  53. }else if (this.json.templateCode){
  54. editorDiv.set("html", this.json.templateCode);
  55. }
  56. var height = this.node.getSize().y;
  57. var editorConfig = config || {};
  58. if (this.form.json.mode=="Mobile"){
  59. if (!editorConfig.toolbar && !editorConfig.toolbarGroups){
  60. editorConfig.toolbar = [
  61. { name: 'paragraph', items: [ 'Bold', 'Italic', "-" , 'TextColor', "BGColor", 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', "-", 'Undo', 'Redo' ] },
  62. { name: 'basicstyles', items: [ 'Styles', 'FontSize']}
  63. ];
  64. }
  65. }
  66. // CKEDITOR.basePath = COMMON.contentPath+"/res/framework/htmleditor/ckeditor/";
  67. // CKEDITOR.plugins.basePath = COMMON.contentPath+"/res/framework/htmleditor/ckeditor/plugins/";
  68. this.editor = CKEDITOR.replace(editorDiv, editorConfig);
  69. this._loadEvents();
  70. debugger;
  71. //this.editor.on("loaded", function(){
  72. // this._loadEvents();
  73. //}.bind(this));
  74. //this.setData(data)
  75. this.editor.on("change", function(){
  76. this._setBusinessData(this.getData());
  77. }.bind(this));
  78. // this._loadEvents();
  79. }.bind(this));
  80. },
  81. _loadEvents: function(editorConfig){
  82. Object.each(this.json.events, function(e, key){
  83. if (e.code){
  84. this.editor.on(key, function(event){
  85. return this.form.Macro.fire(e.code, this, event);
  86. }.bind(this), this);
  87. }
  88. }.bind(this));
  89. },
  90. _loadValue: function(){
  91. var data = this._getBusinessData();
  92. },
  93. resetData: function(){
  94. this.setData(this._getBusinessData());
  95. },
  96. getData: function(){
  97. return this.editor.getData();
  98. },
  99. setData: function(data){
  100. this._setBusinessData(data);
  101. if (this.editor) this.editor.setData(data);
  102. },
  103. createErrorNode: function(text){
  104. var node = new Element("div");
  105. var iconNode = new Element("div", {
  106. "styles": {
  107. "width": "20px",
  108. "height": "20px",
  109. "float": "left",
  110. "background": "url("+"/x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  111. }
  112. }).inject(node);
  113. var textNode = new Element("div", {
  114. "styles": {
  115. "line-height": "20px",
  116. "margin-left": "20px",
  117. "color": "red"
  118. },
  119. "text": text
  120. }).inject(node);
  121. return node;
  122. },
  123. notValidationMode: function(text){
  124. if (!this.isNotValidationMode){
  125. this.isNotValidationMode = true;
  126. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  127. this.node.setStyle("border", "1px solid red");
  128. this.errNode = this.createErrorNode(text).inject(this.node, "after");
  129. this.showNotValidationMode(this.node);
  130. }
  131. },
  132. showNotValidationMode: function(node){
  133. var p = node.getParent("div");
  134. if (p){
  135. if (p.get("MWFtype") == "tab$Content"){
  136. if (p.getParent("div").getStyle("display")=="none"){
  137. var contentAreaNode = p.getParent("div").getParent("div");
  138. var tabAreaNode = contentAreaNode.getPrevious("div");
  139. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  140. var tabNode = tabAreaNode.getChildren()[idx];
  141. tabNode.click();
  142. p = tabAreaNode.getParent("div");
  143. }
  144. }
  145. this.showNotValidationMode(p);
  146. }
  147. },
  148. validationMode: function(){
  149. if (this.isNotValidationMode){
  150. this.isNotValidationMode = false;
  151. this.node.setStyles(this.node.retrieve("borderStyle"));
  152. if (this.errNode){
  153. this.errNode.destroy();
  154. this.errNode = null;
  155. }
  156. }
  157. },
  158. validationConfigItem: function(routeName, data){
  159. var flag = (data.status=="all") ? true: (routeName == data.decision);
  160. if (flag){
  161. var n = this.getData();
  162. var v = (data.valueType=="value") ? n : n.length;
  163. switch (data.operateor){
  164. case "isnull":
  165. if (!v){
  166. this.notValidationMode(data.prompt);
  167. return false;
  168. }
  169. break;
  170. case "notnull":
  171. if (v){
  172. this.notValidationMode(data.prompt);
  173. return false;
  174. }
  175. break;
  176. case "gt":
  177. if (v>data.value){
  178. this.notValidationMode(data.prompt);
  179. return false;
  180. }
  181. break;
  182. case "lt":
  183. if (v<data.value){
  184. this.notValidationMode(data.prompt);
  185. return false;
  186. }
  187. break;
  188. case "equal":
  189. if (v==data.value){
  190. this.notValidationMode(data.prompt);
  191. return false;
  192. }
  193. break;
  194. case "neq":
  195. if (v!=data.value){
  196. this.notValidationMode(data.prompt);
  197. return false;
  198. }
  199. break;
  200. case "contain":
  201. if (v.indexOf(data.value)!=-1){
  202. this.notValidationMode(data.prompt);
  203. return false;
  204. }
  205. break;
  206. case "notcontain":
  207. if (v.indexOf(data.value)==-1){
  208. this.notValidationMode(data.prompt);
  209. return false;
  210. }
  211. break;
  212. }
  213. }
  214. return true;
  215. },
  216. validationConfig: function(routeName, opinion){
  217. debugger;
  218. if (this.json.validationConfig){
  219. if (this.json.validationConfig.length){
  220. for (var i=0; i<this.json.validationConfig.length; i++) {
  221. var data = this.json.validationConfig[i];
  222. if (!this.validationConfigItem(routeName, data)) return false;
  223. }
  224. }
  225. return true;
  226. }
  227. return true;
  228. },
  229. validation: function(routeName, opinion){
  230. if (!this.validationConfig(routeName, opinion)) return false;
  231. if (!this.json.validation) return true;
  232. if (!this.json.validation.code) return true;
  233. var flag = this.form.Macro.exec(this.json.validation.code, this);
  234. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  235. if (flag.toString()!="true"){
  236. this.notValidationMode(flag);
  237. return false;
  238. }
  239. return true;
  240. }
  241. });