FormUtil.js 19 KB

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