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.Refresh |
---|
12 | * A strategy that refreshes the layer. By default the strategy waits for a |
---|
13 | * call to <refresh> before refreshing. By configuring the strategy with |
---|
14 | * the <interval> option, refreshing can take place automatically. |
---|
15 | * |
---|
16 | * Inherits from: |
---|
17 | * - <OpenLayers.Strategy> |
---|
18 | */ |
---|
19 | OpenLayers.Strategy.Refresh = OpenLayers.Class(OpenLayers.Strategy, { |
---|
20 | |
---|
21 | /** |
---|
22 | * Property: force |
---|
23 | * {Boolean} Force a refresh on the layer. Default is false. |
---|
24 | */ |
---|
25 | force: false, |
---|
26 | |
---|
27 | /** |
---|
28 | * Property: interval |
---|
29 | * {Number} Auto-refresh. Default is 0. If > 0, layer will be refreshed |
---|
30 | * every N milliseconds. |
---|
31 | */ |
---|
32 | interval: 0, |
---|
33 | |
---|
34 | /** |
---|
35 | * Property: timer |
---|
36 | * {Number} The id of the timer. |
---|
37 | */ |
---|
38 | timer: null, |
---|
39 | |
---|
40 | /** |
---|
41 | * Constructor: OpenLayers.Strategy.Refresh |
---|
42 | * Create a new Refresh strategy. |
---|
43 | * |
---|
44 | * Parameters: |
---|
45 | * options - {Object} Optional object whose properties will be set on the |
---|
46 | * instance. |
---|
47 | */ |
---|
48 | initialize: function(options) { |
---|
49 | OpenLayers.Strategy.prototype.initialize.apply(this, [options]); |
---|
50 | }, |
---|
51 | |
---|
52 | /** |
---|
53 | * APIMethod: activate |
---|
54 | * Activate the strategy. Register any listeners, do appropriate setup. |
---|
55 | * |
---|
56 | * Returns: |
---|
57 | * {Boolean} True if the strategy was successfully activated. |
---|
58 | */ |
---|
59 | activate: function() { |
---|
60 | var activated = OpenLayers.Strategy.prototype.activate.call(this); |
---|
61 | if(activated) { |
---|
62 | if(this.layer.visibility === true) { |
---|
63 | this.start(); |
---|
64 | } |
---|
65 | this.layer.events.on({ |
---|
66 | "visibilitychanged": this.reset, |
---|
67 | scope: this |
---|
68 | }); |
---|
69 | } |
---|
70 | return activated; |
---|
71 | }, |
---|
72 | |
---|
73 | /** |
---|
74 | * APIMethod: deactivate |
---|
75 | * Deactivate the strategy. Unregister any listeners, do appropriate |
---|
76 | * tear-down. |
---|
77 | * |
---|
78 | * Returns: |
---|
79 | * {Boolean} True if the strategy was successfully deactivated. |
---|
80 | */ |
---|
81 | deactivate: function() { |
---|
82 | var deactivated = OpenLayers.Strategy.prototype.deactivate.call(this); |
---|
83 | if(deactivated) { |
---|
84 | this.stop(); |
---|
85 | } |
---|
86 | return deactivated; |
---|
87 | }, |
---|
88 | |
---|
89 | /** |
---|
90 | * Method: reset |
---|
91 | * Start or cancel the refresh interval depending on the visibility of |
---|
92 | * the layer. |
---|
93 | */ |
---|
94 | reset: function() { |
---|
95 | if(this.layer.visibility === true) { |
---|
96 | this.start(); |
---|
97 | } else { |
---|
98 | this.stop(); |
---|
99 | } |
---|
100 | }, |
---|
101 | |
---|
102 | /** |
---|
103 | * Method: start |
---|
104 | * Start the refresh interval. |
---|
105 | */ |
---|
106 | start: function() { |
---|
107 | if(this.interval && typeof this.interval === "number" && |
---|
108 | this.interval > 0) { |
---|
109 | |
---|
110 | this.timer = window.setInterval( |
---|
111 | OpenLayers.Function.bind(this.refresh, this), |
---|
112 | this.interval); |
---|
113 | } |
---|
114 | }, |
---|
115 | |
---|
116 | /** |
---|
117 | * APIMethod: refresh |
---|
118 | * Tell the strategy to refresh which will refresh the layer. |
---|
119 | */ |
---|
120 | refresh: function() { |
---|
121 | if (this.layer && this.layer.refresh && |
---|
122 | typeof this.layer.refresh == "function") { |
---|
123 | |
---|
124 | this.layer.refresh({force: this.force}); |
---|
125 | } |
---|
126 | }, |
---|
127 | |
---|
128 | /** |
---|
129 | * Method: stop |
---|
130 | * Cancels the refresh interval. |
---|
131 | */ |
---|
132 | stop: function() { |
---|
133 | if(this.timer !== null) { |
---|
134 | window.clearInterval(this.timer); |
---|
135 | this.timer = null; |
---|
136 | } |
---|
137 | }, |
---|
138 | |
---|
139 | CLASS_NAME: "OpenLayers.Strategy.Refresh" |
---|
140 | }); |
---|