root/sandbox/mapgears/geoext.ux/ux/WMSLayerAdder/lib/GeoExt.ux/widgets/WMSLayerAdder.js

Revision 1975, 13.4 kB (checked in by adube, 6 months ago)

wmslayeradder - textfield url fix, selectRow(0)

  • Property svn:mime-type set to text/plain
  • Property svn:eol-style set to native
Line 
1 /**
2  * Copyright (c) 2008-2010 The Open Source Geospatial Foundation
3  *
4  * Published under the BSD license.
5  * See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text
6  * of the license.
7  */
8
9 Ext.namespace("GeoExt.ux")
10
11 /*
12  * @requires GeoExt/widgets/Action.js
13  */
14
15 /** api: (define)
16  *  module = GeoExt.ux
17  *  class = WMSLayerAdder
18  */
19
20 /** api: constructor
21  *  .. class:: WMSLayerAdder
22  */
23 GeoExt.ux.WMSLayerAdder = Ext.extend(Ext.Panel, {
24
25     layout: 'fit',
26
27     minWidth: 300,
28
29     minHeight: 200,
30
31     plain:true,
32
33     bodyStyle:'padding:5px;',
34
35     buttonAlign:'center',
36
37     useIcons: false,
38
39     mapPanel: null,
40
41     serverStore: null,
42
43     serverStoreDisplayField: 'url',
44
45     capabilitiesParams: {},
46
47     defaultCapabilitiesParams: {
48        'service': "WMS",
49        'request': "GetCapabilities",
50        'version': '1.1.1'
51     },
52
53     gridPanelOptions: {
54         'height': 200
55     },
56
57     /** private: method[constructor]
58      */
59     constructor: function(config) {
60         this.serverStore = config.serverStore || null;
61        
62         OpenLayers.Util.applyDefaults(
63             this.capabilitiesParams, this.defaultCapabilitiesParams);
64
65         this.initMyItems();
66         this.initMyToolbar();
67
68         arguments.callee.superclass.constructor.call(this, config);
69     },
70
71     initMyItems: function() {
72         var oItems;
73
74         oItems = [];
75
76         // Top panel
77         oTopPanel = {
78             style:'padding:0px;margin:0px;',
79             region: 'north',
80             id: "wms_field_group",
81             xtype: 'fieldset',
82             layout: 'form',
83             border: false,
84             collapsible: false,
85             autoHeight: true,
86             autoWidth: true,
87             defaults: {width: '100%'},
88             defaultType: 'textfield',
89             buttonAlign:'center',
90             items: [],
91             buttons: []
92         };
93
94         // URL panel
95         var oURLField;
96         if(this.serverStore) {
97             oURLField = {
98                 columnWidth: 0.85,
99                 layout: 'fit',
100                 'name': 'wms_url',
101                 'id': 'wms_url',
102                 xtype: 'combo',
103                 store: this.serverStore,
104                 displayField: this.serverStoreDisplayField,
105                 typeAhead: true,
106                 mode: 'local',
107                 forceSelection: false,
108                 triggerAction: 'all',
109                 emptyText:OpenLayers.i18n('Select or input a server address (URL)'),
110                 selectOnFocus:true
111             };
112         } else {
113             oURLField = {
114                 xtype: "textfield",
115                 columnWidth: 0.85,
116                 layout: 'fit',
117                 'name': 'wms_url',
118                 'id': 'wms_url',
119                 border: false,
120                 'emptyText': OpenLayers.i18n('Input the server address (URL)')
121             };
122         }
123
124         // Top panel - URL and Connect
125         oTopPanel.items.push({
126             style:'padding:2px;margin:2px;',
127             xtype: 'fieldset',
128             layout: 'column',
129             border: false,
130             collapsible: false,
131             collapsed: false,
132             autoHeight: true,
133             autoWidth: true,
134             items: [
135                 oURLField,
136                 {
137                     columnWidth: 0.15,
138                     layout: 'fit',
139                     border: false,
140                     items: [{
141                         xtype: 'button',
142                         text:  OpenLayers.i18n('Connect'),
143                         scope: this,
144                         handler: function(b, e){this.triggerGetCapabilities();}
145                     }]
146                 }
147             ]
148         });
149
150
151         // Top panel - Username and Password
152         oTopPanel.items.push({
153             style:'padding:10px;margin:2px;',
154             xtype: 'fieldset',
155             title: OpenLayers.i18n('Login information (optional)'),
156             layout: 'form',
157             collapsible: true,
158             collapsed: true,
159             autoHeight: true,
160             autoWidth: true,
161             defaults: {width: '100%'},
162             defaultType: 'textfield',
163             items: [{
164                 name: 'wms_username',
165                 id: 'wms_username',
166                 fieldLabel: OpenLayers.i18n('Username')
167             },{
168                 name: 'wms_password',
169                 id: 'wms_password',
170                 inputType: 'password',
171                 fieldLabel: OpenLayers.i18n('Password')
172             }]
173         });
174         oItems.push(oTopPanel);
175
176
177         // Center Panel
178         oCenterPanel = {
179           xtype: 'form',
180             region: 'center',
181             id: "wms_capabilities_panel",
182             layout: 'column',
183             border: false,
184             collapsible: false,
185             autoHeight: true,
186             autoWidth: true,
187             width: 'auto',
188             height: 'auto',
189             defaults: {width: '100%', hideLabel: true},
190             defaultType: 'textfield',
191             buttonAlign:'center',
192             items: []
193         };
194        
195         oItems.push(oCenterPanel);
196
197         Ext.apply(this, {items: oItems});
198     },
199
200     triggerGetCapabilities: function() {
201         var url = Ext.getCmp('wms_url').getValue();
202
203         if(!url) {
204             alert(OpenLayers.i18n('Please, enter an url in the textbox'));
205             return;
206         }
207
208         var params = OpenLayers.Util.getParameterString(this.capabilitiesParams);       
209         url = OpenLayers.Util.urlAppend(url, params);
210
211         if (OpenLayers.ProxyHost && OpenLayers.String.startsWith(url, "http")) {
212             url = OpenLayers.ProxyHost + encodeURIComponent(url);
213         }
214
215         // create a store from the url
216         this.createWMSCapabilitiesStore(url);
217
218         // remove the grid and form panels from the container and recreate them
219         var panel = Ext.getCmp('wms_capabilities_panel')
220         this.removeAllItemsFromObject(panel);
221         panel.add(this.createGridPanel());
222         panel.add(this.createFormPanel());
223         panel.doLayout();
224     },
225
226     removeAllItemsFromObject: function(object){
227         while(object.items.length != 0){
228             var oItem = object.items.items[0];
229             object.remove(oItem, true);
230         }
231     },
232
233     createWMSCapabilitiesStore: function(url) {
234         this.WMSCapabilitiesStore =
235             new GeoExt.data.WMSCapabilitiesStore({'url': url});
236         this.WMSCapabilitiesStore.on(
237             'load', this.onWMSCapabilitiesStoreLoad, this);
238         this.WMSCapabilitiesStore.load();
239     },
240
241     createGridPanel: function() {
242         var columns = [
243             { header: OpenLayers.i18n('Add'),
244               dataIndex: "srsCompatible", hidden: false,
245               renderer: this.boolRenderer, width: 30},
246             { header: OpenLayers.i18n('Title'),
247               dataIndex: "title", id: "title", sortable: true},
248             { header: OpenLayers.i18n('Name'),
249               dataIndex: "name", sortable: true},
250             { header: OpenLayers.i18n('Queryable'),
251               dataIndex: "queryable", sortable: true, hidden: true,
252               renderer: this.boolRenderer},
253             { header: OpenLayers.i18n('Description'),
254               dataIndex: "abstract", hidden: true}
255         ];
256
257         // In order to have a scrollbar, a GridPanel must have a 'height' set,
258         // it can't be left with 'autoHeight': true...
259         var options = {
260             id: 'wms_capabilities_grid_panel',
261             columnWidth: 0.5,
262             layout: 'fit',
263             store: this.WMSCapabilitiesStore,
264             columns: columns,
265             // SelectionModel
266             sm: new Ext.grid.RowSelectionModel({
267                 singleSelect: true,
268                 listeners: {
269                     rowselect: function(sm, row, rec) {
270                         Ext.getCmp("wms_capabilities_panel").getForm().loadRecord(rec);
271                     }
272                 }
273             }),
274             autoExpandColumn: "title",
275             width: 'auto',
276             autoWidth: true,
277             //height: 'auto',
278             //autoHeight: true,
279             listeners: {
280                 rowdblclick: this.mapPreview
281             }
282         };
283
284         options = OpenLayers.Util.applyDefaults(this.gridPanelOptions, options);
285
286         return new Ext.grid.GridPanel(options);
287     },
288
289     createFormPanel: function() {
290         var options = {
291             style:'padding:0px;margin:0px;',
292             columnWidth: 0.5,
293             xtype: 'fieldset',
294             labelWidth: 80,
295             defaults: {width: '100%', border:false},
296             defaultType: 'textfield',
297             autoHeight: true,
298             bodyStyle: Ext.isIE ? 'padding:0 0 0px 0px;' : 'padding:5px 0px;',
299             border: false,
300             style: {
301                 "margin-left": "10px",
302                 "margin-right": Ext.isIE6 ? (Ext.isStrict ? "-10px" : "-13px") : "0"
303             },
304             items: [{
305                 fieldLabel: OpenLayers.i18n('Title'),
306                 name: 'title'
307             },{
308                 fieldLabel: OpenLayers.i18n('Name'),
309                 name: 'name'
310             },{
311                 xtype: 'radiogroup',
312                 columns: 'auto',
313                 fieldLabel: OpenLayers.i18n('Queryable'),
314                 name: 'queryable',
315                 items: [{
316                     inputValue: "true",
317                     boxLabel: OpenLayers.i18n("Yes")
318                 }, {
319                     inputValue: "",
320                     boxLabel: OpenLayers.i18n("No")
321                 }]
322             },{
323                 xtype: 'radiogroup',
324                 columns: 'auto',
325                 fieldLabel: OpenLayers.i18n('Can add ?'),
326                 name: 'srsCompatible',
327                 items: [{
328                     inputValue: "true",
329                     boxLabel: OpenLayers.i18n("Yes")
330                 }, {
331                     inputValue: "false",
332                     boxLabel: OpenLayers.i18n("No")
333                 }]
334             },{
335                 xtype: 'textarea',
336                 fieldLabel: OpenLayers.i18n('Description'),
337                 name: 'abstract',
338                 anchor: '100% -30'
339             }]
340         };
341
342         return options;
343
344     },
345
346     mapPreview: function(grid, index) {
347         var record = grid.getStore().getAt(index);
348         var layer = record.get("layer").clone();
349        
350         var win = new Ext.Window({
351             title: OpenLayers.i18n('Preview') + ": " + record.get("title"),
352             width: 512,
353             height: 256,
354             layout: "fit",
355             items: [{
356                 xtype: "gx_mappanel",
357                 layers: [layer],
358                 extent: record.get("llbbox")
359             }]
360         });
361         win.show();
362     },
363
364     initMyToolbar: function() {
365         var items = [];
366
367         items.push('->');
368
369         // AddLayer action
370         var actionOptions = {
371             handler: this.addLayer,
372             scope: this,
373             tooltip: OpenLayers.i18n('Add currently selected layer')
374         };
375
376         if (this.useIcons === true) {
377             actionOptions.iconCls = "gx-wmslayeradder-addlayer";
378         } else {
379             actionOptions.text = OpenLayers.i18n('Add Layer');
380         }
381
382         var action = new Ext.Action(actionOptions);
383         items.push(action);
384
385         // Cancel/Close action... todo
386
387
388         Ext.apply(this, {bbar: new Ext.Toolbar(items)});
389     },
390
391     addLayer: function() {
392         var grid = Ext.getCmp('wms_capabilities_grid_panel');
393         var record = grid.getSelectionModel().getSelected();
394         if(record) {
395             // check the projection of the map is supported by the layer
396             if (record.get("srsCompatible") === false) {
397                 var error = "This layer can't be added to the current map" +
398                             " because it doesn't support its projection.";
399                 alert(OpenLayers.i18n(error));
400                 return;
401             }
402
403             var copy = record.copy();
404
405             // the following line gives "too much recursion".
406             //copy.set("layer", record.get("layer"));
407             copy.data.layer = record.data.layer;
408
409             copy.get("layer").mergeNewParams({
410                 format: "image/png",
411                 transparent: "true"
412             });
413             this.mapPanel.layers.add(copy);
414
415             // zoom to added layer extent
416             this.mapPanel.map.zoomToExtent(
417                 OpenLayers.Bounds.fromArray(copy.get("llbbox"))
418             );
419         }
420     },
421
422     onWMSCapabilitiesStoreLoad: function(store, records, options) {
423         var srs = this.mapPanel.map.getProjection();
424
425         // loop through all records (layers) to see if they contain the current
426         // map projection
427         for(var i=0; i<records.length; i++) {
428             var record = records[i];
429
430             // Check if the 'srs' contains a 'key' named by the srs OR
431             // Check if the 'srs' is an array and contains the srs
432             if(record.get('srs')[srs] === true ||
433                OpenLayers.Util.indexOf(record.get('srs'), srs) >= 0) {
434                 record.set("srsCompatible", true);
435             } else {
436                 record.set("srsCompatible", false);
437             }
438         }
439
440         // select the first element of the list on load end
441         var grid = Ext.getCmp('wms_capabilities_grid_panel');
442         if(grid.store.totalLength > 0) {
443             grid.getSelectionModel().selectRow(0);
444         }
445     },
446
447     boolRenderer: function(bool) {
448         return (bool)
449             ? '<span style="color:green;">' + OpenLayers.i18n("Yes") + '</span>'
450             : '<span style="color:red;">' + OpenLayers.i18n("No") + '</span>';
451     }
452 });
Note: See TracBrowser for help on using the browser.