UNPKG

3.94 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var core_1 = require("@angular/core");
4var CountoDirective = /** @class */ (function () {
5 function CountoDirective() {
6 this.countoChange = new core_1.EventEmitter();
7 this.countoEnd = new core_1.EventEmitter();
8 }
9 Object.defineProperty(CountoDirective.prototype, "duration", {
10 set: function (duration) {
11 this._duration = parseFloat(duration);
12 this.run();
13 },
14 enumerable: true,
15 configurable: true
16 });
17 Object.defineProperty(CountoDirective.prototype, "countTo", {
18 set: function (countTo) {
19 this._countTo = parseFloat(countTo);
20 this.run();
21 },
22 enumerable: true,
23 configurable: true
24 });
25 Object.defineProperty(CountoDirective.prototype, "countFrom", {
26 set: function (countFrom) {
27 this._countFrom = parseFloat(countFrom);
28 this.run();
29 },
30 enumerable: true,
31 configurable: true
32 });
33 Object.defineProperty(CountoDirective.prototype, "step", {
34 set: function (step) {
35 this._step = parseFloat(step);
36 this.run();
37 },
38 enumerable: true,
39 configurable: true
40 });
41 CountoDirective.prototype.run = function () {
42 var _this = this;
43 clearInterval(_this._timer);
44 if (isNaN(_this._duration)) {
45 return false;
46 }
47 if (isNaN(_this._step)) {
48 return false;
49 }
50 if (isNaN(_this._countFrom)) {
51 return false;
52 }
53 if (isNaN(_this._countTo)) {
54 return false;
55 }
56 if (_this._step <= 0) {
57 console.info('Step must be greater than 0.');
58 return false;
59 }
60 if (_this._duration <= 0) {
61 console.info('Duration must be greater than 0.');
62 return false;
63 }
64 if (_this._step > _this._duration * 1000) {
65 console.info('Step must be equal or smaller than duration.');
66 return false;
67 }
68 var intermediate = _this._countFrom;
69 var increment = Math.abs(_this._countTo - _this._countFrom) / ((_this._duration * 1000) / _this._step);
70 _this.countoChange.emit(intermediate);
71 _this._timer = setInterval(function () {
72 if (_this._countTo < _this._countFrom) {
73 if (intermediate <= _this._countTo) {
74 clearInterval(_this._timer);
75 _this.countoChange.emit(_this._countTo);
76 _this.countoEnd.emit();
77 }
78 else {
79 _this.countoChange.emit(intermediate);
80 intermediate -= increment;
81 }
82 }
83 else {
84 if (intermediate >= _this._countTo) {
85 clearInterval(_this._timer);
86 _this.countoChange.emit(_this._countTo);
87 _this.countoEnd.emit();
88 }
89 else {
90 _this.countoChange.emit(intermediate);
91 intermediate += increment;
92 }
93 }
94 }, _this._step);
95 };
96 CountoDirective.decorators = [
97 { type: core_1.Directive, args: [{
98 selector: '[counto]'
99 },] },
100 ];
101 /** @nocollapse */
102 CountoDirective.ctorParameters = function () { return []; };
103 CountoDirective.propDecorators = {
104 "countoChange": [{ type: core_1.Output },],
105 "countoEnd": [{ type: core_1.Output },],
106 "duration": [{ type: core_1.Input },],
107 "countTo": [{ type: core_1.Input },],
108 "countFrom": [{ type: core_1.Input },],
109 "step": [{ type: core_1.Input },],
110 };
111 return CountoDirective;
112}());
113exports.CountoDirective = CountoDirective;
114//# sourceMappingURL=counto.directive.js.map
\No newline at end of file