dustin@576: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
dustin@576:  * Software engineering by Intevation GmbH
dustin@576:  *
dustin@576:  * This file is Free Software under the GNU GPL (v>=3)
dustin@576:  * and comes with ABSOLUTELY NO WARRANTY! Check out
dustin@576:  * the documentation coming with IMIS-Labordaten-Application for details.
dustin@576:  */
dustin@576: 
dustin@891: /**
raimund@590:  * Window to edit a Messung
dustin@576:  */
dustin@576: Ext.define('Lada.view.window.MessungEdit', {
dustin@576:     extend: 'Ext.window.Window',
dustin@576:     alias: 'widget.messungedit',
dustin@576: 
dustin@581:     requires: [
raimund@590:       'Lada.view.form.Messung',
raimund@595:       'Lada.view.grid.Messwert',
raimund@595:       'Lada.view.grid.Status',
raimund@597:       'Lada.view.grid.MKommentar'
dustin@581:     ],
dustin@576: 
dustin@576:     collapsible: true,
dustin@576:     maximizable: true,
dustin@576:     autoshow: true,
dustin@576:     autoscroll: true,
dustin@576:     layout: 'fit',
dustin@688:     constrain: true,
raimund@690: 
raimund@690:     probe: null,
raimund@690:     parentWindow: null,
dustin@576:     record: null,
raimund@644:     grid: null,
dustin@576: 
dustin@890:     /**
dustin@890:      * This function initialises the Window
dustin@890:      */
raimund@590:     initComponent: function() {
dustin@576:         if (this.record === null) {
dustin@576:             Ext.Msg.alert('Keine valide Messung ausgewählt!');
dustin@576:             this.callParent(arguments);
dustin@576:             return;
dustin@576:         }
raimund@690:         if (this.probe === null) {
raimund@690:             Ext.Msg.alert('Zu der Messung existiert keine Probe!');
raimund@690:             this.callParent(arguments);
raimund@690:             return;
raimund@690:         }
dustin@875: 
dustin@875:         var messstelle = Ext.data.StoreManager.get('messstellen')
dustin@875:             .getById(this.probe.get('mstId'));
dustin@875: 
dustin@875:         this.title = 'Messung: ' + this.record.get('nebenprobenNr')
dustin@875:             + '   zu Probe: ' + this.probe.get('probeIdAlt')
dustin@875:             + ' Mst: ' + messstelle.get('messStelle')
dustin@875:             + ' editieren.';
dustin@875: 
dustin@875: 
dustin@576:         this.buttons = [{
dustin@576:             text: 'Schließen',
dustin@576:             scope: this,
dustin@576:             handler: this.close
dustin@576:         }];
dustin@709: 
dustin@709:         // add listeners to change the window appearence when it becomes inactive
dustin@709:         this.on({
dustin@709:             activate: function(){
dustin@709:                 this.getEl().removeCls('window-inactive');
dustin@709:             },
dustin@709:             deactivate: function(){
dustin@709:                 this.getEl().addCls('window-inactive');
dustin@709:             }
dustin@709:         });
dustin@709: 
dustin@576:         this.width = 700;
dustin@576:         this.height = Ext.getBody().getViewSize().height - 30;
dustin@576: 
raimund@845:         var mStore = Ext.data.StoreManager.get('messgroessen');
raimund@845:         mStore.proxy.extraParams = {mmtId: this.record.get('mmtId')};
raimund@845:         mStore.load();
raimund@845: 
dustin@576:         this.items = [{
dustin@576:             border: 0,
dustin@576:             autoScroll: true,
dustin@576:             items: [{
dustin@581:                 xtype: 'messungform',
dustin@611:                 margin: 5,
raimund@590:                 recordId: this.record.get('id')
dustin@613:            }, {
dustin@576:                 xtype: 'fset',
dustin@576:                 name: 'messwerte',
raimund@590:                 title: 'Messwerte',
raimund@590:                 padding: '5, 5',
raimund@590:                 margin: 5,
raimund@590:                 items: [{
raimund@590:                     xtype: 'messwertgrid',
raimund@845:                     recordId: this.record.get('id'),
raimund@845:                     messgroesseStore: mStore
dustin@613:                 }]
dustin@576:             }, {
dustin@576:                 xtype: 'fset',
dustin@576:                 name: 'messungstatus',
raimund@595:                 title: 'Status',
raimund@595:                 padding: '5, 5',
raimund@595:                 margin: 5,
raimund@595:                 items: [{
raimund@595:                     xtype: 'statusgrid',
raimund@595:                     recordId: this.record.get('id')
raimund@595:                 }]
dustin@576:            }, {
dustin@576:                 xtype: 'fset',
dustin@576:                 name: 'messungskommentare',
raimund@597:                 title: 'Kommentare',
raimund@597:                 padding: '5, 5',
raimund@597:                 margin: 5,
raimund@597:                 items: [{
raimund@597:                     xtype: 'mkommentargrid',
raimund@597:                     recordId: this.record.get('id')
raimund@597:                 }]
dustin@576:            }]
dustin@576:         }];
dustin@576:         this.callParent(arguments);
dustin@576:     },
dustin@576: 
dustin@890:     /**
dustin@890:      * Initialise the Data of this Window
dustin@890:      */
dustin@576:     initData: function() {
dustin@576:         this.clearMessages();
dustin@693:         var that = this;
dustin@576:         Ext.ClassManager.get('Lada.model.Messung').load(this.record.get('id'), {
dustin@696:             failure: function(record, response) {
raimund@590:                 // TODO
raimund@712:                 console.log('An unhandled Failure occured. See following Response and Record');
dustin@696:                 console.log(response);
raimund@590:                 console.log(record);
dustin@576:             },
raimund@590:             success: function(record, response) {
raimund@690:                 var me = this;
raimund@712:                 if (this.probe.get('treeModified') < record.get('parentModified')) {
raimund@690:                     Ext.Msg.show({
raimund@690:                         title: 'Probe nicht aktuell!',
raimund@712:                         msg: 'Die zugehörige Probe wurde verändert.\n' +
raimund@712:                             'Möchten Sie zu der Probe zurückkehren und neu laden?\n' +
raimund@712:                             'Ohne das erneute Laden der Probe wird das Speichern' +
dustin@696:                             ' der Messung nicht möglich sein.',
raimund@690:                         buttons: Ext.Msg.OKCANCEL,
raimund@690:                         icon: Ext.Msg.WARNING,
raimund@690:                         closable: false,
raimund@690:                         fn: function(button) {
raimund@690:                             if (button === 'ok') {
raimund@690:                                 me.close();
raimund@690:                                 me.parentWindow.initData();
raimund@690:                             }
raimund@690:                             else {
raimund@690:                                 me.record.set('treeModified', me.probe.get('treeModified'));
dustin@693:                                 that.disableForm();
raimund@690:                             }
raimund@690:                         }
raimund@690:                     });
raimund@690:                 }
raimund@590:                 this.down('messungform').setRecord(record);
raimund@690:                 this.record = record;
raimund@590:                 var json = Ext.decode(response.response.responseText);
raimund@590:                 if (json) {
raimund@590:                     this.setMessages(json.errors, json.warnings);
raimund@590:                 }
raimund@1036:                 if (this.record.get('readonly') === true ||
raimund@1036:                     this.record.get('owner') === false) {
raimund@710:                     this.disableForm();
raimund@710:                 }
raimund@710:                 else {
raimund@710:                     this.enableForm();
raimund@710:                 }
dustin@965:                 //Check if it is allowed to edit Status
dustin@965:                 if (this.record.get('statusEdit') === true) {
dustin@965:                     this.enableStatusEdit();
dustin@965:                 }
dustin@965:                 else {
dustin@965:                     this.disableStatusEdit();
dustin@965:                 }
dustin@997:                 //Check if it is allowed to reset Status: done in Messungform
dustin@576:             },
dustin@576:             scope: this
dustin@611:         });
dustin@693:     },
dustin@693: 
dustin@890:     /**
dustin@890:      * Disable the Forms in this Window.
dustin@890:      * Also disable this Windows Children
dustin@890:      */
raimund@712:     disableForm: function() {
dustin@693:         this.down('messungform').setReadOnly(true);
dustin@693:         this.disableChildren();
dustin@693:     },
dustin@693: 
dustin@890:     /**
dustin@890:      * Enable the Forms in this Window.
dustin@890:      * Also enble this Windows Children
dustin@890:      */
raimund@712:     enableForm: function() {
dustin@693:         this.down('messungform').setReadOnly(false);
dustin@693:         this.enableChildren();
dustin@693:     },
dustin@693: 
dustin@890:     /**
dustin@890:      * Disable the Chilelements of this window
dustin@890:      */
raimund@712:     disableChildren: function() {
dustin@693:             this.down('fset[name=messwerte]').down('messwertgrid').setReadOnly(true);
dustin@824:             this.down('fset[name=messwerte]').down('messwertgrid').readOnly = true;
dustin@693:             this.down('fset[name=messungskommentare]').down('mkommentargrid').setReadOnly(true);
dustin@824:             this.down('fset[name=messungskommentare]').down('mkommentargrid').readOnly = true;
dustin@1017:             //this.disableStatusEdit();
dustin@1017:             //this.disableStatusReset();
dustin@693:     },
dustin@693: 
dustin@890:     /**
dustin@890:      * Enable the Childelements of this window
dustin@890:      */
raimund@712:     enableChildren: function() {
dustin@693:             this.down('fset[name=messwerte]').down('messwertgrid').setReadOnly(false);
dustin@824:             this.down('fset[name=messwerte]').down('messwertgrid').readOnly = false;
dustin@965:             this.down('fset[name=messungskommentare]').down('mkommentargrid').setReadOnly(false);
dustin@965:             this.down('fset[name=messungskommentare]').down('mkommentargrid').readOnly = false;
dustin@1017:             //this.enableStatusEdit();
dustin@1017:             //this.enableStatusReset();
dustin@965:     },
dustin@965: 
dustin@965:     /**
dustin@996:      * Enable to reset the statusgrid
dustin@996:      */
dustin@996:      enableStatusReset: function() {
dustin@996:             this.down('fset[name=messungstatus]').down('statusgrid').setResetable(true);
dustin@996:      },
dustin@996: 
dustin@996:     /**
dustin@996:      * Disable to reset the statusgrid
dustin@996:      */
dustin@996:      disableStatusReset: function() {
dustin@996:             this.down('fset[name=messungstatus]').down('statusgrid').setResetable(false);
dustin@996:      },
dustin@996:     /**
dustin@965:      * Enable to edit the statusgrid
dustin@965:      */
dustin@996:      enableStatusEdit: function() {
dustin@693:             this.down('fset[name=messungstatus]').down('statusgrid').setReadOnly(false);
dustin@824:             this.down('fset[name=messungstatus]').down('statusgrid').readOnly = false;
dustin@965:      },
dustin@965: 
dustin@965:     /**
dustin@965:      * Disable to edit the statusgrid
dustin@965:      */
dustin@996:      disableStatusEdit: function() {
dustin@965:             this.down('fset[name=messungstatus]').down('statusgrid').setReadOnly(true);
dustin@965:             this.down('fset[name=messungstatus]').down('statusgrid').readOnly = true;
dustin@965:      },
dustin@576: 
dustin@890:     /**
dustin@890:      * Instructs the fields / forms listed in this method to set a message.
dustin@890:      * @param errors These Errors shall be shown
dustin@890:      * @param warnings These Warning shall be shown
dustin@890:      */
dustin@576:     setMessages: function(errors, warnings) {
dustin@707:         this.down('messungform').setMessages(errors, warnings);
dustin@576:     },
dustin@890: 
dustin@890:     /**
dustin@890:      * Instructs the fields / forms listed in this method to clear their messages.
dustin@890:      */
dustin@576:     clearMessages: function() {
dustin@707:         this.down('messungform').clearMessages();
dustin@576:     }
dustin@576: 
dustin@576: });