Form.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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("Organization", "Selector.package", null, false);
  6. MWF.xDesktop.requireApp("process.Xform", "Form", null, false);
  7. MWF.require("MWF.widget.Identity", 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": ["postLoad", "afterLoad", "beforeSave", "afterSave", "beforeClose", "beforePublish", "afterPublish"]
  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. if (this.app){
  38. if (this.app.formNode) this.app.formNode.setStyles(this.json.styles);
  39. }
  40. //if (!this.businessData.control.allowSave) this.setOptions({"readonly": true});
  41. if (this.fireEvent("queryLoad")){
  42. this.fireEvent("beforeLoad");
  43. MWF.xDesktop.requireApp("cms.Xform", "lp."+MWF.language, null, false);
  44. // this.container.setStyles(this.css.container);
  45. this._loadBusinessData();
  46. this.Macro = new MWF.CMSMacro.CMSFormContext(this);
  47. this._loadHtml();
  48. this._loadForm();
  49. this._loadModules(this.node);
  50. if(!this.options.readonly){
  51. if(this.options.autoSave)this.autoSave();
  52. this.app.addEvent("queryClose", function(){
  53. if( this.options.saveOnClose && this.businessData.document.docStatus == "draft" )this.saveDocument(null, true);
  54. //if (this.autoSaveTimerID) window.clearInterval(this.autoSaveTimerID);
  55. Object.each(this.forms, function(module, id){
  56. if( module.json.type == "Htmleditor" && module.editor ){
  57. //if(CKEDITOR.currentImageDialog)CKEDITOR.currentImageDialog.destroy();
  58. //CKEDITOR.currentImageDialog = null;
  59. CKEDITOR.remove( module.editor );
  60. delete module.editor
  61. }
  62. });
  63. }.bind(this));
  64. }
  65. this.fireEvent("postLoad");
  66. this.fireEvent("afterLoad");
  67. }
  68. },
  69. autoSave: function(){
  70. //this.autoSaveTimerID = window.setInterval(function(){
  71. // this.saveDocument();
  72. //}.bind(this), 300000);
  73. },
  74. _loadBusinessData: function(){
  75. if (!this.businessData){
  76. this.businessData = {
  77. "data": {
  78. }
  79. };
  80. }
  81. },
  82. _loadEvents: function(){
  83. Object.each(this.json.events, function(e, key){
  84. if (e.code){
  85. if (this.options.moduleEvents.indexOf(key)!=-1){
  86. this.addEvent(key, function(event){
  87. return this.Macro.fire(e.code, this, event);
  88. }.bind(this));
  89. }else{
  90. if (key=="load"){
  91. this.addEvent("postLoad", function(){
  92. return this.Macro.fire(e.code, this);
  93. }.bind(this));
  94. }else if (key=="submit"){
  95. this.addEvent("beforePublish", function(){
  96. return this.Macro.fire(e.code, this);
  97. }.bind(this));
  98. }else{
  99. this.node.addEvent(key, function(event){
  100. return this.Macro.fire(e.code, this, event);
  101. }.bind(this));
  102. }
  103. }
  104. }
  105. }.bind(this));
  106. },
  107. _loadModules: function(dom){
  108. //var subDom = this.node.getFirst();
  109. //while (subDom){
  110. // if (subDom.get("MWFtype")){
  111. // var json = this._getDomjson(subDom);
  112. // var module = this._loadModule(json, subDom);
  113. // this.modules.push(module);
  114. // }
  115. // subDom = subDom.getNext();
  116. //}
  117. var moduleNodes = this._getModuleNodes(dom);
  118. moduleNodes.each(function(node){
  119. var json = this._getDomjson(node);
  120. if( !this.options.showAttachment && json.type == "Attachment" ){
  121. return;
  122. }
  123. var module = this._loadModule(json, node);
  124. this.modules.push(module);
  125. }.bind(this));
  126. },
  127. _loadModule: function(json, node, beforeLoad){
  128. if( !json )return;
  129. var module = new MWF["CMS"+json.type](node, json, this);
  130. if (beforeLoad) beforeLoad.apply(module);
  131. if (!this.all[json.id]) this.all[json.id] = module;
  132. if (module.field){
  133. if (!this.forms[json.id]) this.forms[json.id] = module;
  134. }
  135. module.readonly = this.options.readonly;
  136. module.load();
  137. return module;
  138. },
  139. //getData: function(){
  140. // var data= Object.clone(this.businessData.data);
  141. // Object.each(this.forms, function(module, id){
  142. // debugger;
  143. // if (module.json.section=="yes"){
  144. // data[id] = this.getSectionData(module, data[id]);
  145. // }else{
  146. // data[id] = module.getData();
  147. // }
  148. // }.bind(this));
  149. //
  150. // this.businessData.data = data;
  151. // this.Macro.environment.setData(this.businessData.data);
  152. // return data;
  153. //},
  154. trim: function( array ){
  155. var arr = [];
  156. array.each(function(v){
  157. if (v) arr.push(v);
  158. });
  159. return arr;
  160. },
  161. transportReaderData : function( data ){
  162. var cnArray = ["公司","部门","人员","群组"];
  163. var keyArray = ["companyValue","departmentValue","personValue","groupValue"];
  164. var result = [];
  165. data.each( function( item ){
  166. for( var key in item ){
  167. var it = item[key];
  168. it.each( function( i ){
  169. result.push({
  170. permission : "阅读",
  171. permissionObjectType : cnArray[ keyArray.indexOf(key) ],
  172. permissionObjectName : i.name
  173. })
  174. })
  175. }
  176. });
  177. return result.length > 0 ? result : null;
  178. },
  179. getSpecialData: function(){
  180. var data= this.businessData.data;
  181. var readers = [];
  182. var pictures = [];
  183. var summary = "";
  184. Object.each(this.forms, function(module, id){
  185. if( module.json.type == "Readerfield" ){
  186. if (module.json.section=="yes"){
  187. readers = readers.concat( this.getSectionData(module, data[id]) );
  188. }else{
  189. readers = readers.concat( module.getData() );
  190. }
  191. }
  192. if( module.json.type == "ImageClipper" ){
  193. var d = module.getData();
  194. if(d)pictures.push( d );
  195. }
  196. if( module.json.type == "Htmleditor" ){
  197. var text = module.getText();
  198. summary = text.substr(0,85);
  199. }
  200. });
  201. if( data.processOwnerList && typeOf( data.processOwnerList ) == "array" ){ //如果是流程中发布的
  202. var owner = { personValue : [] };
  203. data.processOwnerList.each( function( p ){
  204. owner.personValue.push({
  205. name : p,
  206. type: "person"
  207. });
  208. });
  209. readers = readers.concat( owner );
  210. }
  211. return {
  212. readers : this.transportReaderData( readers ),
  213. pictures : pictures,
  214. summary : summary
  215. };
  216. },
  217. getDocumentData: function( formData ){
  218. var data= Object.clone(this.businessData.document);
  219. if( formData.subject ){
  220. data.title = formData.subject;
  221. data.subject = formData.subject;
  222. this.businessData.document.title = formData.subject;
  223. this.businessData.document.subject = formData.subject;
  224. }
  225. data.isNewDocument = false;
  226. return data;
  227. },
  228. saveDocument: function(callback, sync ){
  229. this.fireEvent("beforeSave");
  230. if( this.businessData.document.docStatus == "published" ){
  231. if (!this.formValidation("publish")){
  232. if (callback) callback();
  233. return false;
  234. }
  235. }
  236. var data = this.getData();
  237. var documentData = this.getDocumentData(data);
  238. var specialData = this.getSpecialData();
  239. documentData.permissionList = specialData.readers;
  240. documentData.pictureList = specialData.pictures;
  241. documentData.summary = specialData.summary;
  242. documentData.docData = data;
  243. delete documentData.attachmentList;
  244. this.documentAction.saveDocument(documentData, function(){
  245. //this.documentAction.saveData(function(json){
  246. this.notice(MWF.xApplication.cms.Xform.LP.dataSaved, "success");
  247. this.businessData.data.isNew = false;
  248. this.fireEvent("afterSave");
  249. if (callback) callback();
  250. //}.bind(this), null, this.businessData.document.id, data, !sync );
  251. }.bind(this),null, !sync );
  252. },
  253. closeDocument: function(){
  254. this.fireEvent("beforeClose");
  255. if (this.app){
  256. this.app.close();
  257. }
  258. },
  259. formValidation: function( status ){
  260. if (this.options.readonly) return true;
  261. var flag = true;
  262. //flag = this.validation();
  263. Object.each(this.forms, function(field, key){
  264. field.validationMode();
  265. if (!field.validation( status )){
  266. flag = false;
  267. }
  268. }.bind(this));
  269. return flag;
  270. },
  271. publishDocument: function(callback){
  272. this.fireEvent("beforePublish");
  273. this.app.content.mask({
  274. "destroyOnHide": true,
  275. "style": this.app.css.maskNode
  276. });
  277. if (!this.formValidation("publish")){
  278. this.app.content.unmask();
  279. if (callback) callback();
  280. return false;
  281. }
  282. var data = this.getData();
  283. var specialData = this.getSpecialData();
  284. //this.documentAction.saveData(function(json){
  285. var documentData = this.getDocumentData(data);
  286. documentData.permissionList = specialData.readers;
  287. documentData.pictureList = specialData.pictures;
  288. documentData.summary = specialData.summary;
  289. documentData.docData = data;
  290. delete documentData.attachmentList;
  291. //this.documentAction.saveDocument(documentData, function(){
  292. this.documentAction.publishDocumentComplex(documentData, function(json){
  293. this.businessData.data.isNew = false;
  294. this.fireEvent("afterPublish");
  295. this.fireEvent("postPublish");
  296. if (callback) callback();
  297. this.app.notice(MWF.xApplication.cms.Xform.LP.documentPublished+": “"+this.businessData.document.title+"”", "success");
  298. this.options.saveOnClose = false;
  299. this.app.close();
  300. //this.close();
  301. }.bind(this) );
  302. //}.bind(this))
  303. //}.bind(this), null, this.businessData.document.id, data);
  304. },
  305. //publishDocument_bak: function(callback){
  306. // this.fireEvent("beforePublish");
  307. // this.app.content.mask({
  308. // "destroyOnHide": true,
  309. // "style": this.app.css.maskNode
  310. // });
  311. // if (!this.formValidation("publish")){
  312. // this.app.content.unmask();
  313. // if (callback) callback();
  314. // return false;
  315. // }
  316. //
  317. // var data = this.getData();
  318. // var specialData = this.getSpecialData();
  319. // this.documentAction.saveData(function(json){
  320. // this.businessData.data.isNew = false;
  321. // var documentData = this.getDocumentData(data);
  322. // documentData.permissionList = specialData.readers;
  323. // documentData.pictureList = specialData.pictures;
  324. // documentData.summary = specialData.summary;
  325. // delete documentData.attachmentList;
  326. // this.documentAction.saveDocument(documentData, function(){
  327. // this.documentAction.publishDocument(documentData, function(json){
  328. // this.fireEvent("afterPublish");
  329. // this.fireEvent("postPublish");
  330. // if (callback) callback();
  331. // this.app.notice(MWF.xApplication.cms.Xform.LP.documentPublished+": “"+this.businessData.document.title+"”", "success");
  332. // this.options.saveOnClose = false;
  333. // this.app.close();
  334. // //this.close();
  335. // }.bind(this) );
  336. // }.bind(this))
  337. // }.bind(this), null, this.businessData.document.id, data);
  338. //},
  339. deleteDocument: function(){
  340. var _self = this;
  341. var p = MWF.getCenterPosition(this.app.content, 380, 150);
  342. var event = {
  343. "event":{
  344. "x": p.x,
  345. "y": p.y-200,
  346. "clientX": p.x,
  347. "clientY": p.y-200
  348. }
  349. };
  350. this.app.confirm("infor", event, MWF.xApplication.cms.Xform.LP.deleteDocumentTitle, MWF.xApplication.cms.Xform.LP.deleteDocumentText, 380, 120, function(){
  351. _self.app.content.mask({
  352. "style": {
  353. "background-color": "#999",
  354. "opacity": 0.6
  355. }
  356. });
  357. _self.documentAction.removeDocument(_self.businessData.document.id, function(json){
  358. _self.app.notice(MWF.xApplication.cms.Xform.LP.documentDelete+": “"+_self.businessData.document.title+"”", "success");
  359. _self.options.autoSave = false;
  360. _self.options.saveOnClose = false;
  361. _self.fireEvent("postDelete");
  362. _self.app.close();
  363. this.close();
  364. }.bind(this) );
  365. //this.close();
  366. }, function(){
  367. this.close();
  368. });
  369. },
  370. editDocument: function(){
  371. var options = {"documentId": this.businessData.document.id, "readonly" : false }; //this.explorer.app.options.application.allowControl};
  372. this.app.desktop.openApplication(null, "cms.Document", options);
  373. this.app.close();
  374. },
  375. setPopularDocument : function(){
  376. this.app.setPopularDocument();
  377. },
  378. printWork: function(app, form){
  379. var application = app || this.businessData.work.application;
  380. var form = form;
  381. if (!form){
  382. form = this.json.id;
  383. if (this.json.printForm) form = this.json.printForm;
  384. }
  385. window.open("/x_desktop/printWork.html?workid="+this.businessData.work.id+"&app="+this.businessData.work.application+"&form="+form);
  386. }
  387. });