UNPKG

@angular/material-moment-adapter

Version:
375 lines (367 loc) 10.5 kB
/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import { Inject, Injectable, Optional, InjectionToken, NgModule } from '@angular/core'; import { DateAdapter, MAT_DATE_LOCALE, MAT_DATE_FORMATS } from '@angular/material/core'; import * as _rollupMoment from 'moment'; import _rollupMoment__default, { } from 'moment'; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ const moment = _rollupMoment__default || _rollupMoment; /** * InjectionToken for moment date adapter to configure options. * @type {?} */ const MAT_MOMENT_DATE_ADAPTER_OPTIONS = new InjectionToken('MAT_MOMENT_DATE_ADAPTER_OPTIONS', { providedIn: 'root', factory: MAT_MOMENT_DATE_ADAPTER_OPTIONS_FACTORY }); /** * \@docs-private * @return {?} */ function MAT_MOMENT_DATE_ADAPTER_OPTIONS_FACTORY() { return { useUtc: false }; } /** * Creates an array and fills it with values. * @template T * @param {?} length * @param {?} valueFunction * @return {?} */ function range(length, valueFunction) { /** @type {?} */ const valuesArray = Array(length); for (let i = 0; i < length; i++) { valuesArray[i] = valueFunction(i); } return valuesArray; } /** * Adapts Moment.js Dates for use with Angular Material. */ class MomentDateAdapter extends DateAdapter { /** * @param {?} dateLocale * @param {?=} _options */ constructor(dateLocale, _options) { super(); this._options = _options; this.setLocale(dateLocale || moment.locale()); } /** * @param {?} locale * @return {?} */ setLocale(locale) { super.setLocale(locale); /** @type {?} */ let momentLocaleData = moment.localeData(locale); this._localeData = { firstDayOfWeek: momentLocaleData.firstDayOfWeek(), longMonths: momentLocaleData.months(), shortMonths: momentLocaleData.monthsShort(), dates: range(31, (/** * @param {?} i * @return {?} */ (i) => this.createDate(2017, 0, i + 1).format('D'))), longDaysOfWeek: momentLocaleData.weekdays(), shortDaysOfWeek: momentLocaleData.weekdaysShort(), narrowDaysOfWeek: momentLocaleData.weekdaysMin(), }; } /** * @param {?} date * @return {?} */ getYear(date) { return this.clone(date).year(); } /** * @param {?} date * @return {?} */ getMonth(date) { return this.clone(date).month(); } /** * @param {?} date * @return {?} */ getDate(date) { return this.clone(date).date(); } /** * @param {?} date * @return {?} */ getDayOfWeek(date) { return this.clone(date).day(); } /** * @param {?} style * @return {?} */ getMonthNames(style) { // Moment.js doesn't support narrow month names, so we just use short if narrow is requested. return style == 'long' ? this._localeData.longMonths : this._localeData.shortMonths; } /** * @return {?} */ getDateNames() { return this._localeData.dates; } /** * @param {?} style * @return {?} */ getDayOfWeekNames(style) { if (style == 'long') { return this._localeData.longDaysOfWeek; } if (style == 'short') { return this._localeData.shortDaysOfWeek; } return this._localeData.narrowDaysOfWeek; } /** * @param {?} date * @return {?} */ getYearName(date) { return this.clone(date).format('YYYY'); } /** * @return {?} */ getFirstDayOfWeek() { return this._localeData.firstDayOfWeek; } /** * @param {?} date * @return {?} */ getNumDaysInMonth(date) { return this.clone(date).daysInMonth(); } /** * @param {?} date * @return {?} */ clone(date) { return date.clone().locale(this.locale); } /** * @param {?} year * @param {?} month * @param {?} date * @return {?} */ createDate(year, month, date) { // Moment.js will create an invalid date if any of the components are out of bounds, but we // explicitly check each case so we can throw more descriptive errors. if (month < 0 || month > 11) { throw Error(`Invalid month index "${month}". Month index has to be between 0 and 11.`); } if (date < 1) { throw Error(`Invalid date "${date}". Date has to be greater than 0.`); } /** @type {?} */ const result = this._createMoment({ year, month, date }).locale(this.locale); // If the result isn't valid, the date must have been out of bounds for this month. if (!result.isValid()) { throw Error(`Invalid date "${date}" for month with index "${month}".`); } return result; } /** * @return {?} */ today() { return this._createMoment().locale(this.locale); } /** * @param {?} value * @param {?} parseFormat * @return {?} */ parse(value, parseFormat) { if (value && typeof value == 'string') { return this._createMoment(value, parseFormat, this.locale); } return value ? this._createMoment(value).locale(this.locale) : null; } /** * @param {?} date * @param {?} displayFormat * @return {?} */ format(date, displayFormat) { date = this.clone(date); if (!this.isValid(date)) { throw Error('MomentDateAdapter: Cannot format invalid date.'); } return date.format(displayFormat); } /** * @param {?} date * @param {?} years * @return {?} */ addCalendarYears(date, years) { return this.clone(date).add({ years }); } /** * @param {?} date * @param {?} months * @return {?} */ addCalendarMonths(date, months) { return this.clone(date).add({ months }); } /** * @param {?} date * @param {?} days * @return {?} */ addCalendarDays(date, days) { return this.clone(date).add({ days }); } /** * @param {?} date * @return {?} */ toIso8601(date) { return this.clone(date).format(); } /** * Returns the given value if given a valid Moment or null. Deserializes valid ISO 8601 strings * (https://www.ietf.org/rfc/rfc3339.txt) and valid Date objects into valid Moments and empty * string into null. Returns an invalid date for all other values. * @param {?} value * @return {?} */ deserialize(value) { /** @type {?} */ let date; if (value instanceof Date) { date = this._createMoment(value).locale(this.locale); } else if (this.isDateInstance(value)) { // Note: assumes that cloning also sets the correct locale. return this.clone(value); } if (typeof value === 'string') { if (!value) { return null; } date = this._createMoment(value, moment.ISO_8601).locale(this.locale); } if (date && this.isValid(date)) { return this._createMoment(date).locale(this.locale); } return super.deserialize(value); } /** * @param {?} obj * @return {?} */ isDateInstance(obj) { return moment.isMoment(obj); } /** * @param {?} date * @return {?} */ isValid(date) { return this.clone(date).isValid(); } /** * @return {?} */ invalid() { return moment.invalid(); } /** * Creates a Moment instance while respecting the current UTC settings. * @private * @param {...?} args * @return {?} */ _createMoment(...args) { return (this._options && this._options.useUtc) ? moment.utc(...args) : moment(...args); } } MomentDateAdapter.decorators = [ { type: Injectable }, ]; /** @nocollapse */ MomentDateAdapter.ctorParameters = () => [ { type: String, decorators: [{ type: Optional }, { type: Inject, args: [MAT_DATE_LOCALE,] }] }, { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [MAT_MOMENT_DATE_ADAPTER_OPTIONS,] }] } ]; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ const MAT_MOMENT_DATE_FORMATS = { parse: { dateInput: 'l', }, display: { dateInput: 'l', monthYearLabel: 'MMM YYYY', dateA11yLabel: 'LL', monthYearA11yLabel: 'MMMM YYYY', }, }; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class MomentDateModule { } MomentDateModule.decorators = [ { type: NgModule, args: [{ providers: [ { provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS] } ], },] }, ]; const ɵ0 = MAT_MOMENT_DATE_FORMATS; class MatMomentDateModule { } MatMomentDateModule.decorators = [ { type: NgModule, args: [{ imports: [MomentDateModule], providers: [{ provide: MAT_DATE_FORMATS, useValue: ɵ0 }], },] }, ]; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { MomentDateModule, MatMomentDateModule, MAT_MOMENT_DATE_ADAPTER_OPTIONS_FACTORY, MAT_MOMENT_DATE_ADAPTER_OPTIONS, MomentDateAdapter, MAT_MOMENT_DATE_FORMATS }; //# sourceMappingURL=material-moment-adapter.js.map