Htmleditor.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. /** @class Htmleditor HTML编辑器。
  3. * @example
  4. * //可以在脚本中获取该组件
  5. * //方法1:
  6. * var attachment = this.form.get("name"); //获取组件
  7. * //方法2
  8. * var attachment = this.target; //在组件事件脚本中获取
  9. * @extends MWF.xApplication.process.Xform.$Module
  10. * @category FormComponents
  11. * @hideconstructor
  12. */
  13. MWF.xApplication.process.Xform.Htmleditor = MWF.APPHtmleditor = new Class(
  14. /** @lends MWF.xApplication.process.Xform.Htmleditor# */
  15. {
  16. Extends: MWF.APP$Module,
  17. options: {
  18. "moduleEvents": ["load", "postLoad", "afterLoad"]
  19. },
  20. initialize: function(node, json, form, options){
  21. this.node = $(node);
  22. this.node.store("module", this);
  23. this.json = json;
  24. this.form = form;
  25. this.field = true;
  26. },
  27. load: function(){
  28. if (this.fireEvent("queryLoad")){
  29. this._queryLoaded();
  30. this._loadUserInterface();
  31. this._loadStyles();
  32. //this._loadEvents();
  33. this._afterLoaded();
  34. this.fireEvent("postLoad");
  35. this.fireEvent("load");
  36. }
  37. },
  38. _loadUserInterface: function(){
  39. this.node.empty();
  40. if (this.readonly){
  41. this.node.set("html", this._getBusinessData());
  42. this.node.setStyles({
  43. "-webkit-user-select": "text",
  44. "-moz-user-select": "text"
  45. });
  46. }else{
  47. var config = Object.clone(this.json.editorProperties);
  48. if (this.json.config){
  49. if (this.json.config.code){
  50. var obj = this.form.Macro.exec(this.json.config.code, this);
  51. Object.each(obj, function(v, k){
  52. config[k] = v;
  53. });
  54. }
  55. }
  56. this.loadCkeditor(config);
  57. }
  58. // this._loadValue();
  59. },
  60. loadCkeditor: function(config){
  61. COMMON.AjaxModule.loadDom("ckeditor", function(){
  62. CKEDITOR.disableAutoInline = true;
  63. var editorDiv = new Element("div").inject(this.node);
  64. var htmlData = this._getBusinessData();
  65. if (htmlData){
  66. editorDiv.set("html", htmlData);
  67. }else if (this.json.templateCode){
  68. editorDiv.set("html", this.json.templateCode);
  69. }
  70. var height = this.node.getSize().y;
  71. var editorConfig = config || {};
  72. if (this.form.json.mode==="Mobile"){
  73. if (!editorConfig.toolbar && !editorConfig.toolbarGroups){
  74. editorConfig.toolbar = [
  75. { name: 'paragraph', items: [ 'Bold', 'Italic', "-" , 'TextColor', "BGColor", 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', "-", 'Undo', 'Redo' ] },
  76. { name: 'basicstyles', items: [ 'Styles', 'FontSize']}
  77. ];
  78. }
  79. }
  80. editorConfig.localImageMaxWidth = 800;
  81. editorConfig.reference = this.form.businessData.work.job;
  82. editorConfig.referenceType = "processPlatformJob";
  83. if( editorConfig && editorConfig.extraPlugins ){
  84. var extraPlugins = editorConfig.extraPlugins;
  85. extraPlugins = typeOf( extraPlugins ) === "array" ? extraPlugins : extraPlugins.split(",");
  86. extraPlugins.push( 'pagebreak' );
  87. editorConfig.extraPlugins = extraPlugins;
  88. }else{
  89. editorConfig.extraPlugins = ['pagebreak'];
  90. }
  91. // CKEDITOR.basePath = COMMON.contentPath+"/res/framework/htmleditor/ckeditor/";
  92. // CKEDITOR.plugins.basePath = COMMON.contentPath+"/res/framework/htmleditor/ckeditor/plugins/";
  93. this.editor = CKEDITOR.replace(editorDiv, editorConfig);
  94. this.editor.addCommand("ecnet", {
  95. exec:function(editor){
  96. this.ecnet();
  97. }.bind(this)
  98. });
  99. this.editor.ui.add('ecnet', CKEDITOR.UI_BUTTON, {
  100. label:'智能纠错',
  101. icon: '../x_component_process_Xform/$Form/default/icon/ecnet.png',
  102. command:"ecnet"
  103. });
  104. this._loadEvents();
  105. //this.editor.on("loaded", function(){
  106. // this._loadEvents();
  107. //}.bind(this));
  108. //this.setData(data)
  109. this.editor.on("change", function(){
  110. //this._setBusinessData(this.getData());
  111. }.bind(this));
  112. if (this.json.ecnet==="y"){
  113. // this.editor.on( "key", function( evt ) {
  114. // // var char = evt.data.domEvent.$.char;
  115. // // if ([".", ",", "?", ";", "'", " "].indexOf(char)!==-1){
  116. // // this.ecnet(evt.editor.getData());
  117. // // }
  118. // }.bind(this));
  119. // this.editor.on("blur", function(){
  120. // if (!this.notEcnetFlag) this.ecnet(this.getData());
  121. // }.bind(this));
  122. }
  123. // this._loadEvents();
  124. }.bind(this));
  125. },
  126. getEcnetString: function(node, nodes){
  127. for (var i=0; i<node.childNodes.length; i++){
  128. if (node.childNodes[i].nodeType===Node.TEXT_NODE){
  129. var s = this.ecnetString.length;
  130. this.ecnetString += node.childNodes[i].nodeValue;
  131. var e = this.ecnetString.length;
  132. nodes.push({
  133. "pnode": node,
  134. "node": node.childNodes[i],
  135. "start": s, "end": e
  136. });
  137. }else{
  138. this.getEcnetString(node.childNodes[i], nodes);
  139. }
  140. }
  141. },
  142. createEcnetNode: function(node){
  143. var newNode = node.node.ownerDocument.createElement("span");
  144. var increment = 0;
  145. var html = node.node.nodeValue;;
  146. node.ecnets.each(function(ecnet){
  147. var s = ecnet.begin+increment-node.start;
  148. var e = ecnet.end+increment-node.start;
  149. if (s<0) s=0;
  150. if (e>node.end+increment) e = node.end+increment;
  151. var length = html.length;
  152. var left = html.substring(0, s);
  153. var ecnetStr = html.substring(s, e);
  154. var right = html.substring(e, html.length);
  155. html = left+"<span class='o2_ecnet_item' style='color: red'><u>"+ecnetStr+"</u></span>"+right;
  156. increment += (html.length-length);
  157. }.bind(this));
  158. newNode.innerHTML = html;
  159. node.pnode.replaceChild(newNode, node.node);
  160. node.pnode.textNode = node.node;
  161. node.pnode.ecnetNode = newNode;
  162. var _self = this;
  163. var editorFrame = this.editor.document.$.defaultView.frameElement;
  164. var spans = newNode.getElementsByTagName("span");
  165. if (spans.length){
  166. for (var i = 0; i<spans.length; i++){
  167. var span = spans[i];
  168. if (span.className==="o2_ecnet_item"){
  169. var ecnetNode = new Element("div", {"styles": {
  170. "border": "1px solid #999999",
  171. "box-shadow": "0px 0px 5px #999999",
  172. "background-color": "#ffffff",
  173. "position": "fixed",
  174. "display": "none"
  175. }}).inject(editorFrame, "after");
  176. var correctNode = new Element("div", {
  177. "styles": {
  178. "padding": "3px 10px",
  179. "font-weight": "bold",
  180. "font-size": "12px",
  181. "cursor": "pointer"
  182. },
  183. "text": node.ecnets[i].origin+"->"+node.ecnets[i].correct,
  184. "events": {
  185. "mouseover": function(){this.setStyle("background-color", "#dddddd")},
  186. "mouseout": function(){this.setStyle("background-color", "#ffffff")},
  187. "mousedown": function(){
  188. var ecnetNode = this.getParent();
  189. var node = ecnetNode.node;
  190. var item = ecnetNode.node.ecnets[ecnetNode.idx];
  191. var textNode = node.node.ownerDocument.createTextNode(item.correct);
  192. ecnetNode.span.parentNode.replaceChild(textNode, ecnetNode.span);
  193. ecnetNode.destroy();
  194. node.node.nodeValue = node.pnode.ecnetNode.innerText;
  195. node.ecnets.erase(item);
  196. if (!node.ecnets.length){
  197. _self.ecnetNodes.erase(node);
  198. }
  199. }
  200. }
  201. }).inject(ecnetNode);
  202. var ignoreNode = new Element("div", {
  203. "styles": {
  204. "padding": "3px 10px",
  205. "font-size": "12px",
  206. "cursor": "pointer"
  207. },
  208. "text": MWF.xApplication.process.Xform.LP.ignore,
  209. "events": {
  210. "mouseover": function(){this.setStyle("background-color", "#dddddd")},
  211. "mouseout": function(){this.setStyle("background-color", "#ffffff")},
  212. "mousedown": function(){
  213. var ecnetNode = this.getParent();
  214. var node = ecnetNode.node;
  215. var item = ecnetNode.node.ecnets[ecnetNode.idx];
  216. var textNode = node.node.ownerDocument.createTextNode(ecnetNode.span.innerText);
  217. ecnetNode.span.parentNode.replaceChild(textNode, ecnetNode.span);
  218. ecnetNode.destroy();
  219. node.node.nodeValue = node.pnode.ecnetNode.innerText;
  220. node.ecnets.erase(item);
  221. if (!node.ecnets.length){
  222. _self.ecnetNodes.erase(node);
  223. }
  224. }
  225. }
  226. }).inject(ecnetNode);
  227. ecnetNode.node = node;
  228. ecnetNode.idx = i;
  229. span.ecnetNode = ecnetNode;
  230. ecnetNode.span = span;
  231. span.addEventListener("click", function(){
  232. var ecnetNode = this.ecnetNode;
  233. ecnetNode.show();
  234. var y = this.offsetTop;
  235. var x = this.offsetLeft;
  236. var w = this.offsetWidth;
  237. var h = this.offsetHeight;
  238. var p = editorFrame.getPosition();
  239. var s = ecnetNode.getSize();
  240. var top = y+p.y+h+5;
  241. var left = x+p.x-((s.x-w)/2);
  242. ecnetNode.style.left = ""+left+"px";
  243. ecnetNode.style.top = ""+top+"px";
  244. var _span = this;
  245. var hideEcnetNode = function(){
  246. ecnetNode.hide();
  247. _span.ownerDocument.removeEventListener("mousedown", hideEcnetNode);
  248. };
  249. this.ownerDocument.addEventListener("mousedown", hideEcnetNode);
  250. });
  251. }
  252. }
  253. }
  254. //node.pnode.ecnetInforNode = ecnetNode;
  255. // var spans = newNode.getElementsByTagName("span");
  256. // if (spans.length){
  257. // var span = spans[0];
  258. // span.addEventListener("click", function(){
  259. // ecnetNode.style.display = "block";
  260. // var y = span.offsetTop;
  261. // var x = span.offsetLeft;
  262. // var w = span.offsetWidth;
  263. // var h = span.offsetHeight;
  264. // var p = editorFrame.getPosition();
  265. // var s = ecnetNode.getSize();
  266. // var top = y+p.y+h+5;
  267. // var left = x+p.x-((s.x-w)/2);
  268. //
  269. // ecnetNode.style.left = ""+left+"px";
  270. // ecnetNode.style.top = ""+top+"px";
  271. // });
  272. // span.addEventListener("mouseout", function(){});
  273. // }
  274. },
  275. clearEcnetNodes: function(){
  276. if (this.ecnetNodes && this.ecnetNodes.length){
  277. this.ecnetNodes.each(function(node){
  278. if (node.pnode.ecnetNode){
  279. if (node.pnode.ecnetInforNode) node.pnode.ecnetInforNode.destroy();
  280. node.pnode.ecnetInforNode = null;
  281. node.pnode.replaceChild(node.pnode.textNode, node.pnode.ecnetNode);
  282. }
  283. }.bind(this));
  284. this.ecnetNodes = [];
  285. }
  286. },
  287. ecnet: function(data){
  288. //this.editor.document.$.body.innerText
  289. var editorFrame = this.editor.document.$.defaultView.frameElement;
  290. //var data = this.editor.getData();
  291. var body = this.editor.document.$.body;
  292. if (!this.ecnetNodes) this.ecnetNodes = [];
  293. if (this.ecnetNodes.length) this.clearEcnetNodes();
  294. var nodes = [];
  295. this.ecnetString = "";
  296. this.getEcnetString(body, nodes);
  297. MWF.Actions.get("x_general_assemble_control").ecnetCheck({"value": this.ecnetString}, function(json){
  298. if (json.data.itemList && json.data.itemList.length){
  299. nodes.each(function(node){
  300. var items = [];
  301. json.data.itemList.each(function(item){
  302. 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)){
  303. items.push(item);
  304. }
  305. }.bind(this));
  306. if (items.length){
  307. node.ecnets = items;
  308. this.ecnetNodes.push(node);
  309. }
  310. }.bind(this));
  311. this.ecnetNodes.each(function(node){
  312. this.createEcnetNode(node);
  313. }.bind(this));
  314. // var item = json.data.itemList[0];
  315. // var left = data.substring(0, item.begin);
  316. // var ecnetStr = data.substring(item.begin, item.end);
  317. // var right = data.substring(item.end, data.length);
  318. //
  319. // var newData = left+"<span class='o2_ecnet_item' style='color:red' title='"+item.origin+"->"+item.correc+"'><u>"+ecnetStr+"</u></span>"+right;
  320. //this.editor.document.$.body.setSelectionRange(item.begin, item.end);
  321. //this.editor.setData(newData);
  322. // var iframe = editorFrame.clone();
  323. // iframe.inject(this.node);
  324. // iframe.position({
  325. // "relativeTo": editorFrame,
  326. // "position": 'upperLeft',
  327. // "edge": 'upperLeft'
  328. // });
  329. // iframe.contentWindow.document.body.set("html", newData);
  330. }else{
  331. body = null;
  332. nodes = null;
  333. }
  334. }.bind(this));
  335. },
  336. _loadEvents: function(editorConfig){
  337. Object.each(this.json.events, function(e, key){
  338. if (e.code){
  339. this.editor.on(key, function(event){
  340. return this.form.Macro.fire(e.code, this, event);
  341. }.bind(this), this);
  342. }
  343. }.bind(this));
  344. },
  345. addModuleEvent: function(key, fun){
  346. this.editor.on(key, function(event){
  347. return (fun) ? fun(this, event) : null;
  348. }.bind(this), this);
  349. },
  350. _loadValue: function(){
  351. var data = this._getBusinessData();
  352. },
  353. /**
  354. * @summary 重置组件的值为默认值或置空。
  355. * @example
  356. * this.form.get('fieldName').resetData();
  357. */
  358. resetData: function(){
  359. this.setData(this._getBusinessData());
  360. },
  361. /**
  362. * @summary 判断组件值是否为空.
  363. * @example
  364. * if( this.form.get('fieldName').isEmpty() ){
  365. * this.form.notice('HTML编辑器不能为空', 'warn');
  366. * }
  367. * @return {Boolean} 值是否为空.
  368. */
  369. isEmpty : function(){
  370. return !this.getData().trim();
  371. },
  372. /**
  373. * 当表单上没有对应组件的时候,可以使用this.data[fieldName]获取值,但是this.form.get('fieldName')无法获取到组件。
  374. * @summary 获取组件值。
  375. * @example
  376. * var data = this.form.get('fieldName').getData();
  377. * @example
  378. * //如果无法确定表单上是否有组件,需要判断
  379. * var data;
  380. * if( this.form.get('fieldName') ){ //判断表单是否有无对应组件
  381. * data = this.form.get('fieldName').getData();
  382. * }else{
  383. * data = this.data['fieldName']; //直接从数据中获取字段值
  384. * }
  385. * @return 组件的数据.
  386. */
  387. getData: function(){
  388. this.clearEcnetNodes();
  389. return this.editor ? this.editor.getData() : "";
  390. },
  391. /**
  392. * 当表单上没有对应组件的时候,可以使用this.data[fieldName] = data赋值。
  393. * @summary 为组件赋值。
  394. * @param data{String} .
  395. * @example
  396. * this.form.get("fieldName").setData("test"); //赋文本值
  397. * @example
  398. * //如果无法确定表单上是否有组件,需要判断
  399. * if( this.form.get('fieldName') ){ //判断表单是否有无对应组件
  400. * this.form.get('fieldName').setData( data );
  401. * }else{
  402. * this.data['fieldName'] = data;
  403. * }
  404. */
  405. setData: function(data){
  406. this._setBusinessData(data);
  407. if (this.editor) this.editor.setData(data);
  408. },
  409. createErrorNode: function(text){
  410. var node = new Element("div");
  411. var iconNode = new Element("div", {
  412. "styles": {
  413. "width": "20px",
  414. "height": "20px",
  415. "float": "left",
  416. "background": "url("+"../x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  417. }
  418. }).inject(node);
  419. var textNode = new Element("div", {
  420. "styles": {
  421. "line-height": "20px",
  422. "margin-left": "20px",
  423. "color": "red",
  424. "word-break": "keep-all"
  425. },
  426. "text": text
  427. }).inject(node);
  428. return node;
  429. },
  430. notValidationMode: function(text){
  431. if (!this.isNotValidationMode){
  432. this.isNotValidationMode = true;
  433. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  434. this.node.setStyle("border", "1px solid red");
  435. this.errNode = this.createErrorNode(text).inject(this.node, "after");
  436. this.showNotValidationMode(this.node);
  437. if (!this.node.isIntoView()) this.node.scrollIntoView();
  438. }
  439. },
  440. showNotValidationMode: function(node){
  441. var p = node.getParent("div");
  442. if (p){
  443. if (p.get("MWFtype") == "tab$Content"){
  444. if (p.getParent("div").getStyle("display")=="none"){
  445. var contentAreaNode = p.getParent("div").getParent("div");
  446. var tabAreaNode = contentAreaNode.getPrevious("div");
  447. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  448. var tabNode = tabAreaNode.getLast().getFirst().getChildren()[idx];
  449. tabNode.click();
  450. p = tabAreaNode.getParent("div");
  451. }
  452. }
  453. this.showNotValidationMode(p);
  454. }
  455. },
  456. validationMode: function(){
  457. if (this.isNotValidationMode){
  458. this.isNotValidationMode = false;
  459. this.node.setStyles(this.node.retrieve("borderStyle"));
  460. if (this.errNode){
  461. this.errNode.destroy();
  462. this.errNode = null;
  463. }
  464. }
  465. },
  466. validationConfigItem: function(routeName, data){
  467. var flag = (data.status=="all") ? true: (routeName == data.decision);
  468. if (flag){
  469. var n = this.getData();
  470. var v = (data.valueType=="value") ? n : n.length;
  471. switch (data.operateor){
  472. case "isnull":
  473. if (!v){
  474. this.notValidationMode(data.prompt);
  475. return false;
  476. }
  477. break;
  478. case "notnull":
  479. if (v){
  480. this.notValidationMode(data.prompt);
  481. return false;
  482. }
  483. break;
  484. case "gt":
  485. if (v>data.value){
  486. this.notValidationMode(data.prompt);
  487. return false;
  488. }
  489. break;
  490. case "lt":
  491. if (v<data.value){
  492. this.notValidationMode(data.prompt);
  493. return false;
  494. }
  495. break;
  496. case "equal":
  497. if (v==data.value){
  498. this.notValidationMode(data.prompt);
  499. return false;
  500. }
  501. break;
  502. case "neq":
  503. if (v!=data.value){
  504. this.notValidationMode(data.prompt);
  505. return false;
  506. }
  507. break;
  508. case "contain":
  509. if (v.indexOf(data.value)!=-1){
  510. this.notValidationMode(data.prompt);
  511. return false;
  512. }
  513. break;
  514. case "notcontain":
  515. if (v.indexOf(data.value)==-1){
  516. this.notValidationMode(data.prompt);
  517. return false;
  518. }
  519. break;
  520. }
  521. }
  522. return true;
  523. },
  524. validationConfig: function(routeName, opinion){
  525. if (this.json.validationConfig){
  526. if (this.json.validationConfig.length){
  527. for (var i=0; i<this.json.validationConfig.length; i++) {
  528. var data = this.json.validationConfig[i];
  529. if (!this.validationConfigItem(routeName, data)) return false;
  530. }
  531. }
  532. return true;
  533. }
  534. return true;
  535. },
  536. /**
  537. * @summary 根据组件的校验设置进行校验。
  538. * @param {String} [routeName] - 可选,路由名称.
  539. * @example
  540. * if( !this.form.get('fieldName').validation() ){
  541. * return false;
  542. * }
  543. * @return {Boolean} 是否通过校验
  544. */
  545. validation: function(routeName, opinion){
  546. if (!this.validationConfig(routeName, opinion)) return false;
  547. if (!this.json.validation) return true;
  548. if (!this.json.validation.code) return true;
  549. this.currentRouteName = routeName;
  550. var flag = this.form.Macro.exec(this.json.validation.code, this);
  551. this.currentRouteName = "";
  552. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  553. if (flag.toString()!="true"){
  554. this.notValidationMode(flag);
  555. return false;
  556. }
  557. return true;
  558. }
  559. });