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