Subform.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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.json.isReadonly )module.isReadonly = true;
  219. if (this.form.json.moduleList[key]) {
  220. formKey = this.json.id + "_" + key;
  221. var moduleNode = this.node.getElement("#" + key);
  222. if (moduleNode) moduleNode.set("id", formKey);
  223. module.id = formKey;
  224. module._originId = key;
  225. module._subform = this.json.id;
  226. }
  227. this.form.json.moduleList[formKey] = module;
  228. this.moduleList[formKey] = module;
  229. }.bind(this));
  230. this.fireEvent("modulesLoad");
  231. var moduleNodes = this.form._getModuleNodes(this.node);
  232. moduleNodes.each(function (node) {
  233. if (node.get("MWFtype") !== "form") {
  234. var _self = this;
  235. var json = this.form._getDomjson(node);
  236. //if( json.type === "Subform" || json.moduleName === "subform" )this.form.subformCount++;
  237. var module = this.form._loadModule(json, node, function () {
  238. this.parentformIdList = _self.getParentformIdList();
  239. });
  240. this.form.modules.push(module);
  241. this.modules.push(module);
  242. }
  243. }.bind(this));
  244. this.form.subformLoaded.push(this.subformData.json.id);
  245. this.fireEvent("afterModulesLoad");
  246. //}.bind(this));
  247. }
  248. }
  249. if (!this.checked) {
  250. if (this.form.subformLoadedCount) {
  251. this.form.subformLoadedCount++;
  252. } else {
  253. this.form.subformLoadedCount = 1
  254. }
  255. this.form.checkSubformLoaded();
  256. }
  257. //console.log( "add subformLoadedCount , this.form.subformLoadedCount = "+ this.form.subformLoadedCount)
  258. /**
  259. * @summary 表单是否加载(激活)过。
  260. * @member {Boolean}
  261. * @example
  262. * if( !this.form.get("fieldId").loaded ){ //判断子表单是否加载过
  263. * this.form.get("fieldId").active(); //没有加载过则激活
  264. * }
  265. */
  266. this.loaded = true;
  267. this.checked = true;
  268. },
  269. getSubform: function (callback) {
  270. var method = (this.form.json.mode !== "Mobile" && !layout.mobile) ? "getForm" : "getFormMobile";
  271. if (this.json.subformType === "script") {
  272. if (this.json.subformScript && this.json.subformScript.code) {
  273. var data = this.form.Macro.exec(this.json.subformScript.code, this);
  274. if (data) {
  275. var formName, app;
  276. if (typeOf(data) === "string") {
  277. formName = data;
  278. } else {
  279. if (data.application) app = data.application;
  280. if (data.subform) formName = data.subform;
  281. }
  282. if (formName) {
  283. if (!app) app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  284. MWF.Actions.get("x_processplatform_assemble_surface")[method](formName, app, function (json) {
  285. this.getSubformData(json.data);
  286. if (callback) callback();
  287. }.bind(this));
  288. } else {
  289. if (callback) callback();
  290. }
  291. } else {
  292. if (callback) callback();
  293. }
  294. }
  295. } else {
  296. if (this.json.subformSelected && this.json.subformSelected !== "none") {
  297. var subformData = (this.form.app.relatedFormMap) ? this.form.app.relatedFormMap[this.json.subformSelected] : null;
  298. if (subformData) {
  299. this.getSubformData({"data": subformData.data});
  300. if (callback) callback();
  301. } else {
  302. var app;
  303. if (this.json.subformAppSelected) {
  304. app = this.json.subformAppSelected;
  305. } else {
  306. app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  307. }
  308. MWF.Actions.get("x_processplatform_assemble_surface")[method](this.json.subformSelected, app, function (json) {
  309. this.getSubformData(json.data);
  310. if (callback) callback();
  311. }.bind(this));
  312. }
  313. } else {
  314. if (callback) callback();
  315. }
  316. }
  317. },
  318. getSubformData: function (data) {
  319. if (!data || typeOf(data) !== "object") return;
  320. var subformDataStr = null;
  321. // if ( this.form.json.mode !== "Mobile" && !layout.mobile){
  322. // subformDataStr = data.data;
  323. // }else{
  324. // subformDataStr = data.mobileData;
  325. // }
  326. subformDataStr = data.data;
  327. this.subformData = null;
  328. if (subformDataStr) {
  329. if( this.form.isParseLanguage ) {
  330. var jsonStr = o2.bindJson(MWF.decodeJsonString(subformDataStr), {"lp": MWF.xApplication.process.Xform.LP.form});
  331. this.subformData = JSON.decode(jsonStr);
  332. }else{
  333. this.subformData = JSON.decode(MWF.decodeJsonString(subformDataStr));
  334. }
  335. this.subformData.updateTime = data.updateTime;
  336. }
  337. }
  338. });
  339. MWF.xApplication.process.Xform.SubmitForm = MWF.APPSubmitform = new Class({
  340. Extends: MWF.APPSubform,
  341. _loadUserInterface: function () {
  342. // this.node.empty();
  343. this.getSubform(function () {
  344. this.loadSubform();
  345. }.bind(this));
  346. },
  347. reload: function () {
  348. // this.node.empty();
  349. this.getSubform(function () {
  350. this.loadSubform();
  351. }.bind(this));
  352. },
  353. show: function ( defaultRoute ) {
  354. if (this.json.submitScript && this.json.submitScript.code) {
  355. this.form.Macro.environment.defaultRoute = defaultRoute;
  356. this.form.Macro.exec(this.json.submitScript.code, this);
  357. }
  358. // this.fireSubFormEvent("load");
  359. },
  360. // fireSubFormEvent : function( name ){
  361. // var events = this.subformData.json.events;
  362. // if( events && events[name] && events[name]["code"] ){
  363. // this.form.Macro.exec(events[name]["code"], this);
  364. // }
  365. // },
  366. loadSubform: function () {
  367. if (this.subformData) {
  368. if (!this.checkSubformUnique(this.subformData.json.id)) { //如果提交表单已经嵌入到表单中,那么把这个表单弹出来
  369. // this.form.notice(MWF.xApplication.process.Xform.LP.subformUniqueError, "error");
  370. this.isEmbedded = true;
  371. this.fireEvent("afterModulesLoad");
  372. } else if (!this.checkSubformNested(this.subformData.json.id)) {
  373. this.form.notice(MWF.xApplication.process.Xform.LP.subformNestedError, "error");
  374. } else {
  375. //this.form.addEvent("postLoad", function(){
  376. // this.fireSubFormEvent("queryLoad");
  377. this.fireEvent("beforeModulesLoad");
  378. this.loadCss();
  379. this.node.set("html", this.subformData.html);
  380. Object.each(this.subformData.json.moduleList, function (module, key) {
  381. var formKey = key;
  382. if (this.form.json.moduleList[key]) {
  383. formKey = this.json.id + "_" + key;
  384. var moduleNode = this.node.getElement("#" + key);
  385. if (moduleNode) moduleNode.set("id", formKey);
  386. module.id = formKey;
  387. module._originId = key;
  388. module._subform = this.json.id;
  389. }
  390. this.form.json.moduleList[formKey] = module;
  391. }.bind(this));
  392. var moduleNodes = this.form._getModuleNodes(this.node);
  393. moduleNodes.each(function (node) {
  394. if (node.get("MWFtype") !== "form") {
  395. var _self = this;
  396. var json = this.form._getDomjson(node);
  397. //if( json.type === "Subform" || json.moduleName === "subform" )this.form.subformCount++;
  398. var module = this.form._loadModule(json, node, function () {
  399. this.parentformIdList = _self.getParentformIdList();
  400. });
  401. this.form.modules.push(module);
  402. }
  403. }.bind(this));
  404. this.form.subformLoaded.push(this.subformData.json.id);
  405. this.fireEvent("afterModulesLoad");
  406. // this.fireSubFormEvent("postLoad");
  407. // this.fireSubFormEvent("load");
  408. // this.fireSubFormEvent("afterLoad");
  409. }
  410. }
  411. // if( this.form.subformLoadedCount ){
  412. // this.form.subformLoadedCount++;
  413. // }else{
  414. // this.form.subformLoadedCount = 1
  415. // }
  416. // this.form.checkSubformLoaded();
  417. },
  418. getSubform: function (callback) {
  419. var method = (this.form.json.mode !== "Mobile" && !layout.mobile) ? "getForm" : "getFormMobile";
  420. if (this.json.submitFormType === "script") {
  421. if (this.json.submitFormScript && this.json.submitFormScript.code) {
  422. var data = this.form.Macro.exec(this.json.submitFormScript.code, this);
  423. if (data) {
  424. var formName, app;
  425. if (typeOf(data) === "string") {
  426. formName = data;
  427. } else {
  428. if (data.application) app = data.application;
  429. if (data.form) formName = data.form;
  430. }
  431. if (formName) {
  432. if (!app) app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  433. MWF.Actions.get("x_processplatform_assemble_surface")[method](formName, app, function (json) {
  434. this.getSubformData(json.data);
  435. if (callback) callback();
  436. }.bind(this));
  437. } else {
  438. if (callback) callback();
  439. }
  440. } else {
  441. if (callback) callback();
  442. }
  443. }
  444. } else {
  445. if (this.json.submitFormSelected && this.json.submitFormSelected !== "none") {
  446. var app;
  447. if (this.json.submitFormAppSelected) {
  448. app = this.json.submitFormAppSelected;
  449. } else {
  450. app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  451. }
  452. MWF.Actions.get("x_processplatform_assemble_surface")[method](this.json.submitFormSelected, app, function (json) {
  453. this.getSubformData(json.data);
  454. if (callback) callback();
  455. }.bind(this));
  456. } else {
  457. if (callback) callback();
  458. }
  459. }
  460. }
  461. });