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  * @requires plugins/Tool.js
 12  */
 13 
 14 /**
 15  * @namespace framework.plugins
 16  */
 17 Ext.namespace("framework.plugins");
 18 
 19 /** 
 20  *  Combo to select the baselayer of the map.
 21  *  This implementation, by default, place a comboBox on the center of the map. 
 22  *  The component shoult be put inside a panel to customize position inside the map. 
 23  *  @name_ BaseLayerPlugin 
 24  *  @class Combo to select the baselayer of the map.
 25  *  @constructor
 26  *  @extends <a target="_blank" href="http://gxp.opengeo.org/master/doc/lib/plugins/Tool.html">gxp.plugins.Tool</a>
 27  */
 28 framework.plugins.BaseLayerPlugin = Ext.extend(gxp.plugins.Tool, 
 29 /** 
 30  * @lends framework.plugins.BaseLayerPlugin.prototype 
 31  */
 32     {
 33         /**
 34          * BaseLayerPlugin plugin type.
 35          * @public
 36          * @type String
 37          */
 38         ptype: "framework_baselayerplugin",
 39 
 40         /**
 41          * Target link to the father object.
 42          * @public
 43          * @type Object
 44          */
 45         target: null,
 46 
 47         /** Create comboBox object, the main object of the class.
 48         * @public
 49         * @param {Object} target Target link to the father object
 50         */   
 51         init: function(target) {
 52             this.target = target;
 53             var combo = new Ext.form.ComboBox(Ext.apply({
 54                 listeners: {
 55                     select: this.onComboSelect,
 56                                     afterrender:this.onAfterrender,
 57                                     focus: this.onFocus,
 58                     scope: this
 59                 }
 60             }, this.outputConfig));
 61             var bounds = target.mapPanel.map.restrictedExtent;
 62             this.combo = combo;
 63             return framework.plugins.BaseLayerPlugin.superclass.init.apply(this, arguments);
 64         },
 65 
 66         /** Description.
 67         * @public
 68         * @param {Object} config configuration object
 69         */   
 70         addOutput: function(config) {
 71             return framework.plugins.BaseLayerPlugin.superclass.addOutput.call(this, this.combo);
 72         },
 73 
 74         /** Listener for combo's select event.
 75         * @private
 76         * @param {Object} combo comnoBox object.
 77         * @param {Object} record selected record.
 78         * @param {Object} idx index of selected record.
 79         */   
 80         onComboSelect: function(combo, record, idx) {     
 81             var attiveLayer = record.data.layer;
 82             attiveLayer.setVisibility(true);	
 83             //gestone colore background
 84             var container = Ext.get(combo.map.getViewport());
 85             if (attiveLayer.background)
 86                 container.setStyle('background-color', attiveLayer.background);
 87             else
 88                 container.setStyle('background-color', "FFFFFF");
 89         },
 90 
 91         /** Listener for combo's afterrender event.
 92         * @private
 93         */
 94         onAfterrender: function() {
 95             this.combo.map = this.target.mapPanel.map;
 96             this.inizialize();
 97 
 98             this.combo.map.events.register('addlayer', this, function(obj){
 99                 if(obj.layer.candidateBaseLayer){
100                     this.combo.store.add(new this.combo.store.recordType({
101                         id: this.combo.store.data.length
102                         , name: obj.layer.name,
103                         layer : obj.layer
104                         }));
105                     }
106                 var x = 1;
107             });
108             
109             this.combo.map.events.register('removelayer', this, function(obj){
110                 if(obj.layer.candidateBaseLayer){
111                     var i = 1;
112                     var found = false;
113                     while ((i < this.combo.store.data.length) && (!found)){         
114                             if(this.combo.store.data.items[i].data.name == obj.layer.name) {
115                                     this.combo.store.remove(this.combo.store.getAt(i));
116                                     
117                                     //CONTROLLO LAYER TMS "NON DISPONIBILE
118                                     if (obj.layer.layerNT && obj.layer.visibility == true) {
119                                         obj.layer.layerNT.map.removeLayer(obj.layer.layerNT);
120                                     }
121                             
122                             
123                                     found = true;
124                             }
125                             i++;
126                     }                    
127                 }       
128             });
129 
130             // register event if base layer changes
131             this.combo.map.events.register('changelayer', this, function(obj){
132                     visibleLayers = this.target.mapPanel.map.getLayersBy('visibility', true);
133                     var baseVisibleLayers =[];
134                     var i = 1;
135                     var found = false;
136                     while ((i < visibleLayers.length) && (!found)){         
137                             if(visibleLayers[i].candidateBaseLayer === true) {
138                                     baseVisibleLayers.push(visibleLayers[i]);
139                             }
140                             i++;
141                     }
142                     if (baseVisibleLayers.length === 1) {
143                             this.combo.setValue(baseVisibleLayers[0].name);
144                     }
145             });
146         },
147 
148         /** Listener for combo's focus event.
149         * @private
150         * @param {Object} combo comboBox object.
151          */
152         onFocus: function(combo) {
153              // set the display field
154             this.combo.displayField = this.combo.store.fields.keys[1];
155         },
156 
157         /** Function for inizialize combo's store.
158         * @private
159         */
160         inizialize: function(){
161             if(this.target.map.layers) {
162                 this.combo.store = new Ext.data.ArrayStore({
163                     fields: ['id', 'name'],
164                     data : [],
165                     layer: []
166                 });
167             }        
168         }
169     }
170 );
171 
172 Ext.preg(framework.plugins.BaseLayerPlugin.prototype.ptype, framework.plugins.BaseLayerPlugin);
173