Subform.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. /** @class Subform 子表单组件。
  3. * @o2cn 子表单
  4. * @example
  5. * //可以在脚本中获取该组件
  6. * //方法1:
  7. * var subform = this.form.get("fieldId"); //获取组件
  8. * //方法2
  9. * var subform = this.target; //在组件本身的脚本中获取
  10. * @extends MWF.xApplication.process.Xform.$Module
  11. * @o2category FormComponents
  12. * @o2range {Process|CMS}
  13. * @hideconstructor
  14. */
  15. MWF.xApplication.process.Xform.Subform = MWF.APPSubform = new Class(
  16. /** @lends MWF.xApplication.process.Xform.Subform# */
  17. {
  18. Extends: MWF.APP$Module,
  19. options: {
  20. /**
  21. * 子表单的设计已经获取到,但还没有插入html及生成内部组件。
  22. * @event MWF.xApplication.process.Xform.Subform#beforeModulesLoad
  23. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  24. */
  25. /**
  26. * 子表单的设计已经获取到,已经插入html,组件json已经获取到,但未生成内部组件。
  27. * @example
  28. * //获取子表单所有组件id
  29. * var moduleIdList = Object.keys(this.target.subformData.json.moduleList);
  30. * @event MWF.xApplication.process.Xform.Subform#modulesLoad
  31. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  32. */
  33. /**
  34. * 子表单内部组件加载完成。
  35. * @example
  36. * //获取子表单所有组件id
  37. * var moduleIdList = Object.keys(this.target.subformData.json.moduleList);
  38. * //获取子表单所有组件
  39. * var moduleList = moduleIdList.map(function(id){
  40. * return this.form.get(id, subformId); //subformId为当前子表单ID,布局组件有可能id冲突,通过subformId来确定当前子表单的组件
  41. * }.bind(this))
  42. * @event MWF.xApplication.process.Xform.Subform#afterModulesLoad
  43. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  44. */
  45. "moduleEvents": ["load", "queryLoad", "postLoad", "beforeModulesLoad", "modulesLoad", "afterModulesLoad"]
  46. },
  47. _loadUserInterface: function () {
  48. /**
  49. * @ignore
  50. * @member parentLine
  51. * @memberOf MWF.xApplication.process.Xform.Subform#
  52. */
  53. this.node.empty();
  54. this.modules = [];
  55. this.moduleList = {};
  56. if (this.json.isDelay) {
  57. if (this.form.subformLoadedCount) {
  58. this.form.subformLoadedCount++;
  59. } else {
  60. this.form.subformLoadedCount = 1
  61. }
  62. this.form.checkSubformLoaded();
  63. this.checked = true;
  64. } else {
  65. this.getSubform(function () {
  66. this.loadSubform();
  67. }.bind(this));
  68. }
  69. },
  70. /**
  71. * @summary 当子表单被设置为延迟加载,通过active方法激活
  72. * @param {Function} callback 激活后的回调方法,另外已经激活过该方法还会被执行。
  73. * @example
  74. * var subform = this.form.get("fieldId");
  75. * subform.active(function(){
  76. * //do someting
  77. * })
  78. */
  79. active: function (callback) {
  80. if (!this.loaded) {
  81. this.reload(callback)
  82. } else {
  83. if (callback) callback();
  84. }
  85. },
  86. /**
  87. * @summary 重新加载子表单
  88. * @param {Function} callback
  89. * @example
  90. * this.form.get("fieldId").reload(function(){
  91. * //do someting
  92. * })
  93. */
  94. reload: function (callback) {
  95. this.clean();
  96. this.getSubform(function () {
  97. this.loadSubform();
  98. if (callback) callback();
  99. }.bind(this));
  100. },
  101. clean: function(){
  102. (this.modules || []).each(function(module){
  103. if( module.json && module.json.type === "Subform" ){
  104. if(module.clean)module.clean();
  105. }
  106. if (this.form.all[module.json.id]) delete this.form.all[module.json.id];
  107. if (this.form.forms[module.json.id])delete this.form.forms[module.json.id];
  108. this.form.modules.erase(module);
  109. }.bind(this));
  110. Object.each(this.moduleList || {}, function (module, formKey) {
  111. delete this.form.json.moduleList[formKey];
  112. }.bind(this));
  113. if( this.subformData && this.subformData.json.id ){
  114. var id = this.subformData.json.id;
  115. if( this.form.subformLoaded && this.form.subformLoaded.length ){
  116. this.form.subformLoaded.erase(id);
  117. }
  118. if( this.parentformIdList && this.parentformIdList.length){
  119. this.parentformIdList.erase(id);
  120. }
  121. }
  122. this.modules = [];
  123. this.moduleList = {};
  124. this.node.empty();
  125. },
  126. loadCss: function () {
  127. if (this.subformData.json.css && this.subformData.json.css.code) {
  128. var cssText = this.subformData.json.css.code;
  129. //删除注释
  130. cssText = cssText.replace(/\/\*[\s\S]*?\*\/\n|([^:]|^)\/\/.*\n$/g, '').replace(/\\n/, '');
  131. cssText = this.form.parseCSS(cssText);
  132. var rex = new RegExp("(.+)(?=\\{)", "g");
  133. var match;
  134. var id = this.form.json.id.replace(/\-/g, "");
  135. var prefix = ".css" + id + " ";
  136. while ((match = rex.exec(cssText)) !== null) {
  137. var rulesStr = match[0];
  138. var startWith = rulesStr.substring(0, 1);
  139. if (startWith === "@" || startWith === ":" || rulesStr.indexOf("%") !== -1) {
  140. }else if (rulesStr.trim()==='from' || rulesStr.trim()==='to'){
  141. } else {
  142. if (rulesStr.indexOf(",") != -1) {
  143. //var rules = rulesStr.split(/\s*,\s*/g);
  144. var rules = rulesStr.split(/,/g);
  145. rules = rules.map(function (r) {
  146. return prefix + r;
  147. });
  148. var rule = rules.join(",");
  149. cssText = cssText.substring(0, match.index) + rule + cssText.substring(rex.lastIndex, cssText.length);
  150. rex.lastIndex = rex.lastIndex + (prefix.length * rules.length);
  151. } else {
  152. var rule = prefix + match[0];
  153. cssText = cssText.substring(0, match.index) + rule + cssText.substring(rex.lastIndex, cssText.length);
  154. rex.lastIndex = rex.lastIndex + prefix.length;
  155. }
  156. }
  157. }
  158. var styleNode = $("style" + this.form.json.id);
  159. if (!styleNode) {
  160. var styleNode = document.createElement("style");
  161. styleNode.setAttribute("type", "text/css");
  162. styleNode.id = "style" + this.form.json.id;
  163. styleNode.inject(this.form.container, "before");
  164. }
  165. if (styleNode.styleSheet) {
  166. var setFunc = function () {
  167. styleNode.styleSheet.cssText += cssText;
  168. };
  169. if (styleNode.styleSheet.disabled) {
  170. setTimeout(setFunc, 10);
  171. } else {
  172. setFunc();
  173. }
  174. } else {
  175. var cssTextNode = document.createTextNode(cssText);
  176. styleNode.appendChild(cssTextNode);
  177. }
  178. }
  179. },
  180. checkSubformNested: function (id) {
  181. if (!id) return true;
  182. if (this.parentformIdList) {
  183. return !this.parentformIdList.contains(id);
  184. } else {
  185. return ![this.form.json.id].contains(id);
  186. }
  187. },
  188. checkSubformUnique: function (id) {
  189. if (!id) return true;
  190. if (!this.form.subformLoaded) return true;
  191. return !this.form.subformLoaded.contains(id);
  192. },
  193. getParentformIdList: function () {
  194. var parentformIdList;
  195. if (this.parentformIdList) {
  196. parentformIdList = Array.clone(this.parentformIdList);
  197. parentformIdList.push(this.subformData.json.id)
  198. } else {
  199. parentformIdList = [this.form.json.id, this.subformData.json.id];
  200. }
  201. return parentformIdList;
  202. },
  203. loadSubform: function () {
  204. if (this.subformData) {
  205. if (!this.checkSubformNested(this.subformData.json.id)) {
  206. this.form.notice(MWF.xApplication.process.Xform.LP.subformNestedError, "error");
  207. } else if (!this.checkSubformUnique(this.subformData.json.id)) {
  208. this.form.notice(MWF.xApplication.process.Xform.LP.subformUniqueError, "error");
  209. } else {
  210. //this.form.addEvent("postLoad", function(){
  211. this.fireEvent("beforeModulesLoad");
  212. this.loadCss();
  213. this.modules = [];
  214. this.moduleList = {};
  215. this.node.set("html", this.subformData.html);
  216. Object.each(this.subformData.json.moduleList, function (module, key) {
  217. var formKey = key;
  218. if (this.form.json.moduleList[key]) {
  219. formKey = this.json.id + "_" + key;
  220. var moduleNode = this.node.getElement("#" + key);
  221. if (moduleNode) moduleNode.set("id", formKey);
  222. module.id = formKey;
  223. module._originId = key;
  224. module._subform = this.json.id;
  225. }
  226. this.form.json.moduleList[formKey] = module;
  227. this.moduleList[formKey] = module;
  228. }.bind(this));
  229. this.fireEvent("modulesLoad");
  230. var moduleNodes = this.form._getModuleNodes(this.node);
  231. moduleNodes.each(function (node) {
  232. if (node.get("MWFtype") !== "form") {
  233. var _self = this;
  234. var json = this.form._getDomjson(node);
  235. //if( json.type === "Subform" || json.moduleName === "subform" )this.form.subformCount++;
  236. var module = this.form._loadModule(json, node, function () {
  237. this.parentformIdList = _self.getParentformIdList();
  238. });
  239. this.form.modules.push(module);
  240. this.modules.push(module);
  241. }
  242. }.bind(this));
  243. this.form.subformLoaded.push(this.subformData.json.id);
  244. this.fireEvent("afterModulesLoad");
  245. //}.bind(this));
  246. }
  247. }
  248. if (!this.checked) {
  249. if (this.form.subformLoadedCount) {
  250. this.form.subformLoadedCount++;
  251. } else {
  252. this.form.subformLoadedCount = 1
  253. }
  254. this.form.checkSubformLoaded();
  255. }
  256. //console.log( "add subformLoadedCount , this.form.subformLoadedCount = "+ this.form.subformLoadedCount)
  257. /**
  258. * @summary 表单是否加载(激活)过。
  259. * @member {Boolean}
  260. * @example
  261. * if( !this.form.get("fieldId").loaded ){ //判断子表单是否加载过
  262. * this.form.get("fieldId").active(); //没有加载过则激活
  263. * }
  264. */
  265. this.loaded = true;
  266. this.checked = true;
  267. },
  268. getSubform: function (callback) {
  269. var method = (this.form.json.mode !== "Mobile" && !layout.mobile) ? "getForm" : "getFormMobile";
  270. if (this.json.subformType === "script") {
  271. if (this.json.subformScript && this.json.subformScript.code) {
  272. var data = this.form.Macro.exec(this.json.subformScript.code, this);
  273. if (data) {
  274. var formName, app;
  275. if (typeOf(data) === "string") {
  276. formName = data;
  277. } else {
  278. if (data.application) app = data.application;
  279. if (data.subform) formName = data.subform;
  280. }
  281. if (formName) {
  282. if (!app) app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  283. MWF.Actions.get("x_processplatform_assemble_surface")[method](formName, app, function (json) {
  284. this.getSubformData(json.data);
  285. if (callback) callback();
  286. }.bind(this));
  287. } else {
  288. if (callback) callback();
  289. }
  290. } else {
  291. if (callback) callback();
  292. }
  293. }
  294. } else {
  295. if (this.json.subformSelected && this.json.subformSelected !== "none") {
  296. var subformData = (this.form.app.relatedFormMap) ? this.form.app.relatedFormMap[this.json.subformSelected] : null;
  297. if (subformData) {
  298. this.getSubformData({"data": subformData.data});
  299. if (callback) callback();
  300. } else {
  301. var app;
  302. if (this.json.subformAppSelected) {
  303. app = this.json.subformAppSelected;
  304. } else {
  305. app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  306. }
  307. MWF.Actions.get("x_processplatform_assemble_surface")[method](this.json.subformSelected, app, function (json) {
  308. this.getSubformData(json.data);
  309. if (callback) callback();
  310. }.bind(this));
  311. }
  312. } else {
  313. if (callback) callback();
  314. }
  315. }
  316. },
  317. getSubformData: function (data) {
  318. if (!data || typeOf(data) !== "object") return;
  319. var subformDataStr = null;
  320. // if ( this.form.json.mode !== "Mobile" && !layout.mobile){
  321. // subformDataStr = data.data;
  322. // }else{
  323. // subformDataStr = data.mobileData;
  324. // }
  325. subformDataStr = data.data;
  326. this.subformData = null;
  327. if (subformDataStr) {
  328. if( this.form.isParseLanguage ) {
  329. var jsonStr = o2.bindJson(MWF.decodeJsonString(subformDataStr), {"lp": MWF.xApplication.process.Xform.LP.form});
  330. this.subformData = JSON.decode(jsonStr);
  331. }else{
  332. this.subformData = JSON.decode(MWF.decodeJsonString(subformDataStr));
  333. }
  334. this.subformData.updateTime = data.updateTime;
  335. }
  336. }
  337. });
  338. MWF.xApplication.process.Xform.SubmitForm = MWF.APPSubmitform = new Class({
  339. Extends: MWF.APPSubform,
  340. _loadUserInterface: function () {
  341. // this.node.empty();
  342. this.getSubform(function () {
  343. this.loadSubform();
  344. }.bind(this));
  345. },
  346. reload: function () {
  347. // this.node.empty();
  348. this.getSubform(function () {
  349. this.loadSubform();
  350. }.bind(this));
  351. },
  352. show: function ( defaultRoute ) {
  353. if (this.json.submitScript && this.json.submitScript.code) {
  354. this.form.Macro.environment.defaultRoute = defaultRoute;
  355. this.form.Macro.exec(this.json.submitScript.code, this);
  356. }
  357. // this.fireSubFormEvent("load");
  358. },
  359. // fireSubFormEvent : function( name ){
  360. // var events = this.subformData.json.events;
  361. // if( events && events[name] && events[name]["code"] ){
  362. // this.form.Macro.exec(events[name]["code"], this);
  363. // }
  364. // },
  365. loadSubform: function () {
  366. if (this.subformData) {
  367. if (!this.checkSubformUnique(this.subformData.json.id)) { //如果提交表单已经嵌入到表单中,那么把这个表单弹出来
  368. // this.form.notice(MWF.xApplication.process.Xform.LP.subformUniqueError, "error");
  369. this.isEmbedded = true;
  370. this.fireEvent("afterModulesLoad");
  371. } else if (!this.checkSubformNested(this.subformData.json.id)) {
  372. this.form.notice(MWF.xApplication.process.Xform.LP.subformNestedError, "error");
  373. } else {
  374. //this.form.addEvent("postLoad", function(){
  375. // this.fireSubFormEvent("queryLoad");
  376. this.fireEvent("beforeModulesLoad");
  377. this.loadCss();
  378. this.node.set("html", this.subformData.html);
  379. Object.each(this.subformData.json.moduleList, function (module, key) {
  380. var formKey = key;
  381. if (this.form.json.moduleList[key]) {
  382. formKey = this.json.id + "_" + key;
  383. var moduleNode = this.node.getElement("#" + key);
  384. if (moduleNode) moduleNode.set("id", formKey);
  385. module.id = formKey;
  386. module._originId = key;
  387. module._subform = this.json.id;
  388. }
  389. this.form.json.moduleList[formKey] = module;
  390. }.bind(this));
  391. var moduleNodes = this.form._getModuleNodes(this.node);
  392. moduleNodes.each(function (node) {
  393. if (node.get("MWFtype") !== "form") {
  394. var _self = this;
  395. var json = this.form._getDomjson(node);
  396. //if( json.type === "Subform" || json.moduleName === "subform" )this.form.subformCount++;
  397. var module = this.form._loadModule(json, node, function () {
  398. this.parentformIdList = _self.getParentformIdList();
  399. });
  400. this.form.modules.push(module);
  401. }
  402. }.bind(this));
  403. this.form.subformLoaded.push(this.subformData.json.id);
  404. this.fireEvent("afterModulesLoad");
  405. // this.fireSubFormEvent("postLoad");
  406. // this.fireSubFormEvent("load");
  407. // this.fireSubFormEvent("afterLoad");
  408. }
  409. }
  410. // if( this.form.subformLoadedCount ){
  411. // this.form.subformLoadedCount++;
  412. // }else{
  413. // this.form.subformLoadedCount = 1
  414. // }
  415. // this.form.checkSubformLoaded();
  416. },
  417. getSubform: function (callback) {
  418. var method = (this.form.json.mode !== "Mobile" && !layout.mobile) ? "getForm" : "getFormMobile";
  419. if (this.json.submitFormType === "script") {
  420. if (this.json.submitFormScript && this.json.submitFormScript.code) {
  421. var data = this.form.Macro.exec(this.json.submitFormScript.code, this);
  422. if (data) {
  423. var formName, app;
  424. if (typeOf(data) === "string") {
  425. formName = data;
  426. } else {
  427. if (data.application) app = data.application;
  428. if (data.form) formName = data.form;
  429. }
  430. if (formName) {
  431. if (!app) app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  432. MWF.Actions.get("x_processplatform_assemble_surface")[method](formName, app, function (json) {
  433. this.getSubformData(json.data);
  434. if (callback) callback();
  435. }.bind(this));
  436. } else {
  437. if (callback) callback();
  438. }
  439. } else {
  440. if (callback) callback();
  441. }
  442. }
  443. } else {
  444. if (this.json.submitFormSelected && this.json.submitFormSelected !== "none") {
  445. var app;
  446. if (this.json.submitFormAppSelected) {
  447. app = this.json.submitFormAppSelected;
  448. } else {
  449. app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  450. }
  451. MWF.Actions.get("x_processplatform_assemble_surface")[method](this.json.submitFormSelected, app, function (json) {
  452. this.getSubformData(json.data);
  453. if (callback) callback();
  454. }.bind(this));
  455. } else {
  456. if (callback) callback();
  457. }
  458. }
  459. }
  460. });