dustin@757: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
dustin@757:  * Software engineering by Intevation GmbH
dustin@757:  *
dustin@757:  * This file is Free Software under the GNU GPL (v>=3)
dustin@757:  * and comes with ABSOLUTELY NO WARRANTY! Check out
dustin@757:  * the documentation coming with IMIS-Labordaten-Application for details.
dustin@757:  */
dustin@757: 
dustin@894: /**
dustin@894:  * Form to edit a Messprogramm
dustin@757:  */
dustin@757: Ext.define('Lada.view.form.Messprogramm', {
dustin@757:     extend: 'Ext.form.Panel',
dustin@757:     alias: 'widget.messprogrammform',
dustin@757:     requires: [
dustin@757:         'Lada.view.widget.Datenbasis',
dustin@757:         'Lada.view.widget.Messstelle',
dustin@757:         'Lada.view.widget.Netzbetreiber',
dustin@757:         'Lada.view.widget.Betriebsart',
dustin@757:         'Lada.view.widget.Testdatensatz',
dustin@757:         'Lada.view.widget.Probenart',
dustin@757:         'Lada.view.widget.Umwelt',
dustin@757:         'Lada.view.widget.base.TextField',
dustin@757:         'Lada.view.widget.base.FieldSet',
dustin@757:         'Lada.model.Messprogramm',
dustin@757:         'Lada.model.MmtMessprogramm',
dustin@757:         'Lada.view.widget.Probenintervall',
dustin@797:         'Lada.view.widget.Location',
dustin@757:         'Lada.view.widget.ProbenintervallSlider',
dustin@757:         'Lada.view.widget.base.Datetime',
dustin@757:         'Lada.view.widget.base.DateField'
dustin@757:     ],
dustin@757: 
dustin@757:     model: 'Lada.model.Messprogramm',
dustin@757:     minWidth: 650,
dustin@757:     margin: 5,
dustin@757:     border: 0,
dustin@757: 
dustin@757:     recordId: null,
dustin@797:     ortWindow: null,
dustin@757: 
dustin@757:     trackResetOnLoad: true,
dustin@757: 
dustin@757:     initComponent: function() {
dustin@757:         var me = this;
dustin@757:         var i18n = Lada.getApplication().bundle;
dustin@757:         this.items = [{
dustin@757:             xtype: 'fieldset',
dustin@757:             title: i18n.getMsg('messprogramm.form.fieldset.title'),
dustin@757:             items: [{
dustin@757:                 border: 0,
dustin@757:                 margin: '0, 0, 10, 0',
dustin@757:                 dockedItems: [{
dustin@757:                     xtype: 'toolbar',
dustin@757:                     dock: 'bottom',
dustin@757:                     border: '0, 1, 1, 1',
dustin@757:                     style: {
dustin@757:                         borderBottom: '1px solid #b5b8c8 !important',
dustin@757:                         borderLeft: '1px solid #b5b8c8 !important',
dustin@757:                         borderRight: '1px solid #b5b8c8 !important'
dustin@757:                     },
dustin@757:                     items: ['->', {
dustin@757:                         text: i18n.getMsg('save'),
dustin@757:                         qtip: i18n.getMsg('save.qtip'),
dustin@757:                         icon: 'resources/img/dialog-ok-apply.png',
dustin@757:                         action: 'save',
dustin@757:                         disabled: true
dustin@757:                     }, {
dustin@757:                         text: i18n.getMsg('discard'),
dustin@757:                         qtip: i18n.getMsg('discard.qtip'),
dustin@757:                         icon: 'resources/img/dialog-cancel.png',
dustin@757:                         action: 'discard',
dustin@757:                         disabled: true
dustin@757:                     }]
dustin@757:                 }],
dustin@757:                 items: [{
dustin@757:                     layout: 'hbox',
dustin@757:                     border: 0,
dustin@757:                     items: [{
dustin@757:                         border: 0,
dustin@757:                         width: '50%',
dustin@757:                         minWidth: 290,
dustin@757:                         layout: {
dustin@757:                             type: 'vbox',
dustin@757:                             align: 'stretch'
dustin@757:                         },
dustin@757:                         margin: '0, 10, 0, 0',
dustin@757:                         items: [{
dustin@757:                             xtype: 'messstelle',
dustin@757:                             name: 'mstId',
dustin@757:                             fieldLabel: i18n.getMsg('mstId'),
dustin@757:                             labelWidth: 135,
dustin@757:                             allowBlank: false,
dustin@757:                             editable: true
dustin@757:                         }, {
dustin@777:                             xtype: 'tfield',
dustin@757:                             name: 'name',
dustin@757:                             fieldLabel: i18n.getMsg('name'),
dustin@757:                             labelWidth: 135,
dustin@757:                             allowBlank: false,
dustin@757:                             editable: true
dustin@757:                         }, {
dustin@820:                             xtype: 'textarea',
dustin@757:                             name: 'probeKommentar',
dustin@757:                             labelAlign: 'top',
dustin@757:                             fieldLabel: i18n.getMsg('probeKommentar'),
dustin@757:                             labelwidth: 135,
dustin@757:                             anchor: '100%'
dustin@797:                         }, {
dustin@820:                             layout: 'column',
dustin@797:                             border: 0,
dustin@797:                             items: [{
dustin@797:                                 xtype: 'location',
dustin@797:                                 name: 'ortId',
dustin@797:                                 fieldLabel: i18n.getMsg('ortId'),
dustin@797:                                 labelWidth: 80,
dustin@797:                                 allowBlank: true,
dustin@797:                                 forceSelection: true,
dustin@820:                                 editable: false,
dustin@820:                                 columnWidth: '0.75'
dustin@797:                             }, {
dustin@797:                                 xtype: 'button',
dustin@797:                                 name: 'ortIdButton',
dustin@797:                                 margin: '0 0 0 5',
dustin@797:                                 text: i18n.getMsg('messprogrammort.button.title'),
dustin@820:                                 action: 'ort',
dustin@820:                                 columnWidth: '0.25'
dustin@797:                             }]
dustin@757:                         }]
dustin@757:                     }, {
dustin@757:                         border: 0,
dustin@757:                         width: '50%',
dustin@757:                         minWidth: 300,
dustin@757:                         margin: '0, 1, 0, 0',
dustin@757:                         items: [{
dustin@757:                             xtype: 'fset',
dustin@757:                             title: i18n.getMsg('erwAngaben'),
dustin@757:                             name: 'erwAngaben',
dustin@757:                             collapsible: false,
dustin@757:                             collapsed: false,
dustin@757:                             items: [{
dustin@757:                                 xtype: 'datenbasis',
dustin@757:                                 editable: false,
dustin@757:                                 name: 'datenbasisId',
dustin@757:                                 fieldLabel: i18n.getMsg('datenbasisId'),
dustin@757:                                 anchor: '100%',
dustin@757:                                 labelWidth: 105
dustin@757:                             }, {
dustin@757:                                 xtype: 'betriebsart',
dustin@757:                                 name: 'baId',
dustin@757:                                 fieldLabel: i18n.getMsg('baId'),
dustin@757:                                 anchor: '100%',
dustin@757:                                 labelWidth: 105
dustin@757:                             }, {
dustin@757:                                 xtype: 'testdatensatz',
dustin@757:                                 name: 'test',
dustin@757:                                 fieldLabel: i18n.getMsg('test'),
dustin@757:                                 anchor: '100%',
dustin@757:                                 labelWidth: 105,
dustin@757:                                 allowBlank: false
dustin@757:                             }, {
dustin@757:                                 xtype: 'probenart',
dustin@757:                                 editable: false,
dustin@757:                                 name: 'probenartId',
dustin@757:                                 fieldLabel: i18n.getMsg('probenartId'),
dustin@757:                                 anchor: '100%',
dustin@757:                                 labelWidth: 105,
dustin@757:                                 allowBlank: false
dustin@757:                             }, {
dustin@757:                                 xtype: 'numberfield',
dustin@757:                                 allowDecimals: false,
dustin@757:                                 name: 'probeNehmerId',
dustin@757:                                 fieldLabel: i18n.getMsg('probeNehmerId'),
dustin@757:                                 anchor: '100%',
dustin@757:                                 labelWidth: 105
dustin@757:                             }, {
dustin@757:                                 xtype: 'netzbetreiber',
dustin@757:                                 name: 'netzbetreiberId',
dustin@757:                                 editable: false,
dustin@847:                                 readOnly: true,
dustin@757:                                 fieldLabel: i18n.getMsg('netzbetreiberId'),
dustin@757:                                 anchor: '100%',
dustin@757:                                 labelWidth: 105,
dustin@757:                                 allowBlank: false
dustin@757:                            }]
dustin@757:                         }]
dustin@757:                     }]
dustin@757:                 }, {
dustin@757:                     // Medium
dustin@757:                     xtype: 'fieldset',
dustin@757:                     title: i18n.getMsg('medium'),
dustin@757:                     items: [{
dustin@757:                         border: 0,
dustin@757:                         layout: {
dustin@757:                             type: 'vbox',
dustin@757:                             align: 'stretch'
dustin@757:                         },
dustin@757:                         width: '100%',
dustin@757:                         items: [{
dustin@757:                            xtype: 'textfield',
dustin@757:                             maxLength: 38,
dustin@757:                             enforceMaxLength: true,
dustin@757:                             name: 'mediaDesk',
dustin@757:                             labelWidth: 125,
dustin@757:                             fieldLabel: i18n.getMsg('mediaDesk'),
dustin@757:                             regex: new RegExp('(?:D: ){1}(?:[0-9]{2} ){11}[0-9]{2}'),
dustin@757:                             regexText: i18n.getMsg('err.msg.deskriptorvalidation'),
dustin@757:                             listeners: {
dustin@757:                                 dirtychange: {
dustin@757:                                     fn: this.updateOnChange,
dustin@757:                                     scope: me
dustin@757:                                 }
dustin@757:                             }
dustin@757:                         }, {
dustin@757:                             xtype: 'umwelt',
dustin@757:                             name: 'umwId',
dustin@757:                             fieldLabel: i18n.getMsg('umwId'),
dustin@757:                             labelWidth: 125,
dustin@757:                             allowBlank: false,
dustin@757:                             editable: true,
dustin@757:                             listeners: {
dustin@757:                                 dirtychange: {
dustin@757:                                     fn: this.updateOnChange,
dustin@757:                                     scope: me
dustin@757:                                 }
dustin@757:                             }
dustin@757:                         }, {
dustin@757:                             xtype: 'fieldset',
dustin@757:                             title: i18n.getMsg('deskDetails'),
dustin@757:                             collapsible: true,
dustin@757:                             collapsed: true,
dustin@757:                             defaultType: 'textfield',
dustin@757:                             layout: {
dustin@757:                                 type: 'table',
dustin@757:                                 columns: 3
dustin@757:                             },
dustin@757:                             items: this.buildDescriptors(),
dustin@757:                             listeners: {
dustin@757:                                 dirtychange: {
dustin@757:                                     fn: this.updateOnChange,
dustin@757:                                     scope: me
dustin@757:                                 }
dustin@757:                             }
dustin@757:                         }]
dustin@757:                     }]
dustin@757:                 }, {
dustin@757:                     // Zeit
dustin@757:                     xtype: 'fieldset',
dustin@757:                     title: i18n.getMsg('time'),
dustin@757:                     layout: {
dustin@757:                         type: 'hbox',
dustin@757:                         pack: 'center',
dustin@757:                         align: 'stretch'
dustin@757:                     },
dustin@757:                     items: [{
dustin@757:                         xtype: 'fset',
dustin@757:                         title: i18n.getMsg('validity'),
dustin@777:                         name: 'gueltigPeriodFieldset',
dustin@757:                         anchor: '100%',
dustin@757:                         width: '50%',
dustin@757:                         margin: '0, 5, 5, 5',
dustin@757:                         layout: {
dustin@757:                             type: 'vbox',
dustin@757:                             align: 'stretch'
dustin@757:                         },
dustin@757:                         items: [{
dustin@757:                             xtype: 'datetime',
dustin@757:                             fieldLabel: i18n.getMsg('gueltigVon'),
dustin@757:                             labelWidth: 90,
dustin@757:                             anchor: '100%',
dustin@757:                             name: 'gueltigVon',
dustin@793:                             format: 'd.m.Y',
dustin@757:                             period: 'start'
dustin@757:                         }, {
dustin@757:                             xtype: 'datetime',
dustin@757:                             fieldLabel: i18n.getMsg('gueltigBis'),
dustin@757:                             labelWidth: 90,
dustin@757:                             anchor: '100%',
dustin@757:                             name: 'gueltigBis',
dustin@793:                             format: 'd.m.Y',
dustin@757:                             period: 'end'
dustin@774:                         }, {
dustin@774:                             xtype: 'numberfield',
dustin@774:                             fieldLabel: i18n.getMsg('offset'),
dustin@774:                             labelWidth: 90,
dustin@774:                             anchor: '100%',
dustin@786:                             name: 'intervallOffset'
dustin@757:                         }]
dustin@757:                     }, {
dustin@757:                         xtype: 'fset',
dustin@757:                         title: i18n.getMsg('probenintervall'),
dustin@777:                         name: 'probenIntervallFieldset',
dustin@757:                         anchor: '100%',
dustin@757:                         width: '50%',
dustin@757:                         margin: '0, 5, 5, 5',
dustin@757:                         layout: {
dustin@757:                             type: 'vbox',
dustin@757:                             align: 'stretch'
dustin@757:                         },
dustin@757:                         items: [{
dustin@757:                             xtype: 'probenintervall',
dustin@757:                             fieldLabel: i18n.getMsg('probenintervall'),
dustin@757:                             labelWidth: 90,
dustin@757:                             anchor: '100%',
dustin@757:                             name: 'probenintervall'
dustin@757:                         }, {
dustin@757:                             xtype: 'numberfield',
dustin@757:                             fieldLabel: i18n.getMsg('teilintervallVon'),
dustin@757:                             labelWidth: 90,
dustin@757:                             anchor: '100%',
dustin@757:                             name: 'teilintervallVon',
dustin@757:                             period: 'start'
dustin@757:                         }, {
dustin@757:                             xtype: 'numberfield',
dustin@757:                             fieldLabel: i18n.getMsg('teilintervallBis'),
dustin@757:                             labelWidth: 90,
dustin@757:                             anchor: '100%',
dustin@757:                             name: 'teilintervallBis',
dustin@757:                             period: 'end'
dustin@757:                         }, {
dustin@757:                             xtype: 'probenintervallslider',
dustin@757:                             fieldLabel: i18n.getMsg('intervall'),
dustin@757:                             labelWidth: 90,
dustin@757:                             anchor: '100%',
dustin@786:                             values: [0, 0]
dustin@757:                                 //this will be overridden
dustin@757:                                 // by setRecord
dustin@757:                         }]
dustin@757:                     }]
dustin@757:                 }]
dustin@757:             }]
dustin@757:         }];
dustin@757:         this.callParent(arguments);
dustin@757:     },
dustin@757: 
dustin@757:     populateIntervall: function(record, intervall) {
dustin@757:         //intervall is an identifier of a intervall
dustin@757:         // for instance H, M, J, ...
dustin@757:         // Initialize the probenintervallslider
dustin@757:         var s = this.down('probenintervallslider');
dustin@774:         var i = this.getForm().findField('intervallOffset');
dustin@757:         var v = this.getForm().findField('teilintervallVon');
dustin@757:         var b = this.getForm().findField('teilintervallBis');
dustin@757:         var intervallstore = Ext.data.StoreManager.get('Probenintervall');
dustin@757: 
dustin@757:         var svalUpper = null
dustin@757:         var svalLower = null
dustin@759:         var min = null
dustin@759:         var max = null
dustin@757: 
dustin@757:         if (!intervallstore) {
dustin@757:             intervallstore = Ext.create('Lada.store.Probenintervall');
dustin@757:         }
dustin@757: 
dustin@757:         //It is likely that this method was not
dustin@757:         // called from the controller,
dustin@757:         //and the probenintervall was not changed.
dustin@757:         // Load the records in this case
dustin@759:         if (!intervall && record) {
dustin@757:             intervall = record.get('probenintervall',
dustin@757:                     0, false, false, true);
dustin@757: 
dustin@757:             svalUpper = record.get('teilintervallBis');
dustin@757:             svalLower = record.get('teilintervallVon');
dustin@757:         }
dustin@757: 
dustin@757: 
dustin@757:         var intrec = intervallstore
dustin@757:             .findRecord('probenintervall',
dustin@757:                 intervall, 0, false, false, true);
dustin@757: 
dustin@759:         if (intrec) { // in cases when a new messprogramm is
dustin@759:         // created and the discard function is used, intrec will be null
dustin@759:         // consequently the assertion below will fail.
dustin@759:             min = intrec.get('periodstart');
dustin@759:             max = intrec.get('periodend');
dustin@759:         }
dustin@757: 
dustin@757:         if (!svalUpper) {
dustin@757:             svalUpper = max;
dustin@757:         }
dustin@757:         if (!svalLower) {
dustin@757:             svalLower = min;
dustin@757:         }
dustin@757: 
dustin@757:         //Set Teilintervalle
dustin@757:         v.setMinValue(min);
dustin@757:         v.setMaxValue(max);
dustin@757:         b.setMinValue(min);
dustin@757:         b.setMaxValue(max);
dustin@757: 
dustin@757:         //Set Slider
dustin@757:         s.setMinValue(min);
dustin@757:         s.setMaxValue(max);
dustin@757: 
dustin@757:         v.setValue(svalLower);
dustin@757:         b.setValue(svalUpper);
dustin@757: 
dustin@774:         //Set IntervallOffset
dustin@774:         i.setMinValue(0);
dustin@774:         i.setMaxValue(max-1);
dustin@757:     },
dustin@757: 
dustin@757:     setRecord: function(record) {
dustin@757:         this.clearMessages();
dustin@779: 
dustin@757:         this.getForm().loadRecord(record);
dustin@757:         //Set the intervall numberfields and the slider.
dustin@757:         this.down('probenintervallslider').setValue([
dustin@757:             record.get('teilintervallVon'),
dustin@757:             record.get('teilintervallBis')
dustin@757:         ]);
dustin@757: 
dustin@757:         //TODO Set Sliders MinMaxValue
dustin@779:         this.populateIntervall(record);
dustin@757: 
dustin@757:         this.down('probenintervallslider').on(
dustin@757:             'change',
dustin@757:             Lada.app.getController('Lada.controller.form.Messprogramm')
dustin@757:                 .synchronizeFields
dustin@757:         );
raimund@784:     },
dustin@757: 
raimund@784:     setMediaDesk: function(record) {
raimund@784:         var media = record.get('mediaDesk').split(' ');
raimund@784:         this.setMediaSN(0, media);
raimund@784:     },
raimund@784: 
raimund@784:     setMediaSN: function(ndx, media) {
raimund@784:         if (ndx >= 12) {
raimund@784:             return;
raimund@784:         }
raimund@784:         var me = this;
raimund@784:         var current = this.down('deskriptor[layer=' + ndx + ']');
raimund@784:         var cbox = current.down('combobox');
raimund@784:         if (ndx === 0) {
raimund@784:             cbox.store.proxy.extraParams = {
raimund@784:                 'layer': ndx
raimund@784:             };
raimund@784:         }
raimund@784:         else {
raimund@784:             var parents = current.getParents(current.down('combobox'));
raimund@784:             if (parents.length === 0) {
raimund@784:                 return;
raimund@784:             }
raimund@784:             cbox.store.proxy.extraParams = {
raimund@784:                 'layer': ndx,
raimund@784:                 'parents': parents
raimund@784:             };
raimund@784:         }
raimund@784:         cbox.store.load(function(records, op, success) {
raimund@784:             if (!success) {
raimund@784:                 return;
raimund@784:             }
raimund@784:             cbox.select(cbox.store.findRecord('sn', parseInt(media[ndx + 1], 10)));
raimund@784:             me.setMediaSN(++ndx, media);
raimund@784:         });
dustin@757:     },
dustin@757: 
dustin@757:     setMessages: function(errors, warnings) {
dustin@757:         var key;
dustin@757:         var element;
dustin@757:         var content;
dustin@757:         var i18n = Lada.getApplication().bundle;
dustin@757:         if (warnings) {
dustin@757:             for (key in warnings) {
dustin@757:                 element = this.down('component[name=' + key + ']');
dustin@757:                 if (!element) {
dustin@757:                     continue;
dustin@757:                 }
dustin@757:                 content = warnings[key];
dustin@757:                 var warnText = '';
dustin@757:                 for (var i = 0; i < content.length; i++) {
dustin@757:                     warnText += i18n.getMsg(content[i].toString()) + '\n';
dustin@757:                 }
dustin@757:                 element.showWarnings(warnText);
dustin@757:             }
dustin@757:         }
dustin@757:         if (errors) {
dustin@757:             for (key in errors) {
dustin@757:                 element = this.down('component[name=' + key + ']');
dustin@757:                 if (!element) {
dustin@757:                     continue;
dustin@757:                 }
dustin@757:                 content = errors[key];
dustin@757:                 var errorText = '';
dustin@757:                 for (var i = 0; i < content.length; i++) {
dustin@757:                     errorText += i18n.getMsg(content[i].toString()) + '\n';
dustin@757:                 }
dustin@757:                 element.showErrors(errorText);
dustin@757:             }
dustin@757:         }
dustin@757:     },
dustin@757: 
dustin@757:     clearMessages: function() {
dustin@757:         // TODO
dustin@777:         this.down('cbox[name=mstId]').clearWarningOrError();
dustin@777:         this.down('tfield[name=name]').clearWarningOrError();
dustin@777:         //no clearmsg for probeKommentar
dustin@777:         this.down('fset[name=erwAngaben]').clearMessages();
dustin@777:         this.down('cbox[name=datenbasisId]').clearWarningOrError();
dustin@777:         this.down('cbox[name=baId]').clearWarningOrError();
dustin@777:         this.down('cbox[name=test]').clearWarningOrError();
dustin@777:         this.down('cbox[name=probenartId]').clearWarningOrError();
dustin@777:         this.down('cbox[name=netzbetreiberId]').clearWarningOrError();
dustin@777:         //no clear for probeNehmerId
dustin@777:         // Deskriptoren are missing
dustin@777:         this.down('cbox[name=umwId]').clearWarningOrError();
dustin@777:         this.down('fset[name=gueltigPeriodFieldset]').clearMessages();
dustin@777:         this.down('fset[name=probenIntervallFieldset]').clearMessages();
dustin@757:     },
dustin@757: 
dustin@757:     setReadOnly: function(value) {
dustin@757:         // TODO
dustin@757:     },
dustin@757: 
dustin@757:     buildDescriptors: function() {
dustin@757:         var fields = [];
dustin@757:         for (var i = 0; i < 12; i++) {
dustin@757:             fields[i] = {
raimund@784:                 xtype: 'deskriptor',
dustin@757:                 fieldLabel: 'S' + i,
dustin@757:                 labelWidth: 25,
raimund@784:                 width: 190,
raimund@784:                 layer: i,
dustin@757:                 margin: '0, 10, 5, 0'
dustin@757:             };
dustin@757:         }
dustin@757:         return fields;
dustin@757:     }
dustin@757: });