Ext.namespace( 'instore.extlib.ui.grid' ); instore.extlib.ui.grid.EditorGridPanel = function(config, DWRgetMethod, DWReditMethod, params) { this.DWReditMethod = DWReditMethod || null; instore.extlib.ui.grid.EditorGridPanel.superclass.constructor.call(this, config, DWRgetMethod, params); }; Ext.extend(instore.extlib.ui.grid.EditorGridPanel, instore.extlib.ui.grid.GridPanel, { dirty: false, validateRange: "modified", // "all" or "modified" allowAdd: true, allowRemove: true, showSave: true, showCancel: true, load: function(params, title, getMethod, editMethod, isNew) { if(editMethod != null) this.DWReditMethod = editMethod; instore.extlib.ui.grid.EditorGridPanel.superclass.load.call(this, params, title, getMethod, isNew); }, loadNew: function(params, title, getMethod, editMethod) { this.load(params, title, getMethod, editMethod, true); }, getRenderer: function(field, store) { if(field.renderType == "combo" && field.editorConfig && field.editorConfig.store) { if(typeof(field.lookupRenderInfo) == "undefined") { var lookupRenderInfo = {}; var optionsFromMessages = instore.extlib.ui.Messages.combo[field.editorConfig.comboName]; Ext.each(field.editorConfig.store, function(option) { var valueFromMessages = (optionsFromMessages) ? optionsFromMessages[option[0]] : null; lookupRenderInfo[option[0]] = (valueFromMessages) ? valueFromMessages : option[1]; }); store.fields.get(field.name).lookupRenderInfo = lookupRenderInfo; } return function(val, metaData, record) { return (val != null) ? record.store.fields.get(this.name).lookupRenderInfo[val] : null; } } return instore.extlib.ui.grid.EditorGridPanel.superclass.getRenderer.call(this, field); }, loadData: function(store, meta) { if(this.DWReditMethod) { Ext.each(meta.fields, function(field) { if(field.editorConfig) field.editor = new instore.extlib.ui.grid.GridEditor({grid: this}, field); }.createDelegate(this)); } instore.extlib.ui.grid.EditorGridPanel.superclass.loadData.call(this, store, meta); }, setDirty: function(dirty) { var saveBtn = this.getTopToolbar().items.map.saveBtn; var cancelBtn = this.getTopToolbar().items.map.cancelBtn; if(dirty) { if(saveBtn) saveBtn.setDisabled(this.store.modified.length == 0 && this.store.removed.length == 0); if(cancelBtn) cancelBtn.enable(); } else { if(saveBtn) saveBtn.disable(); if(cancelBtn) cancelBtn.disable(); } this.fireEvent("dirtychange", dirty); this.dirty = dirty; }, add: function() { var cm = this.getColumnModel(); var editableColumns = cm.getColumnsBy(function(cfg) { return cfg.editor != null}); if(editableColumns != null && editableColumns.length > 0) { var newRowData = {}; Ext.each(this.record.prototype.fields.keys, function(field) { newRowData[field] = "" }); this.store.insert(0, [new this.record(newRowData)]); var idx = cm.getIndexById(editableColumns[0].id); this.startEditing(0, idx); } }, remove: function() { Ext.each(this.getSelectionModel().getSelections(), function(row) { this.store.remove(row) }, this); this.getTopToolbar().items.map.removeBtn.setDisabled(true); }, validateRecords: function(store, records) { var grid = this; var cm = grid.getColumnModel(); var valid = true; Ext.each(this.store.fields.keys, function(key) { var colIdx = cm.findColumnIndex(key); if(colIdx != -1 && cm.isCellEditable(colIdx)) { var fieldEditor = cm.getCellEditor(colIdx); var field = fieldEditor.field; if(field.setViewEl) { Ext.each(records, function(record) { var rowIdx = store.indexOfId(record.id); if(rowIdx != -1) { field.setViewEl(new Ext.Element(grid.view.getCell(rowIdx, colIdx))); fieldEditor.setValue(record.get(key)); if(!field.isValid()) { valid = false; return; } } }) } } }) return valid; }, onBeforeSave: function(store) { var isValid = true; if(this.validateRange) { switch(this.validateRange) { case "all": isValid = this.validateRecords(store, store.getRange()); break; case "modified": isValid = this.validateRecords(store, store.modified); break; } } if(!isValid) Ext.Msg.show({title: instore.extlib.ui.Messages.errorTypes.WARNING, msg: instore.extlib.ui.Messages.warnings.invalidFields, buttons: Ext.Msg.OK, icon: Ext.MessageBox.WARNING}); return isValid; }, onBeforeRemove: function() { Ext.Msg.confirm( instore.extlib.ui.Messages.warnings.askDeleteTitle, instore.extlib.ui.Messages.warnings.askDeleteMessage, function(btn) { if(btn == 'yes') this.fireEvent("removeconfirm", true); else this.fireEvent("removeconfirm", false); }.createDelegate(this) ); }, save: function() { if(this.fireEvent("beforesave", this.store) !== false) { var modifiedData = { modified: [], removed: [] }; Ext.each(this.store.modified, function(record) { modifiedData.modified.push(record.data) }); Ext.each(this.store.removed, function(record) { modifiedData.removed.push(record.data) }); if(modifiedData.modified.length > 0 || modifiedData.removed.length > 0) { var DWReditMethod = eval(this.DWReditMethod); DWReditMethod(modifiedData, function(response) { if(response) { this.store.reload(); this.store.modified = []; this.store.removed = []; this.store.added = []; this.setDirty(false); this.fireEvent("modified"); } }.createDelegate(this)) } } }, cancel: function() { if(this.activeEditor) this.activeEditor.field.hide(); this.stopEditing(); this.store.removed = []; this.store.added = []; this.store.removeAll(); this.store.reload(); }, initTbar: function() { this.tbar = []; if(this.allowAdd) { this.tbar.push({ id: "addBtn", text: instore.extlib.ui.Messages.buttons.add, handler: this.add, scope: this }) } if(this.allowRemove) { this.tbar.push({ id: "removeBtn", text: instore.extlib.ui.Messages.buttons.remove, handler: function() { this.fireEvent("beforeremove"); }, disabled: true, scope: this }) } if(this.tbar.length > 0) this.tbar.push({xtype: 'tbseparator'}); if(this.showSave) { this.tbar.push({ id: "saveBtn", disabled: true, text: instore.extlib.ui.Messages.buttons.save, handler: this.save, scope: this }); } if(this.showCancel) { this.tbar.push({ id: "cancelBtn", disabled: true, text: instore.extlib.ui.Messages.buttons.cancel, handler: this.cancel, scope:this }); } }, reconfigure: function(store, colModel) { if(this.allowRemove) { var removeBtn = this.getTopToolbar().items.map.removeBtn; if(removeBtn) removeBtn.disable(); } instore.extlib.ui.grid.EditorGridPanel.superclass.reconfigure.call(this, store, colModel); }, onAdd: function(store, records, idx) { Ext.each(records, function(record) { store.added.push(record); }.createDelegate(this)) this.setDirty(true) }, onRemove: function(store, record, idx) { this.store.removed.push(record); this.setDirty(true); }, onUpdate: function(store, record, operation) { if(operation == Ext.data.Record.EDIT) this.setDirty(true); }, initComponent : function() { this.addEvents("dirtychange", "modified", "beforesave", "beforeremove", "removeconfirm"); this.initTbar(); instore.extlib.ui.grid.EditorGridPanel.superclass.initComponent.call(this); this.store.removed = []; this.store.added = []; this.on('beforesave', this.onBeforeSave); if(this.allowRemove) { this.on('beforeremove', this.onBeforeRemove); this.on('removeconfirm', function(confirm) { if(confirm) this.remove() }.createDelegate(this), this); this.on('rowclick', function(grid) { grid.getTopToolbar().items.map.removeBtn.enable() } ); } this.store.on("load", function() { var recordFields = []; var fieldNames = this.store.fields.keys; Ext.each(fieldNames, function(fieldName) { recordFields.push({ name: fieldName }) }); this.record = Ext.data.Record.create(recordFields); this.setDirty(false); }, this); this.store.on( "update", this.onUpdate, this); this.store.on("add", this.onAdd, this); this.store.on("remove", this.onRemove, this); } });