Add 'loadstart' and 'loadend' events to some of our exciting layers like WFS, GML, GeoRSS, and Text. tests to back it up. (Closes #808)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@4252 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
euzuro
2007-09-13 01:23:06 +00:00
parent aa70587033
commit ff82936fba
9 changed files with 292 additions and 3 deletions

View File

@@ -81,6 +81,7 @@ OpenLayers.Layer.GML = OpenLayers.Class(OpenLayers.Layer.Vector, {
// loaded after the GML is paited.
// See http://trac.openlayers.org/ticket/404
if(this.visibility && !this.loaded){
this.events.triggerEvent("loadstart");
this.loadGML();
}
},
@@ -113,6 +114,7 @@ OpenLayers.Layer.GML = OpenLayers.Class(OpenLayers.Layer.Vector, {
var gml = this.format ? new this.format() : new OpenLayers.Format.GML();
this.addFeatures(gml.read(doc));
this.events.triggerEvent("loadend");
},
/**
@@ -125,6 +127,7 @@ OpenLayers.Layer.GML = OpenLayers.Class(OpenLayers.Layer.Vector, {
*/
requestFailure: function(request) {
alert("Error in loading GML file "+this.url);
this.events.triggerEvent("loadend");
},
CLASS_NAME: "OpenLayers.Layer.GML"

View File

@@ -67,6 +67,7 @@ OpenLayers.Layer.GeoRSS = OpenLayers.Class(OpenLayers.Layer.Markers, {
OpenLayers.Layer.Markers.prototype.initialize.apply(this, [name, options]);
this.location = location;
this.features = [];
this.events.triggerEvent("loadstart");
OpenLayers.loadURL(location, null, this, this.parseData);
},
@@ -209,6 +210,7 @@ OpenLayers.Layer.GeoRSS = OpenLayers.Class(OpenLayers.Layer.Markers, {
marker.events.register('click', feature, this.markerClick);
this.addMarker(marker);
}
this.events.triggerEvent("loadend");
},
/**

View File

@@ -47,7 +47,14 @@ OpenLayers.Layer.Text = OpenLayers.Class(OpenLayers.Layer.Markers, {
OpenLayers.Layer.Markers.prototype.initialize.apply(this, arguments);
this.features = new Array();
if (this.location != null) {
OpenLayers.loadURL(this.location, null, this, this.parseData);
var onFail = function(e) {
this.events.triggerEvent("loadend");
};
this.events.triggerEvent("loadstart");
OpenLayers.loadURL(this.location, null,
this, this.parseData, onFail);
}
},
@@ -155,6 +162,7 @@ OpenLayers.Layer.Text = OpenLayers.Class(OpenLayers.Layer.Markers, {
}
}
}
this.events.triggerEvent("loadend");
},
/**

View File

@@ -22,6 +22,12 @@ OpenLayers.Layer.WFS = OpenLayers.Class(
*/
isBaseLayer: false,
/**
* Property: tile
* {<OpenLayers.Tile.WFS>}
*/
tile: null,
/**
* APIProperty: ratio
* {Float} the ratio of image/tile size to map size (this is the untiled
@@ -234,6 +240,7 @@ OpenLayers.Layer.WFS = OpenLayers.Class(
if (!this.tile) {
this.tile = new OpenLayers.Tile.WFS(this, pos, tileBounds,
url, tileSize);
this.addTileMonitoringHooks(this.tile);
this.tile.draw();
} else {
if (this.vectorMode) {
@@ -243,6 +250,7 @@ OpenLayers.Layer.WFS = OpenLayers.Class(
this.clearMarkers();
}
this.tile.destroy();
this.removeTileMonitoringHooks(this.tile);
this.tile = null;
this.tile = new OpenLayers.Tile.WFS(this, pos, tileBounds,
@@ -252,6 +260,50 @@ OpenLayers.Layer.WFS = OpenLayers.Class(
}
},
/**
* Method: addTileMonitoringHooks
* This function takes a tile as input and adds the appropriate hooks to
* the tile so that the layer can keep track of the loading tile
* (making sure to check that the tile is always the layer's current
* tile before taking any action).
*
* Parameters:
* tile - {<OpenLayers.Tile>}
*/
addTileMonitoringHooks: function(tile) {
tile.onLoadStart = function() {
//if this is the the layer's current tile, then trigger
// a 'loadstart'
if (this == this.layer.tile) {
this.layer.events.triggerEvent("loadstart");
}
};
tile.events.register("loadstart", tile, tile.onLoadStart);
tile.onLoadEnd = function() {
//if this is the the layer's current tile, then trigger
// a 'tileloaded' and 'loadend'
if (this == this.layer.tile) {
this.layer.events.triggerEvent("tileloaded");
this.layer.events.triggerEvent("loadend");
}
};
tile.events.register("loadend", tile, tile.onLoadEnd);
},
/**
* Method: removeTileMonitoringHooks
* This function takes a tile as input and removes the tile hooks
* that were added in addTileMonitoringHooks()
*
* Parameters:
* tile - {<OpenLayers.Tile>}
*/
removeTileMonitoringHooks: function(tile) {
tile.events.unregister("loadstart", tile, tile.onLoadStart);
tile.events.unregister("loadend", tile, tile.onLoadEnd);
},
/**
* Method: onMapResize
* Call the onMapResize method of the appropriate parent class.

View File

@@ -75,6 +75,13 @@ OpenLayers.Tile.WFS = OpenLayers.Class(OpenLayers.Tile, {
*/
draw:function() {
if (OpenLayers.Tile.prototype.draw.apply(this, arguments)) {
if (this.isLoading) {
//if already loading, send 'reload' instead of 'loadstart'.
this.events.triggerEvent("reload");
} else {
this.isLoading = true;
this.events.triggerEvent("loadstart");
}
this.loadFeaturesForRegion(this.requestSuccess);
}
},
@@ -121,6 +128,9 @@ OpenLayers.Tile.WFS = OpenLayers.Class(OpenLayers.Tile, {
this.addResults(resultFeatures);
}
}
if (this.events) {
this.events.triggerEvent("loadend");
}
},
/**

View File

@@ -5,6 +5,14 @@
var name = "GML Layer";
var gml = "./owls.xml";
// if this test is running online, different rules apply
var isMSIE = (navigator.userAgent.indexOf("MSIE") > -1);
if (isMSIE) {
gml = "." + gml;
}
function test_01_Layer_GML_constructor(t) {
t.plan(3);
@@ -14,6 +22,24 @@
t.ok(layer.renderer.CLASS_NAME, "layer has a renderer");
}
function test_Layer_GML_events(t) {
t.plan(3);
var layer = new OpenLayers.Layer.GML(name, gml, {isBaseLayer: true});
layer.events.register("loadstart", layer, function() {
t.ok(true, "loadstart called.")
});
layer.events.register("loadend", layer, function() {
t.ok(true, "loadend called.")
});
var map = new OpenLayers.Map("map");
map.addLayer(layer);
map.zoomToMaxExtent();
t.delay_call(1, function() {
t.ok(true, "waited for 1s");
});
}
</script>
</head>
<body>

View File

@@ -80,7 +80,7 @@
map.addLayer(layer);
map.setCenter(new OpenLayers.LonLat(0,0),0);
var event = {};
t.delay_call( 1, function() {
t.delay_call( 2, function() {
t.ok(layer.markers[0].events, "First marker has an events object");
t.eq(layer.markers[0].events.listeners['click'].length, 1, "Marker events has one object");
layer.markers[0].events.triggerEvent('click', event);
@@ -159,7 +159,26 @@
t.eq(otherLayer.markers[0].icon.url, the_icon.url,"The layer with an icon has that icon.");
});
}
function test_Layer_GeoRSS_loadend_Event(t) {
var browserCode = OpenLayers.Util.getBrowserName();
if (browserCode == "msie") {
t.plan(1);
t.ok(true, "IE fails the GeoRSS test. This could probably be fixed by someone with enough energy to fix it.");
} else {
t.plan(2);
layer = new OpenLayers.Layer.GeoRSS('Test Layer', georss_txt);
t.delay_call(2, function() {
layer.events.register('loadend', layer, function() {
t.ok(true, "Loadend event fired");
});
layer.parseData({
'responseText': '<xml xmlns="http://example.com"><title> </title></xml>'
});
t.ok(true, "Parsing data didn't fail");
});
}
}
function test_99_Layer_GeoRSS_destroy (t) {
t.plan( 1 );
layer = new OpenLayers.Layer.GeoRSS('Test Layer', georss_txt);

View File

@@ -118,6 +118,19 @@
t.eq(map.popups.length, 0, "no popup on second marker");
});
}
function test_Layer_Text_loadend_Event(t) {
t.plan(2);
layer = new OpenLayers.Layer.Text('Test Layer', {location:datafile});
t.delay_call(2, function() {
layer.events.register('loadend', layer, function() {
t.ok(true, "Loadend event fired");
});
layer.parseData({
'responseText':''
});
t.ok(true, "Parsing data didn't fail");
});
}
function test_99_Layer_Text_destroy (t) {
t.plan( 1 );

156
tests/owls.xml Normal file
View File

@@ -0,0 +1,156 @@
<?xml version='1.0' encoding="ISO-8859-1" ?>
<wfs:FeatureCollection
xmlns:bsc="http://www.bsc-eoc.org/bsc"
xmlns:wfs="http://www.opengis.net/wfs"
xmlns:gml="http://www.opengis.net/gml"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengeospatial.net//wfs/1.0.0/WFS-basic.xsd
http://www.bsc-eoc.org/bsc http://www.bsc-eoc.org/cgi-bin/bsc_ows.asp?SERVICE=WFS&amp;VERSION=1.0.0&amp;REQUEST=DescribeFeatureType&amp;TYPENAME=OWLS&amp;OUTPUTFORMAT=XMLSCHEMA">
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates>-89.817223,45.005555 -74.755001,51.701388</gml:coordinates>
</gml:Box>
</gml:boundedBy>
<gml:featureMember><bsc:OWLS>
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates>-79.771668,45.891110 -79.771668,45.891110</gml:coordinates>
</gml:Box>
</gml:boundedBy>
<bsc:msGeometry>
<gml:Point srsName="EPSG:4326">
<gml:coordinates>-79.771668,45.891110</gml:coordinates>
</gml:Point>
</bsc:msGeometry>
</bsc:OWLS>
</gml:featureMember>
<gml:featureMember>
<bsc:OWLS>
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates>-83.755834,46.365277 -83.755834,46.365277</gml:coordinates>
</gml:Box>
</gml:boundedBy>
<bsc:owlname>owl</bsc:owlname>
<bsc:msGeometry>
<gml:Point srsName="EPSG:4326">
<gml:coordinates>-83.755834,46.365277</gml:coordinates>
</gml:Point>
</bsc:msGeometry>
</bsc:OWLS>
</gml:featureMember>
<gml:featureMember>
<bsc:OWLS>
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates>-83.808612,46.175277 -83.808612,46.175277</gml:coordinates>
</gml:Box>
</gml:boundedBy>
<bsc:msGeometry>
<gml:Point srsName="EPSG:4326">
<gml:coordinates>-83.808612,46.175277</gml:coordinates>
</gml:Point>
</bsc:msGeometry>
</bsc:OWLS>
</gml:featureMember>
<gml:featureMember>
<bsc:OWLS>
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates>-84.111112,46.309166 -84.111112,46.309166</gml:coordinates>
</gml:Box>
</gml:boundedBy>
<bsc:msGeometry>
<gml:Point srsName="EPSG:4326">
<gml:coordinates>-84.111112,46.309166</gml:coordinates>
</gml:Point>
</bsc:msGeometry>
</bsc:OWLS>
</gml:featureMember>
<gml:featureMember>
<bsc:OWLS>
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates>-83.678612,46.821110 -83.678612,46.821110</gml:coordinates>
</gml:Box>
</gml:boundedBy>
<bsc:msGeometry>
<gml:Point srsName="EPSG:4326">
<gml:coordinates>-83.678612,46.821110</gml:coordinates>
</gml:Point>
</bsc:msGeometry>
</bsc:OWLS>
</gml:featureMember>
<gml:featureMember>
<bsc:OWLS>
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates>-83.664445,46.518888 -83.664445,46.518888</gml:coordinates>
</gml:Box>
</gml:boundedBy>
<bsc:msGeometry>
<gml:Point srsName="EPSG:4326">
<gml:coordinates>-83.664445,46.518888</gml:coordinates>
</gml:Point>
</bsc:msGeometry>
</bsc:OWLS>
</gml:featureMember>
<gml:featureMember>
<bsc:OWLS>
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates>-80.613334,46.730277 -80.613334,46.730277</gml:coordinates>
</gml:Box>
</gml:boundedBy>
<bsc:msGeometry>
<gml:Point srsName="EPSG:4326">
<gml:coordinates>-80.613334,46.730277</gml:coordinates>
</gml:Point>
</bsc:msGeometry>
</bsc:OWLS>
</gml:featureMember>
<gml:featureMember>
<bsc:OWLS>
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates>-79.676946,45.428054 -79.676946,45.428054</gml:coordinates>
</gml:Box>
</gml:boundedBy>
<bsc:msGeometry>
<gml:Point srsName="EPSG:4326">
<gml:coordinates>-79.676946,45.428054</gml:coordinates>
</gml:Point>
</bsc:msGeometry>
</bsc:OWLS>
</gml:featureMember>
<gml:featureMember>
<bsc:OWLS>
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates>-83.853056,46.236944 -83.853056,46.236944</gml:coordinates>
</gml:Box>
</gml:boundedBy>
<bsc:msGeometry>
<gml:Point srsName="EPSG:4326">
<gml:coordinates>-83.853056,46.236944</gml:coordinates>
</gml:Point>
</bsc:msGeometry>
</bsc:OWLS>
</gml:featureMember>
<gml:featureMember>
<bsc:OWLS>
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates>-82.289167,45.896388 -82.289167,45.896388</gml:coordinates>
</gml:Box>
</gml:boundedBy>
<bsc:msGeometry>
<gml:Point srsName="EPSG:4326">
<gml:coordinates>-82.289167,45.896388</gml:coordinates>
</gml:Point>
</bsc:msGeometry>
</bsc:OWLS>
</gml:featureMember>
</wfs:FeatureCollection>