dustin@984: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz dustin@984: * Software engineering by Intevation GmbH dustin@984: * dustin@984: * This file is Free Software under the GNU GPL (v>=3) dustin@984: * and comes with ABSOLUTELY NO WARRANTY! Check out dustin@984: * the documentation coming with IMIS-Labordaten-Application for details. dustin@984: */ dustin@984: dustin@984: /** dustin@984: * This is a controller for a grid of Probenehmer Stammdaten dustin@984: */ dustin@984: Ext.define('Lada.controller.grid.Probenehmer', { dustin@984: extend: 'Ext.app.Controller', dustin@984: dustin@984: /** dustin@984: * Inhitialize the controller dustin@984: * It has 3 listeners dustin@984: */ dustin@984: init: function() { dustin@984: this.control({ dustin@984: 'probenehmergrid': { dustin@984: edit: this.gridSave, dustin@984: canceledit: this.cancelEdit, dustin@984: select: this.activateButtons, dustin@984: deselect: this.deactivateButtons dustin@984: }, dustin@984: 'probenehmergrid button[action=add]': { dustin@984: click: this.add dustin@984: }, dustin@984: 'probenehmergrid button[action=delete]': { dustin@984: click: this.remove dustin@984: } dustin@984: }); dustin@984: }, dustin@984: dustin@984: /** dustin@984: * This function is called when the grids roweditor saves dustin@984: * the record. dustin@984: * On success it refreshes the windows which contains the grid dustin@984: * On failure it displays a message dustin@984: */ dustin@984: gridSave: function(editor, context) { dustin@984: context.record.save({ dustin@984: success: function(record, response) { dustin@984: //Do Nothing dustin@984: }, dustin@984: failure: function(record, response) { dustin@984: var json = response.request.scope.reader.jsonData; dustin@984: if (json) { dustin@984: if (json.message){ dustin@984: Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title') dustin@984: +' #'+json.message, dustin@984: Lada.getApplication().bundle.getMsg(json.message)); dustin@984: } else { dustin@984: Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'), dustin@984: Lada.getApplication().bundle.getMsg('err.msg.generic.body')); dustin@984: } dustin@984: } dustin@984: } dustin@984: }); dustin@984: }, dustin@984: dustin@984: /** dustin@984: * When the edit was canceled, dustin@984: * the empty row might have been created by the roweditor is removed dustin@984: */ dustin@984: cancelEdit: function(editor, context) { dustin@984: if (!context.record.get('id') || dustin@984: context.record.get('id') === '') { dustin@984: editor.getCmp().store.remove(context.record); dustin@984: } dustin@984: context.grid.getSelectionModel().deselect(context.record); dustin@984: }, dustin@984: dustin@984: /** dustin@984: * This function adds a new row to add a probenehmer dustin@984: */ dustin@984: add: function(button) { dustin@984: var record = Ext.create('Lada.model.Probenehmer'); dustin@984: button.up('probenehmergrid').store.insert(0, record); dustin@984: button.up('probenehmergrid').rowEditing.startEdit(0, 1); dustin@984: }, dustin@984: dustin@984: /** dustin@984: * A record can be removed from the grid with the remove dustin@984: * function. It asks the user for confirmation dustin@984: * If the removal was confirmed, it reloads the parent window on success, dustin@984: * on failure, an error message is shown. dustin@984: */ dustin@984: remove: function(button) { dustin@984: var grid = button.up('grid'); dustin@984: var selection = grid.getView().getSelectionModel().getSelection()[0]; dustin@984: //TODO: i18n dustin@984: Ext.MessageBox.confirm('Löschen', 'Sind Sie sicher?', function(btn) { dustin@984: if (btn === 'yes') { dustin@984: selection.destroy({ dustin@984: success: function() { dustin@984: //DO NOTHING dustin@984: }, dustin@984: failure: function(request, response) { dustin@984: var json = response.request.scope.reader.jsonData; dustin@984: if (json) { dustin@984: if (json.message){ dustin@984: Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.delete.title') dustin@984: +' #'+json.message, dustin@984: Lada.getApplication().bundle.getMsg(json.message)); dustin@984: } else { dustin@984: Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.delete.title'), dustin@984: Lada.getApplication().bundle.getMsg('err.msg.generic.body')); dustin@984: } dustin@984: } else { dustin@984: Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.delete.title'), dustin@984: Lada.getApplication().bundle.getMsg('err.msg.response.body')); dustin@984: } dustin@984: } dustin@984: }); dustin@984: } dustin@984: }); dustin@984: grid.down('button[action=delete]').disable(); dustin@984: }, dustin@984: /** dustin@984: * Toggles the buttons in the toolbar dustin@984: **/ dustin@984: activateButtons: function(rowModel, record) { dustin@984: var grid = rowModel.view.up('grid'); dustin@984: this.buttonToggle(true, grid); dustin@984: }, dustin@984: dustin@984: /** dustin@984: * Toggles the buttons in the toolbar dustin@984: **/ dustin@984: deactivateButtons: function(rowModel, record) { dustin@984: var grid = rowModel.view.up('grid'); dustin@984: // Only disable buttons when nothing is selected dustin@984: if (rowModel.selected.items == 0) { dustin@984: this.buttonToggle(false, grid); dustin@984: } dustin@984: }, dustin@984: dustin@984: /** dustin@984: * Enables/Disables a set of buttons dustin@984: **/ dustin@984: buttonToggle: function(enabled, grid) { dustin@984: if (!enabled) { dustin@984: grid.down('button[action=delete]').disable(); dustin@984: } dustin@984: else { dustin@984: if (!grid.getPlugin('rowedit').editing) { dustin@984: //only enable buttons, when grid is not beeing edited dustin@984: grid.down('button[action=delete]').enable(); dustin@984: } dustin@984: //else turn them off again! dustin@984: else { dustin@984: this.buttonToggle(false, grid); dustin@984: } dustin@984: } dustin@984: }, dustin@984: }); dustin@984: