[76] | 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 | /** api: (define) |
---|
| 10 | * module = GeoExt |
---|
| 11 | * class = LegendPanel |
---|
| 12 | * base_link = `Ext.Panel <http://dev.sencha.com/deploy/dev/docs/?class=Ext.Panel>`_ |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | Ext.namespace('GeoExt'); |
---|
| 16 | |
---|
| 17 | /** api: constructor |
---|
| 18 | * .. class:: LegendPanel(config) |
---|
| 19 | * |
---|
| 20 | * A panel showing legends of all layers in a layer store. |
---|
| 21 | * Depending on the layer type, a legend renderer will be chosen. |
---|
| 22 | */ |
---|
| 23 | GeoExt.LegendPanel = Ext.extend(Ext.Panel, { |
---|
| 24 | |
---|
| 25 | /** api: config[dynamic] |
---|
| 26 | * ``Boolean`` |
---|
| 27 | * If false the LegendPanel will not listen to the add, remove and change |
---|
| 28 | * events of the LayerStore. So it will load with the initial state of |
---|
| 29 | * the LayerStore and not change anymore. |
---|
| 30 | */ |
---|
| 31 | dynamic: true, |
---|
| 32 | |
---|
| 33 | /** api: config[layerStore] |
---|
| 34 | * ``GeoExt.data.LayerStore`` |
---|
| 35 | * The layer store containing layers to be displayed in the legend |
---|
| 36 | * container. If not provided it will be taken from the MapPanel. |
---|
| 37 | */ |
---|
| 38 | layerStore: null, |
---|
| 39 | |
---|
| 40 | /** api: config[preferredTypes] |
---|
| 41 | * ``Array(String)`` An array of preferred legend types. |
---|
| 42 | */ |
---|
| 43 | |
---|
| 44 | /** private: property[preferredTypes] |
---|
| 45 | */ |
---|
| 46 | preferredTypes: null, |
---|
| 47 | |
---|
| 48 | /** api: config[filter] |
---|
| 49 | * ``Function`` |
---|
| 50 | * A function, called in the scope of the legend panel, with a layer record |
---|
| 51 | * as argument. Is expected to return true for layers to be displayed, false |
---|
| 52 | * otherwise. By default, all layers will be displayed. |
---|
| 53 | * |
---|
| 54 | * .. code-block:: javascript |
---|
| 55 | * |
---|
| 56 | * filter: function(record) { |
---|
| 57 | * return record.getLayer().isBaseLayer; |
---|
| 58 | * } |
---|
| 59 | */ |
---|
| 60 | filter: function(record) { |
---|
| 61 | return true; |
---|
| 62 | }, |
---|
| 63 | |
---|
| 64 | /** private: method[initComponent] |
---|
| 65 | * Initializes the legend panel. |
---|
| 66 | */ |
---|
| 67 | initComponent: function() { |
---|
| 68 | GeoExt.LegendPanel.superclass.initComponent.call(this); |
---|
| 69 | }, |
---|
| 70 | |
---|
| 71 | /** private: method[onRender] |
---|
| 72 | * Private method called when the legend panel is being rendered. |
---|
| 73 | */ |
---|
| 74 | onRender: function() { |
---|
| 75 | GeoExt.LegendPanel.superclass.onRender.apply(this, arguments); |
---|
| 76 | if(!this.layerStore) { |
---|
| 77 | this.layerStore = GeoExt.MapPanel.guess().layers; |
---|
| 78 | } |
---|
| 79 | this.layerStore.each(function(record) { |
---|
| 80 | this.addLegend(record); |
---|
| 81 | }, this); |
---|
| 82 | if (this.dynamic) { |
---|
| 83 | this.layerStore.on({ |
---|
| 84 | "add": this.onStoreAdd, |
---|
| 85 | "remove": this.onStoreRemove, |
---|
| 86 | "clear": this.onStoreClear, |
---|
| 87 | scope: this |
---|
| 88 | }); |
---|
| 89 | } |
---|
| 90 | }, |
---|
| 91 | |
---|
| 92 | /** private: method[recordIndexToPanelIndex] |
---|
| 93 | * Private method to get the panel index for a layer represented by a |
---|
| 94 | * record. |
---|
| 95 | * |
---|
| 96 | * :param index ``Integer`` The index of the record in the store. |
---|
| 97 | * |
---|
| 98 | * :return: ``Integer`` The index of the sub panel in this panel. |
---|
| 99 | */ |
---|
| 100 | recordIndexToPanelIndex: function(index) { |
---|
| 101 | var store = this.layerStore; |
---|
| 102 | var count = store.getCount(); |
---|
| 103 | var panelIndex = -1; |
---|
| 104 | var legendCount = this.items ? this.items.length : 0; |
---|
| 105 | var record, layer; |
---|
| 106 | for(var i=count-1; i>=0; --i) { |
---|
| 107 | record = store.getAt(i); |
---|
| 108 | layer = record.getLayer(); |
---|
| 109 | var types = GeoExt.LayerLegend.getTypes(record); |
---|
| 110 | if(layer.displayInLayerSwitcher && types.length > 0 && |
---|
| 111 | (store.getAt(i).get("hideInLegend") !== true)) { |
---|
| 112 | ++panelIndex; |
---|
| 113 | if(index === i || panelIndex > legendCount-1) { |
---|
| 114 | break; |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | return panelIndex; |
---|
| 119 | }, |
---|
| 120 | |
---|
| 121 | /** private: method[getIdForLayer] |
---|
| 122 | * :arg layer: ``OpenLayers.Layer`` |
---|
| 123 | * :returns: ``String`` |
---|
| 124 | * |
---|
| 125 | * Generate an element id that is unique to this panel/layer combo. |
---|
| 126 | */ |
---|
| 127 | getIdForLayer: function(layer) { |
---|
| 128 | return this.id + "-" + layer.id; |
---|
| 129 | }, |
---|
| 130 | |
---|
| 131 | /** private: method[onStoreAdd] |
---|
| 132 | * Private method called when a layer is added to the store. |
---|
| 133 | * |
---|
| 134 | * :param store: ``Ext.data.Store`` The store to which the record(s) was |
---|
| 135 | * added. |
---|
| 136 | * :param record: ``Ext.data.Record`` The record object(s) corresponding |
---|
| 137 | * to the added layers. |
---|
| 138 | * :param index: ``Integer`` The index of the inserted record. |
---|
| 139 | */ |
---|
| 140 | onStoreAdd: function(store, records, index) { |
---|
| 141 | var panelIndex = this.recordIndexToPanelIndex(index+records.length-1); |
---|
| 142 | for (var i=0, len=records.length; i<len; i++) { |
---|
| 143 | this.addLegend(records[i], panelIndex); |
---|
| 144 | } |
---|
| 145 | this.doLayout(); |
---|
| 146 | }, |
---|
| 147 | |
---|
| 148 | /** private: method[onStoreRemove] |
---|
| 149 | * Private method called when a layer is removed from the store. |
---|
| 150 | * |
---|
| 151 | * :param store: ``Ext.data.Store`` The store from which the record(s) was |
---|
| 152 | * removed. |
---|
| 153 | * :param record: ``Ext.data.Record`` The record object(s) corresponding |
---|
| 154 | * to the removed layers. |
---|
| 155 | * :param index: ``Integer`` The index of the removed record. |
---|
| 156 | */ |
---|
| 157 | onStoreRemove: function(store, record, index) { |
---|
| 158 | this.removeLegend(record); |
---|
| 159 | }, |
---|
| 160 | |
---|
| 161 | /** private: method[removeLegend] |
---|
| 162 | * Remove the legend of a layer. |
---|
| 163 | * :param record: ``Ext.data.Record`` The record object from the layer |
---|
| 164 | * store to remove. |
---|
| 165 | */ |
---|
| 166 | removeLegend: function(record) { |
---|
| 167 | if (this.items) { |
---|
| 168 | var legend = this.getComponent(this.getIdForLayer(record.getLayer())); |
---|
| 169 | if (legend) { |
---|
| 170 | this.remove(legend, true); |
---|
| 171 | this.doLayout(); |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | }, |
---|
| 175 | |
---|
| 176 | /** private: method[onStoreClear] |
---|
| 177 | * Private method called when a layer store is cleared. |
---|
| 178 | * |
---|
| 179 | * :param store: ``Ext.data.Store`` The store from which was cleared. |
---|
| 180 | */ |
---|
| 181 | onStoreClear: function(store) { |
---|
| 182 | this.removeAllLegends(); |
---|
| 183 | }, |
---|
| 184 | |
---|
| 185 | /** private: method[removeAllLegends] |
---|
| 186 | * Remove all legends from this legend panel. |
---|
| 187 | */ |
---|
| 188 | removeAllLegends: function() { |
---|
| 189 | this.removeAll(true); |
---|
| 190 | this.doLayout(); |
---|
| 191 | }, |
---|
| 192 | |
---|
| 193 | /** private: method[addLegend] |
---|
| 194 | * Add a legend for the layer. |
---|
| 195 | * |
---|
| 196 | * :param record: ``Ext.data.Record`` The record object from the layer |
---|
| 197 | * store. |
---|
| 198 | * :param index: ``Integer`` The position at which to add the legend. |
---|
| 199 | */ |
---|
| 200 | addLegend: function(record, index) { |
---|
| 201 | if (this.filter(record) === true) { |
---|
| 202 | var layer = record.getLayer(); |
---|
| 203 | index = index || 0; |
---|
| 204 | var legend; |
---|
| 205 | var types = GeoExt.LayerLegend.getTypes(record, |
---|
| 206 | this.preferredTypes); |
---|
| 207 | if(layer.displayInLayerSwitcher && !record.get('hideInLegend') && |
---|
| 208 | types.length > 0) { |
---|
| 209 | this.insert(index, { |
---|
| 210 | xtype: types[0], |
---|
| 211 | id: this.getIdForLayer(layer), |
---|
| 212 | layerRecord: record, |
---|
| 213 | hidden: !((!layer.map && layer.visibility) || |
---|
| 214 | (layer.getVisibility() && layer.calculateInRange())) |
---|
| 215 | }); |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | }, |
---|
| 219 | |
---|
| 220 | /** private: method[onDestroy] |
---|
| 221 | * Private method called during the destroy sequence. |
---|
| 222 | */ |
---|
| 223 | onDestroy: function() { |
---|
| 224 | if(this.layerStore) { |
---|
| 225 | this.layerStore.un("add", this.onStoreAdd, this); |
---|
| 226 | this.layerStore.un("remove", this.onStoreRemove, this); |
---|
| 227 | this.layerStore.un("clear", this.onStoreClear, this); |
---|
| 228 | } |
---|
| 229 | GeoExt.LegendPanel.superclass.onDestroy.apply(this, arguments); |
---|
| 230 | } |
---|
| 231 | }); |
---|
| 232 | |
---|
| 233 | /** api: xtype = gx_legendpanel */ |
---|
| 234 | Ext.reg('gx_legendpanel', GeoExt.LegendPanel); |
---|