FormUtil.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. Ext.define('saas.util.FormUtil', {
  2. statics: {
  3. // 请求页面组件接口模板
  4. baseUrl: '/api/ui/co_view/config?name={viewName}',
  5. // 模板替换正则
  6. urlRe: /(.*){viewName}(.*)/g,
  7. setItems: function(form) {
  8. let me = this,
  9. defaultItems;
  10. defaultItems = form.defaultItems;
  11. me.clearItems(defaultItems);
  12. me.applyCusItemConfig(form)
  13. .then(function(res) {
  14. if(res.success) {
  15. let config = res.data || true, items = defaultItems || [];
  16. if(config) {
  17. let cusItems = config.items || [];
  18. Ext.Array.each(cusItems, function(cusItem) {
  19. let item = items.find(function(item) {
  20. return item.name == cusItem.name;
  21. });
  22. Ext.apply(item, cusItem);
  23. });
  24. items = me.applyDefaultItems(form, items);
  25. items = me.applyItemsGroup(items);
  26. }
  27. form.removeAll();
  28. return form.addItems(items);
  29. }else {
  30. return []
  31. }
  32. })
  33. .then(function(items) {
  34. form.fireEvent('afterSetItems', form, items);
  35. })
  36. .then(function() {
  37. me.loadData(form);
  38. })
  39. .catch(function(response) {
  40. saas.util.BaseUtil.showErrorToast('加载数据错误:' + response.message);
  41. console.error(response);
  42. });
  43. },
  44. clearItems: function(items) {
  45. for(let i = items.length - 1; i >= 0; i--) {
  46. let item = items[i];
  47. if(item.group == '_nogroup') {
  48. item.group = undefined;
  49. }
  50. if(item.xtype == 'container') {
  51. items.splice(i, 1);
  52. }
  53. }
  54. },
  55. /**
  56. * 获得form的客户自定义字段配置
  57. * @param form: form组件
  58. * @param url: url
  59. */
  60. applyCusItemConfig: function(form) {
  61. let me = this,
  62. viewName = form.viewName,
  63. defaultItems = form.defaultItems,
  64. url = me.baseUrl.replace(me.urlRe, '$1' + viewName);
  65. return saas.util.BaseUtil.request({url, async: false});
  66. },
  67. /**
  68. * 处理formitems的一些默认配置
  69. */
  70. applyDefaultItems: function(form, items) {
  71. let me = this,
  72. formModel = form.getViewModel();
  73. Ext.Array.each(items, function(item) {
  74. // 设置必填
  75. if(item.allowBlank==false){
  76. // TODO 需要判断类型
  77. item.beforeLabelTextTpl = "<font color=\"red\" style=\"position:relative; top:2px;right:2px; font-weight: bolder;\">*</font>";
  78. }
  79. if(item.xtype == 'textfield') {
  80. Ext.applyIf(item, {
  81. maxLength: 50
  82. });
  83. }
  84. if(item.xtype == 'datefield') {
  85. Ext.applyIf(item, {
  86. editable: false,
  87. format: 'Y-m-d'
  88. });
  89. }
  90. if(item.xtype == 'numberfield') {
  91. Ext.applyIf(item, {
  92. hideTrigger: true, // 隐藏trigger
  93. mouseWheelEnabled: false // 取消滚轮事件
  94. });
  95. // 设置默认值为0
  96. formModel.set(item.name, 0);
  97. }
  98. // 如果是从表为其绑定store
  99. if(item.xtype == 'detailGridField') {
  100. let index = form.detailCount;
  101. let columns = item.columns,
  102. cnames = columns.filter(function(c) {
  103. return c.dataIndex && !c.ignore;
  104. }).map(function(c) {
  105. return c.dataIndex
  106. }),
  107. defaultValueColumns = {};
  108. Ext.Array.each(columns, function(c) {
  109. if(c.dataIndex && c.defaultValue) {
  110. defaultValueColumns[c.dataIndex] = c.defaultValue;
  111. }
  112. // 不可锁定
  113. Ext.applyIf(c, {
  114. lockable: false,
  115. width: 120
  116. });
  117. //必填
  118. Ext.applyIf(c, {
  119. allowBlank: true
  120. });
  121. if(!c.allowBlank){
  122. c.cls = 'x-grid-necessary';
  123. }
  124. if(c.xtype == 'textfield') {
  125. Ext.applyIf(c, {
  126. maxLength: 50
  127. });
  128. }else if(c.xtype == 'datecolumn') {
  129. Ext.applyIf(c, {
  130. format: 'Y-m-d'
  131. });
  132. }else if(c.xtype == 'numbercolumn') {
  133. Ext.applyIf(c, {
  134. align: 'end'
  135. });
  136. }
  137. let editor = c.editor;
  138. if(editor) {
  139. Ext.applyIf(editor, {
  140. selectOnFocus: true
  141. });
  142. if(editor.xtype == 'numberfield') {
  143. Ext.applyIf(editor, {
  144. hideTrigger: true, // 隐藏trigger
  145. mouseWheelEnabled: false // 取消滚轮事件
  146. });
  147. }else if(editor.xtype == 'datefield') {
  148. Ext.apply(editor, {
  149. format: 'Y-m-d'
  150. });
  151. Ext.applyIf(editor, {
  152. editable: false
  153. });
  154. }
  155. }
  156. });
  157. if(columns[columns.length - 1].flex != 1) {
  158. columns.push({
  159. flex: 1,
  160. allowBlank: true
  161. });
  162. }
  163. cnames.push(item.detnoColumn);
  164. formModel.set('detail' + index + '.detailBindFields', cnames);
  165. item.bind = {
  166. store: '{detail' + index + '.detailStore}'
  167. };
  168. formModel.set('detail' + index + '.detailStore', Ext.create('Ext.data.Store', {
  169. model:item.storeModel,
  170. data: [],
  171. listeners: {
  172. datachanged: function(s, eOpts) {
  173. let g = form.query('detailGridField')[index];
  174. g.fireEvent('datachanged', g, s, eOpts);
  175. },
  176. // 为新增行设置默认值
  177. add: function(store, records, index, eOpts) {
  178. Ext.Array.each(records, function(r) {
  179. for(k in defaultValueColumns) {
  180. r.set(k, defaultValueColumns[k]);
  181. }
  182. r.commit();
  183. });
  184. }
  185. }
  186. }));
  187. form.detailCount++;
  188. }
  189. });
  190. return items;
  191. },
  192. applyItemsGroup: function(items) {
  193. let groups = [];
  194. Ext.Array.each(items, function(item, index) {
  195. let groupName = item.group;
  196. if(!!groupName) {
  197. let idx = groups.findIndex(function(g) {
  198. return g.title == groupName;
  199. }),group;
  200. if(idx == -1) {
  201. group = {
  202. title: groupName,
  203. count: 1
  204. };
  205. groups.push(group);
  206. }else {
  207. group = groups[idx];
  208. group.count++;
  209. }
  210. }
  211. });
  212. Ext.Array.sort(items, function(a, b) {
  213. let gs = groups.concat([{
  214. title: '_nogroup'
  215. }]);
  216. a.group = a.group || '_nogroup';
  217. let v1 = gs.findIndex(function(g) {
  218. return g.title == a.group;
  219. })
  220. let v2 = gs.findIndex(function(g) {
  221. return g.title == b.group;
  222. })
  223. return v1 - v2;
  224. });
  225. Ext.Array.each(groups, function(g) {
  226. let idx = items.findIndex(function(i) {
  227. return i.group == g.title;
  228. });
  229. g.index = idx;
  230. });
  231. Ext.Array.each(groups, function(group, index) {
  232. let formIndex = group.index;
  233. delete group.index;
  234. Ext.Array.insert(items, formIndex + index, [{
  235. xtype: 'container',
  236. userCls: 'x-field-separator',
  237. height: 24,
  238. html: group.title,
  239. columnWidth: 1,
  240. ignore: true,
  241. isValid: function() {
  242. return true;
  243. },
  244. isDirty: function() {
  245. return false;
  246. }
  247. }]);
  248. });
  249. return items;
  250. },
  251. loadData: function(form) {
  252. let me = this;
  253. form.setLoading(true);
  254. if(form.initId && form.initId!=0) {
  255. let url = form._readUrl + '/' + form.initId;
  256. saas.util.BaseUtil.request({url })
  257. .then(function(res) {
  258. form.setLoading(false);
  259. if(res.success) {
  260. let d = res.data;
  261. let o = {
  262. main: d.main
  263. };
  264. if(d.hasOwnProperty('items')) {
  265. o.detail0 = d.items;
  266. }else {
  267. let idx = 1;
  268. while(d.hasOwnProperty('items' + idx)) {
  269. o['detail' + (idx - 1)] = d['items' + idx];
  270. idx++;
  271. }
  272. }
  273. form.initFormData(o);
  274. form.fireEvent('load', form, o);
  275. }
  276. })
  277. .catch(function(response) {
  278. form.setLoading(false);
  279. console.error(response);
  280. saas.util.BaseUtil.showErrorToast(response.message);
  281. });
  282. }else{
  283. //取后台编号
  284. saas.util.BaseUtil.request({
  285. url: '/api/commons/number/getMaxnumber',
  286. headers: {
  287. "Content-Type": 'application/x-www-form-urlencoded;charset=UTF-8'
  288. },
  289. params: {
  290. caller:form.caller
  291. },
  292. method: 'POST',
  293. }).then(function(res) {
  294. form.setLoading(false);
  295. if(res.success){
  296. let code = res.data;
  297. let viewModel = form.getViewModel();
  298. let detailGrids = form.query('detailGridField');
  299. if(code){
  300. let o = {};
  301. o[form._codeField] = code;
  302. let formData = {main: {}};
  303. Ext.apply(formData.main, o);
  304. Ext.Array.each(detailGrids, function(grid, index) {
  305. let detno = 0;
  306. let detnoColumn = grid.detnoColumn;
  307. let datas = [];
  308. let emptyRows = grid.emptyRows;
  309. Ext.Array.each(new Array(emptyRows), function() {
  310. detno += 1;
  311. let data = {};
  312. data[detnoColumn] = detno;
  313. datas.push(data);
  314. })
  315. formData['detail' + index] = datas;
  316. });
  317. return formData;
  318. }else {
  319. throw new Error('请求单据编号错误');
  320. }
  321. }else {
  322. return {
  323. main: {},
  324. }
  325. }
  326. }).then(function(formData) {
  327. let initData = form.initData;
  328. if(initData) {
  329. Ext.apply(initData.main, formData.main);
  330. form.setFormData(initData);
  331. form.fireEvent('load', form, initData);
  332. }else {
  333. form.initFormData(formData);
  334. form.fireEvent('load', form, formData);
  335. }
  336. }).catch(function(res) {
  337. form.clearDirty();
  338. saas.util.BaseUtil.showErrorToast(res.message);
  339. form.setLoading(false);
  340. })
  341. }
  342. }
  343. }
  344. });