1 /**
  2  * @fileOverview  
  3  * Copyright (c) 2013 Regione Autonoma della Sardegna
  4  * Published under the GPL license.<br>
  5  * See <a href="https://sardegnageoportale.it/webgis/license.txt">https://sardegnageoportale.it/webgis/license.txt</a>
  6  * for the full text of the license.
  7  * @version 0.1
  8  */
  9 
 10 /**
 11  * @namespace framework.plugins
 12  */
 13 Ext.namespace("framework.form");
 14 
 15 /**
 16  * Creates a combo box that handles results from a directory service OpenLS.
 17  *  @name_ GenericWfsComboBox
 18  * @class Creates a combo box that handles results from a directory service OpenLS.
 19  * @constructor
 20  @extends <a target="_blank" href="http://docs.sencha.com/extjs/3.4.0/#!/api/Ext.form.ComboBox">Ext.form.ComboBox</a>
 21  */
 22 
 23 framework.form.GenericWfsComboBox = Ext.extend(Ext.form.ComboBox,
 24         /** 
 25          * @lends framework.form.GenericWfsComboBox.prototype 
 26          */
 27                 {
 28                     /** 
 29                      * relative proxy url.
 30                      * @private
 31                      * @type String
 32                      */
 33                     proxy: null,
 34                     /** 
 35                      * URL template for querying the wms/wfs service
 36                      * @private
 37                      * @type String
 38                      */
 39                     url: null,
 40                     /** 
 41                      * Parameter used to querying the wms/wfs service
 42                      * @private
 43                      * @type String
 44                      */
 45                     layerName: null,
 46                     /** 
 47                      * Parameter used to querying the wms/wfs service
 48                      * @private
 49                      * @type String
 50                      */
 51                     sortByField: null,
 52                     /** 
 53                      *  Override init component
 54                      *  @private 
 55                      */
 56                     initComponent: function() {
 57 
 58                         if (!this.map) {
 59                             var mapPanel = this.findParentBy(function(cmp) {
 60                                 return cmp instanceof GeoExt.MapPanel;
 61                             });
 62                             if (mapPanel)
 63                                 this.map = mapPanel.map;
 64                         }
 65 
 66                         var sUrl = "";
 67                         if (this.proxy)
 68                             sUrl = this.proxy + escape(this.url);
 69                         else
 70                             sUrl = this.url;
 71 
 72                         if (!this.store) {
 73                             this.reader.fields = this.storeFields;
 74                             this.reader.layerName = this.layerName;
 75                             this.store = new Ext.data.Store({
 76                                 proxy: new Ext.data.HttpProxy({
 77                                     url: sUrl,
 78                                     method: 'POST'
 79                                 }),
 80                                 //autoLoad: true,
 81                                 fields: this.storeFields,
 82                                 reader: this.reader,
 83                                 listeners: {
 84                                     load: function() {
 85                                         this.combo.fireEvent('viewReady');
 86                                     },
 87                                     beforeload: function(store, operation) {
 88                                         var queryParam = this.combo.queryParam;
 89                                         store.baseParams[queryParam] = "&typename=" + this.combo.layerName + "&SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature" +
 90                                                 (this.combo.sortByField ? "&sortby=" + this.combo.sortByField : '');
 91                                         var x = 0;
 92                                     }
 93                                 },
 94                                 baseParams: {
 95                                     'xmlRequest': ''
 96                                 }
 97                             });
 98                             this.store.combo = this;
 99                         }
100                         ;
101 
102                         this.on({
103                             select: this.handleSelect,
104                             scope: this
105                         });
106                         return framework.form.GenericWfsComboBox.superclass.initComponent.apply(this, arguments);
107                    },
108                     /** 
109                      * Zoom to the selected location, and also draw a poligon on the extent location
110                      * @private
111                      * @param {Object} combo comnoBox object.
112                      * @param {Object} record selected record.
113                      * @param {Object} idx index of selected record.
114                      */
115                     handleSelect: function(combo, record, index) {
116                         var bound, extent;
117                         if (record.data.POLIGONO instanceof OpenLayers.Bounds) {
118                             bound = record.data.POLIGONO;
119                         } else {
120                             bound = new OpenLayers.Bounds(record.data.POLIGONO.left, record.data.POLIGONO.bottom, record.data.POLIGONO.right, record.data.POLIGONO.top);
121                         }
122                         extent = bound.transform(
123                                 new OpenLayers.Projection("EPSG:3003"),
124                                 this.map.getProjectionObject()
125                                 );
126                         this.map.zoomToExtent(extent);
127                     }
128                 });
129 
130         Ext.reg("framework_genericwfscombobox", framework.form.GenericWfsComboBox);