FormUtil.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. // form.add(defaultItems);
  11. saas.util.ViewUtil.getViewConfig(form.viewName)
  12. .then(function(cfg) {
  13. if(cfg) {
  14. let items = [];
  15. items = me.applyItemsGroup(defaultItems || []);
  16. items = me.initItems(items);
  17. items = me.applyCusMainItemConfig(items, cfg);
  18. items = me.applyCusDetailItemConfig(items, cfg);
  19. form.configItems = items;
  20. items = me.applyDefaultItems(form, items);
  21. form.removeAll();
  22. return form.addItems(items);
  23. }else {
  24. return []
  25. }
  26. })
  27. .then(function(items) {
  28. form.fireEvent('afterSetItems', form, items);
  29. })
  30. .then(function() {
  31. me.loadData(form);
  32. })
  33. },
  34. applyItemsGroup: function(items) {
  35. let groups = [],
  36. groupCount = 0,
  37. newItems = [];
  38. Ext.Array.each(items, function(it, index) {
  39. let item = Object.assign({}, it),
  40. groupName = item.group;
  41. if(!!groupName) {
  42. let idx = groups.findIndex(function(g) {
  43. return g.title == groupName;
  44. }),group;
  45. if(idx == -1) {
  46. group = {
  47. title: groupName,
  48. count: 1
  49. };
  50. groups.push(group);
  51. }else {
  52. group = groups[idx];
  53. group.count++;
  54. }
  55. }
  56. newItems.push(item);
  57. });
  58. Ext.Array.sort(newItems, function(a, b) {
  59. let gs = groups.concat([{
  60. title: '_nogroup'
  61. }]);
  62. a.group = a.group || '_nogroup';
  63. b.group = b.group || '_nogroup';
  64. let v1 = gs.findIndex(function(g) {
  65. return g.title == a.group;
  66. })
  67. let v2 = gs.findIndex(function(g) {
  68. return g.title == b.group;
  69. })
  70. return v1 - v2;
  71. });
  72. Ext.Array.each(groups, function(g) {
  73. let idx = newItems.findIndex(function(i) {
  74. return i.group == g.title;
  75. });
  76. g.index = idx;
  77. });
  78. Ext.Array.each(groups, function(group, index) {
  79. let formIndex = group.index;
  80. delete group.index;
  81. Ext.Array.insert(newItems, formIndex + index, [{
  82. xtype: 'separatefield',
  83. name: 'group' + (++groupCount),
  84. html: group.title,
  85. fieldLabel: group.title || '分组' + groupCount
  86. }]);
  87. });
  88. return newItems;
  89. },
  90. initItems: function(items) {
  91. let itemCount = detailCount = 1, newItems = [];
  92. Ext.Array.each(items, function(it, i) {
  93. let item = Object.assign({}, it);
  94. if(item.xtype == 'detailGridField') {
  95. let columns = item.columns,
  96. colCount = 1;
  97. Ext.Array.each(columns, function(col, j) {
  98. if((col.hidden || col.width == 0 || !col.dataIndex) && (!col.hasOwnProperty('initHidden') || col.initHidden)) {
  99. Ext.applyIf(col, {
  100. index: -1,
  101. initHidden: true
  102. });
  103. }else {
  104. Ext.applyIf(col, {
  105. text: '',
  106. hidden: false,
  107. index: colCount++,
  108. allowBlank: true,
  109. width: 100,
  110. initHidden: false
  111. });
  112. }
  113. });
  114. if(!columns[columns.length - 1].flex) {
  115. columns.push({
  116. dataIndex: '',
  117. initHidden: true,
  118. flex: 1,
  119. allowBlank: true
  120. });
  121. }
  122. Ext.applyIf(item, {
  123. allowBlank: false,
  124. columnWidth: 1,
  125. gname: 'detail' + detailCount,
  126. fieldLabel: '从表' + (detailCount++),
  127. });
  128. }else if(item.xtype == 'hidden') {
  129. Ext.applyIf(item, {
  130. fieldLabel: '',
  131. hidden: true,
  132. initHidden: true,
  133. });
  134. }else if(item.xtype == 'separatefield') {
  135. Ext.applyIf(item, {
  136. fieldLabel: item.html,
  137. columnWidth: 1,
  138. });
  139. }else {
  140. Ext.applyIf(item, {
  141. fieldLabel: '',
  142. columnWidth: 0.25,
  143. });
  144. }
  145. if(item.hidden) {
  146. if(item.initHidden || !item.hasOwnProperty('initHidden')) {
  147. Ext.applyIf(item, {
  148. index: -1,
  149. initHidden: true
  150. });
  151. }else {
  152. Ext.applyIf(item, {
  153. index: itemCount++,
  154. initHidden: false
  155. });
  156. }
  157. }else {
  158. Ext.applyIf(item, {
  159. index: itemCount++,
  160. initHidden: false
  161. });
  162. }
  163. Ext.applyIf(item, {
  164. name: 'item' + i,
  165. hidden: false,
  166. allowBlank: true,
  167. group: undefined,
  168. });
  169. newItems.push(item);
  170. });
  171. Ext.Array.sort(newItems, function(a, b) {
  172. return a.index - b.index;
  173. });
  174. return newItems;
  175. },
  176. // 将自定义配置应用到默认items
  177. applyCusMainItemConfig: function(items, cfg) {
  178. let me = this,
  179. cusMainItems = cfg.main || [];
  180. Ext.Array.each(cusMainItems, function(cusItem) {
  181. let item = Ext.Array.findBy(items, function(item) {
  182. return item.name == cusItem.name;
  183. });
  184. if(!!item) {
  185. let keys = Ext.Object.getAllKeys(cusItem);
  186. keys.map(function(k) {
  187. if(me.MAIN_ALLOW_CUS_FIELDS.indexOf(k) != -1) {
  188. if(k != 'hidden' || item.allowBlank) {
  189. item['_init_' + k] = item.hasOwnProperty('_init_' + k) ? item['_init_' + k] : item[k];
  190. item[k] = cusItem[k];
  191. }
  192. }
  193. });
  194. }
  195. });
  196. Ext.Array.sort(items, function(a, b) {
  197. return a.index - b.index;
  198. });
  199. return items;
  200. },
  201. // 将自定义配置应用到从表items
  202. applyCusDetailItemConfig: function(items, cfg) {
  203. let me = this,
  204. detailCount = 1;
  205. Ext.Array.each(items, function(item) {
  206. let gname = item.gname;
  207. if(item.xtype == 'detailGridField' && cfg.hasOwnProperty(gname)) {
  208. let columns = item.columns,
  209. cusColumns = cfg[gname] || [];
  210. Ext.Array.each(cusColumns, function(cusCol) {
  211. let col = Ext.Array.findBy(columns, function(col) {
  212. return col.dataIndex == cusCol.dataIndex;
  213. });
  214. if(!!col) {
  215. let keys = Ext.Object.getAllKeys(cusCol);
  216. keys.map(function(k) {
  217. if(me.DETAIL_ALLOW_CUS_FIELDS.indexOf(k) != -1) {
  218. if(k != 'hidden' || col.allowBlank) {
  219. col['_init_' + k] = col.hasOwnProperty('_init_' + k) ? col['_init_' + k] : col[k];
  220. col[k] = cusCol[k];
  221. }
  222. }
  223. });
  224. }
  225. });
  226. Ext.Array.sort(columns, function(a, b) {
  227. return a.index - b.index;
  228. });
  229. }
  230. });
  231. return items;
  232. },
  233. /**
  234. * 处理formitems的一些默认配置
  235. */
  236. applyDefaultItems: function(form, items) {
  237. let me = this,
  238. formModel = form.getViewModel();
  239. Ext.Array.each(items, function(item) {
  240. // 设置必填
  241. if(item.allowBlank==false){
  242. // TODO 需要判断类型
  243. item.beforeLabelTextTpl = "<font color=\"red\" style=\"position:relative; top:2px;right:2px; font-weight: bolder;\">*</font>";
  244. }
  245. if(item.xtype == 'textfield') {
  246. Ext.applyIf(item, {
  247. maxLength: 50
  248. });
  249. }else if(item.xtype == 'datefield') {
  250. Ext.applyIf(item, {
  251. editable: false,
  252. format: 'Y-m-d'
  253. });
  254. }else 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. }else if(item.xtype == 'condbfindtrigger') {
  262. item.isConField = true;
  263. }else 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. });