Form.js 23 KB

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