Subform.js 14 KB

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