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:  * Controller for the Filter
raimund@548:  * This controller handles all logic related to the filter
raimund@548:  */
raimund@548: Ext.define('Lada.controller.Filter', {
raimund@548:     extend: 'Ext.app.Controller',
raimund@548: 
raimund@548:     requires: [
raimund@548:         'Lada.view.widget.Messstelle',
raimund@548:         'Lada.view.widget.Umwelt'
raimund@548:     ],
raimund@548: 
raimund@548:     stores: [
raimund@609:         'ProbenList'    // List of found Proben
raimund@548:     ],
raimund@548: 
raimund@734:     displayFields: null,
raimund@734: 
dustin@742:     /**
dustin@742:      * Initialize this Controller
dustin@742:      * It has 4 Listeners
dustin@742:      */
raimund@548:     init: function() {
raimund@548:         this.control({
raimund@548:             // CSS like selector to select element in the viewport. See
raimund@548:             // ComponentQuery documentation for more details.
raimund@548:             'combobox[name=filter]': {
dustin@794:                 // Map Select event
dustin@794:                 select: this.selectSql,
raimund@548:             },
raimund@548:             'button[action=search]': {
raimund@548:                 // Map click event on Button.
raimund@548:                 click: this.search
raimund@548:             },
raimund@548:             'button[action=reset]': {
raimund@548:                 // Map click event on Button.
raimund@548:                 click: this.reset
raimund@548:             },
raimund@548:             'menuitem[action=about]': {
raimund@548:                 // Map click event on Button.
raimund@548:                 click: this.about
raimund@548:             }
raimund@548:         });
raimund@548:         this.callParent(arguments);
raimund@548:     },
raimund@548: 
raimund@548:     /**
raimund@548:      * Function called when the user selects a SQL query in the dropdownlist.
raimund@548:      * The function will hide/display additional element related to the
raimund@548:      * selected search query
raimund@548:      */
raimund@548:     selectSql: function(element, record) {
raimund@548:         var filters = element.up('panel[name=main]').down('fieldset[name=filtervariables]');
raimund@548:         var columns = element.up('fieldset').down('displayfield[name=columns]');
raimund@548:         var desc = element.up('fieldset').down('displayfield[name=description]');
raimund@734:         this.displayFields = record[0].data.results;
raimund@548:         var filterFields = record[0].data.filters;
raimund@548: 
raimund@734:         this.reset(element);
raimund@548: 
raimund@548:         var columnString = [];
raimund@734:         for (var i = 0; i < this.displayFields.length; i++) {
raimund@734:             columnString.push(this.displayFields[i].header);
raimund@548:         }
raimund@548:         columns.setValue(columnString.join(', '));
raimund@548:         desc.setValue(record[0].data.description);
raimund@548: 
raimund@548:         // Setup Columns of the probenlist
raimund@734:         this.displayFields.reverse();
raimund@548: 
dustin@742:         /* Setup Filters of the probenlist
dustin@742:          *
dustin@742:          * Allowed types are
dustin@742:          * * text
dustin@742:          * * number
dustin@742:          * * datetime
dustin@742:          * * bool
dustin@742:          * * listmst
dustin@742:          * * listumw
dustin@742:          * * listver
dustin@742:          * * listdbasis
dustin@742:          * * listnetz
dustin@742:          *
dustin@742:          * Iterate over all configured filters and add filters dynamically
dustin@742:          *
dustin@742:          *  1. Empty filters
dustin@742:          */
raimund@548:         filters.removeAll();
raimund@548:         var hide = true;
dustin@742:         /* 2. Iterate over all configured filters */
raimund@548:         var j;
raimund@548:         for (j = 0; j < filterFields.length; j++) {
raimund@548:             var type = filterFields[j].type;
raimund@548:             var name = filterFields[j].dataIndex;
raimund@548:             var label = filterFields[j].label;
raimund@548:             var multi = filterFields[j].multiSelect;
raimund@548:             var field = null;
raimund@548:             if (type === 'text') {
raimund@548:                 field = Ext.create('Ext.form.field.Text', {
raimund@548:                     name: name,
raimund@548:                     fieldLabel: label
raimund@548:                 });
raimund@548:             }
raimund@548:             else if (type === 'number') {
raimund@548:                 field = Ext.create('Ext.form.field.Number', {
raimund@548:                     name: name,
raimund@548:                     labelWidth: 135,
raimund@548:                     fieldLabel: label
raimund@548:                 });
raimund@548:             }
raimund@548:             else if (type === 'datetime') {
raimund@575:                 field = Ext.create('Lada.view.widget.Datetime', {
raimund@548:                     name: name,
raimund@548:                     labelWidth: 135,
raimund@548:                     fieldLabel: label
raimund@548:                 });
raimund@548:             }
raimund@548:             else if (type === 'bool') {
raimund@575:                 field = Ext.create('Lada.view.widget.Testdatensatz', {
raimund@548:                     name: name,
raimund@548:                     labelWidth: 135,
raimund@548:                     fieldLabel: label,
raimund@548:                     emptyText: ''
raimund@548:                 });
raimund@548:             }
raimund@548:             else if (type === 'listmst') {
raimund@575:                 field = Ext.create('Lada.view.widget.Messstelle', {
raimund@548:                     name: name,
raimund@548:                     labelWidth: 135,
raimund@548:                     fieldLabel: label,
raimund@548:                     multiSelect: multi
raimund@548:                 });
raimund@548:             }
raimund@548:             else if (type === 'listumw') {
raimund@575:                 field = Ext.create('Lada.view.widget.Umwelt', {
raimund@548:                     name: name,
raimund@548:                     labelWidth: 135,
raimund@548:                     fieldLabel: label,
raimund@548:                     multiSelect: multi
raimund@548:                 });
raimund@548:             }
raimund@548:             else if (type === 'listdbasis') {
raimund@575:                 field = Ext.create('Lada.view.widget.Datenbasis', {
raimund@548:                     name: name,
raimund@548:                     labelWidth: 135,
raimund@548:                     fieldLabel: label,
raimund@548:                     multiSelect: multi
raimund@548:                 });
raimund@548:             }
raimund@548:             else if (type === 'listver') {
raimund@575:                 field = Ext.create('Lada.view.widget.Verwaltungseinheit', {
raimund@548:                     name: name,
raimund@548:                     labelWidth: 135,
raimund@548:                     fieldLabel: label,
raimund@548:                     multiSelect: multi
raimund@548:                 });
raimund@548:             }
raimund@548:             else if (type === 'listnetz') {
raimund@575:                 field = Ext.create('Lada.view.widget.Netzbetreiber', {
raimund@548:                     name: name,
raimund@548:                     labelWidth: 135,
raimund@548:                     fieldLabel: label,
raimund@548:                     multiSelect: multi
raimund@548:                 });
raimund@548:             }
raimund@548:             if (field) {
raimund@548:                 filters.add(field);
raimund@548:                 filters.show();
raimund@548:                 hide = false;
raimund@548:             }
raimund@548:         }
raimund@548:         if (hide) {
raimund@548:             filters.hide();
raimund@548:         }
raimund@548:     },
raimund@548: 
raimund@548:     /**
raimund@548:      * Function is called when the user clicks the search button. The function
raimund@548:      * will perform a search to the server on refreshes the result list.
raimund@548:      */
raimund@548:     search: function(element) {
raimund@548:         var resultGrid = element.up('panel[name=main]').down('filterresultgrid');
raimund@548:         var filters = element.up('panel[name=main]').down('fieldset[name=filtervariables]');
raimund@548:         var search = element.up('fieldset').down('combobox[name=filter]');
raimund@548: 
raimund@548:         // Get search parameters:
raimund@548:         var searchParams = {};
raimund@548:         searchParams['qid'] = search.getValue();
raimund@548:         for (var i = filters.items.length - 1; i >= 0; i--) {
raimund@548:             var filter = filters.items.items[i];
raimund@548:             var value = filter.getValue();
raimund@548:             if (value instanceof Array) {
raimund@548:                 value = value.join(',');
raimund@548:             }
raimund@548:             searchParams[filter.getName()] = value;
raimund@548:         }
dustin@751:         // Retrieve the mode
dustin@752:         var modes = element.up('panel[name=main]').down('radiogroup').getChecked();
dustin@751:         var sname = modes[0].inputValue;
dustin@751: 
dustin@792:         if (sname === 'ProbeList') {
dustin@751:             sname = 'Lada.store.ProbenList';
dustin@751:         }
dustin@792:         else if (sname === 'MessprogrammList') {
dustin@751:             sname = 'Lada.store.MessprogrammeList';
dustin@751:         }
dustin@751: 
dustin@751:         // Find the store or create a new one.
dustin@751:         var store = Ext.StoreManager.lookup(sname);
dustin@751:         if (!store) {
dustin@751:             store = Ext.create(sname);
dustin@751:         }
dustin@751:         if (store) {
dustin@810:             store.addListener('beforeload', this.loadingAnimationOn, resultGrid);
dustin@810:             store.addListener('load', this.loadingAnimationOff, resultGrid);
dustin@751:             resultGrid.setStore(store);
dustin@751:             resultGrid.setupColumns(this.displayFields);
dustin@751:             resultGrid.getStore().proxy.extraParams = searchParams;
dustin@751:             resultGrid.getStore().load();
dustin@751:             resultGrid.show();
dustin@751:         }
raimund@548:     },
dustin@810: 
dustin@810:     /**
dustin@810:      * Enable the Loading Animation of the Grid.
dustin@810:      */
dustin@810:     loadingAnimationOn: function(store, operation) {
dustin@810:         // this = resultgrid because of the scope which was set in addListener
dustin@810:         this.setLoading(true);
dustin@810:     },
dustin@810: 
dustin@810:     /**
dustin@810:      * Disable the Loading Animation of the Grid.
dustin@810:      */
dustin@810:     loadingAnimationOff: function(store, operation) {
dustin@810:         // this = resultgrid because of the scope which was set in addListener
dustin@810:         this.setLoading(false);
dustin@810:     },
dustin@810: 
dustin@742:     /**
dustin@742:      * This function resets the filters
dustin@742:      */
raimund@734:     reset: function(element) {
raimund@734:         var filters = element.up('panel[name=main]').down('fieldset[name=filtervariables]');
raimund@734:         for (var i = filters.items.length - 1; i >= 0; i--) {
raimund@734:             var filter = filters.items.items[i];
raimund@734:             filter.clearValue();
raimund@734:         }
raimund@548:     },
dustin@742:     /**
dustin@742:      * This Function is supposed to handle the About action
dustin@742:      * It has no function yet.
dustin@742:      */
raimund@548:     about: function() {
dustin@800:         var win = Ext.create('Lada.view.window.About');
dustin@800:         win.show();
raimund@548:     }
raimund@548: });