From 3791712a10f9cc09af237da25b7ccb8759641b91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Fri, 8 Aug 2008 12:23:59 +0000 Subject: [PATCH] maxZIndex becomes NULL when the last feature is removed, p=pvalsecc, r=me (closes #1670) git-svn-id: http://svn.openlayers.org/trunk/openlayers@7712 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Renderer/Elements.js | 24 +++++++-- tests/Renderer/Elements.html | 76 +++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 5 deletions(-) diff --git a/lib/OpenLayers/Renderer/Elements.js b/lib/OpenLayers/Renderer/Elements.js index 1113d0bcee..f40120cc2e 100644 --- a/lib/OpenLayers/Renderer/Elements.js +++ b/lib/OpenLayers/Renderer/Elements.js @@ -128,24 +128,37 @@ OpenLayers.ElementsIndexer = OpenLayers.Class({ // Reset the maxium z-index based on the last item in the // order array. - var lastId = this.order[this.order.length - 1]; - this.maxZIndex = this.indices[lastId]; + if (this.order.length > 0) { + var lastId = this.order[this.order.length - 1]; + this.maxZIndex = this.indices[lastId]; + } else { + this.maxZIndex = 0; + } } }, + /** + * APIMethod: clear + */ + clear: function() { + this.order = []; + this.indices = {}; + this.maxZIndex = 0; + }, + /** * APIMethod: exists - * + * * Parameters: * node- {DOMElement} The node to test for existence. - * + * * Returns: * {Boolean} Whether or not the node exists in the indexer? */ exists: function(node) { return (this.indices[node.id] != null); }, - + /** * APIMethod: getZIndex * Get the z-index value for the current node from the node data itself. @@ -395,6 +408,7 @@ OpenLayers.Renderer.Elements = OpenLayers.Class(OpenLayers.Renderer, { this.root.removeChild(this.root.firstChild); } } + this.indexer.clear(); }, /** diff --git a/tests/Renderer/Elements.html b/tests/Renderer/Elements.html index edd71547b7..a94d4fcca9 100644 --- a/tests/Renderer/Elements.html +++ b/tests/Renderer/Elements.html @@ -452,6 +452,82 @@ OpenLayers.Util.getElement = OpenLayers.Util._getElement; } + function test_Elements_drawAndErase(t) { + t.plan(20); + + setUp(); + + var r = create_renderer(); + var element = document.createElement("div"); + r.root = element; + document.body.appendChild(element); + + r.createNode = function(type, id) { + var element = document.createElement("div"); + element.id = id; + return element; + }; + r.setStyle = function(node, style, options, geometry) { + return node; + }; + + var geometry = { + id: 'foo', + CLASS_NAME: 'bar' + }; + var style = { + graphicZIndex: 10 + }; + var featureId = 'foo'; + r.drawGeometry(geometry, style, featureId); + + function count(obj) { + var result = 0; + for (var i in obj) { + result++; + } + return result; + } + + t.ok(r.root.childNodes.length == 1, "root is correctly filled"); + t.ok(r.indexer.maxZIndex == 10, "indexer.maxZIndex is correctly filled"); + t.ok(r.indexer.order.length == 1, "indexer.order is correctly filled"); + t.ok(count(r.indexer.indices) == 1, "indexer.indices is correctly filled"); + + r.eraseGeometry(geometry); + + t.ok(r.root.childNodes.length == 0, "root is correctly cleared"); + t.ok(r.indexer.maxZIndex == 0, "indexer.maxZIndex is correctly reset"); + t.ok(r.indexer.order.length == 0, "indexer.order is correctly reset"); + t.ok(count(r.indexer.indices) == 0, "indexer.indices is correctly reset"); + + delete(style.graphicZIndex); + r.drawGeometry(geometry, style, featureId); + + t.ok(r.root.childNodes.length == 1, "root is correctly filled"); + t.ok(r.indexer.maxZIndex == 0, "indexer.maxZIndex is correctly filled"); + t.ok(r.indexer.order.length == 1, "indexer.order is correctly filled"); + t.ok(count(r.indexer.indices) == 1, "indexer.indices is correctly filled"); + + r.clear(); + + t.ok(r.root.childNodes.length == 0, "root is correctly cleared"); + t.ok(r.indexer.maxZIndex == 0, "indexer.maxZIndex is correctly reset"); + t.ok(r.indexer.order.length == 0, "indexer.order is correctly reset"); + t.ok(count(r.indexer.indices) == 0, "indexer.indices is correctly reset"); + + style.graphicZIndex = 12; + r.drawGeometry(geometry, style, featureId); + + t.ok(r.root.childNodes.length == 1, "root is correctly filled"); + t.ok(r.indexer.maxZIndex == 12, "indexer.maxZIndex is correctly filled"); + t.ok(r.indexer.order.length == 1, "indexer.order is correctly filled"); + t.ok(count(r.indexer.indices) == 1, "indexer.indices is correctly filled"); + + tearDown(); + } + +