Fixed.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository 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. var activated = OpenLayers.Strategy.prototype.activate.apply(this, arguments);
  41. if(activated) {
  42. this.layer.events.on({
  43. "refresh": this.load,
  44. scope: this
  45. });
  46. if(this.layer.visibility == true || this.preload) {
  47. this.load();
  48. } else {
  49. this.layer.events.on({
  50. "visibilitychanged": this.load,
  51. scope: this
  52. });
  53. }
  54. }
  55. return activated;
  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", {filter: layer.filter});
  85. layer.protocol.read(OpenLayers.Util.applyDefaults({
  86. callback: this.merge,
  87. filter: layer.filter,
  88. scope: this
  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. * If the layer projection differs from the map projection, features
  99. * will be transformed from the layer projection to the map projection.
  100. *
  101. * Parameters:
  102. * resp - {<OpenLayers.Protocol.Response>} The response object passed
  103. * by the protocol.
  104. */
  105. merge: function(resp) {
  106. var layer = this.layer;
  107. layer.destroyFeatures();
  108. var features = resp.features;
  109. if (features && features.length > 0) {
  110. var remote = layer.projection;
  111. var local = layer.map.getProjectionObject();
  112. if(!local.equals(remote)) {
  113. var geom;
  114. for(var i=0, len=features.length; i<len; ++i) {
  115. geom = features[i].geometry;
  116. if(geom) {
  117. geom.transform(remote, local);
  118. }
  119. }
  120. }
  121. layer.addFeatures(features);
  122. }
  123. layer.events.triggerEvent("loadend", {response: resp});
  124. },
  125. CLASS_NAME: "OpenLayers.Strategy.Fixed"
  126. });