dustin@975: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
dustin@975:  * Software engineering by Intevation GmbH
dustin@975:  *
dustin@975:  * This file is Free Software under the GNU GPL (v>=3)
dustin@975:  * and comes with ABSOLUTELY NO WARRANTY! Check out
dustin@975:  * the documentation coming with IMIS-Labordaten-Application for details.
dustin@975:  */
dustin@975: 
dustin@975: /**
dustin@975:  * Grid to list the result of the Filter
dustin@975:  */
dustin@975: Ext.define('Lada.view.widget.DynamicGrid', {
dustin@975:     extend: 'Ext.grid.Panel',
dustin@975: 
dustin@975:     store: null,
dustin@975: 
dustin@980:     border: false,
dustin@975:     multiSelect: true,
dustin@976:     allowDeselect: true,
dustin@976: 
dustin@976:     isDynamic: true,
dustin@975: 
dustin@975:     viewConfig: {
dustin@975:         deferEmptyText: false
dustin@975:     },
dustin@975: 
dustin@975:     initComponent: function() {
dustin@975:         var i18n = Lada.getApplication().bundle;
dustin@975:         this.callParent(arguments);
dustin@975:     },
dustin@975: 
dustin@975:     /**
dustin@975:      * This sets the Store of the DynamicGrid
dustin@975:      */
dustin@975:     setStore: function(store){
dustin@975:         var i18n = Lada.getApplication().bundle;
dustin@975: 
dustin@975:         this.reconfigure(store);
dustin@1021:         var ptbar = this.down('pagingtoolbar');
dustin@1021:         if (ptbar) {
dustin@1021:             this.removeDocked(ptbar);
dustin@1021:         }
dustin@1021: 
dustin@975:         this.addDocked([{
dustin@975:             xtype: 'pagingtoolbar',
dustin@975:             dock: 'bottom',
dustin@975:             store: store,
dustin@975:             displayInfo: true
dustin@975:         }]);
dustin@975: 
dustin@975:     },
dustin@975: 
dustin@975:     /**
dustin@975:      * Setup columns of the Grid dynamically based on a list of given cols.
dustin@975:      * The function is called from the {@link Lada.controller.Filter#search
dustin@975:      * search event}
dustin@975:      * The Images for the Read-Write Icon are defined in CSS
dustin@975:      */
dustin@975:     setupColumns: function(cols) {
dustin@975:         var caf = this.generateColumnsAndFields(cols);
dustin@975:         var columns = caf[0];
dustin@975:         var fields = caf[1];
dustin@975:         this.store.model.setFields(fields);
dustin@975:         this.reconfigure(this.store, columns);
dustin@975:     },
dustin@975: 
dustin@975:     /**
dustin@975:      * generateColumnsAndFields
dustin@975:      * generates an array of columns which are used for the dynamic grid
dustin@975:      * @return an array of two arrays: [0] is an array of colums [1] an array
dustin@975:      *   of fields
dustin@975:      **/
raimund@1077:     generateColumnsAndFields: function(cols) {
dustin@975:         var resultColumns = [];
dustin@975:         var fields = [];
dustin@975: 
dustin@975:         fields.push(new Ext.data.Field({
dustin@975:             name: 'owner'
dustin@975:         }));
dustin@975:         fields.push(new Ext.data.Field({
dustin@975:             name: 'readonly'
dustin@975:         }));
raimund@1082:         fields.push(new Ext.data.Field({
raimund@1082:             name: 'statusEdit'
raimund@1082:         }));
raimund@1111:         fields.push(new Ext.data.Field({
raimund@1111:             name: 'id'
raimund@1111:         }));
dustin@975: 
dustin@975:         resultColumns.push({
dustin@975:             xtype: 'actioncolumn',
dustin@975:             text: 'RW',
dustin@975:             dataIndex: 'readonly',
dustin@975:             sortable: false,
dustin@975:             tooltip: 'Probe öffnen',
dustin@975:             width: 30,
dustin@975:             getClass: function (val, meta, rec) {
raimund@1082:                 if (rec.get('readonly') === false &&
raimund@1082:                     rec.get('owner') === true &&
raimund@1082:                     !rec.get('statusEdit')) {
raimund@1082:                     console.log('edit' + rec.get('statusEdit'));
dustin@1005:                         return 'edit';
dustin@1005:                 }
raimund@1082:                 else if (rec.get('readonly') === false &&
raimund@1082:                     rec.get('owner') === true &&
raimund@1082:                     rec.get('statusEdit')) {
raimund@1082:                         return 'editstatus';
dustin@1005:                 }
raimund@1082:                 else if (rec.get('readonly') === true &&
raimund@1082:                     rec.get('statusEdit')) {
raimund@1082:                         return 'noeditstatus';
raimund@1082:                 }
raimund@1082:                 return 'noedit';
dustin@975:             },
dustin@975:             handler: function(grid, rowIndex, colIndex) {
dustin@975:                 var rec = grid.getStore().getAt(rowIndex);
dustin@975:                 grid.fireEvent('itemdblclick', grid, rec);
dustin@975:              }
dustin@975:         });
dustin@975: 
dustin@975:         for (var i = cols.length - 1; i >= 0; i--) {
raimund@1077:             fields.push(new Ext.data.Field({
raimund@1077:                 name: cols[i].dataIndex
raimund@1077:             }));
raimund@1077:             if (cols[i] === 'id' || cols[i].dataIndex === 'probeId') {
dustin@975:                 continue;
dustin@975:             }
dustin@975:             resultColumns.push(cols[i]);
dustin@975:         }
dustin@975:         var caf = new Array();
dustin@975:         caf[0] = resultColumns;
dustin@975:         caf[1] = fields;
dustin@975:         return caf;
dustin@975:      }
dustin@975: });
dustin@975: