@carto/airship-bridge
Version:
Airship bridge to other libs (CARTO VL, CARTO.js)
122 lines (121 loc) • 5.06 kB
JavaScript
import { select } from '../../util/Utils';
var AnimationControls = /** @class */ (function () {
function AnimationControls(animationWidget, carto, column, variableName, propertyName, duration, fade, autoplay, layer, readyCb, formatCb) {
var _this = this;
if (duration === void 0) { duration = 10; }
if (fade === void 0) { fade = [0.15, 0.15]; }
this._animationWidget = select(animationWidget);
this._column = column;
this._variableName = variableName || 'animation';
this._propertyName = propertyName || 'filter';
this._carto = carto;
this._duration = duration;
this._fade = fade;
this._autoplay = autoplay || false;
this._animationWidget.playing = this._autoplay;
this._animationWidget.isLoading = true;
this._layer = layer;
this._formatCb = formatCb;
if (layer.viz) {
this._onLayerLoaded();
readyCb();
}
else {
layer.on('loaded', function () {
_this._onLayerLoaded();
readyCb();
});
}
}
Object.defineProperty(AnimationControls.prototype, "animation", {
get: function () {
return this._animation;
},
enumerable: true,
configurable: true
});
Object.defineProperty(AnimationControls.prototype, "variableName", {
get: function () {
return this._variableName;
},
enumerable: true,
configurable: true
});
Object.defineProperty(AnimationControls.prototype, "propertyName", {
get: function () {
return this._propertyName;
},
set: function (name) {
this._propertyName = name;
},
enumerable: true,
configurable: true
});
AnimationControls.prototype.restart = function () {
this._animation.setProgressPct(0);
};
AnimationControls.prototype.setRange = function () {
// TODO
};
AnimationControls.prototype._onLayerLoaded = function () {
var _this = this;
this._viz = this._layer.viz;
var expr = this._getAnimationExpression();
if (expr.a && expr.b) {
this._animation = expr.a.expressionName === 'animation' ? expr.a : expr.b;
}
else {
this._animation = expr;
}
this._viz[this._propertyName].blendTo(expr, 0);
this._animation.parent = this._viz;
this._animation.notify = this._viz._changed.bind(this._viz);
this._animationWidget.duration = this._animation.duration.value;
this._animationWidget.playing = this._autoplay;
this._animationWidget.isLoading = false;
this._animationWidget.addEventListener('play', function () {
_this._animation.play();
});
this._animationWidget.addEventListener('pause', function () {
_this._animation.pause();
});
this._animationWidget.addEventListener('seek', function (evt) {
_this._animation.setProgressPct(evt.detail[0] / 100);
_this._animation.notify();
_this._animationWidget.progressValue = _this._formatProgressValue();
});
this._layer.on('updated', function () {
_this._animationWidget.progress = _this._animation.getProgressPct() * 100;
_this._animationWidget.progressValue = _this._formatProgressValue();
});
};
AnimationControls.prototype._getAnimationExpression = function () {
if (this._variableName && this._viz.variables[this._variableName]) {
return this._viz.variables[this._variableName];
}
this._viz.variables[this._variableName] = this._propertyName && this._viz[this._propertyName].isAnimated()
? this._viz[this._propertyName]
: this._createDefaultAnimation();
return this._viz.variables[this._variableName];
};
AnimationControls.prototype._formatProgressValue = function () {
var progressValue = this._animation.getProgressValue();
if (progressValue instanceof Date) {
return this._formatCb ? this._formatCb(progressValue) : progressValue.toISOString();
}
if (progressValue instanceof Object && this._isVLTimeZoneDate(progressValue)) {
return this._formatCb ? this._formatCb(progressValue._date) : progressValue._date.toISOString();
}
return this._formatCb ? this._formatCb(progressValue) : progressValue;
};
AnimationControls.prototype._isVLTimeZoneDate = function (object) {
return '_date' in object;
};
AnimationControls.prototype._createDefaultAnimation = function () {
var s = this._carto.expressions;
var animation = s.animation(s.linear(s.prop(this._column), s.globalMin(s.prop(this._column)), s.globalMax(s.prop(this._column))), this._duration, s.fade(this._fade[0], this._fade[1]));
return animation;
};
return AnimationControls;
}());
export { AnimationControls };