Form.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. MWF.xApplication.cms = MWF.xApplication.cms || {};
  2. MWF.xApplication.cms.Xform = MWF.xApplication.cms.Xform || {};
  3. MWF.require("MWF.widget.Common", null, false);
  4. MWF.require("MWF.xAction.org.express.RestActions", null, false);
  5. MWF.xDesktop.requireApp("Selector", "package", null, false);
  6. MWF.xDesktop.requireApp("process.Xform", "Form", null, false);
  7. MWF.require("MWF.widget.O2Identity", null, false);
  8. MWF.xDesktop.requireApp("cms.Xform", "Package", null, false);
  9. MWF.xApplication.cms.Xform.Form = MWF.CMSForm = new Class({
  10. Implements: [Options, Events],
  11. Extends: MWF.APPForm,
  12. options: {
  13. "style": "default",
  14. "readonly": false,
  15. "cssPath": "",
  16. "autoSave": false,
  17. "saveOnClose": false,
  18. "showAttachment": true,
  19. "moduleEvents": ["queryLoad",
  20. "beforeLoad",
  21. "postLoad",
  22. "afterLoad",
  23. "beforeSave",
  24. "postSave",
  25. "afterSave",
  26. "beforeClose",
  27. "beforePublish",
  28. "postPublish",
  29. "afterPublish",
  30. "beforeDelete",
  31. "afterDelete",
  32. "beforeModulesLoad",
  33. "resize",
  34. "afterModulesLoad"]
  35. },
  36. initialize: function (node, data, options) {
  37. this.setOptions(options);
  38. this.container = $(node);
  39. this.container.setStyle("-webkit-user-select", "text");
  40. this.data = data;
  41. this.json = data.json;
  42. this.html = data.html;
  43. this.path = "/x_component_cms_Xform/$Form/";
  44. this.cssPath = this.options.cssPath || "/x_component_cms_Xform/$Form/" + this.options.style + "/css.wcss";
  45. this._loadCss();
  46. this.modules = [];
  47. this.all = {};
  48. this.forms = {};
  49. //if (!this.personActions) this.personActions = new MWF.xAction.org.express.RestActions();
  50. },
  51. load: function (callback) {
  52. if (this.app) {
  53. if (this.app.formNode) this.app.formNode.setStyles(this.json.styles);
  54. if (this.app.addEvent) this.app.addEvent("resize", function () {
  55. this.fireEvent("resize");
  56. }.bind(this))
  57. }
  58. //if (!this.businessData.control.allowSave) this.setOptions({"readonly": true});
  59. this.Macro = new MWF.CMSMacro.CMSFormContext(this);
  60. this.container.set("html", this.html);
  61. this.node = this.container.getFirst();
  62. this._loadEvents();
  63. if (this.fireEvent("queryLoad")) {
  64. MWF.xDesktop.requireApp("cms.Xform", "lp." + MWF.language, null, false);
  65. // this.container.setStyles(this.css.container);
  66. this._loadBusinessData();
  67. this.fireEvent("beforeLoad");
  68. if (this.app) if (this.app.fireEvent) this.app.fireEvent("beforeLoad");
  69. this.loadContent(callback)
  70. }
  71. },
  72. loadContent: function (callback) {
  73. this.subformCount = 0;
  74. this.subformLoadedCount = 0;
  75. this.subformLoaded = [this.json.id];
  76. this._loadHtml();
  77. this._loadForm();
  78. this.fireEvent("beforeModulesLoad");
  79. this._loadModules(this.node);
  80. if (!this.options.readonly) {
  81. if (this.options.autoSave) this.autoSave();
  82. this.app.addEvent("queryClose", function () {
  83. if (this.options.saveOnClose && this.businessData.document.docStatus == "draft") this.saveDocument(null, true);
  84. //if (this.autoSaveTimerID) window.clearInterval(this.autoSaveTimerID);
  85. Object.each(this.forms, function (module, id) {
  86. if (module.json.type == "Htmleditor" && module.editor) {
  87. //if(CKEDITOR.currentImageDialog)CKEDITOR.currentImageDialog.destroy();
  88. //CKEDITOR.currentImageDialog = null;
  89. CKEDITOR.remove(module.editor);
  90. delete module.editor
  91. }
  92. });
  93. }.bind(this));
  94. }
  95. this.fireEvent("afterModulesLoad");
  96. this.fireEvent("postLoad");
  97. this.fireEvent("afterLoad");
  98. if (this.app && this.app.fireEvent) {
  99. this.app.fireEvent("afterModulesLoad");
  100. this.app.fireEvent("postLoad");
  101. this.app.fireEvent("afterLoad");
  102. }
  103. // 告诉移动端表单加载完成
  104. if (this.app && this.app.mobile) {
  105. if (callback) callback();
  106. }
  107. },
  108. autoSave: function () {
  109. //this.autoSaveTimerID = window.setInterval(function(){
  110. // this.saveDocument();
  111. //}.bind(this), 300000);
  112. },
  113. _loadBusinessData: function () {
  114. if (!this.businessData) {
  115. this.businessData = {
  116. "data": {}
  117. };
  118. }
  119. },
  120. _loadEvents: function () {
  121. Object.each(this.json.events, function (e, key) {
  122. if (e.code) {
  123. if (this.options.moduleEvents.indexOf(key) != -1) {
  124. this.addEvent(key, function (event) {
  125. return this.Macro.fire(e.code, this, event);
  126. }.bind(this));
  127. } else {
  128. if (key == "load") {
  129. this.addEvent("postLoad", function () {
  130. return this.Macro.fire(e.code, this);
  131. }.bind(this));
  132. } else if (key == "submit") {
  133. this.addEvent("beforePublish", function () {
  134. return this.Macro.fire(e.code, this);
  135. }.bind(this));
  136. } else {
  137. this.node.addEvent(key, function (event) {
  138. return this.Macro.fire(e.code, this, event);
  139. }.bind(this));
  140. }
  141. }
  142. }
  143. }.bind(this));
  144. },
  145. _loadModules: function (dom) {
  146. //var subDom = this.node.getFirst();
  147. //while (subDom){
  148. // if (subDom.get("MWFtype")){
  149. // var json = this._getDomjson(subDom);
  150. // var module = this._loadModule(json, subDom);
  151. // this.modules.push(module);
  152. // }
  153. // subDom = subDom.getNext();
  154. //}
  155. var moduleNodes = this._getModuleNodes(dom);
  156. moduleNodes.each(function (node) {
  157. var json = this._getDomjson(node);
  158. if (!this.options.showAttachment && json.type == "Attachment") {
  159. return;
  160. }
  161. //移动端去掉操作栏
  162. if (this.app.mobile && json.type === "Actionbar") {
  163. return;
  164. }
  165. var module = this._loadModule(json, node);
  166. this.modules.push(module);
  167. }.bind(this));
  168. },
  169. _loadModule: function (json, node, beforeLoad) {
  170. if (!json) return;
  171. if (!MWF["CMS" + json.type]) {
  172. MWF.xDesktop.requireApp("cms.Xform", json.type, null, false);
  173. }
  174. var module = new MWF["CMS" + json.type](node, json, this);
  175. if (beforeLoad) beforeLoad.apply(module);
  176. if (!this.all[json.id]) this.all[json.id] = module;
  177. if (module.field) {
  178. if (!this.forms[json.id]) this.forms[json.id] = module;
  179. }
  180. module.readonly = this.options.readonly;
  181. module.load();
  182. return module;
  183. },
  184. //getData: function(){
  185. // var data= Object.clone(this.businessData.data);
  186. // Object.each(this.forms, function(module, id){
  187. // debugger;
  188. // if (module.json.section=="yes"){
  189. // data[id] = this.getSectionData(module, data[id]);
  190. // }else{
  191. // data[id] = module.getData();
  192. // }
  193. // }.bind(this));
  194. //
  195. // this.businessData.data = data;
  196. // this.Macro.environment.setData(this.businessData.data);
  197. // return data;
  198. //},
  199. trim: function (array) {
  200. var arr = [];
  201. array.each(function (v) {
  202. if (v) arr.push(v);
  203. });
  204. return arr;
  205. },
  206. transportPermissionData: function (array, t) {
  207. var result = [];
  208. array.each(function (data) {
  209. var dn = typeOf(data) === "string" ? data : data.distinguishedName;
  210. if (dn) {
  211. var flag = dn.substr(dn.length - 1, 1);
  212. var type;
  213. switch (flag.toLowerCase()) {
  214. case "i":
  215. type = "人员"; //"身份";
  216. break;
  217. case "p":
  218. type = "人员";
  219. break;
  220. case "u":
  221. type = "组织";
  222. break;
  223. case "g":
  224. type = "群组";
  225. break;
  226. case "r":
  227. type = "角色";
  228. break;
  229. default:
  230. type = "";
  231. //result.push( data );
  232. }
  233. if (type) {
  234. result.push({
  235. permission: t == "author" ? "作者" : "阅读",
  236. permissionObjectType: type,
  237. permissionObjectName: dn
  238. })
  239. }
  240. }
  241. });
  242. return result.length > 0 ? result : null;
  243. },
  244. getSpecialData: function () {
  245. var data = this.businessData.data;
  246. var readers = [];
  247. var authors = [];
  248. var pictures = [];
  249. var cloudPictures = [];
  250. var summary = "";
  251. Object.each(this.forms, function (module, id) {
  252. if (module.json.type == "Readerfield" || module.json.type == "Reader") {
  253. if (module.json.section == "yes") {
  254. readers = readers.concat(this.getSectionData(module, data[id]));
  255. } else {
  256. readers = readers.concat(module.getData());
  257. }
  258. }
  259. if (module.json.type == "Authorfield" || module.json.type == "Author") {
  260. if (module.json.section == "yes") {
  261. authors = authors.concat(this.getSectionData(module, data[id]));
  262. } else {
  263. authors = authors.concat(module.getData());
  264. }
  265. }
  266. if (module.json.type == "ImageClipper") {
  267. var d = module.getData();
  268. if (d) pictures.push(d);
  269. }
  270. if (module.json.type == "Htmleditor") {
  271. var text = module.getText();
  272. summary = text.substr(0, 80);
  273. cloudPictures = cloudPictures.concat(module.getImageIds());
  274. }
  275. });
  276. if (data.processOwnerList && typeOf(data.processOwnerList) == "array") { //如果是流程中发布的
  277. var owner = { personValue: [] };
  278. data.processOwnerList.each(function (p) {
  279. owner.personValue.push({
  280. name: p,
  281. type: "person"
  282. });
  283. });
  284. readers = readers.concat(owner);
  285. }
  286. return {
  287. readers: this.transportPermissionData(readers, "reader"),
  288. authors: this.transportPermissionData(authors, "author"),
  289. pictures: pictures,
  290. summary: summary,
  291. cloudPictures: cloudPictures
  292. };
  293. },
  294. getDocumentData: function (formData) {
  295. var data = Object.clone(this.businessData.document);
  296. if (formData.subject) {
  297. data.title = formData.subject;
  298. data.subject = formData.subject;
  299. this.businessData.document.title = formData.subject;
  300. this.businessData.document.subject = formData.subject;
  301. }
  302. data.isNewDocument = false;
  303. return data;
  304. },
  305. saveDocument: function (callback, sync) {
  306. this.fireEvent("beforeSave");
  307. if (this.businessData.document.docStatus == "published") {
  308. if (!this.formValidation("publish")) {
  309. this.app.content.unmask();
  310. //if (callback) callback();
  311. return false;
  312. }
  313. }
  314. if (!this.formSaveValidation()) {
  315. this.app.content.unmask();
  316. if (callback) callback();
  317. return false;
  318. }
  319. var data = this.getData();
  320. var specialData = this.getSpecialData();
  321. var documentData = this.getDocumentData(data);
  322. documentData.readerList = specialData.readers;
  323. documentData.authorList = specialData.authors;
  324. documentData.pictureList = specialData.pictures;
  325. documentData.summary = specialData.summary;
  326. documentData.cloudPictures = specialData.cloudPictures;
  327. documentData.docData = data;
  328. delete documentData.attachmentList;
  329. this.fireEvent("postSave", [documentData]);
  330. if (this.officeList) {
  331. this.officeList.each(function (module) {
  332. module.save(history);
  333. });
  334. }
  335. this.documentAction.saveDocument(documentData, function () {
  336. //this.documentAction.saveData(function(json){
  337. this.app.notice(MWF.xApplication.cms.Xform.LP.dataSaved, "success");
  338. this.businessData.data.isNew = false;
  339. this.fireEvent("afterSave");
  340. if (callback) callback();
  341. //}.bind(this), null, this.businessData.document.id, data, !sync );
  342. }.bind(this), null, !sync);
  343. },
  344. closeDocument: function () {
  345. this.fireEvent("beforeClose");
  346. if (this.app) {
  347. this.app.close();
  348. }
  349. },
  350. printDocument: function (form) {
  351. var form = form;
  352. if (!form) {
  353. form = this.json.id;
  354. if (this.json.printForm && this.json.printForm !== "none") form = this.json.printForm;
  355. }
  356. window.open("/x_desktop/printcmsdoc.html?documentid=" + this.businessData.document.id + "&form=" + form);
  357. },
  358. formValidation: function (status) {
  359. if (this.options.readonly) return true;
  360. var flag = true;
  361. //flag = this.validation();
  362. Object.each(this.forms, function (field, key) {
  363. field.validationMode();
  364. if (!field.validation(status)) {
  365. flag = false;
  366. }
  367. }.bind(this));
  368. return flag;
  369. },
  370. formSaveValidation: function () {
  371. if (!this.json.validationSave) return true;
  372. if (!this.json.validationSave.code) return true;
  373. var flag = this.Macro.exec(this.json.validationSave.code, this);
  374. if (!flag) flag = MWF.xApplication.cms.Xform.LP.notValidation;
  375. if (typeOf(flag) === "string") {
  376. if (flag !== "true") {
  377. this.app.notice(flag, "error");
  378. return false;
  379. }
  380. } else if (flag.toString() != "true") {
  381. return false;
  382. }
  383. return true;
  384. },
  385. formPublishValidation: function () {
  386. if (!this.json.validationPublish) return true;
  387. if (!this.json.validationPublish.code) return true;
  388. var flag = this.Macro.exec(this.json.validationPublish.code, this);
  389. if (!flag) flag = MWF.xApplication.cms.Xform.LP.notValidation;
  390. if (typeOf(flag) === "string") {
  391. if (flag !== "true") {
  392. this.app.notice(flag, "error");
  393. return false;
  394. }
  395. } else if (flag.toString() != "true") {
  396. return false;
  397. }
  398. return true;
  399. },
  400. publishDocument: function (callback) {
  401. this.fireEvent("beforePublish");
  402. this.app.content.mask({
  403. "destroyOnHide": true,
  404. "style": this.app.css.maskNode
  405. });
  406. if (!this.formValidation("publish")) {
  407. this.app.content.unmask();
  408. //if (callback) callback();
  409. return false;
  410. }
  411. if (!this.formPublishValidation()) {
  412. this.app.content.unmask();
  413. if (callback) callback();
  414. return false;
  415. }
  416. var data = this.getData();
  417. var specialData = this.getSpecialData();
  418. //this.documentAction.saveData(function(json){
  419. var documentData = this.getDocumentData(data);
  420. documentData.readerList = specialData.readers;
  421. documentData.authorList = specialData.authors;
  422. documentData.pictureList = specialData.pictures;
  423. documentData.summary = specialData.summary;
  424. documentData.cloudPictures = specialData.cloudPictures;
  425. documentData.docData = data;
  426. delete documentData.attachmentList;
  427. //this.documentAction.saveDocument(documentData, function(){
  428. this.fireEvent("postPublish", [documentData]);
  429. if (this.officeList) {
  430. this.officeList.each(function (module) {
  431. module.save(history);
  432. });
  433. }
  434. this.documentAction.publishDocumentComplex(documentData, function (json) {
  435. this.businessData.data.isNew = false;
  436. this.fireEvent("afterPublish");
  437. if (callback) callback();
  438. if (this.app.mobile) {
  439. this.app.content.unmask();
  440. console.log('这里是移动端');
  441. } else {
  442. if (this.businessData.document.title) {
  443. this.app.notice(MWF.xApplication.cms.Xform.LP.documentPublished + ": “" + this.businessData.document.title + "”", "success");
  444. } else {
  445. this.app.notice(MWF.xApplication.cms.Xform.LP.documentPublished, "success");
  446. }
  447. this.options.saveOnClose = false;
  448. }
  449. this.app.close();
  450. }.bind(this));
  451. //}.bind(this))
  452. //}.bind(this), null, this.businessData.document.id, data);
  453. },
  454. //publishDocument_bak: function(callback){
  455. // this.fireEvent("beforePublish");
  456. // this.app.content.mask({
  457. // "destroyOnHide": true,
  458. // "style": this.app.css.maskNode
  459. // });
  460. // if (!this.formValidation("publish")){
  461. // this.app.content.unmask();
  462. // if (callback) callback();
  463. // return false;
  464. // }
  465. //
  466. // var data = this.getData();
  467. // var specialData = this.getSpecialData();
  468. // this.documentAction.saveData(function(json){
  469. // this.businessData.data.isNew = false;
  470. // var documentData = this.getDocumentData(data);
  471. // documentData.permissionList = specialData.readers;
  472. // documentData.pictureList = specialData.pictures;
  473. // documentData.summary = specialData.summary;
  474. // delete documentData.attachmentList;
  475. // this.documentAction.saveDocument(documentData, function(){
  476. // this.documentAction.publishDocument(documentData, function(json){
  477. // this.fireEvent("afterPublish");
  478. // this.fireEvent("postPublish");
  479. // if (callback) callback();
  480. // this.app.notice(MWF.xApplication.cms.Xform.LP.documentPublished+": “"+this.businessData.document.title+"”", "success");
  481. // this.options.saveOnClose = false;
  482. // this.app.close();
  483. // //this.close();
  484. // }.bind(this) );
  485. // }.bind(this))
  486. // }.bind(this), null, this.businessData.document.id, data);
  487. //},
  488. deleteDocumentForMobile: function () {
  489. if (this.app.mobile) {
  490. this.app.content.mask({
  491. "style": {
  492. "background-color": "#999",
  493. "opacity": 0.6
  494. }
  495. });
  496. this.fireEvent("beforeDelete");
  497. if (this.app && this.app.fireEvent) this.app.fireEvent("beforeDelete");
  498. this.documentAction.removeDocument(this.businessData.document.id, function (json) {
  499. this.fireEvent("afterDelete");
  500. if (this.app && this.app.fireEvent) this.app.fireEvent("afterDelete");
  501. this.app.notice(MWF.xApplication.cms.Xform.LP.documentDelete + ": “" + this.businessData.document.title + "”", "success");
  502. this.options.autoSave = false;
  503. this.options.saveOnClose = false;
  504. this.fireEvent("postDelete");
  505. this.app.close();
  506. }.bind(this));
  507. }
  508. },
  509. deleteDocument: function () {
  510. var _self = this;
  511. var p = MWF.getCenterPosition(this.app.content, 380, 150);
  512. var event = {
  513. "event": {
  514. "x": p.x,
  515. "y": p.y - 200,
  516. "clientX": p.x,
  517. "clientY": p.y - 200
  518. }
  519. };
  520. this.app.confirm("infor", event, MWF.xApplication.cms.Xform.LP.deleteDocumentTitle, MWF.xApplication.cms.Xform.LP.deleteDocumentText, 380, 120, function () {
  521. _self.app.content.mask({
  522. "style": {
  523. "background-color": "#999",
  524. "opacity": 0.6
  525. }
  526. });
  527. _self.fireEvent("beforeDelete");
  528. if (_self.app && _self.app.fireEvent) _self.app.fireEvent("beforeDelete");
  529. _self.documentAction.removeDocument(_self.businessData.document.id, function (json) {
  530. _self.fireEvent("afterDelete");
  531. if (_self.app && _self.app.fireEvent) _self.app.fireEvent("afterDelete");
  532. _self.app.notice(MWF.xApplication.cms.Xform.LP.documentDelete + ": “" + _self.businessData.document.title + "”", "success");
  533. _self.options.autoSave = false;
  534. _self.options.saveOnClose = false;
  535. _self.fireEvent("postDelete");
  536. _self.app.close();
  537. this.close();
  538. }.bind(this));
  539. //this.close();
  540. }, function () {
  541. this.close();
  542. });
  543. },
  544. editDocument: function () {
  545. if (this.app.inBrowser) {
  546. this.modules.each(function (module) {
  547. MWF.release(module);
  548. });
  549. //MWF.release(this);
  550. this.app.node.destroy();
  551. this.app.options.readonly = false;
  552. this.app.loadApplication();
  553. } else {
  554. var options = { "documentId": this.businessData.document.id, "readonly": false }; //this.explorer.app.options.application.allowControl};
  555. if (this.app.options.formEditId) options.formEditId = this.app.options.formEditId;
  556. this.app.desktop.openApplication(null, "cms.Document", options);
  557. this.app.close();
  558. }
  559. },
  560. //2019-11-29 移动端 开启编辑模式
  561. editDocumentForMobile: function () {
  562. if (this.app.mobile) {
  563. this.app.options.readonly = false;
  564. this.app.loadDocument(this.app.options);
  565. }
  566. },
  567. setPopularDocument: function () {
  568. this.app.setPopularDocument();
  569. },
  570. printWork: function (app, form) {
  571. var application = app || this.businessData.work.application;
  572. var form = form;
  573. if (!form) {
  574. form = this.json.id;
  575. if (this.json.printForm) form = this.json.printForm;
  576. }
  577. window.open("/x_desktop/printWork.html?workid=" + this.businessData.work.id + "&app=" + this.businessData.work.application + "&form=" + form);
  578. },
  579. openWindow: function (form, app) {
  580. var form = form;
  581. if (!form) {
  582. form = this.json.id;
  583. }
  584. if (this.businessData.document) {
  585. //var application = app;
  586. //window.open("/x_desktop/printWork.html?workCompletedId="+this.businessData.workCompleted.id+"&app="+application+"&form="+form);
  587. }
  588. },
  589. uploadedAttachment: function (site, id) {
  590. this.documentAction.getAttachment(id, this.businessData.document.id, function (json) {
  591. var att = this.all[site];
  592. if (att) {
  593. if (json.data) att.attachmentController.addAttachment(json.data);
  594. att.attachmentController.checkActions();
  595. att.fireEvent("upload", [json.data]);
  596. }
  597. }.bind(this));
  598. },
  599. replacedAttachment: function (site, id) {
  600. this.documentAction.getAttachment(id, this.businessData.document.id, function (json) {
  601. var att = this.all[site];
  602. if (att) {
  603. var attachmentController = att.attachmentController;
  604. var attachment = null;
  605. for (var i = 0; i < attachmentController.attachments.length; i++) {
  606. if (attachmentController.attachments[i].data.id === id) {
  607. attachment = attachmentController.attachments[i];
  608. break;
  609. }
  610. }
  611. attachment.data = json.data;
  612. attachment.reload();
  613. attachmentController.checkActions();
  614. }
  615. }.bind(this))
  616. }
  617. });