FormUtil.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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, form);
  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, form) {
  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. if(item.fieldLabel == '附件' && item.name.indexOf('_text')>-1){
  197. item.xtype = 'mfilefield'
  198. form.renderMF = item.name
  199. }
  200. });
  201. Ext.Array.sort(items, function(a, b) {
  202. return a.index - b.index;
  203. });
  204. return items;
  205. },
  206. // 将自定义配置应用到从表items
  207. applyCusDetailItemConfig: function(items, cfg) {
  208. let me = this,
  209. detailCount = 1;
  210. Ext.Array.each(items, function(item) {
  211. let gname = item.gname;
  212. if(item.xtype == 'detailGridField' && cfg.hasOwnProperty(gname)) {
  213. let columns = item.columns,
  214. cusColumns = cfg[gname] || [];
  215. Ext.Array.each(cusColumns, function(cusCol) {
  216. let col = Ext.Array.findBy(columns, function(col) {
  217. return col.dataIndex == cusCol.dataIndex;
  218. });
  219. if(!!col) {
  220. let keys = Ext.Object.getAllKeys(cusCol);
  221. keys.map(function(k) {
  222. if(me.DETAIL_ALLOW_CUS_FIELDS.indexOf(k) != -1) {
  223. if(k != 'hidden' || col.allowBlank) {
  224. col['_init_' + k] = col.hasOwnProperty('_init_' + k) ? col['_init_' + k] : col[k];
  225. col[k] = cusCol[k];
  226. }
  227. }
  228. });
  229. }
  230. });
  231. Ext.Array.sort(columns, function(a, b) {
  232. return a.index - b.index;
  233. });
  234. }
  235. });
  236. return items;
  237. },
  238. /**
  239. * 处理formitems的一些默认配置
  240. */
  241. applyDefaultItems: function(form, items) {
  242. let me = this,
  243. formModel = form.getViewModel();
  244. Ext.Array.each(items, function(item) {
  245. // 设置必填
  246. if(item.allowBlank==false){
  247. // TODO 需要判断类型
  248. item.beforeLabelTextTpl = "<font color=\"red\" style=\"position:relative; top:2px;right:2px; font-weight: bolder;\">*</font>";
  249. }
  250. if(item.xtype == 'textfield') {
  251. Ext.applyIf(item, {
  252. maxLength: 50
  253. });
  254. }else if(item.xtype == 'datefield') {
  255. Ext.applyIf(item, {
  256. editable: false,
  257. format: 'Y-m-d'
  258. });
  259. }else if(item.xtype == 'numberfield') {
  260. Ext.applyIf(item, {
  261. hideTrigger: true, // 隐藏trigger
  262. mouseWheelEnabled: false // 取消滚轮事件
  263. });
  264. // 设置默认值为0
  265. formModel.set(item.name, 0);
  266. }else if(item.xtype == 'condbfindtrigger') {
  267. item.isConField = true;
  268. }else if(item.xtype == 'detailGridField') {
  269. let index = form.detailCount;
  270. let columns = item.columns,
  271. cnames = columns.filter(function(c) {
  272. return c.dataIndex && !c.ignore;
  273. }).map(function(c) {
  274. return c.dataIndex
  275. }),
  276. defaultValueColumns = {};
  277. Ext.Array.each(columns, function(c) {
  278. if(c.dataIndex && c.defaultValue) {
  279. defaultValueColumns[c.dataIndex] = c.defaultValue;
  280. }
  281. // 不可锁定
  282. Ext.applyIf(c, {
  283. lockable: false,
  284. width: 120
  285. });
  286. //必填
  287. Ext.applyIf(c, {
  288. allowBlank: true
  289. });
  290. if(!c.allowBlank){
  291. c.cls = 'x-grid-necessary';
  292. }
  293. if(c.xtype == 'textfield') {
  294. Ext.applyIf(c, {
  295. maxLength: 50
  296. });
  297. }else if(c.xtype == 'datecolumn') {
  298. Ext.applyIf(c, {
  299. format: 'Y-m-d'
  300. });
  301. }else if(c.xtype == 'numbercolumn') {
  302. Ext.applyIf(c, {
  303. align: 'end'
  304. });
  305. }
  306. let editor = c.editor;
  307. if(editor) {
  308. Ext.applyIf(editor, {
  309. selectOnFocus: true
  310. });
  311. if(editor.xtype == 'numberfield') {
  312. Ext.applyIf(editor, {
  313. hideTrigger: true, // 隐藏trigger
  314. mouseWheelEnabled: false // 取消滚轮事件
  315. });
  316. }else if(editor.xtype == 'datefield') {
  317. Ext.apply(editor, {
  318. format: 'Y-m-d'
  319. });
  320. Ext.applyIf(editor, {
  321. editable: false
  322. });
  323. }
  324. }
  325. });
  326. cnames.push(item.detnoColumn);
  327. formModel.set('detail' + index + '.detailBindFields', cnames);
  328. item.bind = {
  329. store: '{detail' + index + '.detailStore}'
  330. };
  331. formModel.set('detail' + index + '.detailStore', Ext.create('Ext.data.Store', {
  332. model:item.storeModel,
  333. data: [],
  334. listeners: {
  335. datachanged: function(s, eOpts) {
  336. let g = form.query('detailGridField')[index];
  337. g.fireEvent('datachanged', g, s, eOpts);
  338. },
  339. // 为新增行设置默认值
  340. add: function(store, records, index, eOpts) {
  341. Ext.Array.each(records, function(r) {
  342. for(k in defaultValueColumns) {
  343. r.set(k, defaultValueColumns[k]);
  344. }
  345. r.commit();
  346. });
  347. }
  348. }
  349. }));
  350. form.detailCount++;
  351. }
  352. });
  353. return items;
  354. },
  355. loadData: function(form) {
  356. let me = this;
  357. form.setLoading(true);
  358. if(form.initId && form.initId!=0) {
  359. let url = form._readUrl + '/' + form.initId;
  360. saas.util.BaseUtil.request({url })
  361. .then(function(res) {
  362. form.setLoading(false);
  363. if(res.success) {
  364. let d = res.data;
  365. let o = {
  366. main: d.main
  367. };
  368. if(d.hasOwnProperty('items')) {
  369. o.detail0 = d.items;
  370. }else {
  371. let idx = 1;
  372. while(d.hasOwnProperty('items' + idx)) {
  373. o['detail' + (idx - 1)] = d['items' + idx];
  374. idx++;
  375. }
  376. }
  377. form.initFormData(o);
  378. form.fireEvent('load', form, o);
  379. //渲染附件
  380. if(form.renderMF&&form.down('[name='+form.renderMF+']')){
  381. let MF = form.down('[name='+form.renderMF+']')
  382. MF.value = o.main[form.renderMF]
  383. MF.renderMF(MF)
  384. }
  385. }
  386. })
  387. .catch(function(e) {
  388. form.setLoading(false);
  389. saas.util.BaseUtil.showErrorToast('读取单据数据错误: ' + e.message);
  390. });
  391. }else{
  392. //取后台编号
  393. saas.util.BaseUtil.request({
  394. url: '/api/commons/number/getMaxnumber',
  395. headers: {
  396. "Content-Type": 'application/x-www-form-urlencoded;charset=UTF-8'
  397. },
  398. params: {
  399. caller:form.caller
  400. },
  401. method: 'POST',
  402. }).then(function(res) {
  403. form.setLoading(false);
  404. if(res.success){
  405. let code = res.data;
  406. let viewModel = form.getViewModel();
  407. let detailGrids = form.query('detailGridField');
  408. if(code){
  409. let o = {};
  410. o[form._codeField] = code;
  411. let formData = {main: {}};
  412. Ext.apply(formData.main, o);
  413. Ext.Array.each(detailGrids, function(grid, index) {
  414. let detno = 0;
  415. let detnoColumn = grid.detnoColumn;
  416. let datas = [];
  417. let emptyRows = grid.emptyRows;
  418. Ext.Array.each(new Array(emptyRows), function() {
  419. detno += 1;
  420. let data = {};
  421. data[detnoColumn] = detno;
  422. datas.push(data);
  423. })
  424. formData['detail' + index] = datas;
  425. });
  426. return formData;
  427. }else {
  428. throw new Error('请求单据编号错误');
  429. }
  430. }else {
  431. return {
  432. main: {},
  433. }
  434. }
  435. }).then(function(formData) {
  436. let initData = form.initData;
  437. if(initData) {
  438. Ext.apply(initData.main, formData.main);
  439. form.setFormData(initData);
  440. form.fireEvent('load', form, initData);
  441. }else {
  442. form.initFormData(formData);
  443. form.fireEvent('load', form, formData);
  444. }
  445. }).catch(function(e) {
  446. form.clearDirty();
  447. form.setLoading(false);
  448. saas.util.BaseUtil.showErrorToast('请求单据编号错误: ' + e.message);
  449. })
  450. }
  451. }
  452. }
  453. });