view app/controller/form/Location.js @ 797:b8fd43021c29

Added a Window to alter Orte of a Messprogramm
author Dustin Demuth <dustin@intevation.de>
date Wed, 20 May 2015 16:48:03 +0200
parents 6e28ebbe1a73
children 2b7bcb778f0a
line wrap: on
line source
/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU GPL (v>=3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out
 * the documentation coming with IMIS-Labordaten-Application for details.
 */

/**
 * This is a Controller for a Location Form
 */
Ext.define('Lada.controller.form.Location', {
    extend: 'Ext.app.Controller',
    /**
     * Initialize the Controller with
     * 5 listeners
     */
    init: function() {
        this.control({
            'locationform button[action=save]': {
                click: this.save
            },
            'locationform button[action=discard]': {
                click: this.discard
            },
            'locationform': {
                dirtychange: this.dirtyForm
            },
            'locationform numberfield[name=latitude]': {
                change: this.updateFeatureLatitude
            },
            'locationform numberfield[name=longitude]': {
                change: this.updateFeatureLongitude
            }
        });
    },

    /**
     * The save function saves the content of the Location form.
     * On success it will reload the Store,
     * on failure, it will display an Errormessage
     */
    save: function(button) {
        var formPanel = button.up('form');
        var data = formPanel.getForm().getFieldValues(true);
        for (var key in data) {
            formPanel.getForm().getRecord().set(key, data[key]);
        }
        formPanel.getForm().getRecord().save({
            success: function(record, response) {
                var json = Ext.decode(response.response.responseText);
                if (json) {
                    button.setDisabled(true);
                    button.up('toolbar').down('button[action=discard]')
                        .setDisabled(true);
                    formPanel.clearMessages();
                    formPanel.setRecord(record);
                    formPanel.setMessages(json.errors, json.warnings);
                    button.up('window').down('map').locationRecord = null;
                    var orte = button.up('window').down('ortform combobox');
                    orte.store.reload({
                        callback: function() {
                            orte.setValue(record.data.id);
                        }
                    });
                }
            },
            failure: function(record, response) {
                button.setDisabled(true);
                button.up('toolbar').down('button[action=discard]')
                    .setDisabled(true);
                formPanel.getForm().loadRecord(formPanel.getForm().getRecord());
                var json = response.request.scope.reader.jsonData;
                if (json) {
                    if(json.errors.totalCount > 0 || json.warnings.totalCount > 0){
                        formPanel.setMessages(json.errors, json.warnings);
                    }

                    if(json.message){
                        Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title')
                            +' #'+json.message,
                            Lada.getApplication().bundle.getMsg(json.message));
                    } else {
                         Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'),
                            Lada.getApplication().bundle.getMsg('err.msg.generic.body'));
                    }
                } else {
                    Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'),
                        Lada.getApplication().bundle.getMsg('err.msg.response.body'));
                }
            }
        });
    },

    /**
     * The discard function resets the Location form
     * to its original state.
     */
    discard: function(button) {
        var formPanel = button.up('form');
        formPanel.getForm().loadRecord(formPanel.getForm().getRecord());
        button.up('window').down('map').locationRecord = null;
    },

    /**
     * The dirtyForm function enables or disables the save and discard
     * button which are present in the toolbar of the form.
     * The Buttons are only active if the content of the form was altered
     * (the form is dirty).
     */
    dirtyForm: function(form, dirty) {
        if (dirty) {
            form.owner.down('button[action=save]').setDisabled(false);
            form.owner.down('button[action=discard]').setDisabled(false);
        }
        else {
            form.owner.down('button[action=save]').setDisabled(true);
            form.owner.down('button[action=discard]').setDisabled(true);
        }
    },

    /**
     * This function updates the Latitude (heigth-value / y-value) of a feature
     * (geospatial object). The feature can be a marker in the map
     * @param {Ext.form.field.Field} field the Ext.field which was altered
     * @param {} nValue the new Latitude as WGS84 in decimaldegrees
     */
    updateFeatureLatitude: function(field, nValue) {
        var layer = field.up('window').down('map').selectControl.layer;
        var newLocation = field.up('window').down('map').locationRecord;
        if (layer && layer.selectedFeatures[0] && newLocation) {
            var feature = layer.getFeatureById(layer.selectedFeatures[0].id);
            feature.move(new OpenLayers.LonLat(feature.geometry.x, nValue));
            layer.refresh();
        }
    },

    /**
     * This function updates the Longitude (right-value / x-value) of a feature
     * (geospatial object). The feature can be a marker in the map
     * @param {Ext.form.field.Field} field the Ext.field which was altered
     * @param {} nValue the new Longitude as WGS84 in decimaldegrees
     */
    updateFeatureLongitude: function(field, nValue) {
        var layer = field.up('window').down('map').selectControl.layer;
        var newLocation = field.up('window').down('map').locationRecord;
        if (layer && layer.selectedFeatures[0] && newLocation) {
            var feature = layer.getFeatureById(layer.selectedFeatures[0].id);
            feature.move(new OpenLayers.LonLat(nValue, feature.geometry.y));
            layer.refresh();
        }
    }
});

http://lada.wald.intevation.org