Skip to content

Commit

Permalink
Made a working callback in TilePaintingProvider based on similar uses…
Browse files Browse the repository at this point in the history
… elsewhere - also releasing tiles
  • Loading branch information
migurski committed May 10, 2011
1 parent 28e6990 commit 286d746
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 39 deletions.
60 changes: 41 additions & 19 deletions modestmaps.js
Expand Up @@ -533,27 +533,35 @@ if (!com) {
this.template_provider = template_provider;
this.request_manager = request_manager;

var divs = {};

// this only seems to work as a closure - why?
function onLoaded(mgr, t)
this.divs = {};
}

MM.extend(MM.TilePaintingProvider, MM.MapProvider);

MM.TilePaintingProvider.prototype.getOnTileLoaded = function(img)
{
if(!this._onTileLoaded)
{
if(t.id in divs)
var theProvider = this;

this._onTileLoaded = function(img)
{
divs[t.id].appendChild(t);
delete divs[t.id];
if(img.id in theProvider.divs)
{
img.style.width = '100%';
img.style.height = '100%';
theProvider.divs[img.id].appendChild(img);
delete theProvider.divs[img.id];
}
}
}

this.request_manager.addCallback('requestcomplete', onLoaded);
this.divs = divs;
return this._onTileLoaded;
}

MM.extend(MM.TilePaintingProvider, MM.MapProvider);


