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: 
dustin@893: /**
raimund@548:  * Grid to list Probenzusatzwerte
raimund@548:  */
raimund@548: Ext.define('Lada.view.grid.Probenzusatzwert', {
raimund@548:     extend: 'Ext.grid.Panel',
raimund@548:     alias: 'widget.probenzusatzwertgrid',
raimund@548:     requires: [
tom@1119:         'Lada.view.form.ExpNumberField',
raimund@548:         'Lada.view.widget.Probenzusatzwert'
raimund@548:     ],
raimund@548: 
raimund@548:     maxHeight: 350,
raimund@548:     emptyText: 'Keine Zusatzwerte gefunden.',
raimund@548:     minHeight: 110,
raimund@548:     viewConfig: {
raimund@548:         deferEmptyText: false
raimund@548:     },
raimund@548:     margin: '0, 5, 5, 5',
raimund@548: 
raimund@548:     recordId: null,
dustin@823:     readOnly: true,
dustin@823:     allowDeselect: true,
raimund@548: 
raimund@548:     initComponent: function() {
raimund@571:         this.rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
raimund@548:             clicksToMoveEditor: 1,
dustin@683:             autoCancel: false,
dustin@703:             disabled: false,
dustin@684:             pluginId: 'rowedit',
dustin@683:             listeners:{
dustin@683:                 // Make row ineditable when readonly is set to true
dustin@683:                 // Normally this would belong into a controller an not the view.
dustin@702:                 // But the RowEditPlugin is not handled there.
dustin@683:                 beforeedit: function(e, o) {
dustin@702:                     var readonlywin = o.grid.up('window').record.get('readonly');
dustin@702:                     var readonlygrid = o.record.get('readonly');
dustin@703:                     if (readonlywin == true || readonlygrid == true || this.disabled)  {
dustin@683:                         return false;
dustin@683:                     }
dustin@683:                     return true;
dustin@683:                 }
dustin@683:             }
raimund@548:         });
raimund@571:         this.plugins = [this.rowEditing];
raimund@548:         this.dockedItems = [{
raimund@548:             xtype: 'toolbar',
raimund@548:             dock: 'bottom',
raimund@548:             items: ['->', {
raimund@548:                 text: 'Hinzufügen',
raimund@548:                 icon: 'resources/img/list-add.png',
raimund@548:                 action: 'add',
raimund@548:                 probeId: this.probeId
raimund@548:             }, {
raimund@548:                 text: 'Löschen',
raimund@548:                 icon: 'resources/img/list-remove.png',
raimund@548:                 action: 'delete'
raimund@548:             }]
raimund@548:         }];
raimund@548:         this.columns = [{
raimund@548:             header: 'PZW-ID',
dustin@1271:             dataIndex: 'pzsId',
dustin@718:             flex: 1
raimund@548:         }, {
raimund@548:             header: 'PZW-Größe',
raimund@548:             dataIndex: 'pzsId',
dustin@625:             flex: 3,
raimund@548:             renderer: function(value) {
raimund@571:                 if (!value || value === '') {
raimund@571:                     return '';
raimund@571:                 }
raimund@548:                 var store = Ext.data.StoreManager.get('probenzusaetze');
raimund@548:                 var record = store.getById(value);
raimund@548:                 return record.get('beschreibung');
raimund@548:             },
raimund@548:             editor: {
raimund@559:                 xtype: 'combobox',
raimund@559:                 store: Ext.data.StoreManager.get('probenzusaetze'),
raimund@559:                 displayField: 'beschreibung',
raimund@559:                 valueField: 'id',
dustin@665:                 allowBlank: false,
dustin@665:                 editable: false
raimund@548:             }
raimund@548:         }, {
raimund@548:             header: 'Messwert',
raimund@559:             dataIndex: 'messwertPzs',
dustin@625:             flex: 1,
raimund@559:             editor: {
mstanko@1280:                 xtype: 'expnumberfield'
tom@1119:             },
tom@1119:             renderer: function(value) {
tom@1119:                 if (!value || value === '') {
tom@1119:                     return value;
tom@1119:                 }
tom@1123:                 var strValue = value.toExponential(2).toString()
tom@1123:                     .replace('.', Ext.util.Format.decimalSeparator);
tom@1123:                 var splitted = strValue.split('e');
tom@1123:                 var exponent = parseInt(splitted[1]);
tom@1123:                 return splitted[0] + 'e'
tom@1123:                     + ((exponent < 0) ? '-' : '+')
tom@1123:                     + ((Math.abs(exponent) < 10) ? '0' : '')
tom@1123:                     + Math.abs(exponent).toString();
dustin@665:             }
dustin@665:         }, {
dustin@665:             header: '< NWG',
dustin@665:             flex: 1,
dustin@665:             renderer: function(value, meta, record) {
dustin@665:                 var nwg = record.get('nwgZuMesswert');
dustin@665:                 var mw = record.get('messwertPzs');
dustin@665:                 if ( mw < nwg) {
dustin@665:                     return '<';
dustin@665:                 }
dustin@665:                 return '';
dustin@665:             }
dustin@665:         }, {
dustin@665:             header: 'Nachweisgrenze',
dustin@665:             dataIndex: 'nwgZuMesswert',
dustin@665:             flex: 1,
dustin@665:             editor: {
mstanko@1280:                 xtype: 'expnumberfield'
tom@1119:             },
tom@1119:             renderer: function(value) {
tom@1119:                 if (!value || value === '') {
tom@1119:                     return value;
tom@1119:                 }
tom@1123:                 var strValue = value.toExponential(2).toString()
tom@1123:                     .replace('.', Ext.util.Format.decimalSeparator);
tom@1123:                 var splitted = strValue.split('e');
tom@1123:                 var exponent = parseInt(splitted[1]);
tom@1123:                 return splitted[0] + 'e'
tom@1123:                     + ((exponent < 0) ? '-' : '+')
tom@1123:                     + ((Math.abs(exponent) < 10) ? '0' : '')
tom@1123:                     + Math.abs(exponent).toString();
raimund@559:             }
raimund@559:         }, {
raimund@559:             header: 'Maßeinheit',
raimund@559:             dataIndex: 'pzsId',
dustin@625:             flex: 1,
raimund@548:             renderer: function(value) {
raimund@571:                 if (!value || value === '') {
raimund@571:                     return '';
raimund@571:                 }
raimund@559:                 var zstore = Ext.data.StoreManager.get('probenzusaetze');
raimund@559:                 var mstore = Ext.data.StoreManager.get('messeinheiten');
tom@1217:                 var mehId = zstore.getById(value).get('messEinheitId');
dustin@723:                 var record = mstore.findRecord('id', mehId, 0, false, false, true);
tom@1217:                 if (!record) {
tom@1217:                     return '';
tom@1217:                 }
raimund@559:                 return record.get('einheit');
raimund@559:             }
raimund@559:         }, {
raimund@548:             header: 'rel. Unsich.[%]',
raimund@548:             dataIndex: 'messfehler',
dustin@625:             xtype: 'numbercolumn',
dustin@625:             format: '0',
dustin@625:             flex: 1,
raimund@548:             editor: {
dustin@625:                 xtype: 'numberfield',
dustin@665:                 allowBlank: false,
dustin@665:                 maxLength: 3,
dustin@665:                 enforceMaxLength: true,
dustin@665:                 allowExponential: false,
dustin@665:                 allowDecimal: false
dustin@625:             }
raimund@548:         }];
dustin@823:         this.listeners = {
dustin@823:            select: {
dustin@823:                fn: this.activateRemoveButton,
dustin@823:                scope: this
dustin@823:             },
dustin@823:             deselect: {
dustin@823:                 fn: this.deactivateRemoveButton,
dustin@823:                 scope: this
dustin@823:             }
dustin@823:         };
raimund@548:         this.initData();
raimund@548:         this.callParent(arguments);
dustin@823:         this.setReadOnly(true); //Grid is always initialised as RO
raimund@548:     },
raimund@548: 
raimund@548:     initData: function() {
raimund@548:         this.store = Ext.create('Lada.store.Zusatzwerte');
raimund@548:         this.store.load({
raimund@548:             params: {
raimund@548:                 probeId: this.recordId
raimund@548:             }
raimund@548:         });
dustin@684:     },
dustin@684: 
dustin@684:     setReadOnly: function(b) {
raimund@1034:         this.readOnly = b;
raimund@1034:         if (b) {
dustin@684:             //Readonly
raimund@1035:             if (this.getPlugin('rowedit')) {
dustin@684:                 this.getPlugin('rowedit').disable();
dustin@684:             }
dustin@684:             this.down('button[action=delete]').disable();
dustin@684:             this.down('button[action=add]').disable();
raimund@1035:         }
raimund@1035:         else {
dustin@684:             //Writable
raimund@1035:             if (this.getPlugin('rowedit')) {
dustin@684:                 this.getPlugin('rowedit').enable();
dustin@684:             }
dustin@824:             //this.down('button[action=delete]').enable();
dustin@684:             this.down('button[action=add]').enable();
dustin@684:         }
dustin@823:     },
dustin@823:     /**
dustin@823:      * Activate the Remove Button
dustin@823:      */
dustin@823:     activateRemoveButton: function(selection, record) {
dustin@823:         var grid = this;
dustin@823:         //only enable the remove buttone, when the grid is editable.
dustin@823:         if (! grid.readOnly) {
dustin@823:             grid.down('button[action=delete]').enable();
dustin@823:         }
dustin@823:     },
dustin@823:     /**
dustin@823:      * Activate the Remove Button
dustin@823:      */
dustin@823:     deactivateRemoveButton: function(selection, record) {
dustin@823:         var grid = this;
dustin@823:         //only enable the remove buttone, when the grid is editable.
dustin@823:         if (! grid.readOnly) {
dustin@823:             grid.down('button[action=delete]').disable();
dustin@823:         }
raimund@548:     }
dustin@823: 
raimund@548: });