Subform.js 14 KB

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