Refresh.js 3.5 KB

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