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@1051: 'Lada.view.grid.MessprogrammeList', raimund@1051: 'Lada.view.grid.ProbeList', raimund@1077: 'Lada.view.grid.MessungList', raimund@1077: 'Lada.store.MessungenList', raimund@1015: 'Lada.view.window.FilterManagement', raimund@548: 'Lada.view.widget.Umwelt' raimund@548: ], raimund@548: raimund@548: stores: [ dustin@908: 'ProbenList', // List of found Proben dustin@908: 'MessprogrammeList' //List of found Messprogramme raimund@548: ], raimund@548: raimund@734: displayFields: null, raimund@734: raimund@1015: managementWindow: null, raimund@1015: raimund@1015: mode: 'proben', raimund@1015: 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. dustin@994: 'filterpanel combobox[name=filter]': { dustin@794: // Map Select event dustin@882: select: this.selectSql raimund@548: }, dustin@994: 'filterpanel button[action=search]': { raimund@548: // Map click event on Button. raimund@548: click: this.search raimund@548: }, dustin@994: 'filterpanel button[action=reset]': { raimund@548: // Map click event on Button. raimund@548: click: this.reset raimund@548: }, raimund@1016: 'filterpanel button[action=details]': { raimund@1016: click: this.showDetails raimund@1016: }, raimund@1016: 'filterpanel button[action=manage]': { raimund@1016: click: this.showManagement raimund@1016: }, raimund@1016: 'filterpanel checkbox[name=favorites]': { raimund@1016: change: this.triggerFilterUpdate raimund@1016: }, raimund@1016: 'filterpanel button[action=savedefault]': { raimund@1016: click: this.saveFilterDefaults raimund@1016: }, raimund@1016: 'filterpanel button[action=resetdefault]': { raimund@1016: click: this.resetFilterDefaults raimund@1016: }, 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 dustin@975: * selected search query. dustin@975: * It also replaces the Store of the Filteresultgrid. dustin@975: * Two possibilities exist to do so: Proben/Messprogramme where dynamic columns exist, but the dustin@975: * content remains of the same type and Stammdaten, were columns are fixed but the type might dustin@975: * vary between orte, kategorien, ... raimund@548: */ raimund@548: selectSql: function(element, record) { raimund@1016: var filters = element.up('panel[name=main]').down('panel[name=filtervariables]'); raimund@1016: var filterValues = element.up('panel[name=main]').down('panel[name=filtervalues]'); dustin@979: raimund@1043: var desc = element.up('fieldset').down('displayfield[name=description]'); raimund@1015: if (!record[0]) { raimund@1043: desc.setValue(''); raimund@1015: return; raimund@1015: } dustin@979: // Set "Filter Auswahl" Description dustin@979: desc.setValue(record[0].data.description); dustin@979: raimund@734: this.displayFields = record[0].data.results; raimund@548: var filterFields = record[0].data.filters; dustin@975: var contentPanel = element.up('panel[name=main]').down('panel[name=contentpanel]'); dustin@975: var queryType = record[0].get('type'); //The type of the query, might be proben, messprogramme, dustin@975: // or a stammdatendtype raimund@1015: var details = element.up('panel[name=main]').down('filterdetails'); raimund@1015: details.setRecord(record[0]); raimund@548: raimund@734: this.reset(element); raimund@548: dustin@979: contentPanel.removeAll(); //clear the panel: make space for new grids dustin@979: dustin@979: // Setup Columns dustin@979: if (this.displayFields) { dustin@979: this.displayFields.reverse(); dustin@979: } dustin@975: raimund@1077: if (queryType == 'probe' || raimund@1077: queryType == 'messung' || raimund@1077: queryType == 'messprogramm') { dustin@979: // Dynamic Grids dustin@979: // We need to set both grid and Store. dustin@975: var frgrid; // The Resultgrid dustin@975: var gridstore; // The Store which will be used in the resultgrid. dustin@975: dustin@975: switch (queryType) { dustin@981: case 'probe': dustin@975: gridstore = Ext.create('Lada.store.ProbenList'); raimund@1111: frgrid = Ext.create('Lada.view.grid.ProbeList', { raimund@1111: plugins: [{ raimund@1111: ptype: 'gridrowexpander', raimund@1111: gridType: 'Lada.view.grid.Messung', raimund@1111: gridConfig: { raimund@1111: bottomBar: false raimund@1111: } raimund@1111: }] raimund@1111: }); dustin@975: break; raimund@1077: case 'messung': raimund@1077: gridstore = Ext.create('Lada.store.MessungenList'); raimund@1112: frgrid = Ext.create('Lada.view.grid.MessungList', { raimund@1112: plugins: [{ raimund@1112: ptype: 'gridrowexpander', raimund@1112: gridType: 'Lada.view.grid.Messwert', raimund@1112: gridConfig: { raimund@1112: bottomBar: false raimund@1112: } raimund@1112: }] raimund@1112: }); raimund@1077: break; dustin@981: case 'messprogramm': dustin@975: gridstore = Ext.create('Lada.store.MessprogrammeList'); dustin@975: frgrid = Ext.create('Lada.view.grid.MessprogrammeList'); dustin@975: break; dustin@975: } dustin@975: dustin@975: if (gridstore) { dustin@975: frgrid.setStore(gridstore); dustin@975: } dustin@975: dustin@975: contentPanel.add(frgrid); raimund@548: } dustin@975: else { dustin@975: // Grids which are not build with dynamic columns dustin@979: // The store is configured in each grid, hence we only need to set the dustin@979: // grid dustin@975: var resultGrid; dustin@975: switch (queryType) { dustin@981: case 'messprogrammkategorie': dustin@975: resultGrid = Ext.create('Lada.view.grid.MessprogrammKategorie'); dustin@975: break; dustin@981: case 'datensatzerzeuger': dustin@975: resultGrid = Ext.create('Lada.view.grid.DatensatzErzeuger'); dustin@975: break; dustin@981: case 'ort': dustin@1021: resultGrid = Ext.create('Lada.view.panel.Ort'); dustin@981: break; dustin@981: case 'probenehmer': dustin@981: resultGrid = Ext.create('Lada.view.grid.Probenehmer'); dustin@981: break; dustin@975: } dustin@975: if (resultGrid) { dustin@975: contentPanel.add(resultGrid); dustin@975: } dustin@975: } dustin@975: /* Setup Filters 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@1016: filterValues.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@1056: var multi = filterFields[j].multiselect; raimund@1016: var filterId = filterFields[j].id; raimund@548: var field = null; raimund@1016: var value = filterFields[j].value; raimund@548: if (type === 'text') { raimund@548: field = Ext.create('Ext.form.field.Text', { raimund@548: name: name, raimund@1016: fieldLabel: label, raimund@1016: filterId: filterId, raimund@1016: value: value 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@1016: fieldLabel: label, raimund@1016: filterId: filterId, raimund@1016: value: value 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@1016: fieldLabel: label, raimund@1016: filterId: filterId, raimund@1016: value: value 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@1016: value: value, raimund@1016: filterId: filterId, 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@1016: multiSelect: multi, raimund@1063: forceSelection: false, raimund@1016: filterId: filterId, raimund@1016: value: value 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@1016: value: value, raimund@1016: filterId: filterId, raimund@1063: forceSelection: false, dustin@874: multiSelect: multi, dustin@874: displayTpl: Ext.create('Ext.XTemplate', dustin@874: '{id} ') 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@1063: forceSelection: false, raimund@1016: value: value, raimund@1016: filterId: filterId, 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@1063: forceSelection: false, raimund@1016: value: value, raimund@1016: filterId: filterId, 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@1063: forceSelection: false, raimund@1016: value: value, raimund@1016: filterId: filterId, raimund@548: multiSelect: multi raimund@548: }); raimund@548: } raimund@548: if (field) { raimund@1016: filterValues.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. dustin@975: * To do so it replaces the store of the Resultgrids. raimund@548: */ raimund@548: search: function(element) { dustin@975: var resultGrid = element.up('panel[name=main]').down('panel[name=contentpanel]').down('grid'); raimund@1016: var filters = element.up('panel[name=main]').down('panel[name=filtervalues]'); raimund@548: var search = element.up('fieldset').down('combobox[name=filter]'); raimund@548: raimund@1030: if (!search.getValue()) { raimund@1030: return; raimund@1030: } dustin@975: //Type of the search Proben/Messprogramme/Stammdaten raimund@1016: var type = search.store.getById(search.getValue()).get('type'); dustin@975: 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: dustin@981: //Store depends of the Type... dustin@981: // TODO the switchcasese should be unified withj those in SelectSql dustin@981: switch (type) { dustin@983: case 'probe': dustin@981: sname = 'Lada.store.ProbenList'; dustin@981: break; raimund@1077: case 'messung': raimund@1077: sname = 'Lada.store.MessungenList'; raimund@1077: break; dustin@981: case 'messprogramm': dustin@981: sname = 'Lada.store.MessprogrammeList'; dustin@981: break; dustin@981: case 'messprogrammkategorie': dustin@981: sname = 'Lada.store.MessprogrammKategorie'; dustin@981: break; dustin@981: case 'datensatzerzeuger': dustin@981: sname = 'Lada.store.DatensatzErzeuger'; dustin@981: break; dustin@981: case 'ort': dustin@1004: sname = 'Lada.store.Orte'; dustin@981: break; dustin@981: case 'probenehmer': dustin@981: sname = 'Lada.store.Probenehmer'; dustin@981: break; dustin@975: } 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); raimund@1049: if (type === 'ort') { raimund@1049: var panel = resultGrid.up('ortpanel'); raimund@1049: store.addListener('load', panel.down('map').addLocations, panel.down('map')); raimund@1049: panel.connectListeners(); raimund@1049: } dustin@976: dustin@751: resultGrid.setStore(store); dustin@976: //TODO: Check if this is still necessary, as a Grid exists dustin@976: // for each Type. dustin@976: dustin@976: if (resultGrid.isDynamic) { dustin@976: //only the dynamic resultgrid can and needs to do the following: dustin@976: resultGrid.setupColumns(this.displayFields); dustin@976: } dustin@976: 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@1016: var filters = element.up('panel[name=main]').down('panel[name=filtervalues]'); raimund@1016: var search = element.up('fieldset').down('combobox[name=filter]'); raimund@1016: raimund@1016: //Type of the search Proben/Messprogramme/Stammdaten raimund@1016: var qId = search.getValue(); raimund@1016: var query = search.store.getById(qId); raimund@734: for (var i = filters.items.length - 1; i >= 0; i--) { raimund@734: var filter = filters.items.items[i]; raimund@1016: for (var j = 0; j < query.data.filters.length; j++) { raimund@1016: if (filter.filterId === query.data.filters[j].id) { raimund@1016: filter.setValue(query.data.filters[j].value); raimund@1016: } raimund@844: } 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@1016: }, raimund@1016: raimund@1016: showDetails: function(element) { raimund@1016: var search = element.up('fieldset').down('combobox[name=filter]'); raimund@1016: var details = element.up('panel[name=main]').down('filterdetails'); raimund@1016: raimund@1016: //Type of the search Proben/Messprogramme/Stammdaten raimund@1016: var record = search.store.getById(search.getValue()); raimund@1016: details.setRecord(record); raimund@1016: if (element.pressed) { raimund@1016: details.show(); raimund@1016: } raimund@1016: else { raimund@1016: details.hide(); raimund@1016: } raimund@1016: }, raimund@1016: raimund@1016: showManagement: function() { raimund@1016: if (!this.managementWindow) { raimund@1016: this.managementWindow = Ext.create('Lada.view.window.FilterManagement'); raimund@1016: } raimund@1016: this.managementWindow.show(); raimund@1016: }, raimund@1016: raimund@1016: triggerFilterUpdate: function(checkbox) { raimund@1016: var cbox = checkbox.up('fieldset').down('combobox[name=filter]'); raimund@1016: this.updateFilter(cbox); raimund@1016: }, raimund@1016: raimund@1016: updateFilter: function(combobox) { raimund@1016: var store; raimund@1016: var entries; raimund@1016: var fav = combobox.up('fieldset').down('checkbox[name=favorites]'); raimund@1016: if (this.mode === 'proben') { raimund@1016: store = Ext.StoreManager.get('probequeries'); raimund@1016: } raimund@1016: else if (this.mode === 'messprogramme') { raimund@1016: store = Ext.StoreManager.get('messprogrammqueries'); raimund@1016: } raimund@1016: else if (this.mode === 'stammdaten') { raimund@1016: store = Ext.StoreManager.get('stammdatenqueries'); raimund@1016: } raimund@1077: else if (this.mode === 'messungen') { raimund@1077: store = Ext.StoreManager.get('messungqueries'); raimund@1077: } raimund@1016: else { raimund@1016: return; raimund@1016: } raimund@1048: var allEntries = store.queryBy(function() { raimund@1048: return true; raimund@1048: }); raimund@1048: var favorites = store.queryBy(function(record) { raimund@1048: if (record.get('favorite')) { raimund@1048: return true; raimund@1048: } raimund@1048: }); raimund@1048: combobox.store.removeAll(); raimund@1048: if (fav.checked && favorites.getCount() > 0) { raimund@1048: combobox.store.add(favorites.items); raimund@1048: } raimund@1048: else { raimund@1048: combobox.store.add(allEntries.items); raimund@1048: if (favorites.getCount() === 0) { raimund@1043: fav.setValue(false); raimund@1048: fav.disable(); raimund@1048: } raimund@1048: else { raimund@1048: fav.enable(); raimund@1043: } raimund@1016: } raimund@1016: combobox.select(combobox.store.getAt(0)); raimund@1016: combobox.fireEvent('select', combobox, [combobox.store.getAt(0)]); raimund@1016: }, raimund@1016: raimund@1016: saveFilterDefaults: function(button) { raimund@1016: var filters = button.up('fieldset').down('panel[name=filtervalues]'); raimund@1016: raimund@1016: var search = button.up('fieldset').down('combobox[name=filter]'); raimund@1016: raimund@1016: //Type of the search Proben/Messprogramme/Stammdaten raimund@1016: var qId = search.getValue(); raimund@1016: var query = search.store.getById(qId); raimund@1016: var ndx = 0; raimund@1042: for (var i = filters.items.items.length - 1; i >= 0; i--) { raimund@1016: var filter = filters.items.items[i]; raimund@1016: var filterId = filter.filterId; raimund@1016: var value = filter.getValue(); raimund@1016: if (value instanceof Array) { raimund@1016: value = value.join(','); raimund@1016: } raimund@1016: Ext.Ajax.request({ raimund@1016: url: 'lada-server/rest/filter', raimund@1016: method: 'PUT', raimund@1016: jsonData: { raimund@1016: id: filterId, raimund@1016: value: value raimund@1016: }, raimund@1016: success: function() { raimund@1016: ndx++; raimund@1016: for (var j = 0; j < query.data.filters.length; i++) { raimund@1016: if (query.data.filters[j].id === filterId) { raimund@1016: query.data.filters[j].value = value; raimund@1016: break; raimund@1016: } raimund@1016: } raimund@1042: if (ndx < filters.items.items.length - 1) { raimund@1016: return; raimund@1016: } raimund@1016: if (query.get('type') === 'probe') { raimund@1016: Ext.StoreManager.get('probequeries').load(); raimund@1016: } raimund@1016: else if (query.get('type') === 'messprogramm') { raimund@1016: Ext.StoreManager.get('messprogrammqueries').load(); raimund@1016: } raimund@1077: else if (query.get('type') === 'messung') { raimund@1077: Ext.StoreManager.get('messungqueries').load(); raimund@1077: } raimund@1016: else { raimund@1016: Ext.StoreManager.get('stammdatenqueries').load(); raimund@1016: } raimund@1016: } raimund@1016: }); raimund@1016: } raimund@1016: }, raimund@1016: raimund@1016: resetFilterDefaults: function(button) { raimund@1016: var filters = button.up('fieldset').down('panel[name=filtervalues]'); raimund@1016: raimund@1016: var search = button.up('fieldset').down('combobox[name=filter]'); raimund@1016: raimund@1016: var qId = search.getValue(); raimund@1016: var query = search.store.getById(qId); raimund@1016: for (var i = filters.items.length - 1; i >= 0; i--) { raimund@1016: var filter = filters.items.items[i]; raimund@1016: var filterId = filter.filterId; raimund@1016: var value = filter.getValue(); raimund@1016: if (value instanceof Array) { raimund@1016: value = value.join(','); raimund@1016: } raimund@1016: Ext.Ajax.request({ raimund@1016: url: 'lada-server/rest/filter/' + filterId, raimund@1016: method: 'DELETE', raimund@1016: success: function(response) { raimund@1016: var json = Ext.decode(response.responseText); raimund@1016: if (!json.success) { raimund@1016: return; raimund@1016: } raimund@1016: filter.setValue(json.data.value); raimund@1016: for (var j = 0; j < query.data.filters.length; j++) { raimund@1016: if (query.data.filters[j].id === json.data.id) { raimund@1016: query.data.filters[j].value = json.data.value; raimund@1016: break; raimund@1016: } raimund@1016: } raimund@1016: if (query.get('type') === 'probe') { raimund@1016: Ext.StoreManager.get('probequeries').reload(); raimund@1016: } raimund@1016: else if (query.get('type') === 'messprogramm') { raimund@1016: Ext.StoreManager.get('messprogrammqueries').reload(); raimund@1016: } raimund@1077: else if (query.get('type') === 'messung') { raimund@1077: Ext.StoreManager.get('messungqueries').reload(); raimund@1077: } raimund@1016: else { raimund@1016: Ext.StoreManager.get('stammdatenqueries').reload(); raimund@1016: } raimund@1016: } raimund@1016: }); raimund@1016: } raimund@548: } raimund@548: });