Htmleditor.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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 = this.form.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. CKEDITOR.disableAutoInline = true;
  50. var editorDiv = new Element("div").inject(this.node);
  51. var htmlData = this._getBusinessData();
  52. if (htmlData){
  53. editorDiv.set("html", htmlData);
  54. }else if (this.json.templateCode){
  55. editorDiv.set("html", this.json.templateCode);
  56. }
  57. var height = this.node.getSize().y;
  58. var editorConfig = config || {};
  59. if (this.form.json.mode==="Mobile"){
  60. if (!editorConfig.toolbar && !editorConfig.toolbarGroups){
  61. editorConfig.toolbar = [
  62. { name: 'paragraph', items: [ 'Bold', 'Italic', "-" , 'TextColor', "BGColor", 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', "-", 'Undo', 'Redo' ] },
  63. { name: 'basicstyles', items: [ 'Styles', 'FontSize']}
  64. ];
  65. }
  66. }
  67. editorConfig.localImageMaxWidth = 800;
  68. editorConfig.reference = this.form.businessData.work.job;
  69. editorConfig.referenceType = "processPlatformJob";
  70. if( editorConfig && editorConfig.extraPlugins ){
  71. var extraPlugins = editorConfig.extraPlugins;
  72. extraPlugins = typeOf( extraPlugins ) === "array" ? extraPlugins : extraPlugins.split(",");
  73. extraPlugins.push( 'pagebreak' );
  74. editorConfig.extraPlugins = extraPlugins;
  75. }else{
  76. editorConfig.extraPlugins = ['pagebreak'];
  77. }
  78. // CKEDITOR.basePath = COMMON.contentPath+"/res/framework/htmleditor/ckeditor/";
  79. // CKEDITOR.plugins.basePath = COMMON.contentPath+"/res/framework/htmleditor/ckeditor/plugins/";
  80. this.editor = CKEDITOR.replace(editorDiv, editorConfig);
  81. this.editor.addCommand("ecnet", {
  82. exec:function(editor){
  83. this.ecnet();
  84. }.bind(this)
  85. });
  86. this.editor.ui.add('ecnet', CKEDITOR.UI_BUTTON, {
  87. label:'智能纠错',
  88. icon: '../x_component_process_Xform/$Form/default/icon/ecnet.png',
  89. command:"ecnet"
  90. });
  91. this._loadEvents();
  92. //this.editor.on("loaded", function(){
  93. // this._loadEvents();
  94. //}.bind(this));
  95. //this.setData(data)
  96. this.editor.on("change", function(){
  97. //this._setBusinessData(this.getData());
  98. }.bind(this));
  99. if (this.json.ecnet==="y"){
  100. // this.editor.on( "key", function( evt ) {
  101. // // var char = evt.data.domEvent.$.char;
  102. // // if ([".", ",", "?", ";", "'", " "].indexOf(char)!==-1){
  103. // // this.ecnet(evt.editor.getData());
  104. // // }
  105. // }.bind(this));
  106. // this.editor.on("blur", function(){
  107. // if (!this.notEcnetFlag) this.ecnet(this.getData());
  108. // }.bind(this));
  109. }
  110. // this._loadEvents();
  111. }.bind(this));
  112. },
  113. getEcnetString: function(node, nodes){
  114. for (var i=0; i<node.childNodes.length; i++){
  115. if (node.childNodes[i].nodeType===Node.TEXT_NODE){
  116. var s = this.ecnetString.length;
  117. this.ecnetString += node.childNodes[i].nodeValue;
  118. var e = this.ecnetString.length;
  119. nodes.push({
  120. "pnode": node,
  121. "node": node.childNodes[i],
  122. "start": s, "end": e
  123. });
  124. }else{
  125. this.getEcnetString(node.childNodes[i], nodes);
  126. }
  127. }
  128. },
  129. createEcnetNode: function(node){
  130. var newNode = node.node.ownerDocument.createElement("span");
  131. var increment = 0;
  132. var html = node.node.nodeValue;;
  133. node.ecnets.each(function(ecnet){
  134. var s = ecnet.begin+increment-node.start;
  135. var e = ecnet.end+increment-node.start;
  136. if (s<0) s=0;
  137. if (e>node.end+increment) e = node.end+increment;
  138. var length = html.length;
  139. var left = html.substring(0, s);
  140. var ecnetStr = html.substring(s, e);
  141. var right = html.substring(e, html.length);
  142. html = left+"<span class='o2_ecnet_item' style='color: red'><u>"+ecnetStr+"</u></span>"+right;
  143. increment += (html.length-length);
  144. }.bind(this));
  145. newNode.innerHTML = html;
  146. node.pnode.replaceChild(newNode, node.node);
  147. node.pnode.textNode = node.node;
  148. node.pnode.ecnetNode = newNode;
  149. var _self = this;
  150. var editorFrame = this.editor.document.$.defaultView.frameElement;
  151. var spans = newNode.getElementsByTagName("span");
  152. if (spans.length){
  153. for (var i = 0; i<spans.length; i++){
  154. var span = spans[i];
  155. if (span.className==="o2_ecnet_item"){
  156. var ecnetNode = new Element("div", {"styles": {
  157. "border": "1px solid #999999",
  158. "box-shadow": "0px 0px 5px #999999",
  159. "background-color": "#ffffff",
  160. "position": "fixed",
  161. "display": "none"
  162. }}).inject(editorFrame, "after");
  163. var correctNode = new Element("div", {
  164. "styles": {
  165. "padding": "3px 10px",
  166. "font-weight": "bold",
  167. "font-size": "12px",
  168. "cursor": "pointer"
  169. },
  170. "text": node.ecnets[i].origin+"->"+node.ecnets[i].correct,
  171. "events": {
  172. "mouseover": function(){this.setStyle("background-color", "#dddddd")},
  173. "mouseout": function(){this.setStyle("background-color", "#ffffff")},
  174. "mousedown": function(){
  175. var ecnetNode = this.getParent();
  176. var node = ecnetNode.node;
  177. var item = ecnetNode.node.ecnets[ecnetNode.idx];
  178. var textNode = node.node.ownerDocument.createTextNode(item.correct);
  179. ecnetNode.span.parentNode.replaceChild(textNode, ecnetNode.span);
  180. ecnetNode.destroy();
  181. node.node.nodeValue = node.pnode.ecnetNode.innerText;
  182. node.ecnets.erase(item);
  183. if (!node.ecnets.length){
  184. _self.ecnetNodes.erase(node);
  185. }
  186. }
  187. }
  188. }).inject(ecnetNode);
  189. var ignoreNode = new Element("div", {
  190. "styles": {
  191. "padding": "3px 10px",
  192. "font-size": "12px",
  193. "cursor": "pointer"
  194. },
  195. "text": MWF.xApplication.process.Xform.LP.ignore,
  196. "events": {
  197. "mouseover": function(){this.setStyle("background-color", "#dddddd")},
  198. "mouseout": function(){this.setStyle("background-color", "#ffffff")},
  199. "mousedown": function(){
  200. var ecnetNode = this.getParent();
  201. var node = ecnetNode.node;
  202. var item = ecnetNode.node.ecnets[ecnetNode.idx];
  203. var textNode = node.node.ownerDocument.createTextNode(ecnetNode.span.innerText);
  204. ecnetNode.span.parentNode.replaceChild(textNode, ecnetNode.span);
  205. ecnetNode.destroy();
  206. node.node.nodeValue = node.pnode.ecnetNode.innerText;
  207. node.ecnets.erase(item);
  208. if (!node.ecnets.length){
  209. _self.ecnetNodes.erase(node);
  210. }
  211. }
  212. }
  213. }).inject(ecnetNode);
  214. ecnetNode.node = node;
  215. ecnetNode.idx = i;
  216. span.ecnetNode = ecnetNode;
  217. ecnetNode.span = span;
  218. span.addEventListener("click", function(){
  219. var ecnetNode = this.ecnetNode;
  220. ecnetNode.show();
  221. var y = this.offsetTop;
  222. var x = this.offsetLeft;
  223. var w = this.offsetWidth;
  224. var h = this.offsetHeight;
  225. var p = editorFrame.getPosition();
  226. var s = ecnetNode.getSize();
  227. var top = y+p.y+h+5;
  228. var left = x+p.x-((s.x-w)/2);
  229. ecnetNode.style.left = ""+left+"px";
  230. ecnetNode.style.top = ""+top+"px";
  231. var _span = this;
  232. var hideEcnetNode = function(){
  233. ecnetNode.hide();
  234. _span.ownerDocument.removeEventListener("mousedown", hideEcnetNode);
  235. };
  236. this.ownerDocument.addEventListener("mousedown", hideEcnetNode);
  237. });
  238. }
  239. }
  240. }
  241. //node.pnode.ecnetInforNode = ecnetNode;
  242. // var spans = newNode.getElementsByTagName("span");
  243. // if (spans.length){
  244. // var span = spans[0];
  245. // span.addEventListener("click", function(){
  246. // ecnetNode.style.display = "block";
  247. // var y = span.offsetTop;
  248. // var x = span.offsetLeft;
  249. // var w = span.offsetWidth;
  250. // var h = span.offsetHeight;
  251. // var p = editorFrame.getPosition();
  252. // var s = ecnetNode.getSize();
  253. // var top = y+p.y+h+5;
  254. // var left = x+p.x-((s.x-w)/2);
  255. //
  256. // ecnetNode.style.left = ""+left+"px";
  257. // ecnetNode.style.top = ""+top+"px";
  258. // });
  259. // span.addEventListener("mouseout", function(){});
  260. // }
  261. },
  262. clearEcnetNodes: function(){
  263. if (this.ecnetNodes && this.ecnetNodes.length){
  264. this.ecnetNodes.each(function(node){
  265. if (node.pnode.ecnetNode){
  266. if (node.pnode.ecnetInforNode) node.pnode.ecnetInforNode.destroy();
  267. node.pnode.ecnetInforNode = null;
  268. node.pnode.replaceChild(node.pnode.textNode, node.pnode.ecnetNode);
  269. }
  270. }.bind(this));
  271. this.ecnetNodes = [];
  272. }
  273. },
  274. ecnet: function(data){
  275. //this.editor.document.$.body.innerText
  276. var editorFrame = this.editor.document.$.defaultView.frameElement;
  277. //var data = this.editor.getData();
  278. var body = this.editor.document.$.body;
  279. if (!this.ecnetNodes) this.ecnetNodes = [];
  280. if (this.ecnetNodes.length) this.clearEcnetNodes();
  281. var nodes = [];
  282. this.ecnetString = "";
  283. this.getEcnetString(body, nodes);
  284. MWF.Actions.get("x_general_assemble_control").ecnetCheck({"value": this.ecnetString}, function(json){
  285. if (json.data.itemList && json.data.itemList.length){
  286. nodes.each(function(node){
  287. var items = [];
  288. json.data.itemList.each(function(item){
  289. if ((node.end<=item.end && node.end>item.begin) || (node.start>=item.begin && node.start<item.end) || (node.start<=item.begin && node.end>item.end)){
  290. items.push(item);
  291. }
  292. }.bind(this));
  293. if (items.length){
  294. node.ecnets = items;
  295. this.ecnetNodes.push(node);
  296. }
  297. }.bind(this));
  298. this.ecnetNodes.each(function(node){
  299. this.createEcnetNode(node);
  300. }.bind(this));
  301. // var item = json.data.itemList[0];
  302. // var left = data.substring(0, item.begin);
  303. // var ecnetStr = data.substring(item.begin, item.end);
  304. // var right = data.substring(item.end, data.length);
  305. //
  306. // var newData = left+"<span class='o2_ecnet_item' style='color:red' title='"+item.origin+"->"+item.correc+"'><u>"+ecnetStr+"</u></span>"+right;
  307. //this.editor.document.$.body.setSelectionRange(item.begin, item.end);
  308. //this.editor.setData(newData);
  309. // var iframe = editorFrame.clone();
  310. // iframe.inject(this.node);
  311. // iframe.position({
  312. // "relativeTo": editorFrame,
  313. // "position": 'upperLeft',
  314. // "edge": 'upperLeft'
  315. // });
  316. // iframe.contentWindow.document.body.set("html", newData);
  317. }else{
  318. body = null;
  319. nodes = null;
  320. }
  321. }.bind(this));
  322. },
  323. _loadEvents: function(editorConfig){
  324. Object.each(this.json.events, function(e, key){
  325. if (e.code){
  326. this.editor.on(key, function(event){
  327. return this.form.Macro.fire(e.code, this, event);
  328. }.bind(this), this);
  329. }
  330. }.bind(this));
  331. },
  332. addModuleEvent: function(key, fun){
  333. this.editor.on(key, function(event){
  334. return (fun) ? fun(this, event) : null;
  335. }.bind(this), this);
  336. },
  337. _loadValue: function(){
  338. var data = this._getBusinessData();
  339. },
  340. resetData: function(){
  341. this.setData(this._getBusinessData());
  342. },
  343. isEmpty : function(){
  344. return !this.getData().trim();
  345. },
  346. getData: function(){
  347. this.clearEcnetNodes();
  348. return this.editor ? this.editor.getData() : "";
  349. },
  350. setData: function(data){
  351. this._setBusinessData(data);
  352. if (this.editor) this.editor.setData(data);
  353. },
  354. createErrorNode: function(text){
  355. var node = new Element("div");
  356. var iconNode = new Element("div", {
  357. "styles": {
  358. "width": "20px",
  359. "height": "20px",
  360. "float": "left",
  361. "background": "url("+"../x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  362. }
  363. }).inject(node);
  364. var textNode = new Element("div", {
  365. "styles": {
  366. "line-height": "20px",
  367. "margin-left": "20px",
  368. "color": "red",
  369. "word-break": "keep-all"
  370. },
  371. "text": text
  372. }).inject(node);
  373. return node;
  374. },
  375. notValidationMode: function(text){
  376. if (!this.isNotValidationMode){
  377. this.isNotValidationMode = true;
  378. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  379. this.node.setStyle("border", "1px solid red");
  380. this.errNode = this.createErrorNode(text).inject(this.node, "after");
  381. this.showNotValidationMode(this.node);
  382. if (!this.node.isIntoView()) this.node.scrollIntoView();
  383. }
  384. },
  385. showNotValidationMode: function(node){
  386. var p = node.getParent("div");
  387. if (p){
  388. if (p.get("MWFtype") == "tab$Content"){
  389. if (p.getParent("div").getStyle("display")=="none"){
  390. var contentAreaNode = p.getParent("div").getParent("div");
  391. var tabAreaNode = contentAreaNode.getPrevious("div");
  392. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  393. var tabNode = tabAreaNode.getLast().getFirst().getChildren()[idx];
  394. tabNode.click();
  395. p = tabAreaNode.getParent("div");
  396. }
  397. }
  398. this.showNotValidationMode(p);
  399. }
  400. },
  401. validationMode: function(){
  402. if (this.isNotValidationMode){
  403. this.isNotValidationMode = false;
  404. this.node.setStyles(this.node.retrieve("borderStyle"));
  405. if (this.errNode){
  406. this.errNode.destroy();
  407. this.errNode = null;
  408. }
  409. }
  410. },
  411. validationConfigItem: function(routeName, data){
  412. var flag = (data.status=="all") ? true: (routeName == data.decision);
  413. if (flag){
  414. var n = this.getData();
  415. var v = (data.valueType=="value") ? n : n.length;
  416. switch (data.operateor){
  417. case "isnull":
  418. if (!v){
  419. this.notValidationMode(data.prompt);
  420. return false;
  421. }
  422. break;
  423. case "notnull":
  424. if (v){
  425. this.notValidationMode(data.prompt);
  426. return false;
  427. }
  428. break;
  429. case "gt":
  430. if (v>data.value){
  431. this.notValidationMode(data.prompt);
  432. return false;
  433. }
  434. break;
  435. case "lt":
  436. if (v<data.value){
  437. this.notValidationMode(data.prompt);
  438. return false;
  439. }
  440. break;
  441. case "equal":
  442. if (v==data.value){
  443. this.notValidationMode(data.prompt);
  444. return false;
  445. }
  446. break;
  447. case "neq":
  448. if (v!=data.value){
  449. this.notValidationMode(data.prompt);
  450. return false;
  451. }
  452. break;
  453. case "contain":
  454. if (v.indexOf(data.value)!=-1){
  455. this.notValidationMode(data.prompt);
  456. return false;
  457. }
  458. break;
  459. case "notcontain":
  460. if (v.indexOf(data.value)==-1){
  461. this.notValidationMode(data.prompt);
  462. return false;
  463. }
  464. break;
  465. }
  466. }
  467. return true;
  468. },
  469. validationConfig: function(routeName, opinion){
  470. if (this.json.validationConfig){
  471. if (this.json.validationConfig.length){
  472. for (var i=0; i<this.json.validationConfig.length; i++) {
  473. var data = this.json.validationConfig[i];
  474. if (!this.validationConfigItem(routeName, data)) return false;
  475. }
  476. }
  477. return true;
  478. }
  479. return true;
  480. },
  481. validation: function(routeName, opinion){
  482. if (!this.validationConfig(routeName, opinion)) return false;
  483. if (!this.json.validation) return true;
  484. if (!this.json.validation.code) return true;
  485. this.currentRouteName = routeName;
  486. var flag = this.form.Macro.exec(this.json.validation.code, this);
  487. this.currentRouteName = "";
  488. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  489. if (flag.toString()!="true"){
  490. this.notValidationMode(flag);
  491. return false;
  492. }
  493. return true;
  494. }
  495. });