angular2-counto
Version:
Awesome angular package for counting animations.
114 lines • 3.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var CountoDirective = /** @class */ (function () {
function CountoDirective() {
this.countoChange = new core_1.EventEmitter();
this.countoEnd = new core_1.EventEmitter();
}
Object.defineProperty(CountoDirective.prototype, "duration", {
set: function (duration) {
this._duration = parseFloat(duration);
this.run();
},
enumerable: true,
configurable: true
});
Object.defineProperty(CountoDirective.prototype, "countTo", {
set: function (countTo) {
this._countTo = parseFloat(countTo);
this.run();
},
enumerable: true,
configurable: true
});
Object.defineProperty(CountoDirective.prototype, "countFrom", {
set: function (countFrom) {
this._countFrom = parseFloat(countFrom);
this.run();
},
enumerable: true,
configurable: true
});
Object.defineProperty(CountoDirective.prototype, "step", {
set: function (step) {
this._step = parseFloat(step);
this.run();
},
enumerable: true,
configurable: true
});
CountoDirective.prototype.run = function () {
var _this = this;
clearInterval(_this._timer);
if (isNaN(_this._duration)) {
return false;
}
if (isNaN(_this._step)) {
return false;
}
if (isNaN(_this._countFrom)) {
return false;
}
if (isNaN(_this._countTo)) {
return false;
}
if (_this._step <= 0) {
console.info('Step must be greater than 0.');
return false;
}
if (_this._duration <= 0) {
console.info('Duration must be greater than 0.');
return false;
}
if (_this._step > _this._duration * 1000) {
console.info('Step must be equal or smaller than duration.');
return false;
}
var intermediate = _this._countFrom;
var increment = Math.abs(_this._countTo - _this._countFrom) / ((_this._duration * 1000) / _this._step);
_this.countoChange.emit(intermediate);
_this._timer = setInterval(function () {
if (_this._countTo < _this._countFrom) {
if (intermediate <= _this._countTo) {
clearInterval(_this._timer);
_this.countoChange.emit(_this._countTo);
_this.countoEnd.emit();
}
else {
_this.countoChange.emit(intermediate);
intermediate -= increment;
}
}
else {
if (intermediate >= _this._countTo) {
clearInterval(_this._timer);
_this.countoChange.emit(_this._countTo);
_this.countoEnd.emit();
}
else {
_this.countoChange.emit(intermediate);
intermediate += increment;
}
}
}, _this._step);
};
CountoDirective.decorators = [
{ type: core_1.Directive, args: [{
selector: '[counto]'
},] },
];
/** @nocollapse */
CountoDirective.ctorParameters = function () { return []; };
CountoDirective.propDecorators = {
"countoChange": [{ type: core_1.Output },],
"countoEnd": [{ type: core_1.Output },],
"duration": [{ type: core_1.Input },],
"countTo": [{ type: core_1.Input },],
"countFrom": [{ type: core_1.Input },],
"step": [{ type: core_1.Input },],
};
return CountoDirective;
}());
exports.CountoDirective = CountoDirective;
//# sourceMappingURL=counto.directive.js.map