raimund@638: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz raimund@638: * Software engineering by Intevation GmbH raimund@638: * raimund@638: * This file is Free Software under the GNU GPL (v>=3) raimund@638: * and comes with ABSOLUTELY NO WARRANTY! Check out raimund@638: * the documentation coming with IMIS-Labordaten-Application for details. raimund@638: */ raimund@638: raimund@638: Ext.define('Lada.controller.form.Location', { raimund@638: extend: 'Ext.app.Controller', raimund@638: raimund@638: init: function() { raimund@638: this.control({ raimund@638: 'locationform button[action=save]': { raimund@638: click: this.save raimund@638: }, raimund@638: 'locationform button[action=discard]': { raimund@638: click: this.discard raimund@638: }, raimund@638: 'locationform': { raimund@638: dirtychange: this.dirtyForm raimund@638: }, raimund@638: 'locationform numberfield[name=latitude]': { raimund@638: change: this.updateFeatureLatitude raimund@638: }, raimund@638: 'locationform numberfield[name=longitude]': { raimund@638: change: this.updateFeatureLongitude, raimund@638: } raimund@638: }); raimund@638: }, raimund@638: raimund@638: save: function(button) { raimund@638: var formPanel = button.up('form'); raimund@638: var data = formPanel.getForm().getFieldValues(true); raimund@638: for (var key in data) { raimund@638: formPanel.getForm().getRecord().set(key, data[key]); raimund@638: } raimund@638: formPanel.getForm().getRecord().save({ raimund@638: success: function(record, response) { raimund@638: var json = Ext.decode(response.response.responseText); raimund@638: if (json) { raimund@638: button.setDisabled(true); raimund@638: button.up('toolbar').down('button[action=discard]') raimund@638: .setDisabled(true); raimund@638: formPanel.clearMessages(); raimund@638: formPanel.setRecord(record); raimund@638: formPanel.setMessages(json.errors, json.warnings); raimund@638: button.up('window').down('map').locationRecord = null; raimund@638: var orte = button.up('window').down('ortform combobox'); raimund@638: orte.store.reload({ raimund@638: callback: function() { raimund@638: orte.setValue(record.data.id); raimund@638: } raimund@638: }); raimund@638: } raimund@638: }, raimund@638: failure: function(record, response) { raimund@638: button.setDisabled(true); raimund@638: button.up('toolbar').down('button[action=discard]') raimund@638: .setDisabled(true); raimund@638: formPanel.getForm().loadRecord(formPanel.getForm().getRecord()); raimund@638: var json = response.request.scope.reader.jsonData; raimund@638: 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@695: Ext.Msg.alert(Lada.getApplication().bundle.getMsg('errmsgtitle') dustin@695: +' '+json.message, dustin@695: Lada.getApplication().bundle.getMsg(json.message)); dustin@695: } raimund@638: } raimund@638: } raimund@638: }); raimund@638: }, raimund@638: raimund@638: discard: function(button) { raimund@638: var formPanel = button.up('form'); raimund@638: formPanel.getForm().loadRecord(formPanel.getForm().getRecord()); raimund@638: button.up('window').down('map').locationRecord = null; raimund@638: }, raimund@638: raimund@638: dirtyForm: function(form, dirty) { raimund@638: if (dirty) { raimund@638: form.owner.down('button[action=save]').setDisabled(false); raimund@638: form.owner.down('button[action=discard]').setDisabled(false); raimund@638: } raimund@638: else { raimund@638: form.owner.down('button[action=save]').setDisabled(true); raimund@638: form.owner.down('button[action=discard]').setDisabled(true); raimund@638: } raimund@638: }, raimund@638: raimund@638: updateFeatureLatitude: function(field, nValue) { raimund@638: var layer = field.up('window').down('map').selectControl.layer; raimund@638: var newLocation = field.up('window').down('map').locationRecord; raimund@638: if (layer && layer.selectedFeatures[0] && newLocation) { raimund@638: var feature = layer.getFeatureById(layer.selectedFeatures[0].id); raimund@638: feature.move(new OpenLayers.LonLat(feature.geometry.x, nValue)); raimund@638: layer.refresh(); raimund@638: } raimund@638: }, raimund@638: raimund@638: updateFeatureLongitude: function(field, nValue) { raimund@638: var layer = field.up('window').down('map').selectControl.layer; raimund@638: var newLocation = field.up('window').down('map').locationRecord; raimund@638: if (layer && layer.selectedFeatures[0] && newLocation) { raimund@638: var feature = layer.getFeatureById(layer.selectedFeatures[0].id); raimund@638: feature.move(new OpenLayers.LonLat(nValue, feature.geometry.y)); raimund@638: layer.refresh(); raimund@638: } raimund@638: } raimund@638: });