Form.js 22 KB

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