Subform.js 14 KB

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