FormUtil.js 14 KB

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