angular2-moment
Version:
Moment.JS pipes for Angular2 (timeago and more)
104 lines • 4.37 kB
JavaScript
"use strict";
/* angular2-moment (c) 2015, 2016 Uri Shaked / MIT Licence */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var moment = require("moment");
// under systemjs, moment is actually exported as the default export, so we account for that
var momentConstructor = moment.default || moment;
var TimeAgoPipe = /** @class */ (function () {
function TimeAgoPipe(cdRef, ngZone) {
this.cdRef = cdRef;
this.ngZone = ngZone;
}
TimeAgoPipe.prototype.transform = function (value, omitSuffix) {
if (this.hasChanged(value, omitSuffix)) {
this.lastTime = this.getTime(value);
this.lastValue = value;
this.lastOmitSuffix = omitSuffix;
this.lastLocale = this.getLocale(value);
this.removeTimer();
this.createTimer();
this.lastText = momentConstructor(value).from(momentConstructor(), omitSuffix);
}
else {
this.createTimer();
}
return this.lastText;
};
TimeAgoPipe.prototype.ngOnDestroy = function () {
this.removeTimer();
};
TimeAgoPipe.prototype.createTimer = function () {
var _this = this;
if (this.currentTimer) {
return;
}
var momentInstance = momentConstructor(this.lastValue);
var timeToUpdate = this.getSecondsUntilUpdate(momentInstance) * 1000;
this.currentTimer = this.ngZone.runOutsideAngular(function () {
if (typeof window !== 'undefined') {
return window.setTimeout(function () {
_this.lastText = momentConstructor(_this.lastValue).from(momentConstructor(), _this.lastOmitSuffix);
_this.currentTimer = null;
_this.ngZone.run(function () { return _this.cdRef.markForCheck(); });
}, timeToUpdate);
}
});
};
TimeAgoPipe.prototype.removeTimer = function () {
if (this.currentTimer) {
window.clearTimeout(this.currentTimer);
this.currentTimer = null;
}
};
TimeAgoPipe.prototype.getSecondsUntilUpdate = function (momentInstance) {
var howOld = Math.abs(momentConstructor().diff(momentInstance, 'minute'));
if (howOld < 1) {
return 1;
}
else if (howOld < 60) {
return 30;
}
else if (howOld < 180) {
return 300;
}
else {
return 3600;
}
};
TimeAgoPipe.prototype.hasChanged = function (value, omitSuffix) {
return this.getTime(value) !== this.lastTime
|| this.getLocale(value) !== this.lastLocale
|| omitSuffix !== this.lastOmitSuffix;
};
TimeAgoPipe.prototype.getTime = function (value) {
if (moment.isDate(value)) {
return value.getTime();
}
else if (moment.isMoment(value)) {
return value.valueOf();
}
else {
return momentConstructor(value).valueOf();
}
};
TimeAgoPipe.prototype.getLocale = function (value) {
return moment.isMoment(value) ? value.locale() : null;
};
TimeAgoPipe = __decorate([
core_1.Pipe({ name: 'amTimeAgo', pure: false }),
__metadata("design:paramtypes", [core_1.ChangeDetectorRef, core_1.NgZone])
], TimeAgoPipe);
return TimeAgoPipe;
}());
exports.TimeAgoPipe = TimeAgoPipe;
//# sourceMappingURL=time-ago.pipe.js.map