415 lines
13 KiB
JavaScript
415 lines
13 KiB
JavaScript
import { Pipe, EventEmitter, ChangeDetectorRef, NgZone, InjectionToken, Optional, Inject, NgModule } from '@angular/core';
|
|
import * as moment from 'moment';
|
|
import { isMoment, duration, relativeTimeThreshold, unix, utc, parseZone, isDate, locale } from 'moment';
|
|
|
|
/* ngx-moment (c) 2015, 2016 Uri Shaked / MIT Licence */
|
|
const momentConstructor = moment;
|
|
class AddPipe {
|
|
transform(value, amount, unit) {
|
|
if (typeof amount === 'undefined' ||
|
|
(typeof amount === 'number' && typeof unit === 'undefined')) {
|
|
throw new Error('AddPipe: missing required arguments');
|
|
}
|
|
return momentConstructor(value).add(amount, unit);
|
|
}
|
|
}
|
|
AddPipe.decorators = [
|
|
{ type: Pipe, args: [{ name: 'amAdd' },] }
|
|
];
|
|
|
|
/* ngx-moment (c) 2015, 2016 Uri Shaked / MIT Licence */
|
|
const momentConstructor$1 = moment;
|
|
class CalendarPipe {
|
|
constructor(cdRef, ngZone) {
|
|
this.cdRef = cdRef;
|
|
this.ngZone = ngZone;
|
|
// using a single static timer for all instances of this pipe for performance reasons
|
|
CalendarPipe.initTimer(ngZone);
|
|
CalendarPipe.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.midnight.subscribe(() => {
|
|
this.ngZone.run(() => this.cdRef.markForCheck());
|
|
});
|
|
}
|
|
transform(value, ...args) {
|
|
let formats = null;
|
|
let referenceTime = null;
|
|
for (let i = 0, len = args.length; i < len; i++) {
|
|
if (args[i] !== null) {
|
|
if (typeof args[i] === 'object' && !isMoment(args[i])) {
|
|
formats = args[i];
|
|
}
|
|
else {
|
|
referenceTime = momentConstructor$1(args[i]);
|
|
}
|
|
}
|
|
}
|
|
return momentConstructor$1(value).calendar(referenceTime, formats);
|
|
}
|
|
ngOnDestroy() {
|
|
if (CalendarPipe.refs > 0) {
|
|
CalendarPipe.refs--;
|
|
}
|
|
if (CalendarPipe.refs === 0) {
|
|
CalendarPipe.removeTimer();
|
|
}
|
|
this.midnightSub.unsubscribe();
|
|
}
|
|
static initTimer(ngZone) {
|
|
// initialize the timer
|
|
if (!CalendarPipe.midnight) {
|
|
CalendarPipe.midnight = new EventEmitter();
|
|
if (typeof window !== 'undefined') {
|
|
const timeToUpdate = CalendarPipe._getMillisecondsUntilUpdate();
|
|
CalendarPipe.timer = ngZone.runOutsideAngular(() => {
|
|
return window.setTimeout(() => {
|
|
// emit the current date
|
|
CalendarPipe.midnight.emit(new Date());
|
|
// refresh the timer
|
|
CalendarPipe.removeTimer();
|
|
CalendarPipe.initTimer(ngZone);
|
|
}, timeToUpdate);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
static removeTimer() {
|
|
if (CalendarPipe.timer) {
|
|
window.clearTimeout(CalendarPipe.timer);
|
|
CalendarPipe.timer = null;
|
|
CalendarPipe.midnight = null;
|
|
}
|
|
}
|
|
static _getMillisecondsUntilUpdate() {
|
|
const now = momentConstructor$1();
|
|
const tomorrow = momentConstructor$1().startOf('day').add(1, 'days');
|
|
const timeToMidnight = tomorrow.valueOf() - now.valueOf();
|
|
return timeToMidnight + 1000; // 1 second after midnight
|
|
}
|
|
}
|
|
/**
|
|
* Internal reference counter, so we can clean up when no instances are in use
|
|
*/
|
|
CalendarPipe.refs = 0;
|
|
CalendarPipe.timer = null;
|
|
CalendarPipe.midnight = null;
|
|
CalendarPipe.decorators = [
|
|
{ type: Pipe, args: [{ name: 'amCalendar', pure: false },] }
|
|
];
|
|
CalendarPipe.ctorParameters = () => [
|
|
{ type: ChangeDetectorRef },
|
|
{ type: NgZone }
|
|
];
|
|
|
|
/* ngx-moment (c) 2015, 2016 Uri Shaked / MIT Licence */
|
|
const momentConstructor$2 = moment;
|
|
class DateFormatPipe {
|
|
transform(value, ...args) {
|
|
if (!value) {
|
|
return '';
|
|
}
|
|
return momentConstructor$2(value).format(args[0]);
|
|
}
|
|
}
|
|
DateFormatPipe.decorators = [
|
|
{ type: Pipe, args: [{ name: 'amDateFormat' },] }
|
|
];
|
|
|
|
/* ngx-moment (c) 2015, 2016 Uri Shaked / MIT Licence */
|
|
const momentConstructor$3 = moment;
|
|
class DifferencePipe {
|
|
transform(value, otherValue, unit, precision) {
|
|
const date = momentConstructor$3(value);
|
|
const date2 = otherValue !== null ? momentConstructor$3(otherValue) : momentConstructor$3();
|
|
return date.diff(date2, unit, precision);
|
|
}
|
|
}
|
|
DifferencePipe.decorators = [
|
|
{ type: Pipe, args: [{ name: 'amDifference' },] }
|
|
];
|
|
|
|
const NGX_MOMENT_OPTIONS = new InjectionToken('NGX_MOMENT_OPTIONS');
|
|
|
|
class DurationPipe {
|
|
constructor(momentOptions) {
|
|
this.allowedUnits = ['ss', 's', 'm', 'h', 'd', 'M'];
|
|
this._applyOptions(momentOptions);
|
|
}
|
|
transform(value, ...args) {
|
|
if (typeof args === 'undefined' || args.length !== 1) {
|
|
throw new Error('DurationPipe: missing required time unit argument');
|
|
}
|
|
return duration(value, args[0]).humanize();
|
|
}
|
|
_applyOptions(momentOptions) {
|
|
if (!momentOptions) {
|
|
return;
|
|
}
|
|
if (!!momentOptions.relativeTimeThresholdOptions) {
|
|
const units = Object.keys(momentOptions.relativeTimeThresholdOptions);
|
|
const filteredUnits = units.filter((unit) => this.allowedUnits.indexOf(unit) !== -1);
|
|
filteredUnits.forEach((unit) => {
|
|
relativeTimeThreshold(unit, momentOptions.relativeTimeThresholdOptions[unit]);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
DurationPipe.decorators = [
|
|
{ type: Pipe, args: [{ name: 'amDuration' },] }
|
|
];
|
|
DurationPipe.ctorParameters = () => [
|
|
{ type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [NGX_MOMENT_OPTIONS,] }] }
|
|
];
|
|
|
|
/* ngx-moment (c) 2015, 2016 Uri Shaked / MIT Licence */
|
|
class FromUnixPipe {
|
|
transform(value, ...args) {
|
|
return typeof value === 'string' ? unix(parseInt(value, 10)) : unix(value);
|
|
}
|
|
}
|
|
FromUnixPipe.decorators = [
|
|
{ type: Pipe, args: [{ name: 'amFromUnix' },] }
|
|
];
|
|
|
|
const momentConstructor$4 = moment;
|
|
class ParsePipe {
|
|
transform(value, formats) {
|
|
return momentConstructor$4(value, formats);
|
|
}
|
|
}
|
|
ParsePipe.decorators = [
|
|
{ type: Pipe, args: [{ name: 'amParse' },] }
|
|
];
|
|
|
|
/* ngx-moment (c) 2015, 2016 Uri Shaked / MIT Licence */
|
|
class FromUtcPipe {
|
|
transform(value, formats, ...args) {
|
|
return formats ? utc(value, formats) : utc(value);
|
|
}
|
|
}
|
|
FromUtcPipe.decorators = [
|
|
{ type: Pipe, args: [{ name: 'amFromUtc' },] }
|
|
];
|
|
|
|
const momentConstructor$5 = moment;
|
|
class IsAfterPipe {
|
|
transform(value, otherValue, unit) {
|
|
return momentConstructor$5(value).isAfter(momentConstructor$5(otherValue), unit);
|
|
}
|
|
}
|
|
IsAfterPipe.decorators = [
|
|
{ type: Pipe, args: [{
|
|
name: 'amIsAfter',
|
|
},] }
|
|
];
|
|
|
|
const momentConstructor$6 = moment;
|
|
class IsBeforePipe {
|
|
transform(value, otherValue, unit) {
|
|
return momentConstructor$6(value).isBefore(momentConstructor$6(otherValue), unit);
|
|
}
|
|
}
|
|
IsBeforePipe.decorators = [
|
|
{ type: Pipe, args: [{
|
|
name: 'amIsBefore',
|
|
},] }
|
|
];
|
|
|
|
const momentConstructor$7 = moment;
|
|
class LocalTimePipe {
|
|
transform(value) {
|
|
return momentConstructor$7(value).local();
|
|
}
|
|
}
|
|
LocalTimePipe.decorators = [
|
|
{ type: Pipe, args: [{ name: 'amLocal' },] }
|
|
];
|
|
|
|
// See https://github.com/ng-packagr/ng-packagr/issues/217 for why this is needed:
|
|
const momentConstructor$8 = moment;
|
|
class LocalePipe {
|
|
transform(value, locale) {
|
|
return momentConstructor$8(value).locale(locale);
|
|
}
|
|
}
|
|
LocalePipe.decorators = [
|
|
{ type: Pipe, args: [{ name: 'amLocale' },] }
|
|
];
|
|
|
|
class ParseZonePipe {
|
|
transform(value) {
|
|
return parseZone(value);
|
|
}
|
|
}
|
|
ParseZonePipe.decorators = [
|
|
{ type: Pipe, args: [{ name: 'amParseZone' },] }
|
|
];
|
|
|
|
/* ngx-moment (c) 2015, 2016 Uri Shaked / MIT Licence */
|
|
const momentConstructor$9 = moment;
|
|
class SubtractPipe {
|
|
transform(value, amount, unit) {
|
|
if (typeof amount === 'undefined' ||
|
|
(typeof amount === 'number' && typeof unit === 'undefined')) {
|
|
throw new Error('SubtractPipe: missing required arguments');
|
|
}
|
|
return momentConstructor$9(value).subtract(amount, unit);
|
|
}
|
|
}
|
|
SubtractPipe.decorators = [
|
|
{ type: Pipe, args: [{ name: 'amSubtract' },] }
|
|
];
|
|
|
|
/* ngx-moment (c) 2015, 2016 Uri Shaked / MIT Licence */
|
|
const momentConstructor$a = moment;
|
|
class TimeAgoPipe {
|
|
constructor(cdRef, ngZone) {
|
|
this.cdRef = cdRef;
|
|
this.ngZone = ngZone;
|
|
}
|
|
format(m) {
|
|
return m.from(momentConstructor$a(), this.lastOmitSuffix);
|
|
}
|
|
transform(value, omitSuffix, formatFn) {
|
|
if (this.hasChanged(value, omitSuffix)) {
|
|
this.lastTime = this.getTime(value);
|
|
this.lastValue = value;
|
|
this.lastOmitSuffix = omitSuffix;
|
|
this.lastLocale = this.getLocale(value);
|
|
this.formatFn = formatFn || this.format.bind(this);
|
|
this.removeTimer();
|
|
this.createTimer();
|
|
this.lastText = this.formatFn(momentConstructor$a(value));
|
|
}
|
|
else {
|
|
this.createTimer();
|
|
}
|
|
return this.lastText;
|
|
}
|
|
ngOnDestroy() {
|
|
this.removeTimer();
|
|
}
|
|
createTimer() {
|
|
if (this.currentTimer) {
|
|
return;
|
|
}
|
|
const momentInstance = momentConstructor$a(this.lastValue);
|
|
const timeToUpdate = this.getSecondsUntilUpdate(momentInstance) * 1000;
|
|
this.currentTimer = this.ngZone.runOutsideAngular(() => {
|
|
if (typeof window !== 'undefined') {
|
|
return window.setTimeout(() => {
|
|
this.lastText = this.formatFn(momentConstructor$a(this.lastValue));
|
|
this.currentTimer = null;
|
|
this.ngZone.run(() => this.cdRef.markForCheck());
|
|
}, timeToUpdate);
|
|
}
|
|
else {
|
|
return null;
|
|
}
|
|
});
|
|
}
|
|
removeTimer() {
|
|
if (this.currentTimer) {
|
|
window.clearTimeout(this.currentTimer);
|
|
this.currentTimer = null;
|
|
}
|
|
}
|
|
getSecondsUntilUpdate(momentInstance) {
|
|
const howOld = Math.abs(momentConstructor$a().diff(momentInstance, 'minute'));
|
|
if (howOld < 1) {
|
|
return 1;
|
|
}
|
|
else if (howOld < 60) {
|
|
return 30;
|
|
}
|
|
else if (howOld < 180) {
|
|
return 300;
|
|
}
|
|
else {
|
|
return 3600;
|
|
}
|
|
}
|
|
hasChanged(value, omitSuffix) {
|
|
return (this.getTime(value) !== this.lastTime ||
|
|
this.getLocale(value) !== this.lastLocale ||
|
|
omitSuffix !== this.lastOmitSuffix);
|
|
}
|
|
getTime(value) {
|
|
if (isDate(value)) {
|
|
return value.getTime();
|
|
}
|
|
else if (isMoment(value)) {
|
|
return value.valueOf();
|
|
}
|
|
else {
|
|
return momentConstructor$a(value).valueOf();
|
|
}
|
|
}
|
|
getLocale(value) {
|
|
return isMoment(value) ? value.locale() : locale();
|
|
}
|
|
}
|
|
TimeAgoPipe.decorators = [
|
|
{ type: Pipe, args: [{ name: 'amTimeAgo', pure: false },] }
|
|
];
|
|
TimeAgoPipe.ctorParameters = () => [
|
|
{ type: ChangeDetectorRef },
|
|
{ type: NgZone }
|
|
];
|
|
|
|
const momentConstructor$b = moment;
|
|
class UtcPipe {
|
|
transform(value) {
|
|
return momentConstructor$b(value).utc();
|
|
}
|
|
}
|
|
UtcPipe.decorators = [
|
|
{ type: Pipe, args: [{ name: 'amUtc' },] }
|
|
];
|
|
|
|
const ANGULAR_MOMENT_PIPES = [
|
|
AddPipe,
|
|
CalendarPipe,
|
|
DateFormatPipe,
|
|
DifferencePipe,
|
|
DurationPipe,
|
|
FromUnixPipe,
|
|
ParsePipe,
|
|
SubtractPipe,
|
|
TimeAgoPipe,
|
|
UtcPipe,
|
|
FromUtcPipe,
|
|
LocalTimePipe,
|
|
LocalePipe,
|
|
ParseZonePipe,
|
|
IsBeforePipe,
|
|
IsAfterPipe,
|
|
];
|
|
class MomentModule {
|
|
static forRoot(options) {
|
|
return {
|
|
ngModule: MomentModule,
|
|
providers: [
|
|
{
|
|
provide: NGX_MOMENT_OPTIONS,
|
|
useValue: Object.assign({}, options),
|
|
},
|
|
],
|
|
};
|
|
}
|
|
}
|
|
MomentModule.decorators = [
|
|
{ type: NgModule, args: [{
|
|
declarations: ANGULAR_MOMENT_PIPES,
|
|
exports: ANGULAR_MOMENT_PIPES,
|
|
},] }
|
|
];
|
|
|
|
/**
|
|
* Generated bundle index. Do not edit.
|
|
*/
|
|
|
|
export { AddPipe, CalendarPipe, DateFormatPipe, DifferencePipe, DurationPipe, FromUnixPipe, FromUtcPipe, IsAfterPipe, IsBeforePipe, LocalTimePipe, LocalePipe, MomentModule, NGX_MOMENT_OPTIONS, ParsePipe, ParseZonePipe, SubtractPipe, TimeAgoPipe, UtcPipe };
|
|
//# sourceMappingURL=ngx-moment.js.map
|