From f33c5ef112bf18c0878e5ff011ab846c04395134 Mon Sep 17 00:00:00 2001 From: bartvde Date: Tue, 5 Apr 2011 06:52:42 +0000 Subject: [PATCH] WMSGetFeatureInfo control: relate features to url, thanks erilem for the extensive amount of reviews you have done for OL 2.11, r=erilem (closes #2883) git-svn-id: http://svn.openlayers.org/trunk/openlayers@11875 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Control/WMSGetFeatureInfo.js | 23 +++++++-- tests/Control/WMSGetFeatureInfo.html | 55 +++++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/lib/OpenLayers/Control/WMSGetFeatureInfo.js b/lib/OpenLayers/Control/WMSGetFeatureInfo.js index 68b4508391..6d3775f1ae 100644 --- a/lib/OpenLayers/Control/WMSGetFeatureInfo.js +++ b/lib/OpenLayers/Control/WMSGetFeatureInfo.js @@ -55,6 +55,14 @@ OpenLayers.Control.WMSGetFeatureInfo = OpenLayers.Class(OpenLayers.Control, { */ clickCallback: "click", + /** APIProperty: output + * {String} Either "features" or "object". When triggering a + * getfeatureinfo request should we pass on an array of features + * or an object with with a "features" property and other properties + * (such as the url of the WMS). Default is "features". + */ + output: "features", + /** * Property: layers * {Array()} The layers to query for feature info. @@ -387,7 +395,7 @@ OpenLayers.Control.WMSGetFeatureInfo = OpenLayers.Class(OpenLayers.Control, { url: url, params: OpenLayers.Util.upperCaseObject(params), callback: function(request) { - this.handleResponse(clickPosition, request); + this.handleResponse(clickPosition, request, url); }, scope: this }; @@ -487,7 +495,9 @@ OpenLayers.Control.WMSGetFeatureInfo = OpenLayers.Class(OpenLayers.Control, { * request - {XMLHttpRequest} The request object * xy - {} The position on the map where the * mouse event occurred. - * features - {Array()} + * features - {Array()} or + * {Array({Object}) when output is "object". The object has a url and a + * features property which contains an array of features. */ triggerGetFeatureInfo: function(request, xy, features) { this.events.triggerEvent("getfeatureinfo", { @@ -509,8 +519,9 @@ OpenLayers.Control.WMSGetFeatureInfo = OpenLayers.Class(OpenLayers.Control, { * xy - {} The position on the map where the * mouse event occurred. * request - {XMLHttpRequest} The request object. + * url - {String} The url which was used for this request. */ - handleResponse: function(xy, request) { + handleResponse: function(xy, request, url) { var doc = request.responseXML; if(!doc || !doc.documentElement) { @@ -521,7 +532,13 @@ OpenLayers.Control.WMSGetFeatureInfo = OpenLayers.Class(OpenLayers.Control, { this.triggerGetFeatureInfo(request, xy, features); } else { this._requestCount++; + if (this.output === "object") { + this._features = (this._features || []).concat( + {url: url, features: features} + ); + } else { this._features = (this._features || []).concat(features); + } if (this._requestCount === this._numRequests) { this.triggerGetFeatureInfo(request, xy, this._features.concat()); delete this._features; diff --git a/tests/Control/WMSGetFeatureInfo.html b/tests/Control/WMSGetFeatureInfo.html index cc225e2222..8d63f788c4 100644 --- a/tests/Control/WMSGetFeatureInfo.html +++ b/tests/Control/WMSGetFeatureInfo.html @@ -115,6 +115,61 @@ control.getInfoForHover({xy: {x: 50, y: 50}}); } + function test_getfeatureinfo_event(t) { + + t.plan(5); + + var text = + '' + + '' + + ' ' + + ''; + + var map = new OpenLayers.Map('map'); + + var xy; + var url = "http://foo"; + + // mock up a control with output "object" and drillDown true + var control = new OpenLayers.Control.WMSGetFeatureInfo({ + output: "object", + drillDown: true, + request: function(position) {}, + eventListeners: { + getfeatureinfo: function(evt) { + t.ok(evt.features[0].url === url, "features is an object with a property url when output is object"); + var features = evt.features[0].features; + t.ok(features.length === 1, "features properties has a length of 1"); + t.ok(features[0] instanceof OpenLayers.Feature.Vector, "Feature array contains 1 feature"); + } + } + }); + + // mock up a control with output "features" and drillDown true + var control2 = new OpenLayers.Control.WMSGetFeatureInfo({ + autoActivate: true, + drillDown: true, + request: function(position) {}, + eventListeners: { + getfeatureinfo: function(evt) { + var features = evt.features; + t.ok(features.length === 1, "features properties has a length of 1"); + t.ok(features[0] instanceof OpenLayers.Feature.Vector, "Feature array contains 1 feature"); + } + } + }); + + map.addControls([control, control2]); + control.activate(); + + xy = {x: 50, y: 50}; + control._requestCount = control2._requestCount = 0; + control._numRequests = control2._numRequests = 1; + control.handleResponse({xy: xy}, {responseText: text}, url); + control2.handleResponse({xy: xy}, {responseText: text}, url); + map.destroy(); + } + function test_beforegetfeatureinfo_event(t) { t.plan(2); var map = new OpenLayers.Map('map');