raimund@603: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
raimund@603:  * Software engineering by Intevation GmbH
raimund@603:  *
raimund@603:  * This file is Free Software under the GNU GPL (v>=3)
raimund@603:  * and comes with ABSOLUTELY NO WARRANTY! Check out
raimund@603:  * the documentation coming with IMIS-Labordaten-Application for details.
raimund@603:  */
raimund@603: 
raimund@603: Ext.define('Lada.controller.form.Ort', {
raimund@603:     extend: 'Ext.app.Controller',
raimund@603: 
raimund@603:     init: function() {
raimund@603:         this.control({
raimund@603:             'ortform button[action=save]': {
raimund@603:                 click: this.save
raimund@603:             },
raimund@603:             'ortform button[action=discard]': {
raimund@603:                 click: this.discard
raimund@603:             },
raimund@603:             'ortform': {
raimund@603:                 dirtychange: this.dirtyForm
raimund@603:             },
raimund@603:             'ortform combobox[name=ort]': {
raimund@603:                 select: this.updateDetails
raimund@603:             }
raimund@603:         });
raimund@603:     },
raimund@603: 
raimund@603:     save: function(button) {
raimund@641:         var formPanel = button.up('ortform');
raimund@603:         var data = formPanel.getForm().getFieldValues(true);
raimund@603:         for (var key in data) {
raimund@603:             formPanel.getForm().getRecord().set(key, data[key]);
raimund@603:         }
raimund@603:         formPanel.getForm().getRecord().save({
raimund@603:             success: function(record, response) {
raimund@603:                 var json = Ext.decode(response.response.responseText);
raimund@641:                 if (json) {
raimund@603:                     button.setDisabled(true);
raimund@603:                     button.up('toolbar').down('button[action=discard]')
raimund@603:                         .setDisabled(true);
raimund@603:                     formPanel.clearMessages();
raimund@603:                     formPanel.setRecord(record);
raimund@603:                     formPanel.setMessages(json.errors, json.warnings);
raimund@644:                     formPanel.up('window').grid.store.reload();
raimund@603:                 }
raimund@603:             },
raimund@603:             failure: function(record, response) {
raimund@603:                 button.setDisabled(true);
raimund@603:                 button.up('toolbar').down('button[action=discard]')
raimund@603:                     .setDisabled(true);
raimund@603:                 formPanel.getForm().loadRecord(formPanel.getForm().getRecord());
raimund@603:                 var json = response.request.scope.reader.jsonData;
raimund@603:                 if (json) {
raimund@603:                     formPanel.setMessages(json.errors, json.warnings);
raimund@603:                 }
raimund@603:             }
raimund@603:         });
raimund@603:     },
raimund@603: 
raimund@603:     discard: function(button) {
raimund@603:         var formPanel = button.up('form');
raimund@603:         formPanel.getForm().loadRecord(formPanel.getForm().getRecord());
raimund@615:         var win = button.up('window');
raimund@615:         var id = formPanel.getForm().getRecord().get('ort');
raimund@615:         var toLoad = Ext.data.StoreManager.get('locations').getById(id);
raimund@615:         win.down('locationform').setRecord(toLoad);
raimund@615:         win.down('map').selectFeature(id);
raimund@603:     },
raimund@603: 
raimund@603:     dirtyForm: function(form, dirty) {
raimund@603:         if (dirty) {
raimund@603:             form.owner.down('button[action=save]').setDisabled(false);
raimund@603:             form.owner.down('button[action=discard]').setDisabled(false);
raimund@603:         }
raimund@603:         else {
raimund@603:             form.owner.down('button[action=save]').setDisabled(true);
raimund@603:             form.owner.down('button[action=discard]').setDisabled(true);
raimund@603:         }
raimund@603:     },
raimund@603: 
raimund@603:     updateDetails: function(combobox, record) {
raimund@603:         var win = combobox.up('window');
raimund@603:         var details = win.down('locationform');
raimund@603:         var id = record[0].get('id');
raimund@603:         if (details) {
raimund@615:             var toLoad = Ext.data.StoreManager.get('locations').getById(id);
raimund@615:             win.down('locationform').setRecord(toLoad);
raimund@615:             win.down('map').selectFeature(id);
raimund@603:         }
raimund@603:     }
raimund@603: });