FormUtil.js 19 KB

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