Ext.QuickTips.init(); Ext.define('erp.controller.ma.logic.Config', { extend: 'Ext.app.Controller', stores: ['TreeStore'], views: ['common.main.TreePanel','ma.logic.Config', 'core.form.ColorField', 'common.main.Toolbar','core.trigger.SearchField', 'core.trigger.DbfindTrigger'], refs: [{ ref: 'tree', selector: '#tree-panel' },{ ref: 'configPanel', selector: '#configPanel' },{ ref: 'tabPanel', selector: '#tabpanel' }], init: function(){ var me = this; me.FormUtil = Ext.create('erp.util.FormUtil'); me.Toast = Ext.create('erp.view.core.window.Toast'); this.control({ 'erpTreePanel': { itemmousedown: function(selModel, record){ Ext.defer(function(){ me.onNodeClick(selModel, record); }, 20); }, beforerender: function(tree) { if(window.whoami) tree.hide(); } }, '#configPanel': { boxready: function() { var caller = window.whoami || 'sys'; me.getSetting(caller); } }, 'button[id=btn-close]': { click: function(){ var p = parent.Ext.getCmp('content-panel'); if(p){ p.getActiveTab().close(); } else { window.close(); } } }, 'button[id=btn-save]': { click: function(btn){ me.onSaveClick(); } }, 'dbfindtrigger': { aftertrigger: function(field, record, dbfinds) { Ext.Array.each(dbfinds, function(d){ if(d.field == field.name) { field.setValue(record.get(d.dbGridField)); } }); } }, 'button[cls=x-dd-drop-ok-add]': { click: function(btn) { var f = btn.ownerCt, c = btn.config; f.insert(f.items.length - 1, { xtype: (c.dbfind ? 'dbfindtrigger' : 'textfield'), name: c.dbfind || c.code, readOnly: !c.dbfind && c.editable == 0, editable: c.editable == 1, clearable: true }); } } }); }, onNodeClick: function(selModel, record){ var me = this; if (record.get('leaf')) { me.getSetting(record.raw.caller, record.get('text')); } else { if(record.isExpanded() && record.childNodes.length > 0){ record.collapse(true, true);// 已展开则收拢 } else { //未展开看是否加载了children if(record.childNodes.length == 0){ me.getChildren(record); } else { record.expand(false, true);//展开 } } } }, /** * 从后台加载树节点 */ getChildren: function(record) { var tree = this.getTree(); tree.setLoading(true, tree.body); Ext.Ajax.request({//拿到tree数据 url : basePath + 'common/lazyTree.action', params: { parentId: record.get('id') }, callback : function(opt, s, r){ tree.setLoading(false); var res = new Ext.decode(r.responseText); if(res.tree && record.childNodes.length == 0){ record.appendChild(res.tree); record.expand(false, true);//展开 } else if(res.exceptionInfo){ showError(res.exceptionInfo); } } }); }, getSetting: function(caller, title){ var me = this; if(caller) { if(caller != me.currCaller) { me.loadConfigs(caller, function(configs){ me.currCaller = caller; me.setConfigs(configs); title && me.getConfigPanel().setTitle(title); }); me.loadInterceptors(caller, function(interceptors){ me.setInterceptors(interceptors); var tab = me.getTabPanel(); if(interceptors.length == 0 && !tab.collapsed) tab.collapse(); else if(interceptors.length > 0 && tab.collapsed) tab.expand(); }); } } else { me.currCaller = null; } }, /** * 配置参数 */ loadConfigs: function(caller, callback) { Ext.Ajax.request({ url: basePath + 'ma/setting/configs.action?caller=' + caller, method: 'GET', callback: function(opt, s, r) { if(r && r.status == 200) { var res = Ext.JSON.decode(r.responseText); callback.call(null, res); } } }); }, /** * 配置逻辑 */ loadInterceptors: function(caller, callback) { Ext.Ajax.request({ url: basePath + 'ma/setting/interceptors.action?caller=' + caller, method: 'GET', callback: function(opt, s, r) { if(r && r.status == 200) { var res = Ext.JSON.decode(r.responseText); callback.call(null, res); } } }); }, setConfigs: function(configs) { var me = this, pane = me.getConfigPanel(), items = []; Ext.Array.each(configs, function(c, i){ switch(c.data_type) { case 'YN': items.push({ xtype: 'checkbox', boxLabel: c.title, name: c.code, id: c.id, checked: c.data == 1, columnWidth: 1, margin: c.help ? '4 8 0 8' : '4 8 4 8' }); break; case 'RADIO': var s = []; Ext.Array.each(c.properties, function(p){ s.push({ name: c.code, boxLabel: p.display, inputValue: p.value, checked: p.value == c.data }); }); items.push({ xtype: 'radiogroup', id: c.id, fieldLabel: c.title, columnWidth: 1, columns: 2, vertical: true, items: s }); break; case 'COLOR': items.push({ xtype: 'colorfield', fieldLabel: c.title, id: c.id, name: c.code, value: c.data, readOnly: c.editable == 0, editable: c.editable == 1, labelWidth: 150 }); break; case 'NUMBER': items.push({ xtype: 'numberfield', fieldLabel: c.title, id: c.id, name: c.code, value: c.data, readOnly: c.editable == 0, labelWidth: 150 }); break; default : if(c.multi == 1) { var data = c.data ? c.data.split('\n') : [null], s = []; Ext.Array.each(data, function(d){ s.push({ xtype: (c.dbfind ? 'dbfindtrigger' : 'textfield'), name: c.dbfind || c.code, value: d, readOnly: !c.dbfind && c.editable == 0, editable: c.editable == 1, clearable: true }); }); s.push({ xtype: 'button', text: '添加', width: 22, cls: 'x-dd-drop-ok-add', iconCls: 'x-dd-drop-icon', iconAlign: 'right', config: c }); items.push({ xtype: 'fieldset', title: c.title, id: c.id, name: c.code, columnWidth: 1, layout: 'column', defaults: { columnWidth: .25, margin: '4 8 4 8' }, items: s }); } else { items.push({ xtype: (c.dbfind ? 'dbfindtrigger' : 'textfield'), fieldLabel: c.title, id: c.id, name: c.dbfind || c.code, value: c.data, readOnly: !c.dbfind && c.editable == 0, editable: c.editable == 1, clearable: true, columnWidth: .5, labelWidth: 150 }); } break; } if(c.help) { items.push({ xtype: 'displayfield', value: c.help, columnWidth: ['NUMBER', 'VARCHAR2'].indexOf(c.data_type) > -1 ? .5 : 1, cls: 'help-block', margin: '4 8 8 8' }); } else { if(['NUMBER', 'VARCHAR2'].indexOf(c.data_type) > -1) { items.push({ xtype: 'displayfield' }); } } }); pane.removeAll(); if(items.length == 0) items.push({ html: '没有参数配置', cls: 'x-form-empty' }); pane.add(items); }, /** * 字符长度 */ getCharLength: function(str) { if(str) { for (var len = str.length, c = 0, i = 0; i < len; i++) str.charCodeAt(i) < 27 || str.charCodeAt(i) > 126 ? c += 2 : c++; return c; } return 0; }, setInterceptors: function(interceptors) { var me = this, pane = me.getTabPanel(), panels = []; var types = Ext.Array.unique(Ext.Array.pluck(interceptors, 'type')); types = Ext.Array.sort(types, function(a, b){ return me.getMethodTypes(a).weight > me.getMethodTypes(b).weight; }); Ext.Array.each(types, function(type){ var data = Ext.Array.filter(interceptors, function(i){ return i.type == type; }); Ext.Array.each(data, function(d){ d.enable = d.enable == 1; }); panels.push({ title: me.getMethodTypes(type).text, xtype: 'grid', columns: [{ text: '顺序', dataIndex: 'detno', xtype: 'numbercolumn', align: 'center', format: '0', width: 40 },{ text: '描述', dataIndex: 'title', flex: 10 },{ text: '启用', xtype: 'checkcolumn', dataIndex: 'enable', width: 60 }], columnLines: true, store: new Ext.data.Store({ fields: ['id', 'title', 'type', 'turn', 'detno', 'enable', 'class_', 'method'], data: data, groupField: 'turn', sorters: [{ property: 'detno' }] }), features : [{ ftype: 'grouping', groupHeaderTpl: ' (共 {rows.length} 项)', startCollapsed: false }], viewConfig: { listeners: { render: function(view) { if (!view.tip) { view.tip = Ext.create('Ext.tip.ToolTip', { target : view.el, delegate : view.itemSelector, trackMouse : true, renderTo : Ext.getBody(), tpl: new Ext.XTemplate('
' + '
类:
{class_}
' + '
方法:
{method}
' + '
'), listeners: { beforeshow: function (tip) { var record = view.getRecord(tip.triggerElement); if(record){ tip.update(record.data); } else { tip.on('show', function(){ Ext.defer(tip.hide, 10, tip); }, tip, {single: true}); } } } }); } } } } }); }); pane.removeAll(); pane.add(panels); }, getMethodTypes: function(type) { var types = { 'save': { text: '保存', weight: 1 }, 'update': { text: '更新', weight: 2 }, 'commit': { text: '提交', weight: 3 }, 'resCommit': { text: '反提交', weight: 4 }, 'audit': { text: '审核', weight: 5 }, 'resAudit': { text: '反审核', weight: 6 }, 'post': { text: '过账', weight: 7 }, 'resPost': { text: '反过账', weight: 8 }, 'print': { text: '打印', weight: 9 }, 'turnout': { text: '转出货', weight: 10 }, 'turn': { text: '转单', weight: 11 }, 'delete': { text: '删除', weight: 12 }, 'deletedetail': { text: '删除明细', weight: 13 }, 'finish': { text: '结案', weight: 14 } }; return types[type] || {text: type, weight: 99}; }, onSaveClick: function() { var me = this, pane = me.getConfigPanel(), tab = me.getTabPanel(), updatedConfigs = [], updatedInters = []; Ext.Array.each(pane.items.items, function(field){ if(field.xtype == 'fieldset') { var vals = []; Ext.Array.each(field.items.items, function(i){ if(i.name && typeof i.getValue === 'function' && !Ext.isEmpty(i.getValue())) { vals.push(i.getValue()); } }); updatedConfigs.push({ id: field.id, data: vals.length > 0 ? vals.join('\n') : null }); } else if(typeof field.isDirty === 'function' && field.isDirty()) { var value = field.getValue(); updatedConfigs.push({ id: field.id, data: typeof value === 'boolean' ? (value ? 1 : 0) : (field.xtype == 'radiogroup' ? Ext.Object.getValues(value)[0] : value) }); } }); Ext.Array.each(tab.items.items, function(grid){ var modified = grid.store.getModifiedRecords(); Ext.Array.each(modified, function(m){ updatedInters.push({ id: m.get('id'), enable: m.get('enable') ? 1 : 0, detno: m.get('detno'), turn: m.get('turn') }); }); }); if(updatedConfigs.length > 0) { me.saveConfigs(updatedConfigs, function(){ me.loadConfigs(me.currCaller, function(configs){ me.setConfigs(configs); }); }); } if(updatedInters.length > 0) { me.saveInterceptors(updatedInters, function(){ me.loadInterceptors(me.currCaller, function(interceptors){ me.setInterceptors(interceptors); }); }); } }, /** * 修改参数配置 */ saveConfigs: function(updated, callback) { var me = this; Ext.Ajax.request({ url: basePath + 'ma/setting/configs.action', params: { updated: Ext.JSON.encode(updated) }, method: 'POST', callback: function(opt, s, r) { if(r && r.status == 200) { me.Toast.info('提示', '参数修改成功'); callback.call(); } } }); }, /** * 修改逻辑配置 */ saveInterceptors: function(updated, callback) { var me = this; Ext.Ajax.request({ url: basePath + 'ma/setting/interceptors.action', params: { updated: Ext.JSON.encode(updated) }, method: 'POST', callback: function(opt, s, r) { if(r && r.status == 200) { me.Toast.info('提示', '逻辑修改成功'); callback.call(); } } }); } });