changeset 1337:7194964183f4

Ortserstellung controller separated from form
author Maximilian Krambach <mkrambach@intevation.de>
date Fri, 03 Feb 2017 17:44:41 +0100
parents f879c806f880
children d2f06ce4d43a
files app.js app/controller/form/Ortserstellung.js app/view/form/Ortserstellung.js
diffstat 3 files changed, 172 insertions(+), 168 deletions(-) [+]
line wrap: on
line diff
--- a/app.js	Fri Feb 03 17:43:23 2017 +0100
+++ b/app.js	Fri Feb 03 17:44:41 2017 +0100
@@ -330,6 +330,7 @@
         'Lada.controller.grid.Probenehmer',
         'Lada.controller.form.Probe',
         'Lada.controller.form.Messung',
+        'Lada.controller.form.Ortserstellung',
         'Lada.controller.grid.Probenzusatzwert',
         'Lada.controller.grid.PKommentar',
         'Lada.controller.grid.MKommentar',
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/controller/form/Ortserstellung.js	Fri Feb 03 17:44:41 2017 +0100
@@ -0,0 +1,164 @@
+/* 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 an Ortserstellung Form
+ */
+Ext.define('Lada.controller.form.Ortserstellung', {
+    extend: 'Ext.app.Controller',
+
+    /**
+     * Initialize the Controller
+     */
+    init: function() {
+        this.control({
+            'ortserstellungsform button[action=save]': {
+                click: this.save
+            },
+            'ortserstellungsform button[action=revert]': {
+                click: this.discard
+            },
+            'ortserstellungsform staat' : {
+                change: this.checkCommitEnabled
+            },
+            'ortserstellungsform verwaltungseinheit' : {
+                change: this.checkCommitEnabled
+            },
+            'ortserstellungsform koordinatenart': {
+                change: this.checkCommitEnabled
+            },
+            'ortserstellungsform numfield [name=koordXExtern]': {
+                change: this.checkCommitEnabled
+            },
+            'ortserstellungsform numfield [name=koordYExtern]': {
+                change: this.checkCommitEnabled
+            }
+        });
+    },
+
+    save: function() {
+        var this_panel = this.up('panel');
+        var me = this;
+        var form = this_panel.getForm();
+        var record = form.getRecord();
+        var data = form.getFieldValues(true);
+        for (var key in data) {
+            record.set(key, data[key]);
+        }
+        record.set('id', null);
+        record.set('netzbetreiberId', Lada.netzbetreiber[0]);
+        record.save({
+            success: function(newrecord, response) {
+                form.loadRecord(newrecord);
+                this_panel.down('verwaltungseinheit').store.load(
+                        { id:newrecord.get('gemId') });
+                this_panel.down('staat').store.load(
+                        { id : newrecord.get('staat') });
+                me.setDisabled(true);
+                me.hide();
+                var ozw = this_panel.up().parentWindow;
+                var json = Ext.decode(response.response.responseText);
+                if (json) {
+                    this_panel.clearMessages();
+                    this_panel.setMessages(json.errors, json.warnings);
+                }
+                ozw.ortstore.load({
+                    callback: function(records, operation, success) {
+                        ozw.down('map').addLocations(ozw.ortstore);
+                        var osg = ozw.down('ortstammdatengrid');
+                        osg.setStore(ozw.ortstore);
+                        var id = Ext.decode(response.response.responseText).data.id;
+                        var record = osg.store.getById(id);
+                        var selmod = osg.getView().getSelectionModel();
+                        selmod.select(record);
+                        var resulttext;
+                        if (json) {
+                            if (json.message == '201') {
+                                resulttext = 'Dieser Ort existiert bereits!';
+                            }
+                            if (json.message == '200') {
+                                resulttext = 'Ort erfolgreich angelegt!';
+                            }
+                        }
+                        Ext.Msg.show({
+                            title: Lada.getApplication().bundle.getMsg('success'),
+                            autoScroll: true,
+                            msg: resulttext,
+                            buttons: Ext.Msg.OK
+                        });
+                    },
+                    scope: this
+                });
+
+            },
+            failure: function(record, response) {
+                var json = Ext.decode(response.response.responseText);
+                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'));
+                }
+                me.setDisabled(true);
+            }
+        });
+    },
+
+    discard: function(button) {
+        this.up('panel').getForm().reset();
+        this.up('toolbar').down('button [action=save]').setDisabled(true);
+    },
+
+    /**
+     * checks if the Messpunkt can be committed.
+     * Disables the save button if false
+     */
+    checkCommitEnabled: function(field) {
+        var panel;
+        if (field.up('panel')) {
+            panel = field.up('panel').up('panel');
+        } else {
+            panel = field;
+        }
+        var savebutton =  panel.down('button[action=save]');
+        var form = panel.getForm();
+        if (form.isDirty()) {
+            panel.down('button[action=revert]').setDisabled(false);
+        } else {
+            panel.down('button[action=revert]').setDisabled(true);
+        }
+        if (form.isValid()) {
+            //one of three conditions must apply, the first one depending 
+            // on three fields
+            if (
+                (form.findField('kdaId').getValue()
+                    && form.findField('koordYExtern').getValue()
+                    && form.findField('koordXExtern').getValue()
+                )
+                || form.findField('gemId').getValue() >= 0
+                || form.findField('staatId').getValue() >= 0
+                ) {
+                savebutton.setDisabled(false);
+            } else {
+                savebutton.setDisabled(true);
+            }
+        } else { //form invalid
+            savebutton.setDisabled(true);
+        }
+    }
+});
--- a/app/view/form/Ortserstellung.js	Fri Feb 03 17:43:23 2017 +0100
+++ b/app/view/form/Ortserstellung.js	Fri Feb 03 17:44:41 2017 +0100
@@ -65,24 +65,12 @@
             labelWidth: 125,
             fieldLabel: i18n.getMsg('orte.verwaltungseinheit'),
             forceSelection: true,
