CacheRead.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/Control.js
  7. */
  8. /**
  9. * Class: OpenLayers.Control.CacheRead
  10. * A control for using image tiles cached with <OpenLayers.Control.CacheWrite>
  11. * from the browser's local storage.
  12. *
  13. * Inherits from:
  14. * - <OpenLayers.Control>
  15. */
  16. OpenLayers.Control.CacheRead = OpenLayers.Class(OpenLayers.Control, {
  17. /**
  18. * APIProperty: fetchEvent
  19. * {String} The layer event to listen to for replacing remote resource tile
  20. * URLs with cached data URIs. Supported values are "tileerror" (try
  21. * remote first, fall back to cached) and "tileloadstart" (try cache
  22. * first, fall back to remote). Default is "tileloadstart".
  23. *
  24. * Note that "tileerror" will not work for CORS enabled images (see
  25. * https://developer.mozilla.org/en/CORS_Enabled_Image), i.e. layers
  26. * configured with a <OpenLayers.Tile.Image.crossOriginKeyword> in
  27. * <OpenLayers.Layer.Grid.tileOptions>.
  28. */
  29. fetchEvent: "tileloadstart",
  30. /**
  31. * APIProperty: layers
  32. * {Array(<OpenLayers.Layer.Grid>)}. Optional. If provided, only these
  33. * layers will receive tiles from the cache.
  34. */
  35. layers: null,
  36. /**
  37. * APIProperty: autoActivate
  38. * {Boolean} Activate the control when it is added to a map. Default is
  39. * true.
  40. */
  41. autoActivate: true,
  42. /**
  43. * Constructor: OpenLayers.Control.CacheRead
  44. *
  45. * Parameters:
  46. * options - {Object} Object with API properties for this control
  47. */
  48. /**
  49. * Method: setMap
  50. * Set the map property for the control.
  51. *
  52. * Parameters:
  53. * map - {<OpenLayers.Map>}
  54. */
  55. setMap: function(map) {
  56. OpenLayers.Control.prototype.setMap.apply(this, arguments);
  57. var i, layers = this.layers || map.layers;
  58. for (i=layers.length-1; i>=0; --i) {
  59. this.addLayer({layer: layers[i]});
  60. }
  61. if (!this.layers) {
  62. map.events.on({
  63. addlayer: this.addLayer,
  64. removeLayer: this.removeLayer,
  65. scope: this
  66. });
  67. }
  68. },
  69. /**
  70. * Method: addLayer
  71. * Adds a layer to the control. Once added, tiles requested for this layer
  72. * will be cached.
  73. *
  74. * Parameters:
  75. * evt - {Object} Object with a layer property referencing an
  76. * <OpenLayers.Layer> instance
  77. */
  78. addLayer: function(evt) {
  79. evt.layer.events.register(this.fetchEvent, this, this.fetch);
  80. },
  81. /**
  82. * Method: removeLayer
  83. * Removes a layer from the control. Once removed, tiles requested for this
  84. * layer will no longer be cached.
  85. *
  86. * Parameters:
  87. * evt - {Object} Object with a layer property referencing an
  88. * <OpenLayers.Layer> instance
  89. */
  90. removeLayer: function(evt) {
  91. evt.layer.events.unregister(this.fetchEvent, this, this.fetch);
  92. },
  93. /**
  94. * Method: fetch
  95. * Listener to the <fetchEvent> event. Replaces a tile's url with a data
  96. * URI from the cache.
  97. *
  98. * Parameters:
  99. * evt - {Object} Event object with a tile property.
  100. */
  101. fetch: function(evt) {
  102. if (this.active && window.localStorage &&
  103. evt.tile instanceof OpenLayers.Tile.Image) {
  104. var tile = evt.tile,
  105. url = tile.url;
  106. // deal with modified tile urls when both CacheWrite and CacheRead
  107. // are active
  108. if (!tile.layer.crossOriginKeyword && OpenLayers.ProxyHost &&
  109. url.indexOf(OpenLayers.ProxyHost) === 0) {
  110. url = OpenLayers.Control.CacheWrite.urlMap[url];
  111. }
  112. var dataURI = window.localStorage.getItem("olCache_" + url);
  113. if (dataURI) {
  114. tile.url = dataURI;
  115. if (evt.type === "tileerror") {
  116. tile.setImgSrc(dataURI);
  117. }
  118. }
  119. }
  120. },
  121. /**
  122. * Method: destroy
  123. * The destroy method is used to perform any clean up before the control
  124. * is dereferenced. Typically this is where event listeners are removed
  125. * to prevent memory leaks.
  126. */
  127. destroy: function() {
  128. if (this.layers || this.map) {
  129. var i, layers = this.layers || this.map.layers;
  130. for (i=layers.length-1; i>=0; --i) {
  131. this.removeLayer({layer: layers[i]});
  132. }
  133. }
  134. if (this.map) {
  135. this.map.events.un({
  136. addlayer: this.addLayer,
  137. removeLayer: this.removeLayer,
  138. scope: this
  139. });
  140. }
  141. OpenLayers.Control.prototype.destroy.apply(this, arguments);
  142. },
  143. CLASS_NAME: "OpenLayers.Control.CacheRead"
  144. });