[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 = LayerLegend |
---|
| 12 | * base_link = `Ext.Container <http://dev.sencha.com/deploy/dev/docs/?class=Ext.Container>`_ |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | Ext.namespace('GeoExt'); |
---|
| 16 | |
---|
| 17 | /** api: constructor |
---|
| 18 | * .. class:: LayerLegend(config) |
---|
| 19 | * |
---|
| 20 | * Base class for components of :class:`GeoExt.LegendPanel`. |
---|
| 21 | */ |
---|
| 22 | GeoExt.LayerLegend = Ext.extend(Ext.Container, { |
---|
| 23 | |
---|
| 24 | /** api: config[layerRecord] |
---|
| 25 | * :class:`GeoExt.data.LayerRecord` The layer record for the legend |
---|
| 26 | */ |
---|
| 27 | layerRecord: null, |
---|
| 28 | |
---|
| 29 | /** api: config[showTitle] |
---|
| 30 | * ``Boolean`` |
---|
| 31 | * Whether or not to show the title of a layer. This can be overridden |
---|
| 32 | * on the LayerStore record using the hideTitle property. |
---|
| 33 | */ |
---|
| 34 | showTitle: true, |
---|
| 35 | |
---|
| 36 | /** api: config[legendTitle] |
---|
| 37 | * ``String`` |
---|
| 38 | * Optional title to be displayed instead of the layer title. If this is |
---|
| 39 | * set, the value of ``showTitle`` will be ignored (assumed to be true). |
---|
| 40 | */ |
---|
| 41 | legendTitle: null, |
---|
| 42 | |
---|
| 43 | /** api: config[labelCls] |
---|
| 44 | * ``String`` |
---|
| 45 | * Optional css class to use for the layer title labels. |
---|
| 46 | */ |
---|
| 47 | labelCls: null, |
---|
| 48 | |
---|
| 49 | /** private: property[layerStore] |
---|
| 50 | * :class:`GeoExt.data.LayerStore` |
---|
| 51 | */ |
---|
| 52 | layerStore: null, |
---|
| 53 | |
---|
| 54 | /** private: method[initComponent] |
---|
| 55 | */ |
---|
| 56 | initComponent: function() { |
---|
| 57 | GeoExt.LayerLegend.superclass.initComponent.call(this); |
---|
| 58 | this.autoEl = {}; |
---|
| 59 | this.add({ |
---|
| 60 | xtype: "label", |
---|
| 61 | text: this.getLayerTitle(this.layerRecord), |
---|
| 62 | cls: 'x-form-item x-form-item-label' + |
---|
| 63 | (this.labelCls ? ' ' + this.labelCls : '') |
---|
| 64 | }); |
---|
| 65 | if (this.layerRecord && this.layerRecord.store) { |
---|
| 66 | this.layerStore = this.layerRecord.store; |
---|
| 67 | this.layerStore.on("update", this.onStoreUpdate, this); |
---|
| 68 | } |
---|
| 69 | }, |
---|
| 70 | |
---|
| 71 | /** private: method[onStoreUpdate] |
---|
| 72 | * Update a the legend. Gets called when the store fires the update event. |
---|
| 73 | * This usually means the visibility of the layer, its style or title |
---|
| 74 | * has changed. |
---|
| 75 | * |
---|
| 76 | * :param store: ``Ext.data.Store`` The store in which the record was |
---|
| 77 | * changed. |
---|
| 78 | * :param record: ``Ext.data.Record`` The record object corresponding |
---|
| 79 | * to the updated layer. |
---|
| 80 | * :param operation: ``String`` The type of operation. |
---|
| 81 | */ |
---|
| 82 | onStoreUpdate: function(store, record, operation) { |
---|
| 83 | // if we don't have items, we are already awaiting garbage |
---|
| 84 | // collection after being removed by LegendPanel::removeLegend, and |
---|
| 85 | // updating will cause errors |
---|
| 86 | if (record === this.layerRecord && this.items.getCount() > 0) { |
---|
| 87 | var layer = record.getLayer(); |
---|
| 88 | this.setVisible(layer.getVisibility() && |
---|
| 89 | layer.calculateInRange() && layer.displayInLayerSwitcher && |
---|
| 90 | !record.get('hideInLegend')); |
---|
| 91 | this.update(); |
---|
| 92 | } |
---|
| 93 | }, |
---|
| 94 | |
---|
| 95 | /** private: method[update] |
---|
| 96 | * Updates the legend. |
---|
| 97 | */ |
---|
| 98 | update: function() { |
---|
| 99 | var title = this.getLayerTitle(this.layerRecord); |
---|
| 100 | if (this.items.get(0).text !== title) { |
---|
| 101 | // we need to update the title |
---|
| 102 | this.items.get(0).setText(title); |
---|
| 103 | } |
---|
| 104 | }, |
---|
| 105 | |
---|
| 106 | /** private: method[getLayerTitle] |
---|
| 107 | * :arg record: :class:GeoExt.data.LayerRecord |
---|
| 108 | * :returns: ``String`` |
---|
| 109 | * |
---|
| 110 | * Get a title for the layer. If the record doesn't have a title, use the |
---|
| 111 | * name. |
---|
| 112 | */ |
---|
| 113 | getLayerTitle: function(record) { |
---|
| 114 | var title = this.legendTitle || ""; |
---|
| 115 | if (this.showTitle && !title) { |
---|
| 116 | if (record && !record.get("hideTitle")) { |
---|
| 117 | title = record.get("title") || |
---|
| 118 | record.get("name") || |
---|
| 119 | record.getLayer().name || ""; |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | return title; |
---|
| 123 | }, |
---|
| 124 | |
---|
| 125 | /** private: method[beforeDestroy] |
---|
| 126 | */ |
---|
| 127 | beforeDestroy: function() { |
---|
| 128 | this.layerStore && |
---|
| 129 | this.layerStore.un("update", this.onStoreUpdate, this); |
---|
| 130 | GeoExt.LayerLegend.superclass.beforeDestroy.apply(this, arguments); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | }); |
---|
| 134 | |
---|
| 135 | /** class: method[getTypes] |
---|
| 136 | * :param layerRecord: class:`GeoExt.data.LayerRecord` A layer record to get |
---|
| 137 | * legend types for. If not provided, all registered types will be |
---|
| 138 | * returned. |
---|
| 139 | * :param preferredTypes: ``Array(String)`` Types that should be considered. |
---|
| 140 | * first. If not provided, all legend types will be returned in the order |
---|
| 141 | * they were registered. |
---|
| 142 | * :return: ``Array(String)`` xtypes of legend types that can be used with |
---|
| 143 | * the provided ``layerRecord``. |
---|
| 144 | * |
---|
| 145 | * Gets an array of legend xtypes that support the provided layer record, |
---|
| 146 | * with optionally provided preferred types listed first. |
---|
| 147 | */ |
---|
| 148 | GeoExt.LayerLegend.getTypes = function(layerRecord, preferredTypes) { |
---|
| 149 | var types = (preferredTypes || []).concat(); |
---|
| 150 | var goodTypes = []; |
---|
| 151 | for(var type in GeoExt.LayerLegend.types) { |
---|
| 152 | if(GeoExt.LayerLegend.types[type].supports(layerRecord)) { |
---|
| 153 | // add to goodTypes if not preferred |
---|
| 154 | types.indexOf(type) == -1 && goodTypes.push(type); |
---|
| 155 | } else { |
---|
| 156 | // preferred, but not supported |
---|
| 157 | types.remove(type); |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | // take the remaining preferred types, and add other good types |
---|
| 161 | return types.concat(goodTypes); |
---|
| 162 | }; |
---|
| 163 | |
---|
| 164 | /** private: method[supports] |
---|
| 165 | * :param layerRecord: :class:`GeoExt.data.LayerRecord` The layer record |
---|
| 166 | * to check support for. |
---|
| 167 | * :return: ``Boolean`` true if this legend type supports the layer |
---|
| 168 | * record. |
---|
| 169 | * |
---|
| 170 | * Checks whether this legend type supports the provided layerRecord. |
---|
| 171 | */ |
---|
| 172 | GeoExt.LayerLegend.supports = function(layerRecord) { |
---|
| 173 | // to be implemented by subclasses |
---|
| 174 | }; |
---|
| 175 | |
---|
| 176 | /** class: constant[GeoExt.LayerLegend.types] |
---|
| 177 | * An object containing a name-class mapping of LayerLegend subclasses. |
---|
| 178 | * To register as LayerLegend, a subclass should add itself to this object: |
---|
| 179 | * |
---|
| 180 | * .. code-block:: javascript |
---|
| 181 | * |
---|
| 182 | * GeoExt.GetLegendGraphicLegend = Ext.extend(GeoExt.LayerLegend, { |
---|
| 183 | * }); |
---|
| 184 | * |
---|
| 185 | * GeoExt.LayerLegend.types["getlegendgraphic"] = |
---|
| 186 | * GeoExt.GetLegendGraphicLegend; |
---|
| 187 | */ |
---|
| 188 | GeoExt.LayerLegend.types = {}; |
---|