From d9762c984ea84ff054a87ca55c484d7f3ece487b Mon Sep 17 00:00:00 2001 From: euzuro Date: Sun, 21 May 2006 15:47:10 +0000 Subject: [PATCH] add code to Map.js to allow for the addittion and removal of popups. add a new html file to play around with popups. add a test in the test_Map file for the popup adding/removing git-svn-id: http://svn.openlayers.org/trunk/openlayers@227 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Map.js | 27 +++++++++++++++++++++++++ popups.html | 46 +++++++++++++++++++++++++++++++++++++++++++ tests/test_Map.html | 41 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 popups.html diff --git a/lib/OpenLayers/Map.js b/lib/OpenLayers/Map.js index 6bbe3dc40e..fe10963a69 100644 --- a/lib/OpenLayers/Map.js +++ b/lib/OpenLayers/Map.js @@ -42,6 +42,9 @@ OpenLayers.Map.prototype = { // Array(OpenLayers.Control) controls: null, + // Array(OpenLayers.Popup) + popups: null, + // OpenLayers.LonLat center: null, @@ -85,6 +88,8 @@ OpenLayers.Map.prototype = { this.addControl( new OpenLayers.Control.PanZoom() ); this.addControl( new OpenLayers.Control.MouseDefaults() ); + this.popups = new Array(); + // always call map.destroy() Event.observe(window, 'unload', this.destroy.bindAsEventListener(this)); @@ -142,6 +147,28 @@ OpenLayers.Map.prototype = { } }, + /** + * @param {OpenLayers.Popup} popup + */ + addPopup: function(popup) { + popup.map = this; + this.popups.push(popup); + var popupDiv = popup.draw(); + if (popupDiv) { + popupDiv.style.zIndex = this.Z_INDEX_BASE['Popup'] + + this.popups.length; + this.viewPortDiv.appendChild(popupDiv); + } + }, + + /** + * @param {OpenLayers.Popup} popup + */ + removePopup: function(popup) { + this.popups.remove(popup); + this.viewPortDiv.removeChild(popup.div); + }, + /** * @return {float} */ diff --git a/popups.html b/popups.html new file mode 100644 index 0000000000..553e502b26 --- /dev/null +++ b/popups.html @@ -0,0 +1,46 @@ + + + + + + + + +

OpenLayers Example

+
+
hello
+ + diff --git a/tests/test_Map.html b/tests/test_Map.html index e04c34fac9..8cd4e80cc2 100644 --- a/tests/test_Map.html +++ b/tests/test_Map.html @@ -73,6 +73,47 @@ map.zoomIn(); map.zoomOut(); } + + function test_07_Map_add_remove_popup (t) { + t.plan(5); + + map = new OpenLayers.Map('map'); + + var popup = new OpenLayers.Popup("chicken", + new OpenLayers.Pixel(20,20), + new OpenLayers.Size(200,200)); + + map.addPopup(popup); + t.ok((popup.map == map), "popup's reference back to map set correctly"); + t.eq(map.popups.indexOf(popup), 0, "popup successfully added to Map's internal popups array"); + + var nodes = map.viewPortDiv.childNodes; + + var found = false; + for (var i=0; i < nodes.length; i++) { + if (nodes.item(i) == popup.div) { + found = true; + break; + } + } + t.ok(found, "popup.div successfully added to the map's viewPort"); + + + map.removePopup(popup); + t.eq(map.popups.indexOf(popup), -1, "popup successfully removed from Map's internal popups array"); + + var found = false; + for (var i=0; i < nodes.length; i++) { + if (nodes.item(i) == popup.div) { + found = true; + break; + } + } + t.ok(!found, "popup.div successfully removed from the map's viewPort"); + + + } + function test_99_Map_destroy (t) { t.plan( 2 ); map = new OpenLayers.Map($('map'));