Commit 647b080d authored by Pedro Eduardo Trujillo's avatar Pedro Eduardo Trujillo
Browse files

Habilita y mejora capas WMS con auto-actualización

Limpia la implementación y controla de verdad el refresh, anulándolo
cuando el mapa o la capa no se están usando (y recuperándolo si se
vuelven a usar).

Para lograr esto, se añaden nuevos canales a los módulos de mapa y capa,
permitiendo a estas últimas conocer cuando el mapa (o alguno de sus
ancestros) cambian su estado de visibilidad.
parent a506f09b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -526,7 +526,7 @@ define([
							queryable: item.queryable,
							layerId: layerId,
							layerLabel: layerLabel,
							timeRefresh: item.timeRefresh
							refresh: item.refresh
						}
					}
				};
+9 −1
Original line number Diff line number Diff line
@@ -85,7 +85,9 @@ define([
					LAYER_ADDED_CONFIRMED: "layerAddedConfirmed",
					LAYER_REMOVED_FORWARDED: "layerRemovedForwarded",
					LAYER_REMOVED_CONFIRMED: "layerRemovedConfirmed",
					SET_QUERYABLE_CURSOR: "setQueryableCursor"
					SET_QUERYABLE_CURSOR: "setQueryableCursor",
					MAP_SHOWN: "mapShown",
					MAP_HIDDEN: "mapHidden"
				},

				ownChannel: "map",
@@ -220,6 +222,12 @@ define([
			},{
				event: 'LAYER_INFO_FORWARDED',
				channel: this.getChannel("LAYER_INFO")
			},{
				event: 'ME_OR_ANCESTOR_SHOWN',
				channel: this.getChannel('MAP_SHOWN')
			},{
				event: 'ME_OR_ANCESTOR_HIDDEN',
				channel: this.getChannel('MAP_HIDDEN')
			});
		},

+36 −1
Original line number Diff line number Diff line
@@ -57,7 +57,9 @@ define([
					DELETE_INSTANCE: "deleteInstance",
					ANIMATE_MARKER: "animateMarker",
					FIT_BOUNDS: "fitBounds",
					SET_CENTER: "setCenter"
					SET_CENTER: "setCenter",
					MAP_SHOWN: "mapShown",
					MAP_HIDDEN: "mapHidden"
				}
			};

@@ -100,6 +102,12 @@ define([
			},{
				channel : this.getChannel("SET_CENTER"),
				callback: "_subSetCenter"
			},{
				channel : this._buildChannel(this.mapChannel, this.actions.MAP_SHOWN),
				callback: "_subMapShown"
			},{
				channel : this._buildChannel(this.mapChannel, this.actions.MAP_HIDDEN),
				callback: "_subMapHidden"
			});
		},

