From 01debb1c533dc9ce8dcde7cabfec7f475bbb8677 Mon Sep 17 00:00:00 2001 From: bartvde Date: Mon, 9 Nov 2009 14:43:58 +0000 Subject: [PATCH] Create a possibility to refresh a layer every N seconds: OpenLayers.Strategy.Refresh, excellent patch by krisgeus, r=me (closes #2317) git-svn-id: http://svn.openlayers.org/trunk/openlayers@9790 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers.js | 1 + lib/OpenLayers/Strategy/Refresh.js | 139 +++++++++++++++++++++++++ tests/Strategy/Refresh.html | 162 +++++++++++++++++++++++++++++ tests/list-tests.html | 1 + 4 files changed, 303 insertions(+) create mode 100644 lib/OpenLayers/Strategy/Refresh.js create mode 100644 tests/Strategy/Refresh.html diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index 8239c423a1..4ced6f3020 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -194,6 +194,7 @@ "OpenLayers/Strategy/Paging.js", "OpenLayers/Strategy/BBOX.js", "OpenLayers/Strategy/Save.js", + "OpenLayers/Strategy/Refresh.js", "OpenLayers/Protocol.js", "OpenLayers/Protocol/HTTP.js", "OpenLayers/Protocol/SQL.js", diff --git a/lib/OpenLayers/Strategy/Refresh.js b/lib/OpenLayers/Strategy/Refresh.js new file mode 100644 index 0000000000..44d692fabf --- /dev/null +++ b/lib/OpenLayers/Strategy/Refresh.js @@ -0,0 +1,139 @@ +/* Copyright (c) 2006-2009 MetaCarta, Inc., published under the Clear BSD + * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the + * full text of the license. */ + +/** + * @requires OpenLayers/Strategy.js + */ + +/** + * Class: OpenLayers.Strategy.Refresh + * A strategy that refreshes the layer. By default the strategy waits for a + * call to before refreshing. By configuring the strategy with + * the option, refreshing can take place automatically. + * + * Inherits from: + * - + */ +OpenLayers.Strategy.Refresh = OpenLayers.Class(OpenLayers.Strategy, { + + /** + * Property: force + * {Boolean} Force a refresh on the layer. Default is false. + */ + force: false, + + /** + * Property: interval + * {Number} Auto-refresh. Default is 0. If > 0, layer will be refreshed + * every N milliseconds. + */ + interval: 0, + + /** + * Property: timer + * {Number} The id of the timer. + */ + timer: null, + + /** + * Constructor: OpenLayers.Strategy.Refresh + * Create a new Refresh strategy. + * + * Parameters: + * options - {Object} Optional object whose properties will be set on the + * instance. + */ + initialize: function(options) { + OpenLayers.Strategy.prototype.initialize.apply(this, [options]); + }, + + /** + * APIMethod: activate + * Activate the strategy. Register any listeners, do appropriate setup. + * + * Returns: + * {Boolean} True if the strategy was successfully activated. + */ + activate: function() { + var activated = OpenLayers.Strategy.prototype.activate.call(this); + if(activated) { + if(this.layer.visibility === true) { + this.start(); + } + this.layer.events.on({ + "visibilitychanged": this.reset, + scope: this + }); + } + return activated; + }, + + /** + * APIMethod: deactivate + * Deactivate the strategy. Unregister any listeners, do appropriate + * tear-down. + * + * Returns: + * {Boolean} True if the strategy was successfully deactivated. + */ + deactivate: function() { + var deactivated = OpenLayers.Strategy.prototype.deactivate.call(this); + if(deactivated) { + this.stop(); + } + return deactivated; + }, + + /** + * Method: reset + * Start or cancel the refresh interval depending on the visibility of + * the layer. + */ + reset: function() { + if(this.layer.visibility === true) { + this.start(); + } else { + this.stop(); + } + }, + + /** + * Method: start + * Start the refresh interval. + */ + start: function() { + if(this.interval && typeof this.interval === "number" && + this.interval > 0) { + + this.timer = window.setInterval( + OpenLayers.Function.bind(this.refresh, this), + this.interval); + } + }, + + /** + * APIMethod: refresh + * Tell the strategy to refresh which will refresh the layer. + */ + refresh: function() { + if (this.layer && this.layer.refresh && + typeof this.layer.refresh == "function") { + + this.layer.refresh({force: this.force}); + } + }, + + /** + * Method: stop + * Cancels the refresh interval. + */ + stop: function() { + if(this.timer !== null) { + window.clearInterval(this.timer); + this.timer = null; + } + }, + + CLASS_NAME: "OpenLayers.Strategy.Refresh" +}); diff --git a/tests/Strategy/Refresh.html b/tests/Strategy/Refresh.html new file mode 100644 index 0000000000..d04b55a797 --- /dev/null +++ b/tests/Strategy/Refresh.html @@ -0,0 +1,162 @@ + + + + + + +
+ + diff --git a/tests/list-tests.html b/tests/list-tests.html index 61f4bddbfd..2d4523fa67 100644 --- a/tests/list-tests.html +++ b/tests/list-tests.html @@ -166,6 +166,7 @@
  • Strategy/Fixed.html
  • Strategy/Paging.html
  • Strategy/Save.html
  • +
  • Strategy/Refresh.html
  • Style.html
  • StyleMap.html
  • Tile.html