TinyMCEEditor.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. /** @class TinyMCEEditor HTML编辑器。
  3. * @o2cn TinyMCEEditor编辑器
  4. * @example
  5. * //可以在脚本中获取该组件
  6. * //方法1:
  7. * var tinyMCEEditor = this.form.get("name"); //获取组件
  8. * //方法2
  9. * var tinyMCEEditor = this.target; //在组件事件脚本中获取
  10. * @extends MWF.xApplication.process.Xform.$Module
  11. * @o2category FormComponents
  12. * @o2range {Process|CMS}
  13. * @hideconstructor
  14. */
  15. MWF.xApplication.process.Xform.TinyMCEEditor = MWF.APPTinyMCEEditor = new Class(
  16. /** @lends MWF.xApplication.process.Xform.TinyMCEEditor# */
  17. {
  18. Extends: MWF.APP$Module,
  19. options: {
  20. /**
  21. * 组件异步加载后触发.
  22. * @event MWF.xApplication.process.Xform.TinyMCEEditor#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. } else {
  84. var config = Object.clone(this.json.editorProperties);
  85. if (this.json.config) {
  86. if (this.json.config.code) {
  87. var obj = this.form.Macro.exec(this.json.config.code, this);
  88. Object.each(obj, function (v, k) {
  89. config[k] = v;
  90. });
  91. }
  92. }
  93. this.loadTinyMCEEditor(config);
  94. }
  95. // this._loadValue();
  96. },
  97. loadLazyImage: function (callback) {
  98. o2.require("o2.widget.ImageLazyLoader", function () {
  99. var loadder = new o2.widget.ImageLazyLoader(this.node, this._getBusinessData());
  100. loadder.load(function () {
  101. if (callback) callback();
  102. }.bind(this))
  103. }.bind(this));
  104. },
  105. loadImageViewer: function () {
  106. o2.require("o2.widget.ImageViewer", function () {
  107. var imageViewer = new o2.widget.ImageViewer(this.node);
  108. imageViewer.load();
  109. }.bind(this));
  110. },
  111. loadResource: function ( callback ) {
  112. o2.load([
  113. "../o2_lib/tinymce/tinymce_5.9.2/tinymce.min.js",
  114. "../o2_lib/tinymce/tinymce_5.9.2/o2config.js"
  115. ], function () {
  116. var config = o2.TinyMCEConfig( this.form.json.mode === "Mobile" );
  117. callback( config );
  118. }.bind(this))
  119. },
  120. getImageUploadOption: function(){
  121. return {
  122. localImageMaxWidth : 2000,
  123. reference: this.form.businessData.work.job,
  124. referenceType: "processPlatformJob"
  125. };
  126. },
  127. getEditorId: function(){
  128. return this.form.businessData.work.id +"_"+this.json.id.split(".").join("_") + "_" + (layout.mobile ? "mobile" : "pc");
  129. },
  130. loadTinyMCEEditor: function (config) {
  131. this.loadResource( function( defaultConfig ){
  132. var editorConfig = Object.merge(defaultConfig, config || {});
  133. var id = this.getEditorId();
  134. editorConfig.selector = '#'+id;
  135. var editorDiv = new Element("div", {"id": id}).inject(this.node);
  136. var htmlData = this._getBusinessData();
  137. if (htmlData) {
  138. editorDiv.set("html", htmlData || "");
  139. } else if (this.json.templateCode) {
  140. editorDiv.set("html", this.json.templateCode || "");
  141. }
  142. // var height = this.node.getSize().y;
  143. editorConfig.base64Encode = !layout.mobile && (this.json.base64Encode === "y");
  144. editorConfig.enablePreview = (this.json.enablePreview !== "n");
  145. var options = this.getImageUploadOption();
  146. for(var key in options){
  147. editorConfig[key] = options[key];
  148. }
  149. var setup = editorConfig.setup;
  150. var _self = this;
  151. editorConfig.setup = function(editor) {
  152. this.form.app.addEvent("queryClose", function () {
  153. try{
  154. editor.destroy();
  155. _self.editor = null;
  156. }catch (e) {}
  157. });
  158. this.form.app.addEvent("queryReload", function () {
  159. try{
  160. editor.destroy();
  161. _self.editor = null;
  162. }catch (e) {}
  163. });
  164. this.form.addEvent("reloadReadForm", function () {
  165. try{
  166. editor.destroy();
  167. _self.editor = null;
  168. }catch (e) {}
  169. });
  170. this._loadEvents(editor);
  171. if(setup)setup(editor);
  172. }.bind(this);
  173. var init_instance_callback = editorConfig.init_instance_callback;
  174. editorConfig.init_instance_callback = function(editor) {
  175. this.editor = editor;
  176. this.editor.on("change", function () {
  177. this._setBusinessData(this.getData());
  178. }.bind(this));
  179. if(init_instance_callback)init_instance_callback(editor);
  180. if(editorConfig.init_instance_defaultCallback)editorConfig.init_instance_defaultCallback(editor);
  181. this.fireEvent("afterLoad");
  182. }.bind(this);
  183. tinymce.init(editorConfig);
  184. this.fieldModuleLoaded = true;
  185. }.bind(this));
  186. },
  187. _loadEvents: function (editor) {
  188. Object.each(this.json.events, function (e, key) {
  189. if (e.code) {
  190. editor.on(key, function (event) {
  191. return this.form.Macro.fire(e.code, this, event);
  192. }.bind(this), this);
  193. }
  194. }.bind(this));
  195. },
  196. addModuleEvent: function (key, fun) {
  197. this.editor.on(key, function (event) {
  198. return (fun) ? fun(this, event) : null;
  199. }.bind(this), this);
  200. },
  201. _loadValue: function () {
  202. var data = this._getBusinessData();
  203. },
  204. // /**
  205. // * @summary 重置组件的值为默认值或置空。
  206. // * @example
  207. // * this.form.get('fieldId').resetData();
  208. // */
  209. resetData: function () {
  210. this.setData(this._getBusinessData());
  211. },
  212. /**
  213. * @summary 判断组件值是否为空.
  214. * @example
  215. * if( this.form.get('fieldId').isEmpty() ){
  216. * this.form.notice('HTML编辑器不能为空', 'warn');
  217. * }
  218. * @return {Boolean} 值是否为空.
  219. */
  220. isEmpty: function () {
  221. return !this.getData().trim();
  222. },
  223. /**
  224. * 当表单上没有对应组件的时候,可以使用this.data[fieldId]获取值,但是this.form.get('fieldId')无法获取到组件。
  225. * @summary 获取组件值。
  226. * @example
  227. * var data = this.form.get('fieldId').getData();
  228. * @example
  229. * //如果无法确定表单上是否有组件,需要判断
  230. * var data;
  231. * if( this.form.get('fieldId') ){ //判断表单是否有无对应组件
  232. * data = this.form.get('fieldId').getData();
  233. * }else{
  234. * data = this.data['fieldId']; //直接从数据中获取字段值
  235. * }
  236. * @return 组件的数据.
  237. */
  238. getData: function () {
  239. return (this.editor && this.editor.getContent) ? this.editor.getContent() : this._getBusinessData();
  240. },
  241. /**
  242. * 当表单上没有对应组件的时候,可以使用this.data[fieldId] = data赋值。
  243. * @summary 为组件赋值。
  244. * @param data{String} .
  245. * @example
  246. * this.form.get("fieldId").setData("test"); //赋文本值
  247. * @example
  248. * //如果无法确定表单上是否有组件,需要判断
  249. * if( this.form.get('fieldId') ){ //判断表单是否有无对应组件
  250. * this.form.get('fieldId').setData( data );
  251. * }else{
  252. * this.data['fieldId'] = data;
  253. * }
  254. */
  255. setData: function (data) {
  256. this._setBusinessData(data);
  257. if (this.editor && this.editor.setContent) this.editor.setContent(data);
  258. },
  259. destroy: function(){
  260. if( this.editor ){
  261. this.editor.destroy();
  262. this.editor = null;
  263. }
  264. },
  265. createErrorNode: function (text) {
  266. var node = new Element("div");
  267. var iconNode = new Element("div", {
  268. "styles": {
  269. "width": "20px",
  270. "height": "20px",
  271. "float": "left",
  272. "background": "url(" + "../x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  273. }
  274. }).inject(node);
  275. var textNode = new Element("div", {
  276. "styles": {
  277. "line-height": "20px",
  278. "margin-left": "20px",
  279. "color": "red",
  280. "word-break": "keep-all"
  281. },
  282. "text": text
  283. }).inject(node);
  284. return node;
  285. },
  286. notValidationMode: function (text) {
  287. if (!this.isNotValidationMode) {
  288. this.isNotValidationMode = true;
  289. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  290. this.node.setStyle("border", "1px solid red");
  291. this.errNode = this.createErrorNode(text).inject(this.node, "after");
  292. this.showNotValidationMode(this.node);
  293. if (!this.errNode.isIntoView()) this.errNode.scrollIntoView(false);
  294. }
  295. },
  296. showNotValidationMode: function (node) {
  297. var p = node.getParent("div");
  298. if (p) {
  299. if (p.get("MWFtype") == "tab$Content") {
  300. if (p.getParent("div").getStyle("display") == "none") {
  301. var contentAreaNode = p.getParent("div").getParent("div");
  302. var tabAreaNode = contentAreaNode.getPrevious("div");
  303. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  304. var tabNode = tabAreaNode.getLast().getFirst().getChildren()[idx];
  305. tabNode.click();
  306. p = tabAreaNode.getParent("div");
  307. }
  308. }
  309. this.showNotValidationMode(p);
  310. }
  311. },
  312. validationMode: function () {
  313. if (this.isNotValidationMode) {
  314. this.isNotValidationMode = false;
  315. this.node.setStyles(this.node.retrieve("borderStyle"));
  316. if (this.errNode) {
  317. this.errNode.destroy();
  318. this.errNode = null;
  319. }
  320. }
  321. },
  322. validationConfigItem: function (routeName, data) {
  323. var flag = (data.status == "all") ? true : (routeName == data.decision);
  324. if (flag) {
  325. var n = this.getData();
  326. var v = (data.valueType == "value") ? n : n.length;
  327. switch (data.operateor) {
  328. case "isnull":
  329. if (!v) {
  330. this.notValidationMode(data.prompt);
  331. return false;
  332. }
  333. break;
  334. case "notnull":
  335. if (v) {
  336. this.notValidationMode(data.prompt);
  337. return false;
  338. }
  339. break;
  340. case "gt":
  341. if (v > data.value) {
  342. this.notValidationMode(data.prompt);
  343. return false;
  344. }
  345. break;
  346. case "lt":
  347. if (v < data.value) {
  348. this.notValidationMode(data.prompt);
  349. return false;
  350. }
  351. break;
  352. case "equal":
  353. if (v == data.value) {
  354. this.notValidationMode(data.prompt);
  355. return false;
  356. }
  357. break;
  358. case "neq":
  359. if (v != data.value) {
  360. this.notValidationMode(data.prompt);
  361. return false;
  362. }
  363. break;
  364. case "contain":
  365. if (v.indexOf(data.value) != -1) {
  366. this.notValidationMode(data.prompt);
  367. return false;
  368. }
  369. break;
  370. case "notcontain":
  371. if (v.indexOf(data.value) == -1) {
  372. this.notValidationMode(data.prompt);
  373. return false;
  374. }
  375. break;
  376. }
  377. }
  378. return true;
  379. },
  380. validationConfig: function (routeName, opinion) {
  381. if (this.json.validationConfig) {
  382. if (this.json.validationConfig.length) {
  383. for (var i = 0; i < this.json.validationConfig.length; i++) {
  384. var data = this.json.validationConfig[i];
  385. if (!this.validationConfigItem(routeName, data)) return false;
  386. }
  387. }
  388. return true;
  389. }
  390. return true;
  391. },
  392. validation: function (routeName, opinion) {
  393. if (!this.validationConfig(routeName, opinion)) return false;
  394. if (!this.json.validation) return true;
  395. if (!this.json.validation.code) return true;
  396. this.currentRouteName = routeName;
  397. var flag = this.form.Macro.exec(this.json.validation.code, this);
  398. this.currentRouteName = "";
  399. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  400. if (flag.toString() != "true") {
  401. this.notValidationMode(flag);
  402. return false;
  403. }
  404. return true;
  405. },
  406. getExcelData: function(){
  407. return this.getData();
  408. },
  409. setExcelData: function(data){
  410. if( typeOf(data) === "string" )data = data.replace(/&#10;/g,"<br>"); //excel字段换行是 &#10
  411. this.excelData = data;
  412. this.setData(data, true);
  413. },
  414. validationExcel: function () {
  415. if (!this.isReadonly()){
  416. var errorList = this.validationConfigExcel();
  417. if (errorList.length) return errorList;
  418. if (!this.json.validation) return [];
  419. if (!this.json.validation.code) return [];
  420. var flag = this.form.Macro.exec(this.json.validation.code, this);
  421. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  422. if (flag.toString() !== "true") {
  423. return [flag];
  424. }
  425. }
  426. return [];
  427. },
  428. validationConfigExcel: function () {
  429. var errorList = [];
  430. if (this.json.validationConfig){
  431. if (this.json.validationConfig.length){
  432. for (var i=0; i<this.json.validationConfig.length; i++) {
  433. var flag = this.validationConfigItemExcel(this.json.validationConfig[i]);
  434. if ( flag !== true ){
  435. errorList.push( flag );
  436. }
  437. }
  438. }
  439. }
  440. return errorList;
  441. },
  442. validationConfigItemExcel: function(data){
  443. if ( data.status==="all"){
  444. var n = this._getBusinessData();
  445. var v = (data.valueType==="value") ? n : n.length;
  446. switch (data.operateor){
  447. case "isnull":
  448. if (!v)return data.prompt;
  449. break;
  450. case "notnull":
  451. if (v)return data.prompt;
  452. break;
  453. case "gt":
  454. if (v>data.value)return data.prompt;
  455. break;
  456. case "lt":
  457. if (v<data.value)return data.prompt;
  458. break;
  459. case "equal":
  460. if (v===data.value)return data.prompt;
  461. break;
  462. case "neq":
  463. if (v!==data.value)return data.prompt;
  464. break;
  465. case "contain":
  466. if (v.indexOf(data.value)!==-1) return data.prompt;
  467. break;
  468. case "notcontain":
  469. if (v.indexOf(data.value)===-1)return data.prompt;
  470. break;
  471. }
  472. }
  473. return true;
  474. }
  475. });