@@ -176,8 +184,17 @@ define([

		_subLayerAdded: function(response) {

			// TODO quizá sea mejor retrasar la carga de la leyenda, bajo petición
			this._getLayerLegend(response.layer);
			this._mapInstance = response.mapInstance;

			this._publish(this.getChannel('CONNECT'), {
				actions: ['LAYER_REMOVED', 'MAP_SHOWN', 'MAP_HIDDEN']
			});
			this._publish(this.getChannel('DISCONNECT'), {
				actions: ['LAYER_ADDED']
			});

			this._afterLayerAdded(response);
			this._emitEvt('LAYER_ADDED', this._getLayerInfoToPublish(response));
		},
@@ -185,6 +202,14 @@ define([
		_subLayerRemoved: function(response) {

			this._mapInstance = null;

			this._publish(this.getChannel('DISCONNECT'), {
				actions: ['LAYER_REMOVED', 'MAP_SHOWN', 'MAP_HIDDEN']
			});
			this._publish(this.getChannel('CONNECT'), {
				actions: ['LAYER_ADDED']
			});

			this._afterLayerRemoved(response);
			this._emitEvt('LAYER_REMOVED', this._getLayerInfoToPublish(response));
		},
@@ -248,6 +273,16 @@ define([
		_subDeleteInstance: function(response) {

			this._emitEvt('REMOVE_LAYER', {layer: this.layer});
		},

		_subMapShown: function(response) {

			this._onMapShown(response);
		},

		_subMapHidden: function(response) {

			this._onMapHidden(response);
		}
	});
});
+54 −34
Original line number Diff line number Diff line
define([
	"dojo/_base/declare"
	, "dojo/_base/lang"
	, "leaflet/leaflet"
	, "moment/moment.min"
	, "./MapLayer"
	'dojo/_base/declare'
	, 'dojo/_base/lang'
	, 'leaflet/leaflet'
	, 'moment/moment.min'
	, './MapLayer'
], function(
	declare
	, lang
@@ -20,60 +20,70 @@ define([
		constructor: function(args) {

			this.config = {
				ownChannel: "wmsLayer"
				ownChannel: 'wmsLayer',
				refresh: 0
			};

			lang.mixin(this, this.config, args);

			if (this._isValidTimeRefresh()) {
				this._refreshTimeInParams();
			}
		},

		_addNewData: function(geoJsonData, moduleContext) {},
		_afterLayerAdded: function(data) {

		addData: function(data) {},
			this._setRefreshInterval();
		},

		setStyle: function(geoJsonStyle) {},
		_afterLayerRemoved: function() {

		clear: function() {},
			this._stopRefreshInterval();
		},

		_afterLayerAdded: function(data) {
		_setRefreshInterval: function() {

			if (this._isValidTimeRefresh()) {
			// TODO eliminar excepción para capa AIS cuando se defina refresh en el servicio
			var isAisLayer = this.layer.wmsParams.layers.indexOf('last_position') !== -1;

				this.idSetInterval = setInterval(lang.hitch(this, this._refreshTimeInParams), this.timeRefresh);
			if ((this._checkRefreshIsValid() || isAisLayer) && !this._refreshIntervalHandler) {
				if (isAisLayer) {
					this.refresh = 15;
				}
		},

		_isValidTimeRefresh: function(data) {
				var cbk = lang.hitch(this, this._redraw),
					timeout = this.refresh * 1000;

			if (this.timeRefresh && Number.isInteger(this.timeRefresh)) {
				return true;
				this._refreshIntervalHandler = setInterval(cbk, timeout);
			}
		},

		_checkRefreshIsValid: function() {

			return false;
			return this.refresh && Number.isInteger(this.refresh);
		},

		_refreshTimeInParams: function() {
		_redraw: function() {

			if (!this.layer) {
				return;
			}

			this.layer.setParams({
				time: 'PT1H/' + moment().format('YYYY-MM-DDTHH:mm:ss.SSS[Z]')
				timestamp: Date.now()
			});
		},

		_afterLayerRemoved: function(data) {
		_stopRefreshInterval: function() {

			this.idSetInterval && clearInterval(this.idSetInterval);
			if (!this._refreshIntervalHandler) {
				return;
			}

			clearInterval(this._refreshIntervalHandler);
			delete this._refreshIntervalHandler;
		},

		_getLayerLegend: function(layer) {

			if (this.styleLayer) {
			// TODO cuando se defina leyenda en el servicio de atlas, consultar la imagen correspondiente
			if (0 && this.styleLayer) {
				this._emitEvt('LAYER_LEGEND',
					this._getLayerLegendToPublish(this._obtainLegendUrl(this.styleLayer.url)));
			}
@@ -82,7 +92,7 @@ define([
		_obtainLegendUrl: function(url) {

			var params = {
				legend_options: "fontAntiAliasing:true;dpi:100"
				legend_options: 'fontAntiAliasing:true;dpi:100'
			};

			return this._chkUrlAndAddParams(url, L.Util.getParamString(params));
@@ -90,7 +100,7 @@ define([

		_chkUrlAndAddParams: function(url, paramsStr) {

			var index = url.indexOf("?"),
			var index = url.indexOf('?'),
				params = paramsStr;

			if (index >= 0) {
@@ -113,9 +123,9 @@ define([
			this._emitEvt('GET', {
				target: this.infoTarget,
				requesterId: this.getOwnChannel(),
				id: "",
				id: '',
				options: {
					"X-Requested-With": ""
					'X-Requested-With': ''
				}
			});
		},
@@ -132,9 +142,9 @@ define([
		_obtainGetParams: function(data) {

			var params = {
				request: "GetFeatureInfo",
				srs: "EPSG:4326",
				info_format: "application/json",
				request: 'GetFeatureInfo',
				srs: 'EPSG:4326',
				info_format: 'application/json',
				service: this.layer.wmsParams.service,
				version: this.layer.wmsParams.version,
				layers: this.layer.wmsParams.layers,
@@ -158,6 +168,16 @@ define([
			var layerAddedId = response.layer._leaflet_id;

			return layerAddedId === this.layerId;
		},

		_onMapShown: function(response) {

			this._setRefreshInterval();
		},

		_onMapHidden: function(response) {

			this._stopRefreshInterval();
		}
	});
});
+3 −1
Original line number Diff line number Diff line
@@ -28,7 +28,9 @@ define([
				"_afterLayerRemoved": {},
				"_redraw": {},
				"_animateMarker": {},
				"_setCenter": {}
				"_setCenter": {},
				"_onMapShown": {},
				"_onMapHidden": {}
			});
		}
	});