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
This commit is contained in:
@@ -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}
|
||||
*/
|
||||
|
||||
46
popups.html
Normal file
46
popups.html
Normal file
@@ -0,0 +1,46 @@
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta http-equiv="imagetoolbar" content="no"> <!--ie image gizmo OFF!-->
|
||||
<style type="text/css">
|
||||
#map {
|
||||
width: 800px;
|
||||
height: 475px;
|
||||
border: 1px solid black;
|
||||
}
|
||||
</style>
|
||||
<script src="lib/OpenLayers.js"></script>
|
||||
<script type="text/javascript">
|
||||
<!--
|
||||
var map, layer, popup;
|
||||
|
||||
function init(){
|
||||
map = new OpenLayers.Map('map');
|
||||
layer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
|
||||
"http://octo.metacarta.com/cgi-bin/mapserv",
|
||||
{map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'} );
|
||||
|
||||
map.addLayer(layer);
|
||||
|
||||
popup = new OpenLayers.Popup("chicken",
|
||||
new OpenLayers.Pixel(20,240),
|
||||
new OpenLayers.Size(500,100)
|
||||
);
|
||||
|
||||
map.addPopup(popup);
|
||||
|
||||
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
|
||||
map.addControl(new OpenLayers.Control.LayerSwitcher());
|
||||
}
|
||||
|
||||
function away() {
|
||||
map.removePopup(popup);
|
||||
}
|
||||
// -->
|
||||
</script>
|
||||
</head>
|
||||
<body onload="init()">
|
||||
<h1>OpenLayers Example</h1>
|
||||
<div id="map"></div>
|
||||
<div style="background-color:blue" onclick="away()"> hello</div>
|
||||
</body>
|
||||
</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'));
|
||||
|
||||
Reference in New Issue
Block a user