FormUtil.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. Ext.define('saas.util.FormUtil', {
  2. statics: {
  3. // 主表允许自定义的配置名
  4. MAIN_ALLOW_CUS_FIELDS: ['fieldLabel', 'hidden', 'index', 'columnWidth', 'group'],
  5. // 从表允许自定义的配置名
  6. DETAIL_ALLOW_CUS_FIELDS: ['text', 'hidden', 'index', 'width'],
  7. setItems: function(form) {
  8. let me = this,
  9. defaultItems = form.defaultItems;
  10. saas.util.ViewUtil.getViewConfig(form.viewName)
  11. .then(function(cfg) {
  12. if(cfg) {
  13. let items = [];
  14. items = me.applyItemsGroup(defaultItems || []);
  15. items = me.initItems(items);
  16. items = me.applyCusMainItemConfig(items, cfg);
  17. items = me.applyCusDetailItemConfig(items, cfg);
  18. form.configItems = items;
  19. items = me.applyDefaultItems(form, items);
  20. form.removeAll();
  21. return form.addItems(items);
  22. }else {
  23. return []
  24. }
  25. })
  26. .then(function(items) {
  27. form.fireEvent('afterSetItems', form, items);
  28. })
  29. .then(function() {
  30. me.loadData(form);
  31. })
  32. .catch(function(response) {
  33. console.error(response);
  34. });
  35. },
  36. applyItemsGroup: function(items) {
  37. let groups = [],
  38. groupCount = 0,
  39. newItems = [];
  40. Ext.Array.each(items, function(it, index) {
  41. let item = Object.assign({}, it),
  42. groupName = item.group;
  43. if(!!groupName) {
  44. let idx = groups.findIndex(function(g) {
  45. return g.title == groupName;
  46. }),group;
  47. if(idx == -1) {
  48. group = {
  49. title: groupName,
  50. count: 1
  51. };
  52. groups.push(group);
  53. }else {
  54. group = groups[idx];
  55. group.count++;
  56. }
  57. }
  58. newItems.push(item);
  59. });
  60. Ext.Array.sort(newItems, function(a, b) {
  61. let gs = groups.concat([{
  62. title: '_nogroup'
  63. }]);
  64. a.group = a.group || '_nogroup';
  65. let v1 = gs.findIndex(function(g) {
  66. return g.title == a.group;
  67. })
  68. let v2 = gs.findIndex(function(g) {
  69. return g.title == b.group;
  70. })
  71. return v1 - v2;
  72. });
  73. Ext.Array.each(groups, function(g) {
  74. let idx = newItems.findIndex(function(i) {
  75. return i.group == g.title;
  76. });
  77. g.index = idx;
  78. });
  79. Ext.Array.each(groups, function(group, index) {
  80. let formIndex = group.index;
  81. delete group.index;
  82. Ext.Array.insert(newItems, formIndex + index, [{
  83. xtype: 'separatefield',
  84. name: 'group' + (++groupCount),
  85. html: group.title,
  86. fieldLabel: group.title || '分组' + groupCount
  87. }]);
  88. });
  89. return newItems;
  90. },
  91. initItems: function(items) {
  92. let itemCount = detailCount = 1, newItems = [];
  93. Ext.Array.each(items, function(it, i) {
  94. let item = Object.assign({}, it);
  95. if(item.xtype == 'detailGridField') {
  96. let columns = item.columns,
  97. colCount = 1;
  98. Ext.Array.each(columns, function(col, j) {
  99. if((col.hidden || col.width == 0 || !col.dataIndex) && (!col.hasOwnProperty('initHidden') || col.initHidden)) {
  100. Ext.applyIf(col, {
  101. index: -1,
  102. initHidden: true
  103. });
  104. }else {
  105. Ext.applyIf(col, {
  106. text: '',
  107. hidden: false,
  108. index: colCount++,
  109. allowBlank: true,
  110. width: 100,
  111. initHidden: false
  112. });
  113. }
  114. });
  115. if(!columns[columns.length - 1].flex) {
  116. columns.push({
  117. dataIndex: '',
  118. initHidden: true,
  119. flex: 1,
  120. allowBlank: true
  121. });
  122. }
  123. Ext.applyIf(item, {
  124. allowBlank: false,
  125. columnWidth: 1,
  126. gname: 'detail' + detailCount,
  127. fieldLabel: '从表' + (detailCount++),
  128. });
  129. }else if(item.xtype == 'hidden') {
  130. Ext.applyIf(item, {
  131. fieldLabel: '',
  132. hidden: true,
  133. initHidden: true,
  134. });
  135. }else if(item.xtype == 'separatefield') {
  136. Ext.applyIf(item, {
  137. fieldLabel: item.html,
  138. columnWidth: 1,
  139. });
  140. }else {
  141. Ext.applyIf(item, {
  142. fieldLabel: '',
  143. columnWidth: 0.25,
  144. });
  145. }
  146. if(item.hidden) {
  147. if(item.initHidden || !item.hasOwnProperty('initHidden')) {
  148. Ext.applyIf(item, {
  149. index: -1,
  150. initHidden: true
  151. });
  152. }else {
  153. Ext.applyIf(item, {
  154. index: itemCount++,
  155. initHidden: false
  156. });
  157. }
  158. }else {
  159. Ext.applyIf(item, {
  160. index: itemCount++,
  161. initHidden: false
  162. });
  163. }
  164. Ext.applyIf(item, {
  165. name: 'item' + i,
  166. hidden: false,
  167. allowBlank: true,
  168. group: undefined,
  169. });
  170. newItems.push(item);
  171. });
  172. Ext.Array.sort(newItems, function(a, b) {
  173. return a.index - b.index;
  174. });
  175. return newItems;
  176. },
  177. // 将自定义配置应用到默认items
  178. applyCusMainItemConfig: function(items, cfg) {
  179. let me = this,
  180. cusMainItems = cfg.main || [];
  181. Ext.Array.each(cusMainItems, function(cusItem) {
  182. let item = Ext.Array.findBy(items, function(item) {
  183. return item.name == cusItem.name;
  184. });
  185. if(!!item) {
  186. let keys = Ext.Object.getAllKeys(cusItem);
  187. keys.map(function(k) {
  188. if(me.MAIN_ALLOW_CUS_FIELDS.indexOf(k) != -1) {
  189. if(k != 'hidden' || item.allowBlank) {
  190. item['_init_' + k] = item.hasOwnProperty('_init_' + k) ? item['_init_' + k] : item[k];
  191. item[k] = cusItem[k];
  192. }
  193. }
  194. });
  195. }
  196. });
  197. Ext.Array.sort(items, function(a, b) {
  198. return a.index - b.index;
  199. });
  200. return items;
  201. },
  202. // 将自定义配置应用到从表items
  203. applyCusDetailItemConfig: function(items, cfg) {
  204. let me = this,
  205. detailCount = 1;
  206. Ext.Array.each(items, function(item) {
  207. let gname = item.gname;
  208. if(item.xtype == 'detailGridField' && cfg.hasOwnProperty(gname)) {
  209. let columns = item.columns,
  210. cusColumns = cfg[gname] || [];
  211. Ext.Array.each(cusColumns, function(cusCol) {
  212. let col = Ext.Array.findBy(columns, function(col) {
  213. return col.dataIndex == cusCol.dataIndex;
  214. });
  215. if(!!col) {
  216. let keys = Ext.Object.getAllKeys(cusCol);
  217. keys.map(function(k) {
  218. if(me.DETAIL_ALLOW_CUS_FIELDS.indexOf(k) != -1) {
  219. if(k != 'hidden' || col.allowBlank) {
  220. col['_init_' + k] = col.hasOwnProperty('_init_' + k) ? col['_init_' + k] : col[k];
  221. col[k] = cusCol[k];
  222. }
  223. }
  224. });
  225. }
  226. });
  227. Ext.Array.sort(columns, function(a, b) {
  228. return a.index - b.index;
  229. });
  230. }
  231. });
  232. return items;
  233. },
  234. /**
  235. * 处理formitems的一些默认配置
  236. */
  237. applyDefaultItems: function(form, items) {
  238. let me = this,
  239. formModel = form.getViewModel();
  240. Ext.Array.each(items, function(item) {
  241. // 设置必填
  242. if(item.allowBlank==false){
  243. // TODO 需要判断类型
  244. item.beforeLabelTextTpl = "<font color=\"red\" style=\"position:relative; top:2px;right:2px; font-weight: bolder;\">*</font>";
  245. }
  246. if(item.xtype == 'textfield') {
  247. Ext.applyIf(item, {
  248. maxLength: 50
  249. });
  250. }
  251. if(item.xtype == 'datefield') {
  252. Ext.applyIf(item, {
  253. editable: false,
  254. format: 'Y-m-d'
  255. });
  256. }
  257. if(item.xtype == 'numberfield') {
  258. Ext.applyIf(item, {
  259. hideTrigger: true, // 隐藏trigger
  260. mouseWheelEnabled: false // 取消滚轮事件
  261. });
  262. // 设置默认值为0
  263. formModel.set(item.name, 0);
  264. }
  265. // 如果是从表为其绑定store
  266. if(item.xtype == 'detailGridField') {
  267. let index = form.detailCount;
  268. let columns = item.columns,
  269. cnames = columns.filter(function(c) {
  270. return c.dataIndex && !c.ignore;
  271. }).map(function(c) {
  272. return c.dataIndex
  273. }),
  274. defaultValueColumns = {};
  275. Ext.Array.each(columns, function(c) {
  276. if(c.dataIndex && c.defaultValue) {
  277. defaultValueColumns[c.dataIndex] = c.defaultValue;
  278. }
  279. // 不可锁定
  280. Ext.applyIf(c, {
  281. lockable: false,
  282. width: 120
  283. });
  284. //必填
  285. Ext.applyIf(c, {
  286. allowBlank: true
  287. });
  288. if(!c.allowBlank){
  289. c.cls = 'x-grid-necessary';
  290. }
  291. if(c.xtype == 'textfield') {
  292. Ext.applyIf(c, {
  293. maxLength: 50
  294. });
  295. }else if(c.xtype == 'datecolumn') {
  296. Ext.applyIf(c, {
  297. format: 'Y-m-d'
  298. });
  299. }else if(c.xtype == 'numbercolumn') {
  300. Ext.applyIf(c, {
  301. align: 'end'
  302. });
  303. }
  304. let editor = c.editor;
  305. if(editor) {
  306. Ext.applyIf(editor, {
  307. selectOnFocus: true
  308. });
  309. if(editor.xtype == 'numberfield') {
  310. Ext.applyIf(editor, {
  311. hideTrigger: true, // 隐藏trigger
  312. mouseWheelEnabled: false // 取消滚轮事件
  313. });
  314. }else if(editor.xtype == 'datefield') {
  315. Ext.apply(editor, {
  316. format: 'Y-m-d'
  317. });
  318. Ext.applyIf(editor, {
  319. editable: false
  320. });
  321. }
  322. }
  323. });
  324. cnames.push(item.detnoColumn);
  325. formModel.set('detail' + index + '.detailBindFields', cnames);
  326. item.bind = {
  327. store: '{detail' + index + '.detailStore}'
  328. };
  329. formModel.set('detail' + index + '.detailStore', Ext.create('Ext.data.Store', {
  330. model:item.storeModel,
  331. data: [],
  332. listeners: {
  333. datachanged: function(s, eOpts) {
  334. let g = form.query('detailGridField')[index];
  335. g.fireEvent('datachanged', g, s, eOpts);
  336. },
  337. // 为新增行设置默认值
  338. add: function(store, records, index, eOpts) {
  339. Ext.Array.each(records, function(r) {
  340. for(k in defaultValueColumns) {
  341. r.set(k, defaultValueColumns[k]);
  342. }
  343. r.commit();
  344. });
  345. }
  346. }
  347. }));
  348. form.detailCount++;
  349. }
  350. });
  351. return items;
  352. },
  353. loadData: function(form) {
  354. let me = this;
  355. form.setLoading(true);
  356. if(form.initId && form.initId!=0) {
  357. let url = form._readUrl + '/' + form.initId;
  358. saas.util.BaseUtil.request({url })
  359. .then(function(res) {
  360. form.setLoading(false);
  361. if(res.success) {
  362. let d = res.data;
  363. let o = {
  364. main: d.main
  365. };
  366. if(d.hasOwnProperty('items')) {
  367. o.detail0 = d.items;
  368. }else {
  369. let idx = 1;
  370. while(d.hasOwnProperty('items' + idx)) {
  371. o['detail' + (idx - 1)] = d['items' + idx];
  372. idx++;
  373. }
  374. }
  375. form.initFormData(o);
  376. form.fireEvent('load', form, o);
  377. }
  378. })
  379. .catch(function(response) {
  380. form.setLoading(false);
  381. console.error(response);
  382. saas.util.BaseUtil.showErrorToast(response.message);
  383. });
  384. }else{
  385. //取后台编号
  386. saas.util.BaseUtil.request({
  387. url: '/api/commons/number/getMaxnumber',
  388. headers: {
  389. "Content-Type": 'application/x-www-form-urlencoded;charset=UTF-8'
  390. },
  391. params: {
  392. caller:form.caller
  393. },
  394. method: 'POST',
  395. }).then(function(res) {
  396. form.setLoading(false);
  397. if(res.success){
  398. let code = res.data;
  399. let viewModel = form.getViewModel();
  400. let detailGrids = form.query('detailGridField');
  401. if(code){
  402. let o = {};
  403. o[form._codeField] = code;
  404. let formData = {main: {}};
  405. Ext.apply(formData.main, o);
  406. Ext.Array.each(detailGrids, function(grid, index) {
  407. let detno = 0;
  408. let detnoColumn = grid.detnoColumn;
  409. let datas = [];
  410. let emptyRows = grid.emptyRows;
  411. Ext.Array.each(new Array(emptyRows), function() {
  412. detno += 1;
  413. let data = {};
  414. data[detnoColumn] = detno;
  415. datas.push(data);
  416. })
  417. formData['detail' + index] = datas;
  418. });
  419. return formData;
  420. }else {
  421. throw new Error('请求单据编号错误');
  422. }
  423. }else {
  424. return {
  425. main: {},
  426. }
  427. }
  428. }).then(function(formData) {
  429. let initData = form.initData;
  430. if(initData) {
  431. Ext.apply(initData.main, formData.main);
  432. form.setFormData(initData);
  433. form.fireEvent('load', form, initData);
  434. }else {
  435. form.initFormData(formData);
  436. form.fireEvent('load', form, formData);
  437. }
  438. }).catch(function(res) {
  439. form.clearDirty();
  440. form.setLoading(false);
  441. saas.util.BaseUtil.showErrorToast('请求单据编号错误: ' + res.message);
  442. })
  443. }
  444. }
  445. }
  446. });