Subform.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. MWF.xApplication.process.Xform.Subform = MWF.APPSubform = new Class({
  3. Extends: MWF.APP$Module,
  4. _loadUserInterface: function () {
  5. this.node.empty();
  6. if (this.json.isDelay) {
  7. if (this.form.subformLoadedCount) {
  8. this.form.subformLoadedCount++;
  9. } else {
  10. this.form.subformLoadedCount = 1
  11. }
  12. this.form.checkSubformLoaded();
  13. } else {
  14. this.getSubform(function () {
  15. this.loadSubform();
  16. }.bind(this));
  17. }
  18. },
  19. active: function (callback) {
  20. if (!this.loaded) {
  21. this.reload(callback)
  22. } else {
  23. if (callback) callback();
  24. }
  25. },
  26. reload: function (callback) {
  27. this.node.empty();
  28. this.getSubform(function () {
  29. this.loadSubform();
  30. if (callback) callback();
  31. }.bind(this));
  32. },
  33. loadCss: function () {
  34. if (this.subformData.json.css && this.subformData.json.css.code) {
  35. var cssText = this.form.parseCSS(this.subformData.json.css.code);
  36. var rex = new RegExp("(.+)(?=\\{)", "g");
  37. var match;
  38. var id = this.form.json.id.replace(/\-/g, "");
  39. while ((match = rex.exec(cssText)) !== null) {
  40. var prefix = ".css" + id + " ";
  41. var rule = prefix + match[0];
  42. cssText = cssText.substring(0, match.index) + rule + cssText.substring(rex.lastIndex, cssText.length);
  43. rex.lastIndex = rex.lastIndex + prefix.length;
  44. }
  45. var styleNode = $("style" + this.form.json.id);
  46. if (!styleNode) {
  47. var styleNode = document.createElement("style");
  48. styleNode.setAttribute("type", "text/css");
  49. styleNode.id = "style" + this.form.json.id;
  50. styleNode.inject(this.form.container, "before");
  51. }
  52. if (styleNode.styleSheet) {
  53. var setFunc = function () {
  54. styleNode.styleSheet.cssText += cssText;
  55. };
  56. if (styleNode.styleSheet.disabled) {
  57. setTimeout(setFunc, 10);
  58. } else {
  59. setFunc();
  60. }
  61. } else {
  62. var cssTextNode = document.createTextNode(cssText);
  63. styleNode.appendChild(cssTextNode);
  64. }
  65. }
  66. },
  67. checkSubformNested: function (id) {
  68. if (!id) return true;
  69. if (this.parentformIdList) {
  70. return !this.parentformIdList.contains(id);
  71. } else {
  72. return ![this.form.json.id].contains(id);
  73. }
  74. },
  75. checkSubformUnique: function (id) {
  76. if (!id) return true;
  77. if (!this.form.subformLoaded) return true;
  78. return !this.form.subformLoaded.contains(id);
  79. },
  80. getParentformIdList: function () {
  81. var parentformIdList;
  82. if (this.parentformIdList) {
  83. parentformIdList = Array.clone(this.parentformIdList);
  84. parentformIdList.push(this.subformData.json.id)
  85. } else {
  86. parentformIdList = [this.form.json.id, this.subformData.json.id];
  87. }
  88. return parentformIdList;
  89. },
  90. loadSubform: function () {
  91. if (this.subformData) {
  92. if (!this.checkSubformNested(this.subformData.json.id)) {
  93. this.form.notice(MWF.xApplication.process.Xform.LP.subformNestedError, "error");
  94. } else if (!this.checkSubformUnique(this.subformData.json.id)) {
  95. this.form.notice(MWF.xApplication.process.Xform.LP.subformUniqueError, "error");
  96. } else {
  97. //this.form.addEvent("postLoad", function(){
  98. this.loadCss();
  99. this.node.set("html", this.subformData.html);
  100. Object.each(this.subformData.json.moduleList, function (module, key) {
  101. var formKey = key;
  102. if (this.form.json.moduleList[key]) {
  103. formKey = this.json.id + "_" + key;
  104. var moduleNode = this.node.getElement("#" + key);
  105. if (moduleNode) moduleNode.set("id", formKey);
  106. module.id = formKey;
  107. }
  108. this.form.json.moduleList[formKey] = module;
  109. }.bind(this));
  110. var moduleNodes = this.form._getModuleNodes(this.node);
  111. moduleNodes.each(function (node) {
  112. if (node.get("MWFtype") !== "form") {
  113. var _self = this;
  114. var json = this.form._getDomjson(node);
  115. //if( json.type === "Subform" || json.moduleName === "subform" )this.form.subformCount++;
  116. var module = this.form._loadModule(json, node, function () {
  117. this.parentformIdList = _self.getParentformIdList();
  118. });
  119. this.form.modules.push(module);
  120. }
  121. }.bind(this));
  122. this.form.subformLoaded.push(this.subformData.json.id);
  123. //}.bind(this));
  124. }
  125. }
  126. if (!this.checked) {
  127. if (this.form.subformLoadedCount) {
  128. this.form.subformLoadedCount++;
  129. } else {
  130. this.form.subformLoadedCount = 1
  131. }
  132. this.form.checkSubformLoaded();
  133. }
  134. //console.log( "add subformLoadedCount , this.form.subformLoadedCount = "+ this.form.subformLoadedCount)
  135. this.loaded = true;
  136. this.checked = true;
  137. },
  138. getSubform: function (callback) {
  139. var method = (this.form.json.mode !== "Mobile" && !layout.mobile) ? "getForm" : "getFormMobile";
  140. if (this.json.subformType === "script") {
  141. if (this.json.subformScript && this.json.subformScript.code) {
  142. var data = this.form.Macro.exec(this.json.subformScript.code, this);
  143. if (data) {
  144. var formName, app;
  145. if (typeOf(data) === "string") {
  146. formName = data;
  147. } else {
  148. if (data.application) app = data.application;
  149. if (data.subform) formName = data.subform;
  150. }
  151. if (formName) {
  152. if (!app) app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  153. MWF.Actions.get("x_processplatform_assemble_surface")[method](formName, app, function (json) {
  154. this.getSubformData(json.data);
  155. if (callback) callback();
  156. }.bind(this));
  157. } else {
  158. if (callback) callback();
  159. }
  160. } else {
  161. if (callback) callback();
  162. }
  163. }
  164. } else {
  165. if (this.json.subformSelected && this.json.subformSelected !== "none") {
  166. var subformData = (this.form.app.relatedFormMap) ? this.form.app.relatedFormMap[this.json.subformSelected] : null;
  167. if (subformData) {
  168. this.getSubformData({"data": subformData.data});
  169. if (callback) callback();
  170. } else {
  171. var app;
  172. if (this.json.subformAppSelected) {
  173. app = this.json.subformAppSelected;
  174. } else {
  175. app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  176. }
  177. MWF.Actions.get("x_processplatform_assemble_surface")[method](this.json.subformSelected, app, function (json) {
  178. this.getSubformData(json.data);
  179. if (callback) callback();
  180. }.bind(this));
  181. }
  182. } else {
  183. if (callback) callback();
  184. }
  185. }
  186. },
  187. getSubformData: function (data) {
  188. if (!data || typeOf(data) !== "object") return;
  189. var subformDataStr = null;
  190. // if ( this.form.json.mode !== "Mobile" && !layout.mobile){
  191. // subformDataStr = data.data;
  192. // }else{
  193. // subformDataStr = data.mobileData;
  194. // }
  195. subformDataStr = data.data;
  196. this.subformData = null;
  197. if (subformDataStr) {
  198. this.subformData = JSON.decode(MWF.decodeJsonString(subformDataStr));
  199. this.subformData.updateTime = data.updateTime;
  200. }
  201. }
  202. });
  203. MWF.xApplication.process.Xform.SubmitForm = MWF.APPSubmitform = new Class({
  204. Extends: MWF.APPSubform,
  205. _loadUserInterface: function () {
  206. // this.node.empty();
  207. this.getSubform(function () {
  208. this.loadSubform();
  209. }.bind(this));
  210. },
  211. reload: function () {
  212. // this.node.empty();
  213. this.getSubform(function () {
  214. this.loadSubform();
  215. }.bind(this));
  216. },
  217. show: function () {
  218. if (this.json.submitScript && this.json.submitScript.code) {
  219. this.form.Macro.exec(this.json.submitScript.code, this);
  220. }
  221. // this.fireSubFormEvent("load");
  222. },
  223. // fireSubFormEvent : function( name ){
  224. // var events = this.subformData.json.events;
  225. // if( events && events[name] && events[name]["code"] ){
  226. // this.form.Macro.exec(events[name]["code"], this);
  227. // }
  228. // },
  229. loadSubform: function () {
  230. if (this.subformData) {
  231. if (!this.checkSubformUnique(this.subformData.json.id)) { //如果提交表单已经嵌入到表单中,那么把这个表单弹出来
  232. // this.form.notice(MWF.xApplication.process.Xform.LP.subformUniqueError, "error");
  233. this.isEmbedded = true;
  234. this.fireEvent("afterModulesLoad");
  235. } else if (!this.checkSubformNested(this.subformData.json.id)) {
  236. this.form.notice(MWF.xApplication.process.Xform.LP.subformNestedError, "error");
  237. } else {
  238. //this.form.addEvent("postLoad", function(){
  239. // this.fireSubFormEvent("queryLoad");
  240. this.loadCss();
  241. this.node.set("html", this.subformData.html);
  242. Object.each(this.subformData.json.moduleList, function (module, key) {
  243. var formKey = key;
  244. if (this.form.json.moduleList[key]) {
  245. formKey = this.json.id + "_" + key;
  246. var moduleNode = this.node.getElement("#" + key);
  247. if (moduleNode) moduleNode.set("id", formKey);
  248. module.id = formKey;
  249. }
  250. this.form.json.moduleList[formKey] = module;
  251. }.bind(this));
  252. var moduleNodes = this.form._getModuleNodes(this.node);
  253. moduleNodes.each(function (node) {
  254. if (node.get("MWFtype") !== "form") {
  255. var _self = this;
  256. var json = this.form._getDomjson(node);
  257. //if( json.type === "Subform" || json.moduleName === "subform" )this.form.subformCount++;
  258. var module = this.form._loadModule(json, node, function () {
  259. this.parentformIdList = _self.getParentformIdList();
  260. });
  261. this.form.modules.push(module);
  262. }
  263. }.bind(this));
  264. this.form.subformLoaded.push(this.subformData.json.id);
  265. this.fireEvent("afterModulesLoad");
  266. // this.fireSubFormEvent("postLoad");
  267. // this.fireSubFormEvent("load");
  268. // this.fireSubFormEvent("afterLoad");
  269. }
  270. }
  271. // if( this.form.subformLoadedCount ){
  272. // this.form.subformLoadedCount++;
  273. // }else{
  274. // this.form.subformLoadedCount = 1
  275. // }
  276. // this.form.checkSubformLoaded();
  277. },
  278. getSubform: function (callback) {
  279. var method = (this.form.json.mode !== "Mobile" && !layout.mobile) ? "getForm" : "getFormMobile";
  280. if (this.json.submitFormType === "script") {
  281. if (this.json.submitFormScript && this.json.submitFormScript.code) {
  282. var data = this.form.Macro.exec(this.json.submitFormScript.code, this);
  283. if (data) {
  284. var formName, app;
  285. if (typeOf(data) === "string") {
  286. formName = data;
  287. } else {
  288. if (data.application) app = data.application;
  289. if (data.form) formName = data.form;
  290. }
  291. if (formName) {
  292. if (!app) app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  293. MWF.Actions.get("x_processplatform_assemble_surface")[method](formName, app, function (json) {
  294. this.getSubformData(json.data);
  295. if (callback) callback();
  296. }.bind(this));
  297. } else {
  298. if (callback) callback();
  299. }
  300. } else {
  301. if (callback) callback();
  302. }
  303. }
  304. } else {
  305. if (this.json.submitFormSelected && this.json.submitFormSelected !== "none") {
  306. var app;
  307. if (this.json.submitFormAppSelected) {
  308. app = this.json.submitFormAppSelected;
  309. } else {
  310. app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  311. }
  312. MWF.Actions.get("x_processplatform_assemble_surface")[method](this.json.submitFormSelected, app, function (json) {
  313. this.getSubformData(json.data);
  314. if (callback) callback();
  315. }.bind(this));
  316. } else {
  317. if (callback) callback();
  318. }
  319. }
  320. }
  321. });