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: dustin@742: /* dustin@742: * This is a controller for an Ort Form dustin@742: */ raimund@603: Ext.define('Lada.controller.form.Ort', { raimund@603: extend: 'Ext.app.Controller', raimund@603: dustin@742: /** dustin@742: * Initialize the Controller with 4 listeners dustin@742: */ 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: dustin@742: /** dustin@742: * The save function saves the content of the Location form. dustin@742: * On success it will reload the Store, dustin@742: * on failure, it will display an Errormessage dustin@742: */ dustin@742: 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) { dustin@695: if(json.errors.totalCount > 0 || json.warnings.totalCount > 0){ dustin@695: formPanel.setMessages(json.errors, json.warnings); dustin@695: } dustin@695: dustin@695: if(json.message){ dustin@704: Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title') dustin@701: +' #'+json.message, dustin@695: 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@695: } dustin@701: } else { dustin@701: Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'), dustin@701: Lada.getApplication().bundle.getMsg('err.msg.response.body')); raimund@603: } raimund@603: } raimund@603: }); raimund@603: }, raimund@603: dustin@742: /** dustin@742: * The discard function resets the Location form dustin@742: * to its original state. dustin@742: */ dustin@742: 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: dustin@742: /** dustin@742: * The dirtyForm function enables or disables the save and discard dustin@742: * button which are present in the toolbar of the form. dustin@742: * The Buttons are only active if the content of the form was altered dustin@742: * (the form is dirty). dustin@742: */ 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: dustin@742: /** dustin@742: * updateDetails is used when a value is selected within the ort combobox dustin@742: * When this function is called, the map element within the window dustin@742: * which is embedding this form is updated. dustin@742: */ 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: });