MM.TilePaintingProvider.prototype.getTileElement = function(coord)
{
this.request_manager.requestTile(coord.toKey(), coord, this.template_provider.getTileUrl(coord));
this.request_manager.requestTile(coord.toKey(), coord, this.template_provider.getTileUrl(coord), this.getOnTileLoaded());

if(coord.toKey() in this.divs)
{
Expand All @@ -565,8 +573,12 @@ if (!com) {
return div;
}

MM.TilePaintingProvider.prototype.releaseTileElement = function(element)
MM.TilePaintingProvider.prototype.releaseTileElement = function(coord)
{
if(coord.toKey() in this.divs)
{
delete this.divs[coord.toKey()];
}
}
//////////////////////////// Event Handlers

Expand Down Expand Up @@ -866,9 +878,10 @@ if (!com) {

// TODO: remove dependency on coord (it's for sorting, maybe call it data?)
// TODO: rename to requestImage once it's not tile specific
requestTile: function(key, coord, url) {
// callback is optional; used in getLoadComplete()
requestTile: function(key, coord, url, callback) {
if (!(key in this.requestsById)) {
var request = { key: key, coord: coord.copy(), url: url };
var request = { key: key, coord: coord.copy(), url: url, callback: callback };
// if there's no url just make sure we don't request this image again
this.requestsById[key] = request;
if (url) {
Expand Down Expand Up @@ -943,6 +956,11 @@ if (!com) {

// unset these straight away so we don't call this twice
img.onload = img.onerror = null;

// get the callback if one exists
if('callback' in theManager.requestsById[img.id]) {
var callback = theManager.requestsById[img.id].callback;
}

// pull it back out of the (hidden) DOM
// so that draw will add it correctly later
Expand All @@ -955,6 +973,10 @@ if (!com) {
// NB:- complete is also true onerror if we got a 404
if (img.complete ||
(img.readyState && img.readyState == 'complete')) {
if(callback != undefined) {
callback(img);
}

theManager.dispatchCallback('requestcomplete', img);
}
else {
Expand Down Expand Up @@ -1349,7 +1371,7 @@ if (!com) {
if (this.layers.hasOwnProperty(name)) {
var layer = this.layers[name];
while(layer.firstChild) {
this.provider.releaseTileElement(layer.firstChild);
this.provider.releaseTileElement(layer.firstChild.coord);
layer.removeChild(layer.firstChild);
}
}
Expand Down Expand Up @@ -1501,7 +1523,7 @@ if (!com) {
layer.style.display = 'none';
var visibleTiles = layer.getElementsByTagName('img');
for (var j = visibleTiles.length-1; j >= 0; j--) {
this.provider.releaseTileElement(visibleTiles[j]);
this.provider.releaseTileElement(visibleTiles[j].coord);
layer.removeChild(visibleTiles[j]);
}
}
Expand Down Expand Up @@ -1655,7 +1677,7 @@ if (!com) {
for (var j = visibleTiles.length-1; j >= 0; j--) {
var tile = visibleTiles[j];
if (!valid_tile_keys[tile.id]) {
this.provider.releaseTileElement(tile);
this.provider.releaseTileElement(tile.coord);
layer.removeChild(tile);
}
else {
Expand Down
2 changes: 1 addition & 1 deletion modestmaps.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/map.js
Expand Up @@ -370,7 +370,7 @@
if (this.layers.hasOwnProperty(name)) {
var layer = this.layers[name];
while(layer.firstChild) {
this.provider.releaseTileElement(layer.firstChild);
this.provider.releaseTileElement(layer.firstChild.coord);
layer.removeChild(layer.firstChild);
}
}
Expand Down Expand Up @@ -522,7 +522,7 @@
layer.style.display = 'none';
var visibleTiles = layer.getElementsByTagName('img');
for (var j = visibleTiles.length-1; j >= 0; j--) {
this.provider.releaseTileElement(visibleTiles[j]);
this.provider.releaseTileElement(visibleTiles[j].coord);
layer.removeChild(visibleTiles[j]);
}
}
Expand Down Expand Up @@ -676,7 +676,7 @@
for (var j = visibleTiles.length-1; j >= 0; j--) {
var tile = visibleTiles[j];
if (!valid_tile_keys[tile.id]) {
this.provider.releaseTileElement(tile);
this.provider.releaseTileElement(tile.coord);
layer.removeChild(tile);
}
else {
Expand Down
40 changes: 26 additions & 14 deletions src/provider.js
Expand Up @@ -105,27 +105,35 @@
this.template_provider = template_provider;
this.request_manager = request_manager;

var divs = {};

// this only seems to work as a closure - why?
function onLoaded(mgr, t)
this.divs = {};
}

MM.extend(MM.TilePaintingProvider, MM.MapProvider);

MM.TilePaintingProvider.prototype.getOnTileLoaded = function(img)
{
if(!this._onTileLoaded)
{
if(t.id in divs)
var theProvider = this;

this._onTileLoaded = function(img)
{
divs[t.id].appendChild(t);
delete divs[t.id];
if(img.id in theProvider.divs)
{
img.style.width = '100%';
img.style.height = '100%';
theProvider.divs[img.id].appendChild(img);
delete theProvider.divs[img.id];
}
}
}

this.request_manager.addCallback('requestcomplete', onLoaded);
this.divs = divs;
return this._onTileLoaded;
}

MM.extend(MM.TilePaintingProvider, MM.MapProvider);


MM.TilePaintingProvider.prototype.getTileElement = function(coord)
{
this.request_manager.requestTile(coord.toKey(), coord, this.template_provider.getTileUrl(coord));
this.request_manager.requestTile(coord.toKey(), coord, this.template_provider.getTileUrl(coord), this.getOnTileLoaded());

if(coord.toKey() in this.divs)
{
Expand All @@ -137,6 +145,10 @@
return div;
}

MM.TilePaintingProvider.prototype.releaseTileElement = function(element)
MM.TilePaintingProvider.prototype.releaseTileElement = function(coord)
{
if(coord.toKey() in this.divs)
{
delete this.divs[coord.toKey()];
}
}
14 changes: 12 additions & 2 deletions src/requests.js
Expand Up @@ -96,9 +96,10 @@

// TODO: remove dependency on coord (it's for sorting, maybe call it data?)
// TODO: rename to requestImage once it's not tile specific
requestTile: function(key, coord, url) {
// callback is optional; used in getLoadComplete()
requestTile: function(key, coord, url, callback) {
if (!(key in this.requestsById)) {
var request = { key: key, coord: coord.copy(), url: url };
var request = { key: key, coord: coord.copy(), url: url, callback: callback };
// if there's no url just make sure we don't request this image again
this.requestsById[key] = request;
if (url) {
Expand Down Expand Up @@ -173,6 +174,11 @@

// unset these straight away so we don't call this twice
img.onload = img.onerror = null;

// get the callback if one exists
if('callback' in theManager.requestsById[img.id]) {
var callback = theManager.requestsById[img.id].callback;
}

// pull it back out of the (hidden) DOM
// so that draw will add it correctly later
Expand All @@ -185,6 +191,10 @@
// NB:- complete is also true onerror if we got a 404
if (img.complete ||
(img.readyState && img.readyState == 'complete')) {
if(callback != undefined) {
callback(img);
}

theManager.dispatchCallback('requestcomplete', img);
}
else {
Expand Down

0 comments on commit 286d746

Please sign in to comment.