Form.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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_process_Xform/$Form/";
  29. this.cssPath = this.options.cssPath || "/x_component_process_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. }.bind(this));
  56. }
  57. this.fireEvent("postLoad");
  58. this.fireEvent("afterLoad");
  59. }
  60. },
  61. autoSave: function(){
  62. //this.autoSaveTimerID = window.setInterval(function(){
  63. // this.saveDocument();
  64. //}.bind(this), 300000);
  65. },
  66. _loadBusinessData: function(){
  67. if (!this.businessData){
  68. this.businessData = {
  69. "data": {
  70. }
  71. };
  72. }
  73. },
  74. _loadEvents: function(){
  75. Object.each(this.json.events, function(e, key){
  76. if (e.code){
  77. if (this.options.moduleEvents.indexOf(key)!=-1){
  78. this.addEvent(key, function(event){
  79. return this.Macro.fire(e.code, this, event);
  80. }.bind(this));
  81. }else{
  82. if (key=="load"){
  83. this.addEvent("postLoad", function(){
  84. return this.Macro.fire(e.code, this);
  85. }.bind(this));
  86. }else if (key=="submit"){
  87. this.addEvent("beforePublish", function(){
  88. return this.Macro.fire(e.code, this);
  89. }.bind(this));
  90. }else{
  91. this.node.addEvent(key, function(event){
  92. return this.Macro.fire(e.code, this, event);
  93. }.bind(this));
  94. }
  95. }
  96. }
  97. }.bind(this));
  98. },
  99. _loadModules: function(dom){
  100. //var subDom = this.node.getFirst();
  101. //while (subDom){
  102. // if (subDom.get("MWFtype")){
  103. // var json = this._getDomjson(subDom);
  104. // var module = this._loadModule(json, subDom);
  105. // this.modules.push(module);
  106. // }
  107. // subDom = subDom.getNext();
  108. //}
  109. var moduleNodes = this._getModuleNodes(dom);
  110. moduleNodes.each(function(node){
  111. var json = this._getDomjson(node);
  112. if( !this.options.showAttachment && json.type == "Attachment" ){
  113. return;
  114. }
  115. var module = this._loadModule(json, node);
  116. this.modules.push(module);
  117. }.bind(this));
  118. },
  119. _loadModule: function(json, node, beforeLoad){
  120. if( !json )return;
  121. var module = new MWF["CMS"+json.type](node, json, this);
  122. if (beforeLoad) beforeLoad.apply(module);
  123. if (!this.all[json.id]) this.all[json.id] = module;
  124. if (module.field){
  125. if (!this.forms[json.id]) this.forms[json.id] = module;
  126. }
  127. module.readonly = this.options.readonly;
  128. module.load();
  129. return module;
  130. },
  131. //getData: function(){
  132. // var data= Object.clone(this.businessData.data);
  133. // Object.each(this.forms, function(module, id){
  134. // debugger;
  135. // if (module.json.section=="yes"){
  136. // data[id] = this.getSectionData(module, data[id]);
  137. // }else{
  138. // data[id] = module.getData();
  139. // }
  140. // }.bind(this));
  141. //
  142. // this.businessData.data = data;
  143. // this.Macro.environment.setData(this.businessData.data);
  144. // return data;
  145. //},
  146. trim: function( array ){
  147. var arr = [];
  148. array.each(function(v){
  149. if (v) arr.push(v);
  150. });
  151. return arr;
  152. },
  153. transportReaderData : function( data ){
  154. var cnArray = ["公司","部门","人员","群组"];
  155. var keyArray = ["companyValue","departmentValue","personValue","groupValue"];
  156. var result = [];
  157. data.each( function( item ){
  158. for( var key in item ){
  159. var it = item[key];
  160. it.each( function( i ){
  161. result.push({
  162. permission : "阅读",
  163. permissionObjectType : cnArray[ keyArray.indexOf(key) ],
  164. permissionObjectName : i.name
  165. })
  166. })
  167. }
  168. });
  169. return result.length > 0 ? result : null;
  170. },
  171. getReaderData: function(){
  172. var data= this.businessData.data;
  173. var readers = [];
  174. Object.each(this.forms, function(module, id){
  175. if( module.json.type == "Readerfield" ){
  176. if (module.json.section=="yes"){
  177. readers = readers.concat( this.getSectionData(module, data[id]) );
  178. }else{
  179. readers = readers.concat( module.getData() );
  180. }
  181. }
  182. });
  183. r = this.transportReaderData( readers );
  184. return r;
  185. },
  186. getDocumentData: function( formData ){
  187. var data= Object.clone(this.businessData.document);
  188. if( formData.htmleditor ){
  189. var div = new Element( "div" , {
  190. "styles" : { "display" : "none" },
  191. "html" : formData.htmleditor
  192. } ).inject( this.container );
  193. div.getElements("img").each( function( el ){
  194. el.setStyle( "max-width" , "100%" );
  195. });
  196. formData.htmleditor = div.get("html");
  197. div.destroy();
  198. }
  199. if( formData.subject ){
  200. data.title = formData.subject;
  201. data.subject = formData.subject;
  202. this.businessData.document.title = formData.subject;
  203. this.businessData.document.subject = formData.subject;
  204. }
  205. data.isNewDocument = false;
  206. return data;
  207. },
  208. saveDocument: function(callback, sync ){
  209. this.fireEvent("beforeSave");
  210. if( this.businessData.document.docStatus == "published" ){
  211. if (!this.formValidation("publish")){
  212. if (callback) callback();
  213. return false;
  214. }
  215. }
  216. var data = this.getData();
  217. var documentData = this.getDocumentData(data);
  218. documentData.permissionList = this.getReaderData();
  219. delete documentData.attachmentList;
  220. this.documentAction.saveDocument(documentData, function(){
  221. this.documentAction.saveData(function(json){
  222. this.notice(MWF.xApplication.cms.Xform.LP.dataSaved, "success");
  223. this.businessData.data.isNew = false;
  224. this.fireEvent("afterSave");
  225. if (callback) callback();
  226. }.bind(this), null, this.businessData.document.id, data, !sync );
  227. }.bind(this),null, !sync );
  228. },
  229. closeDocument: function(){
  230. this.fireEvent("beforeClose");
  231. if (this.app){
  232. this.app.close();
  233. }
  234. },
  235. formValidation: function( status ){
  236. if (this.options.readonly) return true;
  237. var flag = true;
  238. //flag = this.validation();
  239. Object.each(this.forms, function(field, key){
  240. field.validationMode();
  241. if (!field.validation( status )){
  242. flag = false;
  243. }
  244. }.bind(this));
  245. return flag;
  246. },
  247. publishDocument: function(callback){
  248. this.fireEvent("beforePublish");
  249. this.app.content.mask({
  250. "destroyOnHide": true,
  251. "style": this.app.css.maskNode
  252. });
  253. if (!this.formValidation("publish")){
  254. this.app.content.unmask();
  255. if (callback) callback();
  256. return false;
  257. }
  258. var data = this.getData();
  259. var readerData = this.getReaderData();
  260. this.documentAction.saveData(function(json){
  261. this.businessData.data.isNew = false;
  262. var documentData = this.getDocumentData(data);
  263. documentData.permissionList = readerData;
  264. delete documentData.attachmentList;
  265. this.documentAction.saveDocument(documentData, function(){
  266. this.documentAction.publishDocument(documentData, function(json){
  267. this.fireEvent("afterPublish");
  268. this.fireEvent("postPublish");
  269. if (callback) callback();
  270. this.app.notice(MWF.xApplication.cms.Xform.LP.documentPublished+": “"+this.businessData.document.title+"”", "success");
  271. this.options.saveOnClose = false;
  272. this.app.close();
  273. //this.close();
  274. }.bind(this) );
  275. }.bind(this))
  276. }.bind(this), null, this.businessData.document.id, data);
  277. },
  278. deleteDocument: function(){
  279. var _self = this;
  280. var p = MWF.getCenterPosition(this.app.content, 380, 150);
  281. var event = {
  282. "event":{
  283. "x": p.x,
  284. "y": p.y-200,
  285. "clientX": p.x,
  286. "clientY": p.y-200
  287. }
  288. }
  289. this.app.confirm("infor", event, MWF.xApplication.cms.Xform.LP.deleteDocumentTitle, MWF.xApplication.cms.Xform.LP.deleteDocumentText, 380, 120, function(){
  290. _self.app.content.mask({
  291. "style": {
  292. "background-color": "#999",
  293. "opacity": 0.6
  294. }
  295. });
  296. _self.documentAction.removeDocument(_self.businessData.document.id, function(json){
  297. _self.app.notice(MWF.xApplication.cms.Xform.LP.documentDelete+": “"+_self.businessData.document.title+"”", "success");
  298. _self.options.autoSave = false;
  299. _self.options.saveOnClose = false;
  300. _self.fireEvent("postDelete");
  301. _self.app.close();
  302. this.close();
  303. }.bind(this) );
  304. //this.close();
  305. }, function(){
  306. this.close();
  307. });
  308. },
  309. editDocument: function(){
  310. var options = {"documentId": this.businessData.document.id, "readonly" : false }; //this.explorer.app.options.application.allowControl};
  311. this.app.desktop.openApplication(null, "cms.Document", options);
  312. this.app.close();
  313. },
  314. setPopularDocument : function(){
  315. this.app.setPopularDocument();
  316. },
  317. printWork: function(app, form){
  318. var application = app || this.businessData.work.application;
  319. var form = form;
  320. if (!form){
  321. form = this.json.id;
  322. if (this.json.printForm) form = this.json.printForm;
  323. }
  324. window.open("/x_desktop/printWork.html?workid="+this.businessData.work.id+"&app="+this.businessData.work.application+"&form="+form);
  325. }
  326. });