raimund@548: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
raimund@548:  * Software engineering by Intevation GmbH
raimund@548:  *
raimund@548:  * This file is Free Software under the GNU GPL (v>=3)
raimund@548:  * and comes with ABSOLUTELY NO WARRANTY! Check out
raimund@548:  * the documentation coming with IMIS-Labordaten-Application for details.
raimund@548:  */
raimund@548: 
raimund@548: /*
raimund@548:  * Window to edit a Probe
raimund@548:  */
raimund@548: Ext.define('Lada.view.window.ProbeEdit', {
raimund@548:     extend: 'Ext.window.Window',
raimund@548:     alias: 'widget.probenedit',
raimund@548: 
raimund@548:     requires: [
raimund@548:         'Lada.view.form.Probe',
raimund@548:         'Lada.view.grid.Ort',
raimund@548:         'Lada.view.grid.Probenzusatzwert',
dustin@561:         'Lada.view.grid.PKommentar',
raimund@592:         'Lada.view.grid.Messung'
raimund@548:     ],
raimund@548: 
raimund@548:     collapsible: true,
raimund@548:     maximizable: true,
raimund@548:     autoShow: true,
raimund@548:     autoScroll: true,
raimund@548:     layout: 'fit',
dustin@688:     constrain: true,
raimund@548: 
raimund@548:     record: null,
raimund@548: 
raimund@548:     initComponent: function() {
raimund@548:         if (this.record === null) {
raimund@548:             Ext.Msg.alert('Keine valide Probe ausgewählt!');
raimund@548:             this.callParent(arguments);
raimund@548:             return;
raimund@548:         }
raimund@639:         var extendedTitle = this.record.get('probeId') ? this.record.get('probeId') : '';
raimund@639:         this.title = '§3-Probe ' + extendedTitle;
raimund@548:         this.buttons = [{
raimund@548:             text: 'Schließen',
raimund@548:             scope: this,
raimund@548:             handler: this.close
raimund@548:         }];
raimund@548:         this.width = 700;
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: 
raimund@548:         this.height = Ext.getBody().getViewSize().height - 30;
raimund@548:         // InitialConfig is the config object passed to the constructor on
raimund@548:         // creation of this window. We need to pass it throuh to the form as
raimund@548:         // we need the "modelId" param to load the correct item.
raimund@548:         this.items = [{
raimund@548:             border: 0,
raimund@548:             autoScroll: true,
raimund@548:             items: [{
raimund@548:                 xtype: 'probeform',
dustin@684:                 recordId: this.record.get('id')
raimund@601:              }, {
raimund@601:                 xtype: 'fset',
raimund@601:                 name: 'messungen',
raimund@601:                 title: 'Messungen',
raimund@601:                 padding: '5, 5',
raimund@601:                 margin: 5,
raimund@601:                 collapsible: false,
raimund@601:                 collapsed: false,
raimund@601:                 items: [{
raimund@601:                     xtype: 'messunggrid',
raimund@601:                     recordId: this.record.get('id')
raimund@601:                 }]
raimund@548:             }, {
raimund@548:                 xtype: 'fset',
raimund@548:                 name: 'orte',
raimund@548:                 title: 'Ortsangaben',
raimund@548:                 padding: '5, 5',
raimund@548:                 margin: 5,
raimund@548:                 items: [{
raimund@548:                     xtype: 'ortgrid',
raimund@548:                     recordId: this.record.get('id')
raimund@548:                 }]
raimund@548:             }, {
raimund@548:                 xtype: 'fset',
dustin@684:                 name: 'probenzusatzwerte',
raimund@548:                 title: 'Zusatzwerte',
raimund@548:                 padding: '5, 5',
raimund@548:                 margin: 5,
raimund@548:                 collapsible: true,
raimund@548:                 collapsed: true,
raimund@548:                 items: [{
raimund@548:                     xtype: 'probenzusatzwertgrid',
raimund@548:                     recordId: this.record.get('id')
raimund@548:                 }]
raimund@548:             }, {
raimund@548:                 xtype: 'fset',
raimund@548:                 name: 'pkommentare',
raimund@548:                 title: 'Kommentare',
raimund@548:                 padding: '5, 5',
raimund@548:                 margin: 5,
raimund@548:                 collapsible: true,
raimund@548:                 collapsed: true,
raimund@548:                 items: [{
raimund@548:                     xtype: 'pkommentargrid',
raimund@548:                     recordId: this.record.get('id')
raimund@548:                 }]
raimund@548:             }]
raimund@548:         }];
raimund@548:         this.callParent(arguments);
raimund@548:     },
raimund@548: 
raimund@548:     initData: function() {
raimund@713:         this.setLoading(true);
raimund@548:         this.clearMessages();
dustin@706:         me = this;
raimund@548:         Ext.ClassManager.get('Lada.model.Probe').load(this.record.get('id'), {
raimund@548:             failure: function(record, action) {
raimund@713:                 me.setLoading(false);
raimund@548:                 // TODO
raimund@712:                 console.log('An unhandled Failure occured. See following Response and Record');
raimund@712:                 console.log(action);
dustin@696:                 console.log(record);
dustin@696:              },
raimund@548:             success: function(record, response) {
raimund@548:                 this.down('probeform').setRecord(record);
raimund@690:                 this.record = record;
dustin@706:                 owner = this.record.get('owner');
dustin@706: 
dustin@706:                 if (owner) {
dustin@706:                     //Always allow to Add Messungen.
dustin@706:                     me.enableAddMessungen();
dustin@706:                 }
dustin@706: 
raimund@548:                 var json = Ext.decode(response.response.responseText);
raimund@548:                 if (json) {
raimund@548:                     this.setMessages(json.errors, json.warnings);
raimund@771:                     if (!json.warnings.mediaDesk) {
raimund@771:                         this.down('probeform').setMediaDesk(record);
raimund@771:                     }
raimund@548:                 }
raimund@710:                 // If the Probe is ReadOnly, disable Inputfields and grids
raimund@710:                 if (this.record.get('readonly') === true) {
raimund@710:                     this.down('probeform').setReadOnly(true);
raimund@710:                     this.disableChildren();
raimund@710:                 }
raimund@710:                 else {
raimund@710:                     this.down('probeform').setReadOnly(false);
raimund@710:                     this.enableChildren();
raimund@710:                 }
raimund@713:                 me.setLoading(false);
raimund@548:             },
raimund@548:             scope: this
raimund@548:         });
dustin@684:     },
dustin@684: 
raimund@712:     enableAddMessungen: function() {
dustin@706:         this.down('fset[name=messungen]').down('messunggrid').setReadOnly(false);
dustin@706:     },
dustin@706: 
raimund@712:     disableChildren: function() {
dustin@707:         if (!this.record.get('owner')) {
dustin@707:             // Disable only when the User is not the owner of the Probe
dustin@707:             // Works in symbiosis with success callback some lines above.
dustin@707:             this.down('fset[name=messungen]').down('messunggrid').setReadOnly(true);
dustin@707:         }
dustin@706:         this.down('fset[name=orte]').down('ortgrid').setReadOnly(true);
dustin@706:         this.down('fset[name=probenzusatzwerte]').down('probenzusatzwertgrid').setReadOnly(true);
dustin@706:         this.down('fset[name=pkommentare]').down('pkommentargrid').setReadOnly(true);
dustin@684:     },
dustin@684: 
raimund@712:     enableChildren: function() {
dustin@706:         this.down('fset[name=messungen]').down('messunggrid').setReadOnly(false);
dustin@706:         this.down('fset[name=orte]').down('ortgrid').setReadOnly(false);
dustin@706:         this.down('fset[name=probenzusatzwerte]').down('probenzusatzwertgrid').setReadOnly(false);
dustin@706:         this.down('fset[name=pkommentare]').down('pkommentargrid').setReadOnly(false);
raimund@548:     },
raimund@548: 
raimund@548:     setMessages: function(errors, warnings) {
raimund@548:         this.down('probeform').setMessages(errors, warnings);
raimund@548:         var errorOrtText = '';
raimund@548:         var errorOrt = false;
raimund@548:         var warningOrtText = '';
raimund@548:         var warningOrt = false;
raimund@548:         var key;
raimund@548:         var content;
raimund@548:         var i;
raimund@548:         var keyText;
raimund@548:         var i18n = Lada.getApplication().bundle;
raimund@548:         for (key in errors) {
raimund@582:             if (key && key.indexOf('Ort') > -1) {
raimund@548:                 errorOrt = true;
raimund@548:                 content = errors[key];
raimund@557:                 keyText = i18n.getMsg(key);
raimund@548:                 for (i = 0; i < content.length; i++) {
raimund@548:                     errorOrtText += keyText + ': ' +
raimund@548:                         i18n.getMsg(content[i].toString()) + '\n';
raimund@548:                 }
raimund@548:             }
raimund@548:         }
raimund@548:         for (key in warnings) {
raimund@582:             if (key && key.indexOf('Ort') > -1) {
raimund@548:                 warningOrt = true;
raimund@548:                 content = warnings[key];
raimund@557:                 keyText = i18n.getMsg(key);
raimund@548:                 for (i = 0; i < content.length; i++) {
raimund@548:                     warningOrtText += keyText + ': ' +
raimund@548:                         i18n.getMsg(content[i].toString()) + '\n';
raimund@548:                 }
raimund@548:             }
raimund@548:         }
raimund@548:         this.down('fset[name=orte]').showWarningOrError(
raimund@548:             warningOrt,
raimund@548:             warningOrtText === '' ? null : warningOrtText,
raimund@548:             errorOrt,
raimund@548:             errorOrtText === '' ? null : errorOrtText);
raimund@548:     },
raimund@548: 
raimund@548:     clearMessages: function() {
raimund@548:         this.down('probeform').clearMessages();
raimund@548:         this.down('fset[name=orte]').clearMessages();
raimund@548:     }
raimund@548: });