From 1b95a25ee1d0fdcdf0d9b374dbb21318038fb6f1 Mon Sep 17 00:00:00 2001
From: pgiraud
Date: Thu, 12 Nov 2009 13:43:40 +0000
Subject: [PATCH] adds the ability for the user to give map options when
reading a WMC document, those options are taken into account with higher
priority\n this patch also introduces the new util method isElement,
r=me,elemoine, thanks Eric (Closes #2339)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@9794 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
---
examples/wmc.html | 11 +++++++----
lib/OpenLayers/Format/WMC.js | 25 +++++++++++++++++--------
lib/OpenLayers/Util.js | 14 ++++++++++++++
tests/Format/WMC.html | 8 +++++++-
tests/Util.html | 18 ++++++++++++++++++
5 files changed, 63 insertions(+), 13 deletions(-)
diff --git a/examples/wmc.html b/examples/wmc.html
index 875cfcdda8..151c2daede 100644
--- a/examples/wmc.html
+++ b/examples/wmc.html
@@ -105,7 +105,9 @@
} else {
map.destroy();
try {
- map = format.read(text, {map: "map"});
+ var jsonFormat = new OpenLayers.Format.JSON();
+ var mapOptions = jsonFormat.read(OpenLayers.Util.getElement('mapOptions').value);
+ map = format.read(text, {map: mapOptions});
map.addControl(new OpenLayers.Control.LayerSwitcher());
} catch(err) {
document.getElementById("wmc").value = err;
@@ -133,9 +135,10 @@
Shows parsing of Web Map Context documents.
-
-
-
+
+ with the following extra map options :
+
+
This is an example of parsing WMC documents.
diff --git a/lib/OpenLayers/Format/WMC.js b/lib/OpenLayers/Format/WMC.js
index be0a46d12f..af1b1b48d9 100644
--- a/lib/OpenLayers/Format/WMC.js
+++ b/lib/OpenLayers/Format/WMC.js
@@ -71,9 +71,11 @@ OpenLayers.Format.WMC = OpenLayers.Class({
* data - {String} or {DOMElement} data to read/parse.
* options - {Object} The options object must contain a map property. If
* the map property is a string, it must be the id of a dom element
- * where the new map will be placed. If the map property is an
+ * where the new map will be placed. If the map property is an
* , the layers from the context document will be added
- * to the map.
+ * to the map. If the map property is an object, this will be
+ * considered as options to create the map with, in most cases, it would
+ * have a div property.
*
* Returns:
* {} A map based on the context.
@@ -95,7 +97,14 @@ OpenLayers.Format.WMC = OpenLayers.Class({
if(options.map instanceof OpenLayers.Map) {
map = this.mergeContextToMap(context, options.map);
} else {
- map = this.contextToMap(context, options.map);
+ var mapOptions = options.map;
+ if(OpenLayers.Util.isElement(mapOptions) ||
+ typeof mapOptions == "string") {
+ // we assume mapOptions references a div
+ // element
+ mapOptions = {div: mapOptions};
+ }
+ map = this.contextToMap(context, mapOptions);
}
} else {
// not documented as part of the API, provided as a non-API option
@@ -231,17 +240,17 @@ OpenLayers.Format.WMC = OpenLayers.Class({
*
* Parameters:
* context - {Object} The context object.
- * id - {String | Element} The dom element or element id that will contain
- * the map.
+ * options - {Object} Default map options.
*
* Returns:
* {} A map based on the context object.
*/
- contextToMap: function(context, id) {
- var map = new OpenLayers.Map(id, {
+ contextToMap: function(context, options) {
+ options = OpenLayers.Util.applyDefaults({
maxExtent: context.maxExtent,
projection: context.projection
- });
+ }, options);
+ var map = new OpenLayers.Map(options);
map.addLayers(this.getLayersFromContext(context.layersContext));
map.setCenter(
context.bounds.getCenterLonLat(),
diff --git a/lib/OpenLayers/Util.js b/lib/OpenLayers/Util.js
index 99a8596634..ae6d4d5615 100644
--- a/lib/OpenLayers/Util.js
+++ b/lib/OpenLayers/Util.js
@@ -31,6 +31,20 @@ OpenLayers.Util.getElement = function() {
return elements;
};
+/**
+ * Function: isElement
+ * A cross-browser implementation of "e instanceof Element".
+ *
+ * Parameters:
+ * o - {Object} The object to test.
+ *
+ * Returns:
+ * {Boolean}
+ */
+OpenLayers.Util.isElement = function(o) {
+ return !!(o && o.nodeType === 1);
+};
+
/**
* Maintain existing definition of $.
*/
diff --git a/tests/Format/WMC.html b/tests/Format/WMC.html
index 92a239f18c..52e4f662bc 100644
--- a/tests/Format/WMC.html
+++ b/tests/Format/WMC.html
@@ -7,7 +7,7 @@
var v1_1_0 = 'landsat7NASA Global Mosaic6299645.76031498228.80image/jpeg4degreestruetruefalsebasicOpenLayers WMS6299645.76031498228.80image/jpeg4degreestruetruefalsena_road:CCRSTransportation Network6200000.00032000000.00image/pngTRUE4degreesfalse0.6falsefalse3:1Radar 3:16299645.76031498228.80image/pngTRUE4degreesfalse0.8truetrue';
function test_Format_WMC_read(t) {
- t.plan(33);
+ t.plan(34);
var format = new OpenLayers.Format.WMC();
var map, layer;
@@ -86,6 +86,12 @@
t.eq(layer.opacity, 0.8,
"(v1.1.0) layer opacity correctly set");
map.destroy();
+
+ // test mapOptions
+ var map = format.read(v1_1_0, {map: {foo: 'bar', div: 'map'}});
+ t.eq(map.foo, "bar",
+ "mapOptions correctly passed to the created map object");
+ map.destroy();
}
diff --git a/tests/Util.html b/tests/Util.html
index 06685977fc..3a214afed1 100644
--- a/tests/Util.html
+++ b/tests/Util.html
@@ -8,6 +8,24 @@