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. By
 17  *  default it uses Directory OpenLS service by Regione Autonoma della Sardegna (http://www.sardegnageoportale.it/index.php?xsl=1598&s=203310&v=2&c=9869&t=1), but it can be configured with a custom store
 18  *  to use other services standard OpenLS. If the user enters a valid address in the search
 19  *  box, the combo's store will be populated with records that match the
 20  *  address.
 21  *  @name_ GeoNamesSearchCombo
 22  * @class Creates a combo box that handles results from a directory service OpenLS.
 23  * @constructor
 24  @extends <a target="_blank" href="http://docs.sencha.com/extjs/3.4.0/#!/api/Ext.form.ComboBox">Ext.form.ComboBox</a>
 25  */
 26 
 27 framework.form.GeoNamesSearchCombo = Ext.extend(Ext.form.ComboBox,
 28 /** 
 29  * @lends framework.form.GeoNamesSearchCombo.prototype 
 30  */
 31 {
 32  
 33     /**
 34     * A configured map or a configuration object
 35     *  for the map constructor, required only if :attr:`zoom` is set to
 36     *  value greater than or equal to 0.
 37     * @public
 38     * @type OpenLayers.Map or Object
 39     */
 40     map: null,
 41 
 42     /**
 43     * See http://www.dev.sencha.com/deploy/dev/docs/source/BoxComponent.html#cfg-Ext.BoxComponent-width,
 44     * default value is 240.
 45     * @public
 46     * @type Number
 47     */                   
 48     width: 350,
 49 
 50     /**
 51     * See http://www.dev.sencha.com/deploy/dev/docs/source/Combo.html#cfg-Ext.form.ComboBox-listWidth,
 52     * default value is 350.
 53     * @public
 54     * @type Number
 55     */
 56     listWidth: 350,
 57 
 58     /**
 59     * See http://www.dev.sencha.com/deploy/dev/docs/source/Combo.html#cfg-Ext.form.ComboBox-loadingText,
 60     * default value is "Search in Geonames...".
 61     * @public
 62     * @type String
 63     */
 64     loadingText: 'Search in Geonames...',
 65 
 66     /**
 67     * Text to display for an empty field
 68     * default value is "Search location in Geonames".
 69     * @public
 70     * @type String
 71     */
 72     emptyText: 'Search location in Geonames',
 73 
 74     /** 
 75     * The minimum zoom level to use when zooming to a location.
 76     * Not used when zooming to a bounding box. 
 77     * Default is 12.
 78     * @public
 79     * @type number
 80     */
 81     zoom: 8,
 82     
 83     /** 
 84     * Minimum number of characters to be typed before search occurs
 85     * Defaults to 1
 86     * @public
 87     * @type number
 88     */
 89     minChars: 1,
 90 
 91     /** 
 92     * Delay before the search occurs.
 93     * Default is 50ms.
 94     * @public
 95     * @type number
 96     */
 97     queryDelay: 50,
 98 
 99     /** 
100     * See: http://www.geonames.org/export/geonames-search.html
101     * The maximum number of rows in the responses, defaults to 20,
102     * maximum allowed value is 1000
103     * @public
104     * @type String
105     */
106     maxRows: '20',
107 
108     /**
109     * see http://www.extjs.com/deploy/dev/docs/output/Ext.XTemplate.html 
110     * Template for presenting the result in the list, 
111     * if not set a default value is provided
112     * @private
113     * @type Ext.XTemplate
114     */
115     tpl: '<tpl for="."><div class="x-combo-list-item"><h1>{name}<br></h1>{fcodeName} - {countryName}</div></tpl>',
116 
117     /** 
118     * See: http://www.geonames.org/export/geonames-search.html
119     * Place name and country name will be returned in the specified language
120     * Default is Italian (it). 
121     * @private
122     * @type String
123     */
124     lang: 'it',
125 
126     /** 
127     * See: http://www.geonames.org/export/geonames-search.html
128     * Country in which to make a GeoNames search, default is all countries.
129     * Providing several countries can be done like: countryString: country=FR&country=GP
130     * @private
131     * @type String
132     */
133     countryString: '',
134 
135     /** 
136     * See: http://www.geonames.org/export/geonames-search.html
137     * Restricts the search for toponym of the given continent
138     * Default is all continents.
139     * @private
140     * @type String
141     */
142     continentCode: 'EU',
143     
144     /** 
145     * See: http://www.geonames.org/export/geonames-search.html
146     * Code of administrative subdivision, default is all administrative subdivisions.
147     * @private
148     * @type String
149     */
150     adminCode1: '',
151 
152     /** 
153     * See: http://www.geonames.org/export/geonames-search.html
154     * Code of administrative subdivision, default is all administrative subdivisions.
155     * @private
156     * @type String
157     */
158     adminCode2: '',
159 
160    /** 
161     * See: http://www.geonames.org/export/geonames-search.html
162     * Code of administrative subdivision, default is all administrative subdivisions.
163     * @private
164     * @type String
165     */
166     adminCode3: '',
167 
168     /** 
169     * See: http://www.geonames.org/export/geonames-search.html
170     * Feature classes in which to make a GeoNames search, default is all feature classes.
171     * Providing several feature classes can be done with
172     * ``featureClassString: "featureClass=P&featureClass=A"``
173     * @private
174     * @type String
175     */
176     featureClassString: '',
177 
178     /** 
179     * See: http://www.geonames.org/export/geonames-search.html
180     * Feature code in which to make a GeoNames search, default is all feature codes.
181     * Providing several feature codes can be done with
182     * ``featureCodeString: "featureCode=PPLC&featureCode=PPLX"``
183     * @private
184     * @type String
185     */
186     featureCodeString: '',
187     
188     /** 
189     * See: http://www.geonames.org/export/geonames-search.html
190     * Search for toponyms tagged with the specified tag, default is all tags.
191     * @private
192     * @type String
193     */
194     tag: '',
195 
196     /** 
197     * See: http://www.geonames.org/export/geonames-search.html
198     * Defines the encoding used for the document returned by the web service, defaults to 'UTF8'.
199     * @private
200     * @type String
201     */
202     charset: 'UTF8',
203 
204     /** 
205     * Hide trigger of the combo.
206     * @private
207     * @type boolean
208     */
209     hideTrigger: true,
210 
211     /** 
212     * Display field name.
213     * @private
214     * @type String
215     */
216     displayField: 'name',
217 
218     /** 
219     * Force selection.
220     * @private
221     * @type boolean
222     */
223     forceSelection: true,
224 
225     /** 
226     * Query parameter.
227     * @private
228     * @type String
229     */
230     queryParam: 'name_startsWith',
231 
232     /** 
233     * Url of the GeoNames service: http://www.GeoNames.org/export/GeoNames-search.html
234     * @private
235     * @type String
236     */
237     //url: 'http://ws.geonames.org/searchJSON?',
238     url: 'http://ws.geonames.org/searchJSON?postalcode=9011&username=demo',
239     
240 
241     /** Init the component creating a store and object'events.
242     * @private
243     */ 
244     initComponent: function() {
245         framework.form.GeoNamesSearchCombo.superclass.initComponent.apply(this, arguments);
246 
247         var urlAppendString = '';
248 
249         if (this.countryString.length > 0) {
250             urlAppendString = urlAppendString + this.countryString;      
251         }
252 
253         if (this.featureClassString.length > 0) {
254             urlAppendString = urlAppendString + this.featureClassString;
255         }
256 
257         if (this.featureCodeString.length > 0) {
258             urlAppendString = urlAppendString + this.featureCodeString;
259         }
260         
261         this.store = new Ext.data.Store({
262             proxy: new Ext.data.ScriptTagProxy({
263                 url: this.url + urlAppendString,
264                 method: 'GET'
265             }),
266             baseParams: {
267                 maxRows: this.maxRows,
268                 lang: this.lang,
269                 continentCode: this.continentCode,
270                 adminCode1: this.adminCode1,
271                 adminCode2: this.adminCode2,
272                 adminCode3: this.adminCode3,
273                 tag: this.tag,
274                 charset: this.charset
275             },
276             reader: new Ext.data.JsonReader({
277                 idProperty: 'geonameId',
278                 root: "geonames",
279                 totalProperty: "totalResultsCount",
280                 fields: [
281                     {
282                         name: 'geonameId'
283                     },
284                     {
285                         name: 'countryName'
286                     },
287                     {
288                         name: 'lng'
289                     },
290                     {
291                         name: 'lat'
292                     },
293                     {
294                         name: 'name'
295                     },
296                     {
297                         name: 'fcodeName'
298                     },
299                     {
300                         name: 'adminCode1'
301                     },
302                     {
303                         name: 'fclName'
304                     },
305                     {
306                         name: 'countryCode'
307                     },
308                     {
309                         name: 'fcl'
310                     },
311                     {
312                         name: 'fcode'
313                     },
314                     {
315                         name: 'population'
316                     },
317                     {
318                         name: 'adminName1'
319                     }
320                 ]
321             })
322         });
323 
324         if(this.zoom > 0) {
325             this.on("select", function(combo, record, index) {
326                 var position = new OpenLayers.LonLat(
327                     record.data.lng, record.data.lat
328                 );
329                 position.transform(
330                     new OpenLayers.Projection("EPSG:4326"),
331                     this.map.getProjectionObject()
332                 );
333                 this.map.setCenter(position, this.zoom);
334             }, this);
335         }
336     }
337 });
338 
339 /** api: xtype = gxux_geonamessearchcombo */
340 Ext.reg('framework_geonamessearchcombo', framework.form.GeoNamesSearchCombo);
341