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,
raimund@1078:                 select: this.select,
raimund@1039:                 deselect: this.buttonToggle,
raimund@1039:                 itemdblclick: this.edit
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: 
raimund@1078:     select: function(rowModel, record) {
raimund@1078:         this.checkEdit(rowModel, record);
raimund@1078:         this.buttonToggle(rowModel, record);
raimund@1078:     },
raimund@1078: 
raimund@1078:     checkEdit: function(rowModel, record) {
raimund@1078:         if (!Ext.Array.contains(Lada.netzbetreiber,
raimund@1089:             record.get('netzbetreiberId')) &&
raimund@1089:             record.get('netzbetreiberId') !== '') {
raimund@1078:             var grid = Ext.ComponentQuery.query('probenehmergrid')[0];
raimund@1078:             grid.rowEditing.cancelEdit();
raimund@1078:             return;
raimund@1078:         }
raimund@1078:     },
raimund@1078: 
raimund@1078:     edit: function (gridView, record) {
raimund@1039:         var grid = Ext.ComponentQuery.query('probenehmergrid')[0];
raimund@1078:         if (!Ext.Array.contains(Lada.netzbetreiber,
raimund@1089:             record.get('netzbetreiberId')) &&
raimund@1089:             record.get('netzbetreiberId') !== '') {
raimund@1078:             grid.rowEditing.cancelEdit();
raimund@1078:             return;
raimund@1078:         }
raimund@1081:         grid.rowEditing.startEdit(record, 0);
raimund@1039:         grid.down('button[action=delete]').disable();
raimund@1039:     },
raimund@1039: 
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@1003:         var i18n = Lada.getApplication().bundle;
dustin@984:         context.record.save({
dustin@984:             success: function(record, response) {
raimund@1073:                 var grid = Ext.ComponentQuery.query('probenehmergrid')[0];
raimund@1073:                 grid.store.reload();
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@1003:                     Ext.Msg.alert(i18n.getMsg('err.msg.save.title')
dustin@984:                         +' #'+json.message,
dustin@1003:                         i18n.getMsg(json.message));
dustin@984:                    } else {
dustin@1003:                          Ext.Msg.alert(i18n.getMsg('err.msg.save.title'),
dustin@1003:                             i18n.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);
raimund@1039:             this.buttonToggle();
dustin@984:         }
raimund@1039:         else {
raimund@1039:             this.buttonToggle(context.grid.getSelectionModel(), context.record);
raimund@1039:         }
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');
raimund@1073:         if (!record.get('letzteAenderung')) {
raimund@1073:             record.data.letzteAenderung = new Date();
raimund@1073:         }
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@1003:         var i18n = Lada.getApplication().bundle;
dustin@1003:         Ext.MessageBox.confirm(i18n.getMsg('delete'),
dustin@1003:                                 i18n.getMsg('confirmation.question'),
dustin@1003:                                 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@1003:                                 Ext.Msg.alert(i18n.getMsg('err.msg.delete.title')
dustin@984:                                     +' #'+json.message,
dustin@1003:                                     i18n.getMsg(json.message));
dustin@984:                             } else {
dustin@1003:                                 Ext.Msg.alert(i18n.getMsg('err.msg.delete.title'),
dustin@1003:                                     i18n.getMsg('err.msg.generic.body'));
dustin@984:                             }
dustin@984:                         } else {
dustin@1003:                             Ext.Msg.alert(i18n.getMsg('err.msg.delete.title'),
dustin@1003:                                 i18n.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:     /**
dustin@984:      * Enables/Disables a set of buttons
dustin@984:      **/
raimund@1039:     buttonToggle: function(rowModel, record) {
raimund@1039:         if (!Ext.Array.contains(Lada.funktionen, 4)) {
raimund@1039:             return;
raimund@1039:         }
raimund@1039:         var grid = Ext.ComponentQuery.query('probenehmergrid')[0];
raimund@1078:         if (!record ||
raimund@1078:             !Ext.Array.contains(Lada.netzbetreiber,
raimund@1078:             record.get('netzbetreiberId'))) {
raimund@1039:             grid.down('button[action=delete]').disable();
raimund@1059:             return;
raimund@1039:         }
raimund@1039:         if (record.get('readonly') ||
raimund@1039:             rowModel.selected.items.length === 0) {
dustin@984:             grid.down('button[action=delete]').disable();
dustin@984:         }
dustin@984:         else {
raimund@1039:             if (grid.getPlugin('rowedit').editing) {
dustin@984:             //only enable buttons, when grid is not beeing edited
raimund@1039:                 grid.down('button[action=delete]').disable();
raimund@1039:             }
raimund@1039:             else {
dustin@984:                 grid.down('button[action=delete]').enable();
dustin@984:             }
dustin@984:         }
raimund@1039:     }
dustin@984: });