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.data |
---|
11 | * class = ScaleStore |
---|
12 | * base_link = `Ext.data.Store <http://dev.sencha.com/deploy/dev/docs/?class=Ext.data.Store>`_ |
---|
13 | */ |
---|
14 | Ext.namespace("GeoExt.data"); |
---|
15 | |
---|
16 | /** api: constructor |
---|
17 | * .. class:: ScaleStore |
---|
18 | * |
---|
19 | * A store that contains a cache of available zoom levels. The store can |
---|
20 | * optionally be kept synchronized with an ``OpenLayers.Map`` or |
---|
21 | * :class:`GeoExt.MapPanel` object. |
---|
22 | * |
---|
23 | * Records have the following fields: |
---|
24 | * |
---|
25 | * * level - ``Number`` The zoom level. |
---|
26 | * * scale - ``Number`` The scale denominator. |
---|
27 | * * resolution - ``Number`` The map units per pixel. |
---|
28 | */ |
---|
29 | GeoExt.data.ScaleStore = Ext.extend(Ext.data.Store, { |
---|
30 | |
---|
31 | /** api: config[map] |
---|
32 | * ``OpenLayers.Map`` or :class:`GeoExt.MapPanel` |
---|
33 | * Optional map or map panel from which to derive scale values. |
---|
34 | */ |
---|
35 | map: null, |
---|
36 | |
---|
37 | /** private: method[constructor] |
---|
38 | * Construct a ScaleStore from a configuration. The ScaleStore accepts |
---|
39 | * some custom parameters addition to the fields accepted by Ext.Store. |
---|
40 | */ |
---|
41 | constructor: function(config) { |
---|
42 | var map = (config.map instanceof GeoExt.MapPanel ? config.map.map : config.map); |
---|
43 | delete config.map; |
---|
44 | config = Ext.applyIf(config, {reader: new Ext.data.JsonReader({}, [ |
---|
45 | "level", |
---|
46 | "resolution", |
---|
47 | "scale" |
---|
48 | ])}); |
---|
49 | |
---|
50 | GeoExt.data.ScaleStore.superclass.constructor.call(this, config); |
---|
51 | |
---|
52 | if (map) { |
---|
53 | this.bind(map); |
---|
54 | } |
---|
55 | }, |
---|
56 | |
---|
57 | /** api: method[bind] |
---|
58 | * :param map: :class:`GeoExt.MapPanel` or ``OpenLayers.Map`` Panel or map |
---|
59 | * to which we should bind. |
---|
60 | * |
---|
61 | * Bind this store to a map; that is, maintain the zoom list in sync with |
---|
62 | * the map's current configuration. If the map does not currently have a |
---|
63 | * set scale list, then the store will remain empty until the map is |
---|
64 | * configured with one. |
---|
65 | */ |
---|
66 | bind: function(map, options) { |
---|
67 | this.map = (map instanceof GeoExt.MapPanel ? map.map : map); |
---|
68 | this.map.events.register('changebaselayer', this, this.populateFromMap); |
---|
69 | if (this.map.baseLayer) { |
---|
70 | this.populateFromMap(); |
---|
71 | } else { |
---|
72 | this.map.events.register('addlayer', this, this.populateOnAdd); |
---|
73 | } |
---|
74 | }, |
---|
75 | |
---|
76 | /** api: method[unbind] |
---|
77 | * Un-bind this store from the map to which it is currently bound. The |
---|
78 | * currently stored zoom levels will remain, but no further changes from |
---|
79 | * the map will affect it. |
---|
80 | */ |
---|
81 | unbind: function() { |
---|
82 | if (this.map) { |
---|
83 | this.map.events.unregister('addlayer', this, this.populateOnAdd); |
---|
84 | this.map.events.unregister('changebaselayer', this, this.populateFromMap); |
---|
85 | delete this.map; |
---|
86 | } |
---|
87 | }, |
---|
88 | |
---|
89 | /** private: method[populateOnAdd] |
---|
90 | * :param evt: ``Object`` |
---|
91 | * |
---|
92 | * This method handles the case where we have bind() called on a |
---|
93 | * not-fully-configured map so that the zoom levels can be detected when a |
---|
94 | * baselayer is finally added. |
---|
95 | */ |
---|
96 | populateOnAdd: function(evt) { |
---|
97 | if (evt.layer.isBaseLayer) { |
---|
98 | this.populateFromMap(); |
---|
99 | this.map.events.unregister('addlayer', this, this.populateOnAdd); |
---|
100 | } |
---|
101 | }, |
---|
102 | |
---|
103 | /** private: method[populateFromMap] |
---|
104 | * This method actually loads the zoom level information from the |
---|
105 | * OpenLayers.Map and converts it to Ext Records. |
---|
106 | */ |
---|
107 | populateFromMap: function() { |
---|
108 | var zooms = []; |
---|
109 | var resolutions = this.map.baseLayer.resolutions; |
---|
110 | var units = this.map.baseLayer.units; |
---|
111 | |
---|
112 | for (var i=resolutions.length-1; i >= 0; i--) { |
---|
113 | var res = resolutions[i]; |
---|
114 | zooms.push({ |
---|
115 | level: i, |
---|
116 | resolution: res, |
---|
117 | scale: OpenLayers.Util.getScaleFromResolution(res, units) |
---|
118 | }); |
---|
119 | } |
---|
120 | |
---|
121 | this.loadData(zooms); |
---|
122 | }, |
---|
123 | |
---|
124 | /** private: method[destroy] |
---|
125 | */ |
---|
126 | destroy: function() { |
---|
127 | this.unbind(); |
---|
128 | GeoExt.data.ScaleStore.superclass.destroy.apply(this, arguments); |
---|
129 | } |
---|
130 | }); |
---|