From 96c56e9d841374918020a07043090f16224c9f45 Mon Sep 17 00:00:00 2001 From: euzuro Date: Sat, 12 Aug 2006 15:54:51 +0000 Subject: [PATCH] added 'drawn' property to all tiles. removed redraw() and adapted all draw()s to auto handle redraw. remove getPosition() accessor that noone was using. set generic Tile's moveTo() to auto trigger the clear() and the redraw() (if desired). Update Image and WFS tile classes correspondingly. also update tests git-svn-id: http://svn.openlayers.org/trunk/openlayers@1198 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Tile.js | 34 ++++++------- lib/OpenLayers/Tile/Image.js | 93 +++++++++++++++++++++--------------- lib/OpenLayers/Tile/WFS.js | 65 +++++++++++-------------- tests/test_Tile_Image.html | 6 ++- 4 files changed, 105 insertions(+), 93 deletions(-) diff --git a/lib/OpenLayers/Tile.js b/lib/OpenLayers/Tile.js index 93b06ce6f2..0a6fe7f8ff 100644 --- a/lib/OpenLayers/Tile.js +++ b/lib/OpenLayers/Tile.js @@ -32,6 +32,9 @@ OpenLayers.Tile.prototype = { * @type OpenLayers.Pixel */ position:null, + /** @type Boolean */ + drawn: false, + /** * @constructor * @@ -66,38 +69,35 @@ OpenLayers.Tile.prototype = { /** */ draw:function() { + this.drawn = true; }, - - redraw: function () { - this.draw(); - }, - + /** * @param {OpenLayers.Bounds} * @param {OpenLayers.pixel} position + * @param {Boolean} redraw Redraw tile after moving? + * Default is true */ - moveTo: function (bounds, position) { + moveTo: function (bounds, position, redraw) { + if (redraw == null) { + redraw = true; + } + + this.clear(); this.bounds = bounds.clone(); this.position = position.clone(); - this.redraw(); + if (redraw) { + this.draw(); + } }, - /** Clear the tile of any bounds/position-related data so that it can * be reused in a new location. */ clear: function() { - this.bounds = null; - this.position = null; + this.drawn = false; }, - /** - * @type OpenLayers.Pixel - */ - getPosition: function() { - return this.position; - }, - /** @final @type String */ CLASS_NAME: "OpenLayers.Tile" }; diff --git a/lib/OpenLayers/Tile/Image.js b/lib/OpenLayers/Tile/Image.js index 0e04819308..fb89d61e70 100644 --- a/lib/OpenLayers/Tile/Image.js +++ b/lib/OpenLayers/Tile/Image.js @@ -1,7 +1,9 @@ /* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license. * See http://svn.openlayers.org/trunk/openlayers/license.txt for the full * text of the license. */ + // @require: OpenLayers/Tile.js + /** * @class */ @@ -24,7 +26,10 @@ OpenLayers.Tile.Image.prototype = initialize: function(layer, position, bounds, url, size) { OpenLayers.Tile.prototype.initialize.apply(this, arguments); }, - + + /** + * + */ destroy: function() { if ((this.imgDiv != null) && (this.imgDiv.parentNode == this.layer.div)) { this.layer.div.removeChild(this.imgDiv); @@ -34,46 +39,15 @@ OpenLayers.Tile.Image.prototype = }, /** - */ + * + */ draw:function() { - if (this.layer.alpha) { - this.imgDiv = OpenLayers.Util.createAlphaImageDiv(null, - this.position, - this.size, - this.url, - "absolute", - null, - null, - true); - } else { - this.imgDiv = OpenLayers.Util.createImage(null, - this.position, - this.size, - this.url, - "absolute", - null, - true); + OpenLayers.Tile.prototype.draw.apply(this, arguments); + + if (this.imgDiv == null) { + this.initImgDiv(); } - this.layer.div.appendChild(this.imgDiv); - }, - /** Clear the tile of any bounds/position-related data so that it can - * be reused in a new location. - */ - clear: function() { - this.imgDiv.style.display = "none"; - }, - - /** - * @param {OpenLayers.Bounds} - * @param {OpenLayers.pixel} position - */ - moveTo: function (bounds, position) { - this.url = this.layer.getURL(bounds); - OpenLayers.Tile.prototype.moveTo.apply(this, arguments); - }, - - redraw: function () { this.imgDiv.style.display = "none"; if (this.layer.alpha) { OpenLayers.Util.modifyAlphaImageDiv(this.imgDiv, @@ -85,6 +59,49 @@ OpenLayers.Tile.Image.prototype = } }, + /** Clear the tile of any bounds/position-related data so that it can + * be reused in a new location. + */ + clear: function() { + OpenLayers.Tile.prototype.clear.apply(this, arguments); + this.imgDiv.style.display = "none"; + }, + + /** + * @param {OpenLayers.Bounds} + * @param {OpenLayers.pixel} position + * @param {Boolean} redraw + */ + moveTo: function (bounds, position, redraw) { + this.url = this.layer.getURL(bounds); + OpenLayers.Tile.prototype.moveTo.apply(this, arguments); + }, + + /** + * + */ + initImgDiv: function() { + if (this.layer.alpha) { + this.imgDiv = OpenLayers.Util.createAlphaImageDiv(null, + this.position, + this.size, + null, + "absolute", + null, + null, + true); + } else { + this.imgDiv = OpenLayers.Util.createImage(null, + this.position, + this.size, + null, + "absolute", + null, + true); + } + this.layer.div.appendChild(this.imgDiv); + }, + /** @final @type String */ CLASS_NAME: "OpenLayers.Tile.Image" } diff --git a/lib/OpenLayers/Tile/WFS.js b/lib/OpenLayers/Tile/WFS.js index 0754e41ae9..cf38e67c40 100644 --- a/lib/OpenLayers/Tile/WFS.js +++ b/lib/OpenLayers/Tile/WFS.js @@ -39,38 +39,31 @@ OpenLayers.Tile.WFS.prototype = * */ destroy: function() { - this.clear(); - this.urls = null; OpenLayers.Tile.prototype.destroy.apply(this, arguments); + this.destroyAllFeatures(); + this.features = null; + this.urls = null; }, /** Clear the tile of any bounds/position-related data so that it can * be reused in a new location. */ clear: function() { - while(this.features.length > 0) { - var feature = this.features.shift(); - feature.destroy(); - } + OpenLayers.Tile.prototype.clear.apply(this, arguments); + this.destroyAllFeatures(); }, /** - */ + * + */ draw:function() { + if (this.drawn) { + this.clear(); + } + OpenLayers.Tile.prototype.draw.apply(this, arguments); this.loadFeaturesForRegion(this.requestSuccess); }, - /** - * @param {OpenLayers.Bounds} - * @param {OpenLayers.pixel} position - */ - moveTo: function (bounds, position) { - - this.loaded = false; - - OpenLayers.Tile.prototype.moveTo.apply(this, arguments); - }, - /** get the full request string from the ds and the tile params * and call the AJAX loadURL(). * @@ -81,22 +74,13 @@ OpenLayers.Tile.WFS.prototype = */ loadFeaturesForRegion:function(success, failure) { - if (!this.loaded) { + if (this.urls != null) { - if (this.urls != null) { - - // TODO: Hmmm, this stops multiple loads of the data when a - // result isn't immediately retrieved, but it's hacky. - // Do it better. - this.loaded = true; - - for(var i=0; i < this.urls.length; i++) { - var params ={ BBOX:bounds.toBBOX() }; - var url = this.urls[i] + - OpenLayers.Util.getParameterString(params); - OpenLayers.loadURL(url, null, this, - success, failure); - } + for(var i=0; i < this.urls.length; i++) { + var params = { BBOX:bounds.toBBOX() }; + var url = this.urls[i] + + OpenLayers.Util.getParameterString(params); + OpenLayers.loadURL(url, null, this, success, failure); } } }, @@ -121,16 +105,25 @@ OpenLayers.Tile.WFS.prototype = * @param {Object} results */ addResults: function(results) { - for (var i=0; i < results.length; i++) { - var feature = new this.layer.featureClass(this.layer, results[i]); this.features.push(feature); } - }, + /** Iterate through and call destroy() on each feature, removing it from + * the local array + * + * @private + */ + destroyAllFeatures: function() { + while(this.features.length > 0) { + var feature = this.features.shift(); + feature.destroy(); + } + }, + /** @final @type String */ CLASS_NAME: "OpenLayers.Tile.WFS" } diff --git a/tests/test_Tile_Image.html b/tests/test_Tile_Image.html index ceecc543cb..f22ef2effb 100644 --- a/tests/test_Tile_Image.html +++ b/tests/test_Tile_Image.html @@ -8,7 +8,9 @@ function test_01_Tile_Image_constructor (t) { t.plan( 6 ); - var layer = new Object(); //bogus layer + var layer = new OpenLayers.Layer.WMS( "OpenLayers WMS", + "http://labs.metacarta.com/wms/vmap0?", {layers: 'basic'}); + var position = new OpenLayers.Pixel(20,30); var bounds = new OpenLayers.Bounds(1,2,3,4); var url = "http://www.openlayers.org/dev/tests/tileimage"; @@ -16,7 +18,7 @@ tile = new OpenLayers.Tile.Image(layer, position, bounds, url, size); t.ok( tile instanceof OpenLayers.Tile.Image, "new OpenLayers.Tile returns Tile object" ); - t.eq( tile.layer, layer, "tile.layer is set correctly"); + t.ok( tile.layer == layer, "tile.layer is set correctly"); t.ok( tile.position.equals(position), "tile.position is set correctly"); t.ok( tile.bounds.equals(bounds), "tile.bounds is set correctly"); t.eq( tile.url, url, "tile.url is set correctly");