Commit 909ff6bb authored by Pedro Eduardo Trujillo's avatar Pedro Eduardo Trujillo
Browse files

Refactoriza y mejora ajuste de límites temporales

Tras un cambio de capas con dimensión temporal, se ajusta la posición
actual para reflejar el valor en su nueva posición. Cuando este valor
queda fuera del rango actual, se ajusta al límite más cercano, en lugar
de asignar siempre el límite inferior.
parent 45ea9fdb
Loading
Loading
Loading
Loading
+36 −8
Original line number Diff line number Diff line
@@ -80,26 +80,54 @@ define([

		_updateTimeDimensionWidget: function() {

			var timeLimitsObj = this._getCurrentLayersTimeLimits(),
				times = L.TimeDimension.Util.explodeTimeRange(timeLimitsObj.startTime, timeLimitsObj.endTime, 'P1D');

			this._timeDimensionInstance.setAvailableTimes(times, 'replace');

			this._setValidTimePosition(timeLimitsObj);
		},

		_getCurrentLayersTimeLimits: function() {

			var layersTimeDefinitions = Object.values(this._layersWithTimeDimension),
				startTime, endTime;
				startMoment, endMoment;

			layersTimeDefinitions.forEach(function(item) {

				var itemStartMoment = moment(item.startDate),
					itemEndMoment = moment(item.endDate);

				if (!startTime || itemStartMoment.isBefore(startTime)) {
					startTime = itemStartMoment;
				if (!startMoment || itemStartMoment.isBefore(startMoment)) {
					startMoment = itemStartMoment;
				}
				if (!endTime || itemEndMoment.isAfter(endTime)) {
					endTime = itemEndMoment;
				if (!endMoment || itemEndMoment.isAfter(endMoment)) {
					endMoment = itemEndMoment;
				}
			});

			var times = L.TimeDimension.Util.explodeTimeRange(startTime.toDate(), endTime.toDate(), 'P1D');
			return {
				startMoment: startMoment,
				endMoment: endMoment,
				startTime: startMoment.toDate(),
				endTime: endMoment.toDate()
			};
		},

			this._timeDimensionInstance.setAvailableTimes(times, 'replace');
			this._timeDimensionInstance.setCurrentTime(startTime);
		_setValidTimePosition: function(timeLimitsObj) {

			var currStartMoment = timeLimitsObj.startMoment,
				currEndMoment = timeLimitsObj.endMoment,
				currTime = this._timeDimensionInstance.getCurrentTime(),
				validTimePosition = currTime;

			if (currStartMoment.isAfter(currTime)) {
				validTimePosition = timeLimitsObj.startTime;
			} else if (currEndMoment.isBefore(currTime)) {
				validTimePosition = timeLimitsObj.endTime;
			}

			this._timeDimensionInstance.setCurrentTime(validTimePosition);
		},

		_addTimeDimensionWidget: function() {