Form.js 19 KB

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