Ext.define('saas.util.ViewUtil', { statics: { // 请求页面组件接口模板 getCfgUrl: '/api/ui/customize/getConfig?name={viewName}', // 保存自定义配置 saveCfgUrl: '/api/ui/customize/saveConfig', // 模板替换正则 urlRe: /(.*){viewName}(.*)/g, // 从表允许自定义的配置名 DETAIL_ALLOW_CUS_FIELDS: ['text', 'hidden', 'index', 'width'], getViewConfig: function (viewName) { let me = this, url = me.getCfgUrl.replace(me.urlRe, '$1' + viewName); return saas.util.BaseUtil.request({ url, async: true }) .then(function (res) { let cfg = me.decodeViewConfig(res.data); return cfg; }) .catch(function (e) { saas.util.BaseUtil.showErrorToast('读取界面配置失败:' + e.message); return null; }); }, decodeViewConfig: function (cfgs) { cfgs = cfgs || []; let cfg = {}; for (let i = 0; i < cfgs.length; i++) { let c = cfgs[i], pos = c.position; if (pos) { cfg[pos] = JSON.parse(c.content); } } return cfg; }, applyColumns: function (viewName, columns) { let me = this; return me.getViewConfig(viewName) .then(function (cfg) { let newColumns = []; newColumns = me.initColumnItems(columns); if (cfg) { return me.applyColumnConfig(newColumns, cfg['columns']); } else { return newColumns; } }); }, /** * 为列字段添加可能会被自定义的初始化配置 */ initColumnItems: function (columns) { columns = Ext.isArray(columns) ? columns : []; let colCount = 1, newColumns = []; Ext.Array.each(columns, function (c, j) { let col = Object.assign({}, c); if (col.hidden || col.width == 0 || !col.dataIndex) { Ext.applyIf(col, { index: -1, initHidden: true }); } else { Ext.applyIf(col, { text: '', hidden: false, index: colCount++, allowBlank: true, width: 100, initHidden: false }); } newColumns.push(col); }); Ext.Array.sort(newColumns, function (a, b) { return a.index - b.index; }); return newColumns; }, /** * 应用自定义配置到列字段 */ applyColumnConfig: function (columns, cusColumns) { let me = this; Ext.Array.each(cusColumns, function (cusCol) { let col = Ext.Array.findBy(columns, function (col) { return col.dataIndex == cusCol.dataIndex; }); if (!!col) { let keys = Ext.Object.getAllKeys(cusCol); keys.map(function (k) { if (me.DETAIL_ALLOW_CUS_FIELDS.indexOf(k) != -1) { if (k != 'hidden' || col.allowBlank) { col['_init_' + k] = col.hasOwnProperty('_init_' + k) ? col['_init_' + k] : col[k]; col[k] = cusCol[k]; } } }); } }); Ext.Array.sort(columns, function (a, b) { return a.index - b.index; }); return columns; }, saveViewConfig: function (name, position, content) { let me = this; return saas.util.BaseUtil.request({ url: me.saveCfgUrl, params: JSON.stringify({ name: name, position: position, content: JSON.stringify(content) }), method: 'POST', }); } } });