New beforeload event and slightly changed loading sequence

Having the TileManager remove an image from the DOM, then setting the
cached image, and then having to position it felt a bit awkward. With the
new beforeload event, the setImage method and putting renderTile before
positionTile, providing the cached image feels way more natural.
This commit is contained in:
ahocevar
2012-12-18 01:21:11 +01:00
parent 64df7e3d04
commit a02e08ad2a
3 changed files with 38 additions and 17 deletions

View File

@@ -20,6 +20,23 @@
*/
OpenLayers.Tile.Image = OpenLayers.Class(OpenLayers.Tile, {
/**
* APIProperty: events
* {<OpenLayers.Events>} An events object that handles all
* events on the tile.
*
* Register a listener for a particular event with the following syntax:
* (code)
* tile.events.register(type, obj, listener);
* (end)
*
* Supported event types (in addition to the <OpenLayers.Tile> events):
* beforeload - Triggered before an image is prepared for loading, when the
* url for the image is known already. Listeners may call <setImage> on
* the tile instance. If they do so, that image will be used and no new
* one will be created.
*/
/**
* APIProperty: url
* {String} The URL of the image being requested. No default. Filled in by
@@ -164,8 +181,8 @@ OpenLayers.Tile.Image = OpenLayers.Class(OpenLayers.Tile, {
this.isLoading = true;
this._loadEvent = "loadstart";
}
this.positionTile();
this.renderTile();
this.positionTile();
} else if (shouldDraw === false) {
this.unload();
}
@@ -178,7 +195,6 @@ OpenLayers.Tile.Image = OpenLayers.Class(OpenLayers.Tile, {
* position it correctly, and set its url.
*/
renderTile: function() {
this.layer.div.appendChild(this.getTile());
if (this.layer.async) {
// Asynchronous image requests call the asynchronous getURL method
// on the layer to fetch an image that covers 'this.bounds'.
@@ -279,12 +295,26 @@ OpenLayers.Tile.Image = OpenLayers.Class(OpenLayers.Tile, {
return this.imgDiv;
},
/**
* APIMethod: setImage
* Sets the image element or this tile. This method should only be called
* from beforeload listeners.
*
* Paramters
* img - {HTMLImageElement} The image to use for this tile.
*/
setImage: function(img) {
this.imgDiv = img;
},
/**
* Method: initImage
* Creates the content for the frame on the tile.
*/
initImage: function() {
this.events.triggerEvent('beforeload');
this.layer.div.appendChild(this.getTile());
this.events.triggerEvent(this._loadEvent);
var img = this.getImage();
if (this.url && img.getAttribute("src") == this.url) {