-            name: 'gemId',
-            listeners: {
-                change: {
-                    fn: function() { me.checkCommitEnabled() }
-                }
-            }
+            name: 'gemId'
         }, {
             xtype: 'koordinatenart',
             labelWidth: 125,
             fieldLabel: i18n.getMsg('orte.kda'),
-            allowDecimals: false,
-            maxLength: 1,
-            name: 'kdaId',
-            listeners: {
-                change: {
-                    fn: function() { me.checkCommitEnabled() }
-                }
-            }
+            name: 'kdaId'
         }, {
             xtype: 'numfield',
             labelWidth: 125,
@@ -90,12 +78,7 @@
             name: 'koordXExtern',
             allowDecimals: true,
             decimalPrecision: 5,
-            maxLength: 22,
-            listeners: {
-                change: {
-                    fn: function() { me.checkCommitEnabled() }
-                }
-            }
+            maxLength: 22
         }, {
             xtype: 'numfield',
             labelWidth: 125,
@@ -103,12 +86,7 @@
             name: 'koordYExtern',
             allowDecimals: true,
             decimalPrecision: 5,
-            maxLength: 22,
-            listeners: {
-                change: {
-                    fn: function() { me.checkCommitEnabled() }
-                }
-            }
+            maxLength: 22
         }, {
             xtype: 'numfield',
             labelWidth: 125,
@@ -204,151 +182,13 @@
                 qtip: 'Ă„nderungen verwerfen',
                 icon: 'resources/img/dialog-cancel.png',
                 action: 'revert',
+                disabled: true
             }]
         }];
-
         this.callParent(arguments);
         this.getForm().loadRecord(this.record);
