Subform.js 15 KB

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