ng-pick-datetime
Version:
Angular Date Time Picker
304 lines (303 loc) • 14.1 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
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 extendStatics(d, b);
}
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) || function () {
__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;
};
return __assign.apply(this, arguments);
};
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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
import { Inject, Injectable, Optional } from '@angular/core';
import { Platform } from '@angular/cdk/platform';
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 ɵ0 = function (i) { return String(i + 1); };
var DEFAULT_DATE_NAMES = range(31, ɵ0);
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, platform) {
var _this = _super.call(this) || this;
_this.owlDateTimeLocale = owlDateTimeLocale;
_super.prototype.setLocale.call(_this, owlDateTimeLocale);
_this.useUtcForDisplay = !platform.TRIDENT;
_this._clampDate = platform.TRIDENT || platform.EDGE;
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', timeZone: 'utc' });
return this.stripDirectionalityCharacters(this._format(dtf, 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, timeZone: 'utc' });
return range(12, function (i) { return _this.stripDirectionalityCharacters(_this._format(dtf_1, 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, timeZone: 'utc' });
return range(7, function (i) { return _this.stripDirectionalityCharacters(_this._format(dtf_2, new Date(2017, 0, i + 1))); });
}
return DEFAULT_DAY_OF_WEEK_NAMES[style];
};
NativeDateTimeAdapter.prototype.getDateNames = function () {
var _this = this;
if (SUPPORTS_INTL_API) {
var dtf_3 = new Intl.DateTimeFormat(this.locale, { day: 'numeric', timeZone: 'utc' });
return range(31, function (i) { return _this.stripDirectionalityCharacters(_this._format(dtf_3, new Date(2017, 0, i + 1))); });
}
return DEFAULT_DATE_NAMES;
};
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 this.createDate(this.getYear(date), this.getMonth(date), this.getDate(date), this.getHours(date), this.getMinutes(date), this.getSeconds(date));
};
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._clampDate && (date.getFullYear() < 1 || date.getFullYear() > 9999)) {
date = this.clone(date);
date.setFullYear(Math.max(1, Math.min(9999, date.getFullYear())));
}
displayFormat = __assign({}, displayFormat, { timeZone: 'utc' });
var dtf = new Intl.DateTimeFormat(this.locale, displayFormat);
return this.stripDirectionalityCharacters(this._format(dtf, 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.prototype._format = function (dtf, date) {
var d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds()));
return dtf.format(d);
};
NativeDateTimeAdapter = __decorate([
Injectable(),
__param(0, Optional()), __param(0, Inject(OWL_DATE_TIME_LOCALE)),
__metadata("design:paramtypes", [String, Platform])
], NativeDateTimeAdapter);
return NativeDateTimeAdapter;
}(DateTimeAdapter));
export { NativeDateTimeAdapter };
export { ɵ0 };