Form.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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(){
  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()
  70. }
  71. },
  72. loadContent: function(){
  73. this._loadHtml();
  74. this._loadForm();
  75. this.fireEvent("beforeModulesLoad");
  76. this._loadModules(this.node);
  77. if(!this.options.readonly){
  78. if(this.options.autoSave)this.autoSave();
  79. this.app.addEvent("queryClose", function(){
  80. if( this.options.saveOnClose && this.businessData.document.docStatus == "draft" )this.saveDocument(null, true);
  81. //if (this.autoSaveTimerID) window.clearInterval(this.autoSaveTimerID);
  82. Object.each(this.forms, function(module, id){
  83. if( module.json.type == "Htmleditor" && module.editor ){
  84. //if(CKEDITOR.currentImageDialog)CKEDITOR.currentImageDialog.destroy();
  85. //CKEDITOR.currentImageDialog = null;
  86. CKEDITOR.remove( module.editor );
  87. delete module.editor
  88. }
  89. });
  90. }.bind(this));
  91. }
  92. this.fireEvent("afterModulesLoad");
  93. this.fireEvent("postLoad");
  94. this.fireEvent("afterLoad");
  95. if (this.app && this.app.fireEvent){
  96. this.app.fireEvent("afterModulesLoad");
  97. this.app.fireEvent("postLoad");
  98. this.app.fireEvent("afterLoad");
  99. }
  100. },
  101. autoSave: function(){
  102. //this.autoSaveTimerID = window.setInterval(function(){
  103. // this.saveDocument();
  104. //}.bind(this), 300000);
  105. },
  106. _loadBusinessData: function(){
  107. if (!this.businessData){
  108. this.businessData = {
  109. "data": {}
  110. };
  111. }
  112. },
  113. _loadEvents: function(){
  114. Object.each(this.json.events, function(e, key){
  115. if (e.code){
  116. if (this.options.moduleEvents.indexOf(key)!=-1){
  117. this.addEvent(key, function(event){
  118. return this.Macro.fire(e.code, this, event);
  119. }.bind(this));
  120. }else{
  121. if (key=="load"){
  122. this.addEvent("postLoad", function(){
  123. return this.Macro.fire(e.code, this);
  124. }.bind(this));
  125. }else if (key=="submit"){
  126. this.addEvent("beforePublish", function(){
  127. return this.Macro.fire(e.code, this);
  128. }.bind(this));
  129. }else{
  130. this.node.addEvent(key, function(event){
  131. return this.Macro.fire(e.code, this, event);
  132. }.bind(this));
  133. }
  134. }
  135. }
  136. }.bind(this));
  137. },
  138. _loadModules: function(dom){
  139. //var subDom = this.node.getFirst();
  140. //while (subDom){
  141. // if (subDom.get("MWFtype")){
  142. // var json = this._getDomjson(subDom);
  143. // var module = this._loadModule(json, subDom);
  144. // this.modules.push(module);
  145. // }
  146. // subDom = subDom.getNext();
  147. //}
  148. var moduleNodes = this._getModuleNodes(dom);
  149. moduleNodes.each(function(node){
  150. var json = this._getDomjson(node);
  151. if( !this.options.showAttachment && json.type == "Attachment" ){
  152. return;
  153. }
  154. var module = this._loadModule(json, node);
  155. this.modules.push(module);
  156. }.bind(this));
  157. },
  158. _loadModule: function(json, node, beforeLoad){
  159. if( !json )return;
  160. if (!MWF["CMS"+json.type]){
  161. MWF.xDesktop.requireApp("cms.Xform", json.type, null, false);
  162. }
  163. var module = new MWF["CMS"+json.type](node, json, this);
  164. if (beforeLoad) beforeLoad.apply(module);
  165. if (!this.all[json.id]) this.all[json.id] = module;
  166. if (module.field){
  167. if (!this.forms[json.id]) this.forms[json.id] = module;
  168. }
  169. module.readonly = this.options.readonly;
  170. module.load();
  171. return module;
  172. },
  173. //getData: function(){
  174. // var data= Object.clone(this.businessData.data);
  175. // Object.each(this.forms, function(module, id){
  176. // debugger;
  177. // if (module.json.section=="yes"){
  178. // data[id] = this.getSectionData(module, data[id]);
  179. // }else{
  180. // data[id] = module.getData();
  181. // }
  182. // }.bind(this));
  183. //
  184. // this.businessData.data = data;
  185. // this.Macro.environment.setData(this.businessData.data);
  186. // return data;
  187. //},
  188. trim: function( array ){
  189. var arr = [];
  190. array.each(function(v){
  191. if (v) arr.push(v);
  192. });
  193. return arr;
  194. },
  195. transportPermissionData : function( array , t ){
  196. var result = [];
  197. array.each( function( data ){
  198. var dn = typeOf( data ) === "string" ? data : data.distinguishedName;
  199. if ( dn ){
  200. var flag = dn.substr(dn.length-1, 1);
  201. var type;
  202. switch (flag.toLowerCase()){
  203. case "i":
  204. type = "人员"; //"身份";
  205. break;
  206. case "p":
  207. type = "人员";
  208. break;
  209. case "u":
  210. type = "组织";
  211. break;
  212. case "g":
  213. type = "群组";
  214. break;
  215. case "r":
  216. type = "角色";
  217. break;
  218. default:
  219. type = "";
  220. //result.push( data );
  221. }
  222. if( type ){
  223. result.push({
  224. permission : t == "author" ? "作者" : "阅读",
  225. permissionObjectType : type,
  226. permissionObjectName : dn
  227. })
  228. }
  229. }
  230. });
  231. return result.length > 0 ? result : null;
  232. },
  233. getSpecialData: function(){
  234. var data= this.businessData.data;
  235. var readers = [];
  236. var authors = [];
  237. var pictures = [];
  238. var cloudPictures = [];
  239. var summary = "";
  240. Object.each(this.forms, function(module, id){
  241. if( module.json.type == "Readerfield" ){
  242. if (module.json.section=="yes"){
  243. readers = readers.concat( this.getSectionData(module, data[id]) );
  244. }else{
  245. readers = readers.concat( module.getData() );
  246. }
  247. }
  248. if( module.json.type == "Authorfield" ){
  249. if (module.json.section=="yes"){
  250. authors = authors.concat( this.getSectionData(module, data[id]) );
  251. }else{
  252. authors = authors.concat( module.getData() );
  253. }
  254. }
  255. if( module.json.type == "ImageClipper" ){
  256. var d = module.getData();
  257. if(d)pictures.push( d );
  258. }
  259. if( module.json.type == "Htmleditor" ){
  260. var text = module.getText();
  261. summary = text.substr(0,80);
  262. cloudPictures = cloudPictures.concat( module.getImageIds() );
  263. }
  264. });
  265. if( data.processOwnerList && typeOf( data.processOwnerList ) == "array" ){ //如果是流程中发布的
  266. var owner = { personValue : [] };
  267. data.processOwnerList.each( function( p ){
  268. owner.personValue.push({
  269. name : p,
  270. type: "person"
  271. });
  272. });
  273. readers = readers.concat( owner );
  274. }
  275. return {
  276. readers : this.transportPermissionData( readers, "reader" ),
  277. authors : this.transportPermissionData( authors, "author" ),
  278. pictures : pictures,
  279. summary : summary,
  280. cloudPictures : cloudPictures
  281. };
  282. },
  283. getDocumentData: function( formData ){
  284. var data= Object.clone(this.businessData.document);
  285. if( formData.subject ){
  286. data.title = formData.subject;
  287. data.subject = formData.subject;
  288. this.businessData.document.title = formData.subject;
  289. this.businessData.document.subject = formData.subject;
  290. }
  291. data.isNewDocument = false;
  292. return data;
  293. },
  294. saveDocument: function(callback, sync ){
  295. this.fireEvent("beforeSave");
  296. if( this.businessData.document.docStatus == "published" ){
  297. if (!this.formValidation("publish")){
  298. this.app.content.unmask();
  299. //if (callback) callback();
  300. return false;
  301. }
  302. }
  303. if (!this.formSaveValidation()){
  304. this.app.content.unmask();
  305. if (callback) callback();
  306. return false;
  307. }
  308. var data = this.getData();
  309. var specialData = this.getSpecialData();
  310. var documentData = this.getDocumentData(data);
  311. documentData.readerList = specialData.readers;
  312. documentData.authorList = specialData.authors;
  313. documentData.pictureList = specialData.pictures;
  314. documentData.summary = specialData.summary;
  315. documentData.cloudPictures = specialData.cloudPictures;
  316. documentData.docData = data;
  317. delete documentData.attachmentList;
  318. this.fireEvent("postSave", [documentData]);
  319. if (this.officeList){
  320. this.officeList.each(function(module){
  321. module.save(history);
  322. });
  323. }
  324. this.documentAction.saveDocument(documentData, function(){
  325. //this.documentAction.saveData(function(json){
  326. this.notice(MWF.xApplication.cms.Xform.LP.dataSaved, "success");
  327. this.businessData.data.isNew = false;
  328. this.fireEvent("afterSave");
  329. if (callback) callback();
  330. //}.bind(this), null, this.businessData.document.id, data, !sync );
  331. }.bind(this),null, !sync );
  332. },
  333. closeDocument: function(){
  334. this.fireEvent("beforeClose");
  335. if (this.app){
  336. this.app.close();
  337. }
  338. },
  339. formValidation: function( status ){
  340. if (this.options.readonly) return true;
  341. var flag = true;
  342. //flag = this.validation();
  343. Object.each(this.forms, function(field, key){
  344. field.validationMode();
  345. if (!field.validation( status )){
  346. flag = false;
  347. }
  348. }.bind(this));
  349. return flag;
  350. },
  351. formSaveValidation : function(){
  352. if (!this.json.validationSave) return true;
  353. if (!this.json.validationSave.code) return true;
  354. var flag = this.Macro.exec(this.json.validationSave.code, this);
  355. if (!flag) flag = MWF.xApplication.cms.Xform.LP.notValidation;
  356. if( typeOf( flag ) === "string" ){
  357. if( flag !== "true" ){
  358. this.app.notice( flag , "error");
  359. return false;
  360. }
  361. }else if (flag.toString()!="true"){
  362. return false;
  363. }
  364. return true;
  365. },
  366. formPublishValidation : function(){
  367. if (!this.json.validationPublish) return true;
  368. if (!this.json.validationPublish.code) return true;
  369. var flag = this.Macro.exec(this.json.validationPublish.code, this);
  370. if (!flag) flag = MWF.xApplication.cms.Xform.LP.notValidation;
  371. if( typeOf( flag ) === "string" ){
  372. if( flag !== "true" ){
  373. this.app.notice( flag , "error");
  374. return false;
  375. }
  376. }else if (flag.toString()!="true"){
  377. return false;
  378. }
  379. return true;
  380. },
  381. publishDocument: function(callback){
  382. this.fireEvent("beforePublish");
  383. this.app.content.mask({
  384. "destroyOnHide": true,
  385. "style": this.app.css.maskNode
  386. });
  387. if (!this.formValidation("publish")){
  388. this.app.content.unmask();
  389. //if (callback) callback();
  390. return false;
  391. }
  392. if (!this.formPublishValidation()){
  393. this.app.content.unmask();
  394. if (callback) callback();
  395. return false;
  396. }
  397. var data = this.getData();
  398. var specialData = this.getSpecialData();
  399. //this.documentAction.saveData(function(json){
  400. var documentData = this.getDocumentData(data);
  401. documentData.readerList = specialData.readers;
  402. documentData.authorList = specialData.authors;
  403. documentData.pictureList = specialData.pictures;
  404. documentData.summary = specialData.summary;
  405. documentData.cloudPictures = specialData.cloudPictures;
  406. documentData.docData = data;
  407. delete documentData.attachmentList;
  408. //this.documentAction.saveDocument(documentData, function(){
  409. this.fireEvent("postPublish", [documentData]);
  410. if (this.officeList){
  411. this.officeList.each(function(module){
  412. module.save(history);
  413. });
  414. }
  415. this.documentAction.publishDocumentComplex(documentData, function(json){
  416. this.businessData.data.isNew = false;
  417. this.fireEvent("afterPublish");
  418. if (callback) callback();
  419. if( this.businessData.document.title ){
  420. this.app.notice(MWF.xApplication.cms.Xform.LP.documentPublished+": “"+this.businessData.document.title+"”", "success");
  421. }else{
  422. this.app.notice(MWF.xApplication.cms.Xform.LP.documentPublished, "success");
  423. }
  424. this.options.saveOnClose = false;
  425. this.app.close();
  426. //this.close();
  427. }.bind(this) );
  428. //}.bind(this))
  429. //}.bind(this), null, this.businessData.document.id, data);
  430. },
  431. //publishDocument_bak: function(callback){
  432. // this.fireEvent("beforePublish");
  433. // this.app.content.mask({
  434. // "destroyOnHide": true,
  435. // "style": this.app.css.maskNode
  436. // });
  437. // if (!this.formValidation("publish")){
  438. // this.app.content.unmask();
  439. // if (callback) callback();
  440. // return false;
  441. // }
  442. //
  443. // var data = this.getData();
  444. // var specialData = this.getSpecialData();
  445. // this.documentAction.saveData(function(json){
  446. // this.businessData.data.isNew = false;
  447. // var documentData = this.getDocumentData(data);
  448. // documentData.permissionList = specialData.readers;
  449. // documentData.pictureList = specialData.pictures;
  450. // documentData.summary = specialData.summary;
  451. // delete documentData.attachmentList;
  452. // this.documentAction.saveDocument(documentData, function(){
  453. // this.documentAction.publishDocument(documentData, function(json){
  454. // this.fireEvent("afterPublish");
  455. // this.fireEvent("postPublish");
  456. // if (callback) callback();
  457. // this.app.notice(MWF.xApplication.cms.Xform.LP.documentPublished+": “"+this.businessData.document.title+"”", "success");
  458. // this.options.saveOnClose = false;
  459. // this.app.close();
  460. // //this.close();
  461. // }.bind(this) );
  462. // }.bind(this))
  463. // }.bind(this), null, this.businessData.document.id, data);
  464. //},
  465. deleteDocument: function(){
  466. var _self = this;
  467. var p = MWF.getCenterPosition(this.app.content, 380, 150);
  468. var event = {
  469. "event":{
  470. "x": p.x,
  471. "y": p.y-200,
  472. "clientX": p.x,
  473. "clientY": p.y-200
  474. }
  475. };
  476. this.app.confirm("infor", event, MWF.xApplication.cms.Xform.LP.deleteDocumentTitle, MWF.xApplication.cms.Xform.LP.deleteDocumentText, 380, 120, function(){
  477. _self.app.content.mask({
  478. "style": {
  479. "background-color": "#999",
  480. "opacity": 0.6
  481. }
  482. });
  483. _self.fireEvent("beforeDelete");
  484. if (_self.app && _self.app.fireEvent) _self.app.fireEvent("beforeDelete");
  485. _self.documentAction.removeDocument(_self.businessData.document.id, function(json){
  486. _self.fireEvent("afterDelete");
  487. if (_self.app && _self.app.fireEvent) _self.app.fireEvent("afterDelete");
  488. _self.app.notice(MWF.xApplication.cms.Xform.LP.documentDelete+": “"+_self.businessData.document.title+"”", "success");
  489. _self.options.autoSave = false;
  490. _self.options.saveOnClose = false;
  491. _self.fireEvent("postDelete");
  492. _self.app.close();
  493. this.close();
  494. }.bind(this) );
  495. //this.close();
  496. }, function(){
  497. this.close();
  498. });
  499. },
  500. editDocument: function(){
  501. if( this.app.inBrowser ){
  502. this.modules.each(function(module){
  503. MWF.release(module);
  504. });
  505. //MWF.release(this);
  506. this.app.node.destroy();
  507. this.app.options.readonly = false;
  508. this.app.loadApplication();
  509. }else{
  510. var options = {"documentId": this.businessData.document.id, "readonly" : false }; //this.explorer.app.options.application.allowControl};
  511. this.app.desktop.openApplication(null, "cms.Document", options);
  512. this.app.close();
  513. }
  514. },
  515. setPopularDocument : function(){
  516. this.app.setPopularDocument();
  517. },
  518. printWork: function(app, form){
  519. var application = app || this.businessData.work.application;
  520. var form = form;
  521. if (!form){
  522. form = this.json.id;
  523. if (this.json.printForm) form = this.json.printForm;
  524. }
  525. window.open("/x_desktop/printWork.html?workid="+this.businessData.work.id+"&app="+this.businessData.work.application+"&form="+form);
  526. },
  527. openWindow: function(form, app){
  528. var form = form;
  529. if (!form){
  530. form = this.json.id;
  531. }
  532. if (this.businessData.document){
  533. //var application = app;
  534. //window.open("/x_desktop/printWork.html?workCompletedId="+this.businessData.workCompleted.id+"&app="+application+"&form="+form);
  535. }
  536. },
  537. uploadedAttachment: function(site, id){
  538. this.documentAction.getAttachment(id, this.businessData.document.id, function(json){
  539. var att = this.all[site];
  540. if (att){
  541. if (json.data) att.attachmentController.addAttachment(json.data);
  542. att.attachmentController.checkActions();
  543. att.fireEvent("upload", [json.data]);
  544. }
  545. }.bind(this));
  546. },
  547. replacedAttachment: function(site, id){
  548. this.documentAction.getAttachment(id, this.businessData.document.id, function(json){
  549. var att = this.all[site];
  550. if (att){
  551. var attachmentController = att.attachmentController;
  552. var attachment = null;
  553. for (var i=0; i<attachmentController.attachments.length; i++){
  554. if (attachmentController.attachments[i].data.id===id){
  555. attachment = attachmentController.attachments[i];
  556. break;
  557. }
  558. }
  559. attachment.data = json.data;
  560. attachment.reload();
  561. attachmentController.checkActions();
  562. }
  563. }.bind(this))
  564. }
  565. });