FormUtil.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. }else if(item.xtype == 'datefield') {
  248. Ext.applyIf(item, {
  249. editable: false,
  250. format: 'Y-m-d'
  251. });
  252. }else if(item.xtype == 'numberfield') {
  253. Ext.applyIf(item, {
  254. hideTrigger: true, // 隐藏trigger
  255. mouseWheelEnabled: false // 取消滚轮事件
  256. });
  257. // 设置默认值为0
  258. formModel.set(item.name, 0);
  259. }else if(item.xtype == 'condbfindtrigger') {
  260. item.isConField = true;
  261. }else if(item.xtype == 'detailGridField') {
  262. let index = form.detailCount;
  263. let columns = item.columns,
  264. cnames = columns.filter(function(c) {
  265. return c.dataIndex && !c.ignore;
  266. }).map(function(c) {
  267. return c.dataIndex
  268. }),
  269. defaultValueColumns = {};
  270. Ext.Array.each(columns, function(c) {
  271. if(c.dataIndex && c.defaultValue) {
  272. defaultValueColumns[c.dataIndex] = c.defaultValue;
  273. }
  274. // 不可锁定
  275. Ext.applyIf(c, {
  276. lockable: false,
  277. width: 120
  278. });
  279. //必填
  280. Ext.applyIf(c, {
  281. allowBlank: true
  282. });
  283. if(!c.allowBlank){
  284. c.cls = 'x-grid-necessary';
  285. }
  286. if(c.xtype == 'textfield') {
  287. Ext.applyIf(c, {
  288. maxLength: 50
  289. });
  290. }else if(c.xtype == 'datecolumn') {
  291. Ext.applyIf(c, {
  292. format: 'Y-m-d'
  293. });
  294. }else if(c.xtype == 'numbercolumn') {
  295. Ext.applyIf(c, {
  296. align: 'end'
  297. });
  298. }
  299. let editor = c.editor;
  300. if(editor) {
  301. Ext.applyIf(editor, {
  302. selectOnFocus: true
  303. });
  304. if(editor.xtype == 'numberfield') {
  305. Ext.applyIf(editor, {
  306. hideTrigger: true, // 隐藏trigger
  307. mouseWheelEnabled: false // 取消滚轮事件
  308. });
  309. }else if(editor.xtype == 'datefield') {
  310. Ext.apply(editor, {
  311. format: 'Y-m-d'
  312. });
  313. Ext.applyIf(editor, {
  314. editable: false
  315. });
  316. }
  317. }
  318. });
  319. cnames.push(item.detnoColumn);
  320. formModel.set('detail' + index + '.detailBindFields', cnames);
  321. item.bind = {
  322. store: '{detail' + index + '.detailStore}'
  323. };
  324. formModel.set('detail' + index + '.detailStore', Ext.create('Ext.data.Store', {
  325. model:item.storeModel,
  326. data: [],
  327. listeners: {
  328. datachanged: function(s, eOpts) {
  329. let g = form.query('detailGridField')[index];
  330. g.fireEvent('datachanged', g, s, eOpts);
  331. },
  332. // 为新增行设置默认值
  333. add: function(store, records, index, eOpts) {
  334. Ext.Array.each(records, function(r) {
  335. for(k in defaultValueColumns) {
  336. r.set(k, defaultValueColumns[k]);
  337. }
  338. r.commit();
  339. });
  340. }
  341. }
  342. }));
  343. form.detailCount++;
  344. }
  345. });
  346. return items;
  347. },
  348. loadData: function(form) {
  349. let me = this;
  350. form.setLoading(true);
  351. if(form.initId && form.initId!=0) {
  352. let url = form._readUrl + '/' + form.initId;
  353. saas.util.BaseUtil.request({url })
  354. .then(function(res) {
  355. form.setLoading(false);
  356. if(res.success) {
  357. let d = res.data;
  358. let o = {
  359. main: d.main
  360. };
  361. if(d.hasOwnProperty('items')) {
  362. o.detail0 = d.items;
  363. }else {
  364. let idx = 1;
  365. while(d.hasOwnProperty('items' + idx)) {
  366. o['detail' + (idx - 1)] = d['items' + idx];
  367. idx++;
  368. }
  369. }
  370. form.initFormData(o);
  371. form.fireEvent('load', form, o);
  372. }
  373. })
  374. .catch(function(e) {
  375. form.setLoading(false);
  376. saas.util.BaseUtil.showErrorToast('读取单据数据错误: ' + e.message);
  377. });
  378. }else{
  379. //取后台编号
  380. saas.util.BaseUtil.request({
  381. url: '/api/commons/number/getMaxnumber',
  382. headers: {
  383. "Content-Type": 'application/x-www-form-urlencoded;charset=UTF-8'
  384. },
  385. params: {
  386. caller:form.caller
  387. },
  388. method: 'POST',
  389. }).then(function(res) {
  390. form.setLoading(false);
  391. if(res.success){
  392. let code = res.data;
  393. let viewModel = form.getViewModel();
  394. let detailGrids = form.query('detailGridField');
  395. if(code){
  396. let o = {};
  397. o[form._codeField] = code;
  398. let formData = {main: {}};
  399. Ext.apply(formData.main, o);
  400. Ext.Array.each(detailGrids, function(grid, index) {
  401. let detno = 0;
  402. let detnoColumn = grid.detnoColumn;
  403. let datas = [];
  404. let emptyRows = grid.emptyRows;
  405. Ext.Array.each(new Array(emptyRows), function() {
  406. detno += 1;
  407. let data = {};
  408. data[detnoColumn] = detno;
  409. datas.push(data);
  410. })
  411. formData['detail' + index] = datas;
  412. });
  413. return formData;
  414. }else {
  415. throw new Error('请求单据编号错误');
  416. }
  417. }else {
  418. return {
  419. main: {},
  420. }
  421. }
  422. }).then(function(formData) {
  423. let initData = form.initData;
  424. if(initData) {
  425. Ext.apply(initData.main, formData.main);
  426. form.setFormData(initData);
  427. form.fireEvent('load', form, initData);
  428. }else {
  429. form.initFormData(formData);
  430. form.fireEvent('load', form, formData);
  431. }
  432. }).catch(function(e) {
  433. form.clearDirty();
  434. form.setLoading(false);
  435. saas.util.BaseUtil.showErrorToast('请求单据编号错误: ' + e.message);
  436. })
  437. }
  438. }
  439. }
  440. });