1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | /** |
---|
7 | * @requires OpenLayers/Strategy.js |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Class: OpenLayers.Strategy.Fixed |
---|
12 | * A simple strategy that requests features once and never requests new data. |
---|
13 | * |
---|
14 | * Inherits from: |
---|
15 | * - <OpenLayers.Strategy> |
---|
16 | */ |
---|
17 | OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, { |
---|
18 | |
---|
19 | /** |
---|
20 | * APIProperty: preload |
---|
21 | * {Boolean} Load data before layer made visible. Enabling this may result |
---|
22 | * in considerable overhead if your application loads many data layers |
---|
23 | * that are not visible by default. Default is false. |
---|
24 | */ |
---|
25 | preload: false, |
---|
26 | |
---|
27 | /** |
---|
28 | * Constructor: OpenLayers.Strategy.Fixed |
---|
29 | * Create a new Fixed strategy. |
---|
30 | * |
---|
31 | * Parameters: |
---|
32 | * options - {Object} Optional object whose properties will be set on the |
---|
33 | * instance. |
---|
34 | */ |
---|
35 | initialize: function(options) { |
---|
36 | OpenLayers.Strategy.prototype.initialize.apply(this, [options]); |
---|
37 | }, |
---|
38 | |
---|
39 | /** |
---|
40 | * APIMethod: destroy |
---|
41 | * Clean up the strategy. |
---|
42 | */ |
---|
43 | destroy: function() { |
---|
44 | OpenLayers.Strategy.prototype.destroy.apply(this, arguments); |
---|
45 | }, |
---|
46 | |
---|
47 | /** |
---|
48 | * Method: activate |
---|
49 | * Activate the strategy: load data or add listener to load when visible |
---|
50 | * |
---|
51 | * Returns: |
---|
52 | * {Boolean} True if the strategy was successfully activated or false if |
---|
53 | * the strategy was already active. |
---|
54 | */ |
---|
55 | activate: function() { |
---|
56 | if(OpenLayers.Strategy.prototype.activate.apply(this, arguments)) { |
---|
57 | this.layer.events.on({ |
---|
58 | "refresh": this.load, |
---|
59 | scope: this |
---|
60 | }); |
---|
61 | if(this.layer.visibility == true || this.preload) { |
---|
62 | this.load(); |
---|
63 | } else { |
---|
64 | this.layer.events.on({ |
---|
65 | "visibilitychanged": this.load, |
---|
66 | scope: this |
---|
67 | }); |
---|
68 | } |
---|
69 | return true; |
---|
70 | } |
---|
71 | return false; |
---|
72 | }, |
---|
73 | |
---|
74 | /** |
---|
75 | * Method: deactivate |
---|
76 | * Deactivate the strategy. Undo what is done in <activate>. |
---|
77 | * |
---|
78 | * Returns: |
---|
79 | * {Boolean} The strategy was successfully deactivated. |
---|
80 | */ |
---|
81 | deactivate: function() { |
---|
82 | var deactivated = OpenLayers.Strategy.prototype.deactivate.call(this); |
---|
83 | if(deactivated) { |
---|
84 | this.layer.events.un({ |
---|
85 | "refresh": this.load, |
---|
86 | "visibilitychanged": this.load, |
---|
87 | scope: this |
---|
88 | }); |
---|
89 | } |
---|
90 | return deactivated; |
---|
91 | }, |
---|
92 | |
---|
93 | /** |
---|
94 | * Method: load |
---|
95 | * Tells protocol to load data and unhooks the visibilitychanged event |
---|
96 | * |
---|
97 | * Parameters: |
---|
98 | * options - {Object} options to pass to protocol read. |
---|
99 | */ |
---|
100 | load: function(options) { |
---|
101 | this.layer.events.triggerEvent("loadstart"); |
---|
102 | this.layer.protocol.read(OpenLayers.Util.applyDefaults({ |
---|
103 | callback: this.merge, |
---|
104 | filter: this.layer.filter, |
---|
105 | scope: this |
---|
106 | }, options)); |
---|
107 | this.layer.events.un({ |
---|
108 | "visibilitychanged": this.load, |
---|
109 | scope: this |
---|
110 | }); |
---|
111 | }, |
---|
112 | |
---|
113 | /** |
---|
114 | * Method: merge |
---|
115 | * Add all features to the layer. |
---|
116 | */ |
---|
117 | merge: function(resp) { |
---|
118 | this.layer.destroyFeatures(); |
---|
119 | var features = resp.features; |
---|
120 | if (features && features.length > 0) { |
---|
121 | var remote = this.layer.projection; |
---|
122 | var local = this.layer.map.getProjectionObject(); |
---|
123 | if(!local.equals(remote)) { |
---|
124 | var geom; |
---|
125 | for(var i=0, len=features.length; i<len; ++i) { |
---|
126 | geom = features[i].geometry; |
---|
127 | if(geom) { |
---|
128 | geom.transform(remote, local); |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | this.layer.addFeatures(features); |
---|
133 | } |
---|
134 | this.layer.events.triggerEvent("loadend"); |
---|
135 | }, |
---|
136 | |
---|
137 | CLASS_NAME: "OpenLayers.Strategy.Fixed" |
---|
138 | }); |
---|