UNPKG

ng4-pick-datetime

Version:
270 lines (269 loc) 12 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; import { Inject, Injectable, Optional } from '@angular/core'; import { DateTimeAdapter, OWL_DATE_TIME_LOCALE } from './date-time-adapter.class'; var DEFAULT_MONTH_NAMES = { 'long': [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ], 'short': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], 'narrow': ['J', 'F', 'M', 'A', 'M', 'J', 'J', 'A', 'S', 'O', 'N', 'D'] }; var DEFAULT_DAY_OF_WEEK_NAMES = { 'long': ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], 'short': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], 'narrow': ['S', 'M', 'T', 'W', 'T', 'F', 'S'] }; var SUPPORTS_INTL_API = typeof Intl !== 'undefined'; var ISO_8601_REGEX = /^\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|(?:(?:\+|-)\d{2}:\d{2}))?)?$/; function range(length, valueFunction) { var valuesArray = Array(length); for (var i = 0; i < length; i++) { valuesArray[i] = valueFunction(i); } return valuesArray; } var NativeDateTimeAdapter = (function (_super) { __extends(NativeDateTimeAdapter, _super); function NativeDateTimeAdapter(owlDateTimeLocale) { var _this = _super.call(this) || this; _this.owlDateTimeLocale = owlDateTimeLocale; _super.prototype.setLocale.call(_this, owlDateTimeLocale); _this.useUtcForDisplay = !(typeof document === 'object' && !!document && /(msie|trident)/i.test(navigator.userAgent)); return _this; } NativeDateTimeAdapter.prototype.getYear = function (date) { return date.getFullYear(); }; NativeDateTimeAdapter.prototype.getMonth = function (date) { return date.getMonth(); }; NativeDateTimeAdapter.prototype.getDay = function (date) { return date.getDay(); }; NativeDateTimeAdapter.prototype.getDate = function (date) { return date.getDate(); }; NativeDateTimeAdapter.prototype.getHours = function (date) { return date.getHours(); }; NativeDateTimeAdapter.prototype.getMinutes = function (date) { return date.getMinutes(); }; NativeDateTimeAdapter.prototype.getSeconds = function (date) { return date.getSeconds(); }; NativeDateTimeAdapter.prototype.getTime = function (date) { return date.getTime(); }; NativeDateTimeAdapter.prototype.getNumDaysInMonth = function (date) { var lastDateOfMonth = this.createDateWithOverflow(this.getYear(date), this.getMonth(date) + 1, 0); return this.getDate(lastDateOfMonth); }; NativeDateTimeAdapter.prototype.differenceInCalendarDays = function (dateLeft, dateRight) { if (this.isValid(dateLeft) && this.isValid(dateRight)) { var dateLeftStartOfDay = this.createDate(this.getYear(dateLeft), this.getMonth(dateLeft), this.getDate(dateLeft)); var dateRightStartOfDay = this.createDate(this.getYear(dateRight), this.getMonth(dateRight), this.getDate(dateRight)); var timeStampLeft = this.getTime(dateLeftStartOfDay) - dateLeftStartOfDay.getTimezoneOffset() * this.milliseondsInMinute; var timeStampRight = this.getTime(dateRightStartOfDay) - dateRightStartOfDay.getTimezoneOffset() * this.milliseondsInMinute; return Math.round((timeStampLeft - timeStampRight) / this.millisecondsInDay); } else { return null; } }; NativeDateTimeAdapter.prototype.getYearName = function (date) { if (SUPPORTS_INTL_API) { var dtf = new Intl.DateTimeFormat(this.locale, { year: 'numeric' }); return this.stripDirectionalityCharacters(dtf.format(date)); } return String(this.getYear(date)); }; NativeDateTimeAdapter.prototype.getMonthNames = function (style) { var _this = this; if (SUPPORTS_INTL_API) { var dtf_1 = new Intl.DateTimeFormat(this.locale, { month: style }); return range(12, function (i) { return _this.stripDirectionalityCharacters(dtf_1.format(new Date(2017, i, 1))); }); } return DEFAULT_MONTH_NAMES[style]; }; NativeDateTimeAdapter.prototype.getDayOfWeekNames = function (style) { var _this = this; if (SUPPORTS_INTL_API) { var dtf_2 = new Intl.DateTimeFormat(this.locale, { weekday: style }); return range(7, function (i) { return _this.stripDirectionalityCharacters(dtf_2.format(new Date(2017, 0, i + 1))); }); } return DEFAULT_DAY_OF_WEEK_NAMES[style]; }; NativeDateTimeAdapter.prototype.toIso8601 = function (date) { return date.toISOString(); }; NativeDateTimeAdapter.prototype.isEqual = function (dateLeft, dateRight) { if (this.isValid(dateLeft) && this.isValid(dateRight)) { return dateLeft.getTime() === dateRight.getTime(); } else { return false; } }; NativeDateTimeAdapter.prototype.isSameDay = function (dateLeft, dateRight) { if (this.isValid(dateLeft) && this.isValid(dateRight)) { var dateLeftStartOfDay = this.clone(dateLeft); var dateRightStartOfDay = this.clone(dateRight); dateLeftStartOfDay.setHours(0, 0, 0, 0); dateRightStartOfDay.setHours(0, 0, 0, 0); return dateLeftStartOfDay.getTime() === dateRightStartOfDay.getTime(); } else { return false; } }; NativeDateTimeAdapter.prototype.isValid = function (date) { return date && !isNaN(date.getTime()); }; NativeDateTimeAdapter.prototype.invalid = function () { return new Date(NaN); }; NativeDateTimeAdapter.prototype.isDateInstance = function (obj) { return obj instanceof Date; }; NativeDateTimeAdapter.prototype.addCalendarYears = function (date, amount) { return this.addCalendarMonths(date, amount * 12); }; NativeDateTimeAdapter.prototype.addCalendarMonths = function (date, amount) { var result = this.clone(date); amount = Number(amount); var desiredMonth = result.getMonth() + amount; var dateWithDesiredMonth = new Date(0); dateWithDesiredMonth.setFullYear(result.getFullYear(), desiredMonth, 1); dateWithDesiredMonth.setHours(0, 0, 0, 0); var daysInMonth = this.getNumDaysInMonth(dateWithDesiredMonth); result.setMonth(desiredMonth, Math.min(daysInMonth, result.getDate())); return result; }; NativeDateTimeAdapter.prototype.addCalendarDays = function (date, amount) { var result = this.clone(date); amount = Number(amount); result.setDate(result.getDate() + amount); return result; }; NativeDateTimeAdapter.prototype.setHours = function (date, amount) { var result = this.clone(date); result.setHours(amount); return result; }; NativeDateTimeAdapter.prototype.setMinutes = function (date, amount) { var result = this.clone(date); result.setMinutes(amount); return result; }; NativeDateTimeAdapter.prototype.setSeconds = function (date, amount) { var result = this.clone(date); result.setSeconds(amount); return result; }; NativeDateTimeAdapter.prototype.createDate = function (year, month, date, hours, minutes, seconds) { if (hours === void 0) { hours = 0; } if (minutes === void 0) { minutes = 0; } if (seconds === void 0) { seconds = 0; } 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."); } if (hours < 0 || hours > 23) { throw Error("Invalid hours \"" + hours + "\". Hours has to be between 0 and 23."); } if (minutes < 0 || minutes > 59) { throw Error("Invalid minutes \"" + minutes + "\". Minutes has to between 0 and 59."); } if (seconds < 0 || seconds > 59) { throw Error("Invalid seconds \"" + seconds + "\". Seconds has to be between 0 and 59."); } var result = this.createDateWithOverflow(year, month, date, hours, minutes, seconds); if (result.getMonth() !== month) { throw Error("Invalid date \"" + date + "\" for month with index \"" + month + "\"."); } return result; }; NativeDateTimeAdapter.prototype.clone = function (date) { return new Date(date.getTime()); }; NativeDateTimeAdapter.prototype.now = function () { return new Date(); }; NativeDateTimeAdapter.prototype.format = function (date, displayFormat) { if (!this.isValid(date)) { throw Error('JSNativeDate: Cannot format invalid date.'); } if (SUPPORTS_INTL_API) { if (this.useUtcForDisplay) { date = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds())); displayFormat = __assign({}, displayFormat, { timeZone: 'utc' }); } var dtf = new Intl.DateTimeFormat(this.locale, displayFormat); return this.stripDirectionalityCharacters(dtf.format(date)); } return this.stripDirectionalityCharacters(date.toDateString()); }; NativeDateTimeAdapter.prototype.parse = function (value, parseFormat) { if (typeof value === 'number') { return new Date(value); } return value ? new Date(Date.parse(value)) : null; }; NativeDateTimeAdapter.prototype.deserialize = function (value) { if (typeof value === 'string') { if (!value) { return null; } if (ISO_8601_REGEX.test(value)) { var date = new Date(value); if (this.isValid(date)) { return date; } } } return _super.prototype.deserialize.call(this, value); }; NativeDateTimeAdapter.prototype.createDateWithOverflow = function (year, month, date, hours, minutes, seconds) { if (hours === void 0) { hours = 0; } if (minutes === void 0) { minutes = 0; } if (seconds === void 0) { seconds = 0; } var result = new Date(year, month, date, hours, minutes, seconds); if (year >= 0 && year < 100) { result.setFullYear(this.getYear(result) - 1900); } return result; }; NativeDateTimeAdapter.prototype.stripDirectionalityCharacters = function (str) { return str.replace(/[\u200e\u200f]/g, ''); }; NativeDateTimeAdapter.decorators = [ { type: Injectable }, ]; NativeDateTimeAdapter.ctorParameters = function () { return [ { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [OWL_DATE_TIME_LOCALE,] },] }, ]; }; return NativeDateTimeAdapter; }(DateTimeAdapter)); export { NativeDateTimeAdapter };