Htmleditor.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. /** @class Htmleditor HTML编辑器。
  3. * @o2cn HTML编辑器
  4. * @example
  5. * //可以在脚本中获取该组件
  6. * //方法1:
  7. * var htmlEditor = this.form.get("name"); //获取组件
  8. * //方法2
  9. * var htmlEditor = this.target; //在组件事件脚本中获取
  10. * @extends MWF.xApplication.process.Xform.$Module
  11. * @o2category FormComponents
  12. * @o2range {Process|CMS}
  13. * @hideconstructor
  14. */
  15. MWF.xApplication.process.Xform.Htmleditor = MWF.APPHtmleditor = new Class(
  16. /** @lends MWF.xApplication.process.Xform.Htmleditor# */
  17. {
  18. Extends: MWF.APP$Module,
  19. options: {
  20. /**
  21. * 组件异步加载后触发.
  22. * @event MWF.xApplication.process.Xform.Htmleditor#afterLoad
  23. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  24. */
  25. "moduleEvents": ["queryLoad", "load", "postLoad", "afterLoad"]
  26. },
  27. initialize: function(node, json, form, options){
  28. this.node = $(node);
  29. this.node.store("module", this);
  30. this.json = json;
  31. this.form = form;
  32. this.field = true;
  33. this.fieldModuleLoaded = false;
  34. },
  35. load: function(){
  36. this._loadModuleEvents();
  37. if (this.fireEvent("queryLoad")){
  38. this._queryLoaded();
  39. this._loadUserInterface();
  40. this._loadStyles();
  41. //this._loadEvents();
  42. this._afterLoaded();
  43. this.fireEvent("postLoad");
  44. this.fireEvent("load");
  45. }
  46. },
  47. _loadUserInterface: function(){
  48. this.node.empty();
  49. if (this.isReadonly()){
  50. // this.node.set("html", this._getBusinessData());
  51. this.node.setStyles({
  52. "-webkit-user-select": "text",
  53. "-moz-user-select": "text"
  54. });
  55. if( layout.mobile ){
  56. this.loadLazyImage(function () { //图片懒加载
  57. var images = this.node.getElements("img");
  58. //移动端设置图片宽度为100%
  59. images.each( function( img ){
  60. if( img.hasClass("lozad") ){
  61. img.setStyles({
  62. "max-width" : "100%"
  63. });
  64. }else{
  65. img.setStyles({
  66. "height": "auto",
  67. "max-width" : "100%"
  68. });
  69. }
  70. }.bind(this));
  71. this.fireEvent("afterLoad");
  72. this.fieldModuleLoaded = true;
  73. }.bind(this))
  74. }else{
  75. this.loadLazyImage(function () { //图片懒加载
  76. if(this.json.enablePreview !== "n"){
  77. this.loadImageViewer(); //PC端点击显示大图
  78. this.fireEvent("afterLoad");
  79. this.fieldModuleLoaded = true;
  80. }
  81. }.bind(this));
  82. }
  83. this.node.loadCss("../o2_lib/htmleditor/ckeditor4161/contents_o2.css");
  84. }else{
  85. var config = Object.clone(this.json.editorProperties);
  86. if (this.json.config){
  87. if (this.json.config.code){
  88. var obj = this.form.Macro.exec(this.json.config.code, this);
  89. Object.each(obj, function(v, k){
  90. config[k] = v;
  91. });
  92. }
  93. }
  94. this.loadCkeditor(config);
  95. }
  96. // this._loadValue();
  97. },
  98. loadLazyImage: function(callback){
  99. o2.require("o2.widget.ImageLazyLoader", function(){
  100. var loadder = new o2.widget.ImageLazyLoader(this.node, this._getBusinessData());
  101. loadder.load(function(){
  102. if(callback)callback();
  103. }.bind(this))
  104. }.bind(this));
  105. },
  106. loadImageViewer: function(){
  107. o2.require("o2.widget.ImageViewer", function(){
  108. var imageViewer = new o2.widget.ImageViewer(this.node);
  109. imageViewer.load();
  110. }.bind(this));
  111. },
  112. loadCkeditor: function(config){
  113. COMMON.AjaxModule.loadDom("ckeditor", function(){
  114. CKEDITOR.disableAutoInline = true;
  115. var editorDiv = new Element("div").inject(this.node);
  116. var htmlData = this._getBusinessData();
  117. if (htmlData){
  118. editorDiv.set("html", htmlData);
  119. }else if (this.json.templateCode){
  120. editorDiv.set("html", this.json.templateCode);
  121. }
  122. var height = this.node.getSize().y;
  123. var editorConfig = config || {};
  124. if (this.form.json.mode==="Mobile"){
  125. if (!editorConfig.toolbar && !editorConfig.toolbarGroups){
  126. editorConfig.toolbar = [
  127. { name: 'paragraph', items: [ 'Bold', 'Italic', "-" , 'TextColor', "BGColor", 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', "-", 'Undo', 'Redo' ] },
  128. { name: 'basicstyles', items: [ 'Styles', 'FontSize']},
  129. { name: 'insert', items : [ 'Image' ] }
  130. ];
  131. }
  132. }
  133. editorConfig.base64Encode = !layout.mobile && (this.json.base64Encode === "y");
  134. editorConfig.enablePreview = (this.json.enablePreview !== "n");
  135. editorConfig.localImageMaxWidth = 2000;
  136. if(this.form.options.macro === "PageContext"){
  137. editorConfig.reference = this.form.json.id;
  138. editorConfig.referenceType = "portalPage";
  139. }else{
  140. editorConfig.reference = this.form.businessData.work.job;
  141. editorConfig.referenceType = "processPlatformJob";
  142. }
  143. if( editorConfig && editorConfig.extraPlugins ){
  144. var extraPlugins = editorConfig.extraPlugins;
  145. extraPlugins = typeOf( extraPlugins ) === "array" ? extraPlugins : extraPlugins.split(",");
  146. extraPlugins.push( 'lineheight' );
  147. extraPlugins.push( 'pagebreak' );
  148. extraPlugins.push( 'o2image' );
  149. extraPlugins.push( 'o2uploadimage' );
  150. extraPlugins.push('o2uploadremoteimage');
  151. editorConfig.extraPlugins = extraPlugins;
  152. }else{
  153. editorConfig.extraPlugins = ['lineheight','pagebreak', 'o2image','o2uploadimage', 'o2uploadremoteimage'];
  154. }
  155. if( editorConfig && editorConfig.removePlugins ){
  156. var removePlugins = editorConfig.removePlugins;
  157. removePlugins = typeOf( removePlugins ) === "array" ? removePlugins : removePlugins.split(",");
  158. editorConfig.removePlugins = removePlugins.concat(['image','easyimage','exportpdf','cloudservices']);
  159. }else{
  160. editorConfig.removePlugins = ['image','easyimage','exportpdf','cloudservices'];
  161. }
  162. // CKEDITOR.basePath = COMMON.contentPath+"/res/framework/htmleditor/ckeditor/";
  163. // CKEDITOR.plugins.basePath = COMMON.contentPath+"/res/framework/htmleditor/ckeditor/plugins/";
  164. this.editor = CKEDITOR.replace(editorDiv, editorConfig);
  165. this.editor.addCommand("ecnet", {
  166. exec:function(editor){
  167. this.ecnet();
  168. }.bind(this)
  169. });
  170. this.editor.ui.add('ecnet', CKEDITOR.UI_BUTTON, {
  171. label:MWF.xApplication.process.Xform.LP.intelligentCorrection,
  172. icon: '/x_component_process_Xform/$Form/default/icon/ecnet.png',
  173. command:"ecnet"
  174. });
  175. this._loadEvents();
  176. //this.editor.on("loaded", function(){
  177. // this._loadEvents();
  178. //}.bind(this));
  179. //this.setData(data)
  180. this.editor.on("change", function(){
  181. //this._setBusinessData(this.getData());
  182. //在数据模板和数据表格中时
  183. if( this.parentLine )this._setBusinessData(this.getData());
  184. }.bind(this));
  185. if (this.json.ecnet==="y"){
  186. // this.editor.on( "key", function( evt ) {
  187. // // var char = evt.data.domEvent.$.char;
  188. // // if ([".", ",", "?", ";", "'", " "].indexOf(char)!==-1){
  189. // // this.ecnet(evt.editor.getData());
  190. // // }
  191. // }.bind(this));
  192. // this.editor.on("blur", function(){
  193. // if (!this.notEcnetFlag) this.ecnet(this.getData());
  194. // }.bind(this));
  195. }
  196. this.fireEvent("afterLoad");
  197. this.fieldModuleLoaded = true;
  198. // this._loadEvents();
  199. }.bind(this));
  200. },
  201. getEcnetString: function(node, nodes){
  202. for (var i=0; i<node.childNodes.length; i++){
  203. if (node.childNodes[i].nodeType===Node.TEXT_NODE){
  204. var s = this.ecnetString.length;
  205. this.ecnetString += node.childNodes[i].nodeValue;
  206. var e = this.ecnetString.length;
  207. nodes.push({
  208. "pnode": node,
  209. "node": node.childNodes[i],
  210. "start": s, "end": e
  211. });
  212. }else{
  213. this.getEcnetString(node.childNodes[i], nodes);
  214. }
  215. }
  216. },
  217. createEcnetNode: function(node){
  218. var newNode = node.node.ownerDocument.createElement("span");
  219. var increment = 0;
  220. var html = node.node.nodeValue;
  221. node.ecnets.each(function(ecnet){
  222. var s = ecnet.begin+increment-node.start;
  223. var e = ecnet.end+increment-node.start;
  224. if (s<0) s=0;
  225. if (e>node.end+increment) e = node.end+increment;
  226. var length = html.length;
  227. var left = html.substring(0, s);
  228. var ecnetStr = html.substring(s, e);
  229. var right = html.substring(e, html.length);
  230. html = left+"<span class='o2_ecnet_item' style='color: red'><u>"+ecnetStr+"</u></span>"+right;
  231. increment += (html.length-length);
  232. }.bind(this));
  233. newNode.innerHTML = html;
  234. node.pnode.replaceChild(newNode, node.node);
  235. node.pnode.textNode = node.node;
  236. node.pnode.ecnetNode = newNode;
  237. var _self = this;
  238. var editorFrame = this.editor.document.$.defaultView.frameElement;
  239. var spans = newNode.getElementsByTagName("span");
  240. if (spans.length){
  241. for (var i = 0; i<spans.length; i++){
  242. var span = spans[i];
  243. if (span.className==="o2_ecnet_item"){
  244. var ecnetNode = new Element("div", {"styles": {
  245. "border": "1px solid #999999",
  246. "box-shadow": "0px 0px 5px #999999",
  247. "background-color": "#ffffff",
  248. "position": "fixed",
  249. "display": "none"
  250. }}).inject(editorFrame, "after");
  251. var correctNode = new Element("div", {
  252. "styles": {
  253. "padding": "3px 10px",
  254. "font-weight": "bold",
  255. "font-size": "12px",
  256. "cursor": "pointer"
  257. },
  258. "text": node.ecnets[i].origin+"->"+node.ecnets[i].correct,
  259. "events": {
  260. "mouseover": function(){this.setStyle("background-color", "#dddddd")},
  261. "mouseout": function(){this.setStyle("background-color", "#ffffff")},
  262. "mousedown": function(){
  263. var ecnetNode = this.getParent();
  264. var node = ecnetNode.node;
  265. var item = ecnetNode.node.ecnets[ecnetNode.idx];
  266. var textNode = node.node.ownerDocument.createTextNode(item.correct);
  267. ecnetNode.span.parentNode.replaceChild(textNode, ecnetNode.span);
  268. ecnetNode.destroy();
  269. node.node.nodeValue = node.pnode.ecnetNode.innerText;
  270. node.ecnets.erase(item);
  271. if (!node.ecnets.length){
  272. _self.ecnetNodes.erase(node);
  273. }
  274. }
  275. }
  276. }).inject(ecnetNode);
  277. var ignoreNode = new Element("div", {
  278. "styles": {
  279. "padding": "3px 10px",
  280. "font-size": "12px",
  281. "cursor": "pointer"
  282. },
  283. "text": MWF.xApplication.process.Xform.LP.ignore,
  284. "events": {
  285. "mouseover": function(){this.setStyle("background-color", "#dddddd")},
  286. "mouseout": function(){this.setStyle("background-color", "#ffffff")},
  287. "mousedown": function(){
  288. var ecnetNode = this.getParent();
  289. var node = ecnetNode.node;
  290. var item = ecnetNode.node.ecnets[ecnetNode.idx];
  291. var textNode = node.node.ownerDocument.createTextNode(ecnetNode.span.innerText);
  292. ecnetNode.span.parentNode.replaceChild(textNode, ecnetNode.span);
  293. ecnetNode.destroy();
  294. node.node.nodeValue = node.pnode.ecnetNode.innerText;
  295. node.ecnets.erase(item);
  296. if (!node.ecnets.length){
  297. _self.ecnetNodes.erase(node);
  298. }
  299. }
  300. }
  301. }).inject(ecnetNode);
  302. ecnetNode.node = node;
  303. ecnetNode.idx = i;
  304. span.ecnetNode = ecnetNode;
  305. ecnetNode.span = span;
  306. span.addEventListener("click", function(){
  307. var ecnetNode = this.ecnetNode;
  308. ecnetNode.show();
  309. var y = this.offsetTop;
  310. var x = this.offsetLeft;
  311. var w = this.offsetWidth;
  312. var h = this.offsetHeight;
  313. var p = editorFrame.getPosition();
  314. var s = ecnetNode.getSize();
  315. var top = y+p.y+h+5;
  316. var left = x+p.x-((s.x-w)/2);
  317. ecnetNode.style.left = ""+left+"px";
  318. ecnetNode.style.top = ""+top+"px";
  319. var _span = this;
  320. var hideEcnetNode = function(){
  321. ecnetNode.hide();
  322. _span.ownerDocument.removeEventListener("mousedown", hideEcnetNode);
  323. };
  324. this.ownerDocument.addEventListener("mousedown", hideEcnetNode);
  325. });
  326. }
  327. }
  328. }
  329. //node.pnode.ecnetInforNode = ecnetNode;
  330. // var spans = newNode.getElementsByTagName("span");
  331. // if (spans.length){
  332. // var span = spans[0];
  333. // span.addEventListener("click", function(){
  334. // ecnetNode.style.display = "block";
  335. // var y = span.offsetTop;
  336. // var x = span.offsetLeft;
  337. // var w = span.offsetWidth;
  338. // var h = span.offsetHeight;
  339. // var p = editorFrame.getPosition();
  340. // var s = ecnetNode.getSize();
  341. // var top = y+p.y+h+5;
  342. // var left = x+p.x-((s.x-w)/2);
  343. //
  344. // ecnetNode.style.left = ""+left+"px";
  345. // ecnetNode.style.top = ""+top+"px";
  346. // });
  347. // span.addEventListener("mouseout", function(){});
  348. // }
  349. },
  350. clearEcnetNodes: function(){
  351. if (this.ecnetNodes && this.ecnetNodes.length){
  352. this.ecnetNodes.each(function(node){
  353. if (node.pnode.ecnetNode){
  354. if (node.pnode.ecnetInforNode) node.pnode.ecnetInforNode.destroy();
  355. node.pnode.ecnetInforNode = null;
  356. node.pnode.replaceChild(node.pnode.textNode, node.pnode.ecnetNode);
  357. }
  358. }.bind(this));
  359. this.ecnetNodes = [];
  360. }
  361. },
  362. ecnet: function(data){
  363. //this.editor.document.$.body.innerText
  364. var editorFrame = this.editor.document.$.defaultView.frameElement;
  365. //var data = this.editor.getData();
  366. var body = this.editor.document.$.body;
  367. if (!this.ecnetNodes) this.ecnetNodes = [];
  368. if (this.ecnetNodes.length) this.clearEcnetNodes();
  369. var nodes = [];
  370. this.ecnetString = "";
  371. this.getEcnetString(body, nodes);
  372. MWF.Actions.get("x_general_assemble_control").ecnetCheck({"value": this.ecnetString}, function(json){
  373. if (json.data.itemList && json.data.itemList.length){
  374. nodes.each(function(node){
  375. var items = [];
  376. json.data.itemList.each(function(item){
  377. 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)){
  378. items.push(item);
  379. }
  380. }.bind(this));
  381. if (items.length){
  382. node.ecnets = items;
  383. this.ecnetNodes.push(node);
  384. }
  385. }.bind(this));
  386. this.ecnetNodes.each(function(node){
  387. this.createEcnetNode(node);
  388. }.bind(this));
  389. // var item = json.data.itemList[0];
  390. // var left = data.substring(0, item.begin);
  391. // var ecnetStr = data.substring(item.begin, item.end);
  392. // var right = data.substring(item.end, data.length);
  393. //
  394. // var newData = left+"<span class='o2_ecnet_item' style='color:red' title='"+item.origin+"->"+item.correc+"'><u>"+ecnetStr+"</u></span>"+right;
  395. //this.editor.document.$.body.setSelectionRange(item.begin, item.end);
  396. //this.editor.setData(newData);
  397. // var iframe = editorFrame.clone();
  398. // iframe.inject(this.node);
  399. // iframe.position({
  400. // "relativeTo": editorFrame,
  401. // "position": 'upperLeft',
  402. // "edge": 'upperLeft'
  403. // });
  404. // iframe.contentWindow.document.body.set("html", newData);
  405. }else{
  406. body = null;
  407. nodes = null;
  408. }
  409. }.bind(this));
  410. },
  411. _loadEvents: function(editorConfig){
  412. Object.each(this.json.events, function(e, key){
  413. if (e.code){
  414. this.editor.on(key, function(event){
  415. return this.form.Macro.fire(e.code, this, event);
  416. }.bind(this), this);
  417. }
  418. }.bind(this));
  419. },
  420. addModuleEvent: function(key, fun){
  421. this.editor.on(key, function(event){
  422. return (fun) ? fun(this, event) : null;
  423. }.bind(this), this);
  424. },
  425. _loadValue: function(){
  426. var data = this._getBusinessData();
  427. },
  428. // /**
  429. // * @summary 重置组件的值为默认值或置空。
  430. // * @example
  431. // * this.form.get('fieldId').resetData();
  432. // */
  433. resetData: function(){
  434. this.setData(this._getBusinessData());
  435. },
  436. /**
  437. * @summary 判断组件值是否为空.
  438. * @example
  439. * if( this.form.get('fieldId').isEmpty() ){
  440. * this.form.notice('HTML编辑器不能为空', 'warn');
  441. * }
  442. * @return {Boolean} 值是否为空.
  443. */
  444. isEmpty : function(){
  445. return !this.getData().trim();
  446. },
  447. /**
  448. * 当表单上没有对应组件的时候,可以使用this.data[fieldId]获取值,但是this.form.get('fieldId')无法获取到组件。
  449. * @summary 获取组件值。
  450. * @example
  451. * var data = this.form.get('fieldId').getData();
  452. * @example
  453. * //如果无法确定表单上是否有组件,需要判断
  454. * var data;
  455. * if( this.form.get('fieldId') ){ //判断表单是否有无对应组件
  456. * data = this.form.get('fieldId').getData();
  457. * }else{
  458. * data = this.data['fieldId']; //直接从数据中获取字段值
  459. * }
  460. * @return 组件的数据.
  461. */
  462. getData: function(){
  463. this.clearEcnetNodes();
  464. return this.editor ? this.editor.getData() : this._getBusinessData();
  465. },
  466. /**
  467. * 当表单上没有对应组件的时候,可以使用this.data[fieldId] = data赋值。
  468. * @summary 为组件赋值。
  469. * @param data{String} .
  470. * @example
  471. * this.form.get("fieldId").setData("test"); //赋文本值
  472. * @example
  473. * //如果无法确定表单上是否有组件,需要判断
  474. * if( this.form.get('fieldId') ){ //判断表单是否有无对应组件
  475. * this.form.get('fieldId').setData( data );
  476. * }else{
  477. * this.data['fieldId'] = data;
  478. * }
  479. */
  480. setData: function(data){
  481. this._setBusinessData(data);
  482. if (this.editor) this.editor.setData(data);
  483. },
  484. destroy: function(){
  485. if( this.editor )this.editor.destroy();
  486. },
  487. createErrorNode: function(text){
  488. var node = new Element("div");
  489. var iconNode = new Element("div", {
  490. "styles": {
  491. "width": "20px",
  492. "height": "20px",
  493. "float": "left",
  494. "background": "url("+"../x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  495. }
  496. }).inject(node);
  497. var textNode = new Element("div", {
  498. "styles": {
  499. "line-height": "20px",
  500. "margin-left": "20px",
  501. "color": "red",
  502. "word-break": "keep-all"
  503. },
  504. "text": text
  505. }).inject(node);
  506. return node;
  507. },
  508. notValidationMode: function(text){
  509. if (!this.isNotValidationMode){
  510. this.isNotValidationMode = true;
  511. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  512. this.node.setStyle("border", "1px solid red");
  513. this.errNode = this.createErrorNode(text).inject(this.node, "after");
  514. this.showNotValidationMode(this.node);
  515. if (!this.errNode.isIntoView()) this.errNode.scrollIntoView(false);
  516. }
  517. },
  518. showNotValidationMode: function(node){
  519. var p = node.getParent("div");
  520. if (p){
  521. if (p.get("MWFtype") == "tab$Content"){
  522. if (p.getParent("div").getStyle("display")=="none"){
  523. var contentAreaNode = p.getParent("div").getParent("div");
  524. var tabAreaNode = contentAreaNode.getPrevious("div");
  525. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  526. var tabNode = tabAreaNode.getLast().getFirst().getChildren()[idx];
  527. tabNode.click();
  528. p = tabAreaNode.getParent("div");
  529. }
  530. }
  531. this.showNotValidationMode(p);
  532. }
  533. },
  534. validationMode: function(){
  535. if (this.isNotValidationMode){
  536. this.isNotValidationMode = false;
  537. this.node.setStyles(this.node.retrieve("borderStyle"));
  538. if (this.errNode){
  539. this.errNode.destroy();
  540. this.errNode = null;
  541. }
  542. }
  543. },
  544. validationConfigItem: function(routeName, data){
  545. var flag = (data.status=="all") ? true: (routeName == data.decision);
  546. if (flag){
  547. var n = this.getData();
  548. var v = (data.valueType=="value") ? n : n.length;
  549. switch (data.operateor){
  550. case "isnull":
  551. if (!v){
  552. this.notValidationMode(data.prompt);
  553. return false;
  554. }
  555. break;
  556. case "notnull":
  557. if (v){
  558. this.notValidationMode(data.prompt);
  559. return false;
  560. }
  561. break;
  562. case "gt":
  563. if (v>data.value){
  564. this.notValidationMode(data.prompt);
  565. return false;
  566. }
  567. break;
  568. case "lt":
  569. if (v<data.value){
  570. this.notValidationMode(data.prompt);
  571. return false;
  572. }
  573. break;
  574. case "equal":
  575. if (v==data.value){
  576. this.notValidationMode(data.prompt);
  577. return false;
  578. }
  579. break;
  580. case "neq":
  581. if (v!=data.value){
  582. this.notValidationMode(data.prompt);
  583. return false;
  584. }
  585. break;
  586. case "contain":
  587. if (v.indexOf(data.value)!=-1){
  588. this.notValidationMode(data.prompt);
  589. return false;
  590. }
  591. break;
  592. case "notcontain":
  593. if (v.indexOf(data.value)==-1){
  594. this.notValidationMode(data.prompt);
  595. return false;
  596. }
  597. break;
  598. }
  599. }
  600. return true;
  601. },
  602. validationConfig: function(routeName, opinion){
  603. if (this.json.validationConfig){
  604. if (this.json.validationConfig.length){
  605. for (var i=0; i<this.json.validationConfig.length; i++) {
  606. var data = this.json.validationConfig[i];
  607. if (!this.validationConfigItem(routeName, data)) return false;
  608. }
  609. }
  610. return true;
  611. }
  612. return true;
  613. },
  614. validation: function(routeName, opinion){
  615. if (!this.validationConfig(routeName, opinion)) return false;
  616. if (!this.json.validation) return true;
  617. if (!this.json.validation.code) return true;
  618. this.currentRouteName = routeName;
  619. var flag = this.form.Macro.exec(this.json.validation.code, this);
  620. this.currentRouteName = "";
  621. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  622. if (flag.toString()!="true"){
  623. this.notValidationMode(flag);
  624. return false;
  625. }
  626. return true;
  627. },
  628. getExcelData: function(){
  629. return this.getData();
  630. },
  631. setExcelData: function(data){
  632. if( typeOf(data) === "string" )data = data.replace(/&#10;/g,"<br>"); //excel字段换行是 &#10
  633. this.excelData = data;
  634. this.setData(data, true);
  635. },
  636. validationExcel: function () {
  637. if (!this.isReadonly()){
  638. var errorList = this.validationConfigExcel();
  639. if (errorList.length) return errorList;
  640. if (!this.json.validation) return [];
  641. if (!this.json.validation.code) return [];
  642. var flag = this.form.Macro.exec(this.json.validation.code, this);
  643. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  644. if (flag.toString() !== "true") {
  645. return [flag];
  646. }
  647. }
  648. return [];
  649. },
  650. validationConfigExcel: function () {
  651. var errorList = [];
  652. if (this.json.validationConfig){
  653. if (this.json.validationConfig.length){
  654. for (var i=0; i<this.json.validationConfig.length; i++) {
  655. var flag = this.validationConfigItemExcel(this.json.validationConfig[i]);
  656. if ( flag !== true ){
  657. errorList.push( flag );
  658. }
  659. }
  660. }
  661. }
  662. return errorList;
  663. },
  664. validationConfigItemExcel: function(data){
  665. if ( data.status==="all"){
  666. var n = this._getBusinessData();
  667. var v = (data.valueType==="value") ? n : n.length;
  668. switch (data.operateor){
  669. case "isnull":
  670. if (!v)return data.prompt;
  671. break;
  672. case "notnull":
  673. if (v)return data.prompt;
  674. break;
  675. case "gt":
  676. if (v>data.value)return data.prompt;
  677. break;
  678. case "lt":
  679. if (v<data.value)return data.prompt;
  680. break;
  681. case "equal":
  682. if (v===data.value)return data.prompt;
  683. break;
  684. case "neq":
  685. if (v!==data.value)return data.prompt;
  686. break;
  687. case "contain":
  688. if (v.indexOf(data.value)!==-1) return data.prompt;
  689. break;
  690. case "notcontain":
  691. if (v.indexOf(data.value)===-1)return data.prompt;
  692. break;
  693. }
  694. }
  695. return true;
  696. }
  697. });