UNPKG

angular2-moment

Version:

Moment.JS pipes for Angular2 (timeago and more)

104 lines 4.78 kB
"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 CalendarPipe = /** @class */ (function () { function CalendarPipe(cdRef, ngZone) { var _this = this; this.cdRef = cdRef; this.ngZone = ngZone; // using a single static timer for all instances of this pipe for performance reasons CalendarPipe_1.initTimer(ngZone); CalendarPipe_1.refs++; // values such as Today will need to be replaced with Yesterday after midnight, // so make sure we subscribe to an EventEmitter that we set up to emit at midnight this.midnightSub = CalendarPipe_1.midnight.subscribe(function () { _this.ngZone.run(function () { return _this.cdRef.markForCheck(); }); }); } CalendarPipe_1 = CalendarPipe; CalendarPipe.prototype.transform = function (value) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } var formats = null; var referenceTime = null; for (var i = 0, len = args.length; i < len; i++) { if (args[i] !== null) { if (typeof args[i] === 'object' && !moment.isMoment(args[i])) { formats = args[i]; } else { referenceTime = momentConstructor(args[i]); } } } return momentConstructor(value).calendar(referenceTime, formats); }; CalendarPipe.prototype.ngOnDestroy = function () { if (CalendarPipe_1.refs > 0) { CalendarPipe_1.refs--; } if (CalendarPipe_1.refs === 0) { CalendarPipe_1.removeTimer(); } this.midnightSub.unsubscribe(); }; CalendarPipe.initTimer = function (ngZone) { // initialize the timer if (!CalendarPipe_1.midnight) { CalendarPipe_1.midnight = new core_1.EventEmitter(); if (typeof window !== 'undefined') { var timeToUpdate_1 = CalendarPipe_1._getMillisecondsUntilUpdate(); CalendarPipe_1.timer = ngZone.runOutsideAngular(function () { return window.setTimeout(function () { // emit the current date CalendarPipe_1.midnight.emit(new Date()); // refresh the timer CalendarPipe_1.removeTimer(); CalendarPipe_1.initTimer(ngZone); }, timeToUpdate_1); }); } } }; CalendarPipe.removeTimer = function () { if (CalendarPipe_1.timer) { window.clearTimeout(CalendarPipe_1.timer); CalendarPipe_1.timer = null; CalendarPipe_1.midnight = null; } }; CalendarPipe._getMillisecondsUntilUpdate = function () { var now = momentConstructor(); var tomorrow = momentConstructor().startOf('day').add(1, 'days'); var timeToMidnight = tomorrow.valueOf() - now.valueOf(); return timeToMidnight + 1000; // 1 second after midnight }; /** * @private Internal reference counter, so we can clean up when no instances are in use * @type {number} */ CalendarPipe.refs = 0; CalendarPipe = CalendarPipe_1 = __decorate([ core_1.Pipe({ name: 'amCalendar', pure: false }), __metadata("design:paramtypes", [core_1.ChangeDetectorRef, core_1.NgZone]) ], CalendarPipe); return CalendarPipe; var CalendarPipe_1; }()); exports.CalendarPipe = CalendarPipe; //# sourceMappingURL=calendar.pipe.js.map