476 lines
No EOL
14 KiB
JavaScript
476 lines
No EOL
14 KiB
JavaScript
"use strict";
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.AdapterMoment = void 0;
|
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
var _moment = _interopRequireDefault(require("moment"));
|
|
// From https://momentjs.com/docs/#/displaying/format/
|
|
const formatTokenMap = {
|
|
// Year
|
|
Y: 'year',
|
|
YY: 'year',
|
|
YYYY: {
|
|
sectionType: 'year',
|
|
contentType: 'digit',
|
|
maxLength: 4
|
|
},
|
|
// Month
|
|
M: {
|
|
sectionType: 'month',
|
|
contentType: 'digit',
|
|
maxLength: 2
|
|
},
|
|
MM: 'month',
|
|
MMM: {
|
|
sectionType: 'month',
|
|
contentType: 'letter'
|
|
},
|
|
MMMM: {
|
|
sectionType: 'month',
|
|
contentType: 'letter'
|
|
},
|
|
// Day of the month
|
|
D: {
|
|
sectionType: 'day',
|
|
contentType: 'digit',
|
|
maxLength: 2
|
|
},
|
|
DD: 'day',
|
|
Do: {
|
|
sectionType: 'day',
|
|
contentType: 'digit-with-letter'
|
|
},
|
|
// Day of the week
|
|
E: {
|
|
sectionType: 'weekDay',
|
|
contentType: 'digit',
|
|
maxLength: 1
|
|
},
|
|
// eslint-disable-next-line id-denylist
|
|
e: {
|
|
sectionType: 'weekDay',
|
|
contentType: 'digit',
|
|
maxLength: 1
|
|
},
|
|
d: {
|
|
sectionType: 'weekDay',
|
|
contentType: 'digit',
|
|
maxLength: 1
|
|
},
|
|
dd: {
|
|
sectionType: 'weekDay',
|
|
contentType: 'letter'
|
|
},
|
|
ddd: {
|
|
sectionType: 'weekDay',
|
|
contentType: 'letter'
|
|
},
|
|
dddd: {
|
|
sectionType: 'weekDay',
|
|
contentType: 'letter'
|
|
},
|
|
// Meridiem
|
|
A: 'meridiem',
|
|
a: 'meridiem',
|
|
// Hours
|
|
H: {
|
|
sectionType: 'hours',
|
|
contentType: 'digit',
|
|
maxLength: 2
|
|
},
|
|
HH: 'hours',
|
|
h: {
|
|
sectionType: 'hours',
|
|
contentType: 'digit',
|
|
maxLength: 2
|
|
},
|
|
hh: 'hours',
|
|
// Minutes
|
|
m: {
|
|
sectionType: 'minutes',
|
|
contentType: 'digit',
|
|
maxLength: 2
|
|
},
|
|
mm: 'minutes',
|
|
// Seconds
|
|
s: {
|
|
sectionType: 'seconds',
|
|
contentType: 'digit',
|
|
maxLength: 2
|
|
},
|
|
ss: 'seconds'
|
|
};
|
|
const defaultFormats = {
|
|
year: 'YYYY',
|
|
month: 'MMMM',
|
|
monthShort: 'MMM',
|
|
dayOfMonth: 'D',
|
|
dayOfMonthFull: 'Do',
|
|
weekday: 'dddd',
|
|
weekdayShort: 'ddd',
|
|
hours24h: 'HH',
|
|
hours12h: 'hh',
|
|
meridiem: 'A',
|
|
minutes: 'mm',
|
|
seconds: 'ss',
|
|
fullDate: 'll',
|
|
keyboardDate: 'L',
|
|
shortDate: 'MMM D',
|
|
normalDate: 'D MMMM',
|
|
normalDateWithWeekday: 'ddd, MMM D',
|
|
fullTime12h: 'hh:mm A',
|
|
fullTime24h: 'HH:mm',
|
|
keyboardDateTime12h: 'L hh:mm A',
|
|
keyboardDateTime24h: 'L HH:mm'
|
|
};
|
|
const MISSING_TIMEZONE_PLUGIN = ['Missing timezone plugin', 'To be able to use timezones, you have to pass the default export from `moment-timezone` to the `dateLibInstance` prop of `LocalizationProvider`', 'Find more information on https://mui.com/x/react-date-pickers/timezone/#moment-and-timezone'].join('\n');
|
|
/**
|
|
* Based on `@date-io/moment`
|
|
*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2017 Dmitriy Kovalenko
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
class AdapterMoment {
|
|
isMUIAdapter = true;
|
|
isTimezoneCompatible = true;
|
|
lib = 'moment';
|
|
escapedCharacters = {
|
|
start: '[',
|
|
end: ']'
|
|
};
|
|
formatTokenMap = formatTokenMap;
|
|
constructor({
|
|
locale,
|
|
formats,
|
|
instance
|
|
} = {}) {
|
|
this.moment = instance || _moment.default;
|
|
this.locale = locale;
|
|
this.formats = (0, _extends2.default)({}, defaultFormats, formats);
|
|
}
|
|
setLocaleToValue = value => {
|
|
const expectedLocale = this.getCurrentLocaleCode();
|
|
if (expectedLocale === value.locale()) {
|
|
return value;
|
|
}
|
|
return value.locale(expectedLocale);
|
|
};
|
|
hasTimezonePlugin = () => typeof this.moment.tz !== 'undefined';
|
|
createSystemDate = value => {
|
|
const parsedValue = this.moment(value).local();
|
|
if (this.locale === undefined) {
|
|
return parsedValue;
|
|
}
|
|
return parsedValue.locale(this.locale);
|
|
};
|
|
createUTCDate = value => {
|
|
const parsedValue = this.moment.utc(value);
|
|
if (this.locale === undefined) {
|
|
return parsedValue;
|
|
}
|
|
return parsedValue.locale(this.locale);
|
|
};
|
|
createTZDate = (value, timezone) => {
|
|
/* v8 ignore next 3 */
|
|
if (!this.hasTimezonePlugin()) {
|
|
throw new Error(MISSING_TIMEZONE_PLUGIN);
|
|
}
|
|
const parsedValue = timezone === 'default' ? this.moment(value) : this.moment.tz(value, timezone);
|
|
if (this.locale === undefined) {
|
|
return parsedValue;
|
|
}
|
|
return parsedValue.locale(this.locale);
|
|
};
|
|
date = (value, timezone = 'default') => {
|
|
if (value === null) {
|
|
return null;
|
|
}
|
|
if (timezone === 'UTC') {
|
|
return this.createUTCDate(value);
|
|
}
|
|
if (timezone === 'system' || timezone === 'default' && !this.hasTimezonePlugin()) {
|
|
return this.createSystemDate(value);
|
|
}
|
|
return this.createTZDate(value, timezone);
|
|
};
|
|
getInvalidDate = () => this.moment(new Date('Invalid Date'));
|
|
getTimezone = value => {
|
|
// @ts-ignore
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
const zone = value._z?.name;
|
|
const defaultZone = value.isUTC() ? 'UTC' : 'system';
|
|
|
|
// @ts-ignore
|
|
return zone ?? this.moment.defaultZone?.name ?? defaultZone;
|
|
};
|
|
setTimezone = (value, timezone) => {
|
|
if (this.getTimezone(value) === timezone) {
|
|
return value;
|
|
}
|
|
if (timezone === 'UTC') {
|
|
return value.clone().utc();
|
|
}
|
|
if (timezone === 'system') {
|
|
return value.clone().local();
|
|
}
|
|
if (!this.hasTimezonePlugin()) {
|
|
/* v8 ignore next 3 */
|
|
if (timezone !== 'default') {
|
|
throw new Error(MISSING_TIMEZONE_PLUGIN);
|
|
}
|
|
return value;
|
|
}
|
|
const cleanZone = timezone === 'default' ?
|
|
// @ts-ignore
|
|
this.moment.defaultZone?.name ?? 'system' : timezone;
|
|
if (cleanZone === 'system') {
|
|
return value.clone().local();
|
|
}
|
|
const newValue = value.clone();
|
|
newValue.tz(cleanZone);
|
|
return newValue;
|
|
};
|
|
toJsDate = value => {
|
|
return value.toDate();
|
|
};
|
|
parse = (value, format) => {
|
|
if (value === '') {
|
|
return null;
|
|
}
|
|
if (this.locale) {
|
|
return this.moment(value, format, this.locale, true);
|
|
}
|
|
return this.moment(value, format, true);
|
|
};
|
|
getCurrentLocaleCode = () => {
|
|
return this.locale || _moment.default.locale();
|
|
};
|
|
is12HourCycleInCurrentLocale = () => {
|
|
return /A|a/.test(_moment.default.localeData(this.getCurrentLocaleCode()).longDateFormat('LT'));
|
|
};
|
|
expandFormat = format => {
|
|
// @see https://github.com/moment/moment/blob/develop/src/lib/format/format.js#L6
|
|
const localFormattingTokens = /(\[[^[]*\])|(\\)?(LTS|LT|LL?L?L?|l{1,4})|./g;
|
|
return format.match(localFormattingTokens).map(token => {
|
|
const firstCharacter = token[0];
|
|
if (firstCharacter === 'L' || firstCharacter === ';') {
|
|
return _moment.default.localeData(this.getCurrentLocaleCode()).longDateFormat(token);
|
|
}
|
|
return token;
|
|
}).join('');
|
|
};
|
|
isValid = value => {
|
|
if (value == null) {
|
|
return false;
|
|
}
|
|
return value.isValid();
|
|
};
|
|
format = (value, formatKey) => {
|
|
return this.formatByString(value, this.formats[formatKey]);
|
|
};
|
|
formatByString = (value, formatString) => {
|
|
const clonedDate = value.clone();
|
|
clonedDate.locale(this.getCurrentLocaleCode());
|
|
return clonedDate.format(formatString);
|
|
};
|
|
formatNumber = numberToFormat => {
|
|
return numberToFormat;
|
|
};
|
|
isEqual = (value, comparing) => {
|
|
if (value === null && comparing === null) {
|
|
return true;
|
|
}
|
|
if (value === null || comparing === null) {
|
|
return false;
|
|
}
|
|
return value.isSame(comparing);
|
|
};
|
|
isSameYear = (value, comparing) => {
|
|
return value.isSame(comparing, 'year');
|
|
};
|
|
isSameMonth = (value, comparing) => {
|
|
return value.isSame(comparing, 'month');
|
|
};
|
|
isSameDay = (value, comparing) => {
|
|
return value.isSame(comparing, 'day');
|
|
};
|
|
isSameHour = (value, comparing) => {
|
|
return value.isSame(comparing, 'hour');
|
|
};
|
|
isAfter = (value, comparing) => {
|
|
return value.isAfter(comparing);
|
|
};
|
|
isAfterYear = (value, comparing) => {
|
|
return value.isAfter(comparing, 'year');
|
|
};
|
|
isAfterDay = (value, comparing) => {
|
|
return value.isAfter(comparing, 'day');
|
|
};
|
|
isBefore = (value, comparing) => {
|
|
return value.isBefore(comparing);
|
|
};
|
|
isBeforeYear = (value, comparing) => {
|
|
return value.isBefore(comparing, 'year');
|
|
};
|
|
isBeforeDay = (value, comparing) => {
|
|
return value.isBefore(comparing, 'day');
|
|
};
|
|
isWithinRange = (value, [start, end]) => {
|
|
return value.isBetween(start, end, null, '[]');
|
|
};
|
|
startOfYear = value => {
|
|
return value.clone().startOf('year');
|
|
};
|
|
startOfMonth = value => {
|
|
return value.clone().startOf('month');
|
|
};
|
|
startOfWeek = value => {
|
|
return this.setLocaleToValue(value.clone()).startOf('week');
|
|
};
|
|
startOfDay = value => {
|
|
return value.clone().startOf('day');
|
|
};
|
|
endOfYear = value => {
|
|
return value.clone().endOf('year');
|
|
};
|
|
endOfMonth = value => {
|
|
return value.clone().endOf('month');
|
|
};
|
|
endOfWeek = value => {
|
|
return this.setLocaleToValue(value.clone()).endOf('week');
|
|
};
|
|
endOfDay = value => {
|
|
return value.clone().endOf('day');
|
|
};
|
|
addYears = (value, amount) => {
|
|
return amount < 0 ? value.clone().subtract(Math.abs(amount), 'years') : value.clone().add(amount, 'years');
|
|
};
|
|
addMonths = (value, amount) => {
|
|
return amount < 0 ? value.clone().subtract(Math.abs(amount), 'months') : value.clone().add(amount, 'months');
|
|
};
|
|
addWeeks = (value, amount) => {
|
|
return amount < 0 ? value.clone().subtract(Math.abs(amount), 'weeks') : value.clone().add(amount, 'weeks');
|
|
};
|
|
addDays = (value, amount) => {
|
|
return amount < 0 ? value.clone().subtract(Math.abs(amount), 'days') : value.clone().add(amount, 'days');
|
|
};
|
|
addHours = (value, amount) => {
|
|
return amount < 0 ? value.clone().subtract(Math.abs(amount), 'hours') : value.clone().add(amount, 'hours');
|
|
};
|
|
addMinutes = (value, amount) => {
|
|
return amount < 0 ? value.clone().subtract(Math.abs(amount), 'minutes') : value.clone().add(amount, 'minutes');
|
|
};
|
|
addSeconds = (value, amount) => {
|
|
return amount < 0 ? value.clone().subtract(Math.abs(amount), 'seconds') : value.clone().add(amount, 'seconds');
|
|
};
|
|
getYear = value => {
|
|
return value.get('year');
|
|
};
|
|
getMonth = value => {
|
|
return value.get('month');
|
|
};
|
|
getDate = value => {
|
|
return value.get('date');
|
|
};
|
|
getHours = value => {
|
|
return value.get('hours');
|
|
};
|
|
getMinutes = value => {
|
|
return value.get('minutes');
|
|
};
|
|
getSeconds = value => {
|
|
return value.get('seconds');
|
|
};
|
|
getMilliseconds = value => {
|
|
return value.get('milliseconds');
|
|
};
|
|
setYear = (value, year) => {
|
|
return value.clone().year(year);
|
|
};
|
|
setMonth = (value, month) => {
|
|
return value.clone().month(month);
|
|
};
|
|
setDate = (value, date) => {
|
|
return value.clone().date(date);
|
|
};
|
|
setHours = (value, hours) => {
|
|
return value.clone().hours(hours);
|
|
};
|
|
setMinutes = (value, minutes) => {
|
|
return value.clone().minutes(minutes);
|
|
};
|
|
setSeconds = (value, seconds) => {
|
|
return value.clone().seconds(seconds);
|
|
};
|
|
setMilliseconds = (value, milliseconds) => {
|
|
return value.clone().milliseconds(milliseconds);
|
|
};
|
|
getDaysInMonth = value => {
|
|
return value.daysInMonth();
|
|
};
|
|
getWeekArray = value => {
|
|
const start = this.startOfWeek(this.startOfMonth(value));
|
|
const end = this.endOfWeek(this.endOfMonth(value));
|
|
let count = 0;
|
|
let current = start;
|
|
let currentDayOfYear = current.get('dayOfYear');
|
|
const nestedWeeks = [];
|
|
while (current.isBefore(end)) {
|
|
const weekNumber = Math.floor(count / 7);
|
|
nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || [];
|
|
nestedWeeks[weekNumber].push(current);
|
|
const prevDayOfYear = currentDayOfYear;
|
|
current = this.addDays(current, 1);
|
|
currentDayOfYear = current.get('dayOfYear');
|
|
|
|
// If there is a TZ change at midnight, adding 1 day may only increase the date by 23 hours to 11pm
|
|
// To fix, bump the date into the next day (add 12 hours) and then revert to the start of the day
|
|
// See https://github.com/moment/moment/issues/4743#issuecomment-811306874 for context.
|
|
if (prevDayOfYear === currentDayOfYear) {
|
|
current = current.add(12, 'h').startOf('day');
|
|
}
|
|
count += 1;
|
|
}
|
|
return nestedWeeks;
|
|
};
|
|
getWeekNumber = value => {
|
|
return value.week();
|
|
};
|
|
getDayOfWeek = value => {
|
|
return value.day() + 1;
|
|
};
|
|
getYearRange([start, end]) {
|
|
const startDate = this.startOfYear(start);
|
|
const endDate = this.endOfYear(end);
|
|
const years = [];
|
|
let current = startDate;
|
|
while (this.isBefore(current, endDate)) {
|
|
years.push(current);
|
|
current = this.addYears(current, 1);
|
|
}
|
|
return years;
|
|
}
|
|
}
|
|
exports.AdapterMoment = AdapterMoment; |