Fixed.js 3.9 KB

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