[Lada-commits] [PATCH] Added controllers for map and location form and handle new locations

Wald Commits scm-commit at wald.intevation.org
Tue Mar 17 14:32:28 CET 2015


# HG changeset patch
# User Raimund Renkert <raimund.renkert at intevation.de>
# Date 1426599193 -3600
# Node ID d21048cbdbb3259554cf683978462605cf553035
# Parent  8acb3123b46cae862e0c09b4d78146ad35d8da02
Added controllers for map and location form and handle new locations.

diff -r 8acb3123b46c -r d21048cbdbb3 app.js
--- a/app.js	Tue Mar 17 10:21:29 2015 +0100
+++ b/app.js	Tue Mar 17 14:33:13 2015 +0100
@@ -112,6 +112,8 @@
         'Lada.controller.grid.MKommentar',
         'Lada.controller.grid.Messung',
         'Lada.controller.grid.Messwert',
-        'Lada.controller.grid.Status'
+        'Lada.controller.grid.Status',
+        'Lada.controller.Map',
+        'Lada.controller.form.Location'
     ]
 });
diff -r 8acb3123b46c -r d21048cbdbb3 app/controller/Map.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/controller/Map.js	Tue Mar 17 14:33:13 2015 +0100
@@ -0,0 +1,35 @@
+/* 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.
+ */
+
+/**
+ */
+Ext.define('Lada.controller.Map', {
+    extend: 'Ext.app.Controller',
+
+    requires: [
+    ],
+
+    init: function() {
+        this.control({
+            'maptoolbar button[action=add]': {
+                click: this.addLocation
+            }
+        });
+        this.callParent(arguments);
+    },
+
+    addLocation: function(button) {
+        var mapPanel = button.up('map');
+        var details = button.up('window').down('locationform');
+        var newLocation = Ext.create('Lada.model.Location');
+        details.setRecord(newLocation);
+        details.setReadOnly(false);
+
+        mapPanel.activateDraw(newLocation);
+    }
+});
diff -r 8acb3123b46c -r d21048cbdbb3 app/controller/form/Location.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/controller/form/Location.js	Tue Mar 17 14:33:13 2015 +0100
@@ -0,0 +1,106 @@
+/* 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.
+ */
+
+Ext.define('Lada.controller.form.Location', {
+    extend: 'Ext.app.Controller',
+
+    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,
+            }
+        });
+    },
+
+    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) {
+                    formPanel.setMessages(json.errors, json.warnings);
+                }
+            }
+        });
+    },
+
+    discard: function(button) {
+        var formPanel = button.up('form');
+        formPanel.getForm().loadRecord(formPanel.getForm().getRecord());
+        button.up('window').down('map').locationRecord = null;
+    },
+
+    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);
+        }
+    },
+
+    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();
+        }
+    },
+
+    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();
+        }
+    }
+});
diff -r 8acb3123b46c -r d21048cbdbb3 app/view/panel/Map.js
--- a/app/view/panel/Map.js	Tue Mar 17 10:21:29 2015 +0100
+++ b/app/view/panel/Map.js	Tue Mar 17 14:33:13 2015 +0100
@@ -10,6 +10,7 @@
     alias: 'widget.map',
 
     record: null,
+    locationRecord: null,
 
     /**
      * @cfg
@@ -103,9 +104,17 @@
     },
 
     selectedFeature: function(feature) {
-        var record = Ext.data.StoreManager.get('locations').getById(feature.attributes.id);
-        this.up('window').down('locationform').setRecord(record);
-        this.up('window').down('ortform').down('combobox').setValue(record.id);
+        if (feature.attributes.id &&
+            feature.attributes.id !== '') {
+            var record = Ext.data.StoreManager.get('locations').getById(feature.attributes.id);
+            this.up('window').down('locationform').setRecord(record);
+            this.up('window').down('locationform').setReadOnly(true);
+            this.up('window').down('ortform').down('combobox').setValue(record.id);
+        }
+        else {
+            this.up('window').down('locationform').setRecord(this.locationRecord);
+            this.up('window').down('locationform').setReadOnly(false);
+        }
     },
 
     selectFeature: function(id) {
@@ -117,6 +126,26 @@
         this.selectControl.select(feature[0]);
     },
 
+    activateDraw: function(record) {
+        this.locationRecord = record;
+        if (!this.drawPoint) {
+            this.drawPoint = new OpenLayers.Control.DrawFeature(this.featureLayer,
+                OpenLayers.Handler.Point);
+            this.map.addControl(this.drawPoint);
+        }
+        this.drawPoint.activate();
+        this.drawPoint.events.register('featureadded', this, this.featureAdded);
+    },
+
+    featureAdded: function(features) {
+        this.locationRecord.set('latitude', features.feature.geometry.y);
+        this.locationRecord.set('longitude', features.feature.geometry.x);
+        this.drawPoint.deactivate();
+        this.selectControl.unselectAll();
+        this.selectControl.select(features.feature);
+        console.log(arguments);
+    },
+
     /**
      * @private
      * Override to display and update the map view in the panel.


More information about the Lada-commits mailing list