Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

Purge of stale layers now done via setTimeout

Mike McGann156b716ceeb2
diff --git a/js/Worldview/Map/CanvasTile.js b/js/Worldview/Map/CanvasTile.js
index 890c8cafd..4f68da3ce 100644
--- a/js/Worldview/Map/CanvasTile.js
+++ b/js/Worldview/Map/CanvasTile.js
@@ -158,16 +158,22 @@ Worldview.Map.CanvasTile = OpenLayers.Class(OpenLayers.Tile.Image, {
self.graphics.putImageData(imageData, 0, 0);
- OpenLayers.Event.stopObservingElement(self.imgDiv);
canvas.style.visibility = "inherit";
canvas.style.opacity = self.layer.opacity;
- self.isLoading = false;
self.canvasContext = null;
- self.events.triggerEvent("loadend");
+ self.isLoading = false;
+ self.events.triggerEvent("loadend");
self.log.debug(self.id + ": rendered");
},
- applyLookup: function() {
+ applyLookup: function(imageLoaded) {
+ if ( !this.canvas ) {
+ if ( imageLoaded ) {
+ this.isLoading = false;
+ this.events.triggerEvent("loadend");
+ }
+ return;
+ }
this.isLoading = true;
//this.canvas.style.visibility = "hidden";
//this.canvas.style.opacity = 0;
@@ -193,6 +199,8 @@ Worldview.Map.CanvasTile = OpenLayers.Class(OpenLayers.Tile.Image, {
* Draws the loaded image to the canvas and applies a lookup table if
*/
onImageLoad: function() {
+ OpenLayers.Event.stopObservingElement(this.imgDiv);
+
this.log.debug(this.id + ": tile loaded");
this.canvasOriginal = document.createElement("canvas");
this.graphicsOriginal = this.canvasOriginal.getContext("2d");
@@ -204,9 +212,9 @@ Worldview.Map.CanvasTile = OpenLayers.Class(OpenLayers.Tile.Image, {
canvas.height = this.canvasOriginal.height;
this.graphicsOriginal.drawImage(this.imgDiv, 0, 0);
-
+
if ( this.layer.lookupTable ) {
- this.applyLookup();
+ this.applyLookup(true);
} else {
this.canvas.style.visibility = "inherit";
this.canvas.style.opacity = this.layer.opacity;
diff --git a/js/Worldview/Map/DailyProduct.js b/js/Worldview/Map/DailyProduct.js
index b6e864639..915df7809 100644
--- a/js/Worldview/Map/DailyProduct.js
+++ b/js/Worldview/Map/DailyProduct.js
@@ -40,17 +40,11 @@ Worldview.Map.DailyProduct = function(map, config) {
// The current z-index for all layers.
var zIndex = 0;
- // The function used to create a new layer
- var createLayer;
-
- // The layer that is currently displayed on the screen and all layers
- // that have their opacity set to zero for quick display when moving
- // the date.
- var validLayers = {};
+ var cachedLayers = {};
// Layers that are no longer valid because the map has moved. These
// can be removed on the next map redrawn
- var invalidLayers = [];
+ var staleLayers = [];
// The layer that is currently visible and seen by the user
var currentLayer = null;
@@ -60,6 +54,8 @@ Worldview.Map.DailyProduct = function(map, config) {
var lookupTable = null;
+ var reaperId = null;
+
//-------------------------------------------------------------------------
// Public
//-------------------------------------------------------------------------
@@ -71,46 +67,18 @@ Worldview.Map.DailyProduct = function(map, config) {
map.events.register("zoomend", self, onZoomEnd);
};
- /**
- * Method: setDay
- * Changes the map to display the product for the given day.
- *
- * Parameters:
- * d - The day to display.
- */
- self.setDay = function(d) {
- if ( !d ) {
- return;
- }
- var ds = Worldview.toISODateString(d);
- if ( ds === currentDay ) {
- return;
- }
- currentDay = ds;
-
+ var fetchLayer = function() {
var previousLayer = currentLayer;
-
+
// If a layer was visible, hide it.
if ( previousLayer ) {
previousLayer.div.style.opacity = 0;
}
- // If the layer has already been created, set it as the current
- // layer.
- if ( currentDay in validLayers ) {
- currentLayer = validLayers[currentDay];
-
- /*
- // If the layer was invalidated and scheduled for removal, reuse
- // it and bring it back to the valid set
- } else if ( currentDay in invalidLayers ) {
- currentLayer = invalidLayers[currentDay];
- validLayers[currentDay] = currentLayer;
- delete invalidLayers[currentDay];
- */
-
- // Otherwise, create a new layer
- } else {
+ if ( currentDay in cachedLayers ) {
+ currentLayer = cachedLayers[currentDay];
+ delete cachedLayers[currentDay];
+ } else {
var additionalOptions = null;
if ( lookupTable !== null ) {
additionalOptions = {
@@ -122,11 +90,10 @@ Worldview.Map.DailyProduct = function(map, config) {
if ( lookupTable !== null ) {
currentLayer.lookupTable = lookupTable;
}
- validLayers[currentDay] = currentLayer;
currentLayer.div.style.opacity = 0;
- map.addLayer(currentLayer);
+ map.addLayer(currentLayer);
}
-
+
// The visible layer is one level higher than all the other layers
if ( previousLayer ) {
previousLayer.setZIndex(zIndex);
@@ -137,54 +104,50 @@ Worldview.Map.DailyProduct = function(map, config) {
currentLayer.div.style.opacity = currentLayer.opacity;
if ( currentLayer.getVisibility() === false ) {
currentLayer.setVisibility(true);
- }
+ }
+ }
+
+ /**
+ * Method: setDay
+ * Changes the map to display the product for the given day.
+ *
+ * Parameters:
+ * d - The day to display.
+ */
+ self.setDay = function(d) {
+ if ( !d ) {
+ return;
+ }
+ var ds = Worldview.toISODateString(d);
+ if ( currentLayer && ds === currentDay ) {
+ return;
+ }
+ if ( currentLayer ) {
+ cachedLayers[currentDay] = currentLayer;
+ }
+ currentDay = ds;
+ fetchLayer();
};
self.setLookup = function(lookup) {
- if ( lookupTable === null ) {
- purge(true);
- currentLayer = self.createLayer({
- tileClass: Worldview.Map.CanvasTile
- });
- currentLayer.lookupTable = lookup;
- validLayers[currentDay] = currentLayer;
- map.addLayer(currentLayer);
- currentLayer.setZIndex(zIndex + 1);
+ var resetRequired = (lookupTable === null);
+ lookupTable = lookup;
+ if ( resetRequired ) {
+ reset();
+ fetchLayer();
} else {
- invalidate();
+ clearCache();
currentLayer.lookupTable = lookup;
applyLookup(currentLayer);
}
- lookupTable = lookup;
};
self.clearLookup = function() {
if ( lookupTable !== null ) {
- purge(true);
- currentLayer = self.createLayer();
- validLayers[currentDay] = currentLayer;
- map.addLayer(currentLayer);
- currentLayer.setZIndex(zIndex + 1);
+ lookupTable = null;
+ reset();
+ fetchLayer();
}
- lookupTable = null;
- };
-
- /**
- * Method: getStatistics
- * Gets number of valid and invalid layers.
- *
- * Returns:
- * A object containing two properites:
- * - valid: The number of layers that are active and can be used for
- * quickly flipping through days.
- * - invalid: The number of layers that can no longer be used for display
- * and will be removed the next time the map is drawn.
- */
- self.getStatistics = function() {
- return {
- valid: Worldview.size(validLayers),
- invalid: Worldview.size(invalidLayers)
- };
};
/**
@@ -205,14 +168,10 @@ Worldview.Map.DailyProduct = function(map, config) {
* Remove all layers from the map.
*/
self.dispose = function() {
- $.each(validLayers, function(index, layer) {
- map.removeLayer(layer);
- });
- validLayers = {};
- $.each(invalidLayers, function(index, layer) {
- map.removeLayer(layer);
- });
- invalidLayers = [];
+ reset();
+ if ( reaperId !== null ) {
+ clearTimeout(repaerId);
+ }
map.events.unregister("movestart", self, onMoveStart);
map.events.unregister("zoomend", self, onZoomEnd);
};
@@ -233,7 +192,7 @@ Worldview.Map.DailyProduct = function(map, config) {
* Sets the z-index on all layers.
*/
var refreshZOrder = function() {
- $.each(validLayers, function(i, layer) {
+ $.each(cachedLayers, function(i, layer) {
layer.setZIndex(zIndex);
});
if ( currentLayer ) {
@@ -241,54 +200,49 @@ Worldview.Map.DailyProduct = function(map, config) {
}
};
+ var reset = function() {
+ clearCache();
+ map.removeLayer(currentLayer);
+ currentLayer = null;
+ }
+
/*
* Hide all layers that are not currently being displayed and move them
* to the invalid set.
*/
- var invalidate = function() {
- if ( Worldview.size(validLayers) <= 1 ) {
- return;
- }
+ var clearCache = function() {
+ $.each(cachedLayers, function(day, layer) {
+ staleLayers.push(layer);
+ });
+ cachedLayers = {};
- for ( var day in validLayers ) {
- if ( validLayers.hasOwnProperty(day) ) {
- if ( day !== currentDay ) {
- invalidLayers.push(validLayers[day]);
- validLayers[day].setVisibility(false);
- }
- }
+ if ( reaperId !== null ) {
+ clearTimeout(reaperId);
}
- previousLayer = null;
- validLayers = {};
- validLayers[currentDay] = currentLayer;
+ reaperId = setTimeout(function() { reaper(); }, 2000);
};
+
/*
* Remove all layers in the invalid set from the map.
*/
- var purge = function(all) {
- invalidate();
- if ( all ) {
- validLayers = {};
- invalidLayers.push(currentLayer);
- currentLayer = null;
- }
- $.each(invalidLayers, function(index, layer) {
+ var reaper = function(all) {
+ $.each(staleLayers, function(index, layer) {
if ( map.getLayerIndex(layer) >= 0 ) {
map.removeLayer(layer);
}
- });
- previousLayer = null;
- invalidLayers = [];
+ });
+ staleLayers = [];
refreshZOrder();
+ reaperId = null;
};
var onMoveStart = function() {
- invalidate();
+ clearCache();
};
var onZoomEnd = function() {
- purge();
+ clearCache();
};
init();