raimund@596: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
raimund@596:  * Software engineering by Intevation GmbH
raimund@596:  *
raimund@596:  * This file is Free Software under the GNU GPL (v>=3)
raimund@596:  * and comes with ABSOLUTELY NO WARRANTY! Check out
raimund@596:  * the documentation coming with IMIS-Labordaten-Application for details.
raimund@596:  */
raimund@596: 
dustin@742: /**
dustin@742:  * This is a controller for a grid of MKommentar
dustin@742:  * MKommentar are comments which are associated to a
dustin@742:  * Measurement
dustin@742:  */
raimund@596: Ext.define('Lada.controller.grid.MKommentar', {
raimund@596:     extend: 'Ext.app.Controller',
raimund@596: 
dustin@742:     /**
dustin@742:      * Inhitialize the controller
dustin@742:      * It has 3 listeners
dustin@742:      */
raimund@596:     init: function() {
raimund@596:         this.control({
raimund@596:             'mkommentargrid': {
dustin@700:                 edit: this.gridSave,
raimund@637:                 canceledit: this.cancelEdit
raimund@596:             },
raimund@596:             'mkommentargrid button[action=add]': {
raimund@596:                 click: this.add
raimund@596:             },
raimund@596:             'mkommentargrid button[action=delete]': {
raimund@596:                 click: this.remove
raimund@596:             }
raimund@596:         });
raimund@596:     },
raimund@596: 
dustin@742:     /**
dustin@742:      * This function is called when the grids roweditor saves
dustin@742:      * the record.
dustin@742:      * On success it refreshes the windows which contains the grid
dustin@742:      * On failure it displays a message
dustin@742:      */
dustin@700:     gridSave: function(editor, context) {
mstanko@944:         context.record.set('datum', new Date());
mstanko@944:         context.record.save({
dustin@697:             success: function(record, response) {
raimund@596:                 context.grid.initData();
raimund@596:                 context.grid.up('window').initData();
raimund@596:             },
dustin@697:             failure: function(record, response) {
dustin@697:               var json = response.request.scope.reader.jsonData;
dustin@697:               if (json) {
dustin@697:                 if (json.message){
dustin@701:                     Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title')
dustin@701:                         +' #'+json.message,
dustin@697:                         Lada.getApplication().bundle.getMsg(json.message));
dustin@701:                    } else {
dustin@701:                          Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'),
dustin@701:                             Lada.getApplication().bundle.getMsg('err.msg.generic.body'));
dustin@701:                    }
dustin@701:               }
raimund@596:             }
raimund@596:         });
raimund@596:     },
raimund@596: 
dustin@742:     /**
dustin@742:      * When the edit was canceled,
dustin@742:      * the empty row might have been created by the roweditor is removed
dustin@742:      */
raimund@637:     cancelEdit: function(editor, context) {
raimund@637:         if (!context.record.get('id') ||
raimund@637:             context.record.get('id') === '') {
raimund@637:             editor.getCmp().store.remove(context.record);
raimund@637:         }
raimund@637:     },
raimund@637: 
dustin@742:     /**
dustin@742:      * This function adds a new row to add a MKommentar
dustin@742:      */
raimund@596:     add: function(button) {
raimund@596:         var record = Ext.create('Lada.model.MKommentar');
dustin@970:         record.data.datum = new Date();
raimund@596:         record.set('messungsId', button.up('mkommentargrid').recordId);
raimund@596:         button.up('mkommentargrid').store.insert(0, record);
raimund@596:         button.up('mkommentargrid').rowEditing.startEdit(0, 1);
raimund@596:     },
raimund@596: 
dustin@742:     /**
dustin@742:      * A Mkommentar-row can be removed from the grid with the remove
dustin@742:      * function. It asks the user for confirmation
dustin@742:      * If the removal was confirmed, it reloads the parent window on success,
dustin@742:      * on failure, an error message is shown.
dustin@742:      */
raimund@596:     remove: function(button) {
raimund@596:         var grid = button.up('grid');
raimund@596:         var selection = grid.getView().getSelectionModel().getSelection()[0];
raimund@596:         Ext.MessageBox.confirm('Löschen', 'Sind Sie sicher?', function(btn) {
raimund@596:             if (btn === 'yes') {
raimund@596:                 selection.destroy({
raimund@596:                     success: function() {
raimund@596:                         button.up('window').initData();
raimund@596:                     },
dustin@701:                     failure: function(request, response) {
dustin@701:                         var json = response.request.scope.reader.jsonData;
dustin@701:                         if (json) {
dustin@701:                             if (json.message){
dustin@701:                                 Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.delete.title')
dustin@701:                                     +' #'+json.message,
dustin@701:                                     Lada.getApplication().bundle.getMsg(json.message));
dustin@701:                             } else {
dustin@701:                                 Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.delete.title'),
dustin@701:                                     Lada.getApplication().bundle.getMsg('err.msg.generic.body'));
dustin@701:                             }
dustin@701:                         } else {
dustin@701:                             Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.delete.title'),
dustin@701:                                 Lada.getApplication().bundle.getMsg('err.msg.response.body'));
dustin@701:                         }
raimund@596:                     }
raimund@596:                 });
raimund@596:             }
raimund@596:         });
dustin@826:         grid.down('button[action=delete]').disable();
raimund@596:     }
raimund@596: });