Subform.js 15 KB

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