Subform.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. ,
  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 app;
  171. if (this.json.subformAppSelected) {
  172. app = this.json.subformAppSelected;
  173. } else {
  174. app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  175. }
  176. MWF.Actions.get("x_processplatform_assemble_surface")[method](this.json.subformSelected, app, function (json) {
  177. this.getSubformData(json.data);
  178. if (callback) callback();
  179. }.bind(this));
  180. } else {
  181. if (callback) callback();
  182. }
  183. }
  184. }
  185. ,
  186. getSubformData: function (data) {
  187. if (!data || typeOf(data) !== "object")return;
  188. var subformDataStr = null;
  189. // if ( this.form.json.mode !== "Mobile" && !layout.mobile){
  190. // subformDataStr = data.data;
  191. // }else{
  192. // subformDataStr = data.mobileData;
  193. // }
  194. subformDataStr = data.data;
  195. this.subformData = null;
  196. if (subformDataStr) {
  197. this.subformData = JSON.decode(MWF.decodeJsonString(subformDataStr));
  198. this.subformData.updateTime = data.updateTime;
  199. }
  200. }
  201. });
  202. MWF.xApplication.process.Xform.SubmitForm = MWF.APPSubmitform = new Class({
  203. Extends: MWF.APPSubform,
  204. _loadUserInterface: function () {
  205. // this.node.empty();
  206. this.getSubform(function () {
  207. this.loadSubform();
  208. }.bind(this));
  209. },
  210. reload: function () {
  211. // this.node.empty();
  212. this.getSubform(function () {
  213. this.loadSubform();
  214. }.bind(this));
  215. },
  216. show: function () {
  217. if (this.json.submitScript && this.json.submitScript.code) {
  218. this.form.Macro.exec(this.json.submitScript.code, this);
  219. }
  220. // this.fireSubFormEvent("load");
  221. },
  222. // fireSubFormEvent : function( name ){
  223. // var events = this.subformData.json.events;
  224. // if( events && events[name] && events[name]["code"] ){
  225. // this.form.Macro.exec(events[name]["code"], this);
  226. // }
  227. // },
  228. loadSubform: function () {
  229. if (this.subformData) {
  230. if (!this.checkSubformUnique(this.subformData.json.id)) { //如果提交表单已经嵌入到表单中,那么把这个表单弹出来
  231. // this.form.notice(MWF.xApplication.process.Xform.LP.subformUniqueError, "error");
  232. this.isEmbedded = true;
  233. this.fireEvent("afterModulesLoad");
  234. } else if (!this.checkSubformNested(this.subformData.json.id)) {
  235. this.form.notice(MWF.xApplication.process.Xform.LP.subformNestedError, "error");
  236. } else {
  237. //this.form.addEvent("postLoad", function(){
  238. // this.fireSubFormEvent("queryLoad");
  239. this.loadCss();
  240. this.node.set("html", this.subformData.html);
  241. Object.each(this.subformData.json.moduleList, function (module, key) {
  242. var formKey = key;
  243. if (this.form.json.moduleList[key]) {
  244. formKey = this.json.id + "_" + key;
  245. var moduleNode = this.node.getElement("#" + key);
  246. if (moduleNode) moduleNode.set("id", formKey);
  247. module.id = formKey;
  248. }
  249. this.form.json.moduleList[formKey] = module;
  250. }.bind(this));
  251. var moduleNodes = this.form._getModuleNodes(this.node);
  252. moduleNodes.each(function (node) {
  253. if (node.get("MWFtype") !== "form") {
  254. var _self = this;
  255. var json = this.form._getDomjson(node);
  256. //if( json.type === "Subform" || json.moduleName === "subform" )this.form.subformCount++;
  257. var module = this.form._loadModule(json, node, function () {
  258. this.parentformIdList = _self.getParentformIdList();
  259. });
  260. this.form.modules.push(module);
  261. }
  262. }.bind(this));
  263. this.form.subformLoaded.push(this.subformData.json.id);
  264. this.fireEvent("afterModulesLoad");
  265. // this.fireSubFormEvent("postLoad");
  266. // this.fireSubFormEvent("load");
  267. // this.fireSubFormEvent("afterLoad");
  268. }
  269. }
  270. // if( this.form.subformLoadedCount ){
  271. // this.form.subformLoadedCount++;
  272. // }else{
  273. // this.form.subformLoadedCount = 1
  274. // }
  275. // this.form.checkSubformLoaded();
  276. },
  277. getSubform: function (callback) {
  278. var method = (this.form.json.mode !== "Mobile" && !layout.mobile) ? "getForm" : "getFormMobile";
  279. if (this.json.submitFormType === "script") {
  280. if (this.json.submitFormScript && this.json.submitFormScript.code) {
  281. var data = this.form.Macro.exec(this.json.submitFormScript.code, this);
  282. if (data) {
  283. var formName, app;
  284. if (typeOf(data) === "string") {
  285. formName = data;
  286. } else {
  287. if (data.application)app = data.application;
  288. if (data.form)formName = data.form;
  289. }
  290. if (formName) {
  291. if (!app)app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  292. MWF.Actions.get("x_processplatform_assemble_surface")[method](formName, app, function (json) {
  293. this.getSubformData(json.data);
  294. if (callback) callback();
  295. }.bind(this));
  296. } else {
  297. if (callback) callback();
  298. }
  299. } else {
  300. if (callback) callback();
  301. }
  302. }
  303. } else {
  304. if (this.json.submitFormSelected && this.json.submitFormSelected !== "none") {
  305. var app;
  306. if (this.json.submitFormAppSelected) {
  307. app = this.json.submitFormAppSelected;
  308. } else {
  309. app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  310. }
  311. MWF.Actions.get("x_processplatform_assemble_surface")[method](this.json.submitFormSelected, app, function (json) {
  312. this.getSubformData(json.data);
  313. if (callback) callback();
  314. }.bind(this));
  315. } else {
  316. if (callback) callback();
  317. }
  318. }
  319. }
  320. });