-    },
-
-    /**
-     * checks if the Messpunkt can be committed.
-     * Disables the save button if false
-     */
-    checkCommitEnabled: function() {
-        var savebutton =  this.down('toolbar').down('button[action=save]');
-        var form = this.getForm();
-        if (form.isDirty()) {
-            if (form.findField('kdaId').getValue() ||
-                form.findField('koordYExtern').getValue() ||
-                form.findField('koordXExtern').getValue()) {
-                if (this.checkCoordinates()) {
-                    savebutton.setDisabled(false);
-                } else {
-                    savebutton.setDisabled(true);
-                }
-            } else if (form.findField('gemId').getValue() ||
-                form.findField('staatId').getValue() >= 0 ) {
-                savebutton.setDisabled(false);
-            } else {
-                savebutton.setDisabled(true);
-            }
-        } else {
-            savebutton.setDisabled(true);
-        }
-    },
-
-    /**
-     * Validates the coordinate fields kdaId, koordXExtern, koordYExtern
-     */
-    checkCoordinates: function() {
-        var x = this.getForm().findField('koordXExtern').getValue();
-        var y = this.getForm().findField('koordYExtern').getValue();
-        var kda = this.getForm().findField('kdaId').getValue();
-        if (x && y && kda) {
-            if (kda === 4){
-                if (x > -180 && x < 180
-                    && y > -90 && y < 90) {
-                    return true;
-                } else {
-                    // TODO: WGS84 (degrees- decimal), coordinates invalid
-                    return false;
-                }
-            } else if (kda === 5){
-                if (x >= 1000000 && x < 61000000 &&
-                    y > -10000000 && y < 10000000) {
-                    return true;
-                } else {
-                    // TODO: UTM, coordinates invalid
-                    return false;
-                }
-            } else {
-                // TODO KDA not supported
-                return false;
-            }
-        } else {
-            // TODO: not all fields filled in
-            return false;
-        }
-    },
-
-    saveOrt: function() {
-        var this_panel = this.up('panel');
-        var me = this;
-        var form = this_panel.getForm();
-        var record = form.getRecord();
-        var data = form.getFieldValues(true);
-        for (var key in data) {
-            record.set(key, data[key]);
-        }
-        record.set('id', null);
-        record.set('netzbetreiberId', Lada.netzbetreiber[0]);
-        record.save({
-            success: function(newrecord, response) {
-                form.loadRecord(newrecord);
-                this_panel.down('verwaltungseinheit').store.load(
-                        { id:newrecord.get('gemId') });
-                console.log(this_panel.down('verwaltungseinheit'));
-                this_panel.down('staat').store.load(
-                        { id : newrecord.get('staat') });
-                me.setDisabled(true);
-                me.hide();
-                var ozw = this_panel.up().parentWindow;
-                var json = Ext.decode(response.response.responseText);
-                if (json) {
-                    this_panel.clearMessages();
-                    this_panel.setMessages(json.errors, json.warnings);
-                }
-                ozw.ortstore.load({
-                    callback: function(records, operation, success) {
-                        ozw.down('map').addLocations(ozw.ortstore);
-                        var osg = ozw.down('ortstammdatengrid');
-                        osg.setStore(ozw.ortstore);
-                        var id = Ext.decode(response.response.responseText).data.id;
-                        var record = osg.store.getById(id);
-                        var selmod = osg.getView().getSelectionModel();
-                        selmod.select(record);
-                        var resulttext;
-                        if (json) {
-                            if (json.message == '201') {
-                                resulttext = 'Dieser Ort existiert bereits!';
-                            }
-                            if (json.message == '200') {
-                                resulttext = 'Ort erfolgreich angelegt!';
-                            }
-                        }
-                        Ext.Msg.show({
-                            title: Lada.getApplication().bundle.getMsg('success'),
-                            autoScroll: true,
-                            msg: resulttext,
-                            buttons: Ext.Msg.OK
-                        });
-                    },
-                    scope: this
-                });
-
-            },
-            failure: function(record, response) {
-                var json = Ext.decode(response.response.responseText);
-                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'));
-                }
-                me.setDisabled(true);
-            }
-        });
+        var controller = Lada.app.getController('Lada.controller.form.Ortserstellung')
+        controller.checkCommitEnabled(this);
     },
 
     setMessages: function(errors, warnings) {
@@ -389,5 +229,4 @@
     clearMessages: function() {
         // TODO: this is a stub
      }
-
 });

http://lada.wald.intevation.org