UNPKG

md2

Version:

Angular2 based Material Design components, directives and services are Accordion, Autocomplete, Chips(Tags), Collapse, Colorpicker, Data Table, Datepicker, Dialog(Modal), Menu, Multiselect, Select, Tabs, Tags(Chips), Toast and Tooltip.

184 lines 8.56 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 __()); }; })(); import { DateAdapter } from './date-adapter'; // TODO(mmalerba): Remove when we no longer support safari 9. /** Whether the browser supports the Intl API. */ var SUPPORTS_INTL_API = typeof Intl != 'undefined'; /** The default month names to use if Intl API is not available. */ 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'] }; /** The default date names to use if Intl API is not available. */ var DEFAULT_DATE_NAMES = range(31, function (i) { return String(i + 1); }); /** The default day of the week names to use if Intl API is not available. */ 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'] }; /** Creates an array and fills it with values. */ function range(length, valueFunction) { var valuesArray = Array(length); for (var i = 0; i < length; i++) { valuesArray[i] = valueFunction(i); } return valuesArray; } /** Adapts the native JS Date for use with cdk-based components that work with dates. */ var NativeDateAdapter = (function (_super) { __extends(NativeDateAdapter, _super); function NativeDateAdapter() { return _super !== null && _super.apply(this, arguments) || this; } NativeDateAdapter.prototype.getYear = function (date) { return date.getFullYear(); }; NativeDateAdapter.prototype.getMonth = function (date) { return date.getMonth(); }; NativeDateAdapter.prototype.getDate = function (date) { return date.getDate(); }; NativeDateAdapter.prototype.getDayOfWeek = function (date) { return date.getDay(); }; NativeDateAdapter.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]; }; NativeDateAdapter.prototype.getDateNames = function () { var _this = this; if (SUPPORTS_INTL_API) { var dtf_2 = new Intl.DateTimeFormat(this.locale, { day: 'numeric' }); return range(31, function (i) { return _this._stripDirectionalityCharacters(dtf_2.format(new Date(2017, 0, i + 1))); }); } return DEFAULT_DATE_NAMES; }; NativeDateAdapter.prototype.getDayOfWeekNames = function (style) { var _this = this; if (SUPPORTS_INTL_API) { var dtf_3 = new Intl.DateTimeFormat(this.locale, { weekday: style }); return range(7, function (i) { return _this._stripDirectionalityCharacters(dtf_3.format(new Date(2017, 0, i + 1))); }); } return DEFAULT_DAY_OF_WEEK_NAMES[style]; }; NativeDateAdapter.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)); }; NativeDateAdapter.prototype.getFirstDayOfWeek = function () { // We can't tell using native JS Date what the first day of the week is, we default to Sunday. return 0; }; NativeDateAdapter.prototype.getNumDaysInMonth = function (date) { return this.getDate(this._createDateWithOverflow(this.getYear(date), this.getMonth(date) + 1, 0)); }; NativeDateAdapter.prototype.clone = function (date) { return this.createDate(this.getYear(date), this.getMonth(date), this.getDate(date)); }; NativeDateAdapter.prototype.createDate = function (year, month, date) { // Check for invalid month and date (except upper bound on date which we have to check after // creating the Date). if (month < 0 || month > 11 || date < 1) { return null; } var result = this._createDateWithOverflow(year, month, date); // Check that the date wasn't above the upper bound for the month, causing the month to // overflow. if (result.getMonth() != month) { return null; } return result; }; NativeDateAdapter.prototype.today = function () { return new Date(); }; NativeDateAdapter.prototype.parse = function (value) { // We have no way using the native JS Date to set the parse format or locale, so we ignore these // parameters. var timestamp = typeof value == 'number' ? value : Date.parse(value); return isNaN(timestamp) ? null : new Date(timestamp); }; NativeDateAdapter.prototype.format = function (date, displayFormat) { if (SUPPORTS_INTL_API) { var dtf = new Intl.DateTimeFormat(this.locale, displayFormat); return this._stripDirectionalityCharacters(dtf.format(date)); } return this._stripDirectionalityCharacters(date.toDateString()); }; NativeDateAdapter.prototype.addCalendarYears = function (date, years) { return this.addCalendarMonths(date, years * 12); }; NativeDateAdapter.prototype.addCalendarMonths = function (date, months) { var newDate = this._createDateWithOverflow(this.getYear(date), this.getMonth(date) + months, this.getDate(date)); // It's possible to wind up in the wrong month if the original month has more days than the new // month. In this case we want to go to the last day of the desired month. // Note: the additional + 12 % 12 ensures we end up with a positive number, since JS % doesn't // guarantee this. if (this.getMonth(newDate) != ((this.getMonth(date) + months) % 12 + 12) % 12) { newDate = this._createDateWithOverflow(this.getYear(newDate), this.getMonth(newDate), 0); } return newDate; }; NativeDateAdapter.prototype.addCalendarDays = function (date, days) { return this._createDateWithOverflow(this.getYear(date), this.getMonth(date), this.getDate(date) + days); }; NativeDateAdapter.prototype.getISODateString = function (date) { return [ date.getUTCFullYear(), this._2digit(date.getUTCMonth() + 1), this._2digit(date.getUTCDate()) ].join('-'); }; /** Creates a date but allows the month and date to overflow. */ NativeDateAdapter.prototype._createDateWithOverflow = function (year, month, date) { var result = new Date(year, month, date); // We need to correct for the fact that JS native Date treats years in range [0, 99] as // abbreviations for 19xx. if (year >= 0 && year < 100) { result.setFullYear(this.getYear(result) - 1900); } return result; }; /** * Pads a number to make it two digits. * @param n The number to pad. * @returns The padded number. */ NativeDateAdapter.prototype._2digit = function (n) { return ('00' + n).slice(-2); }; /** * Strip out unicode LTR and RTL characters. Edge and IE insert these into formatted dates while * other browsers do not. We remove them to make output consistent and because they interfere with * date parsing. * @param s The string to strip direction characters from. * @returns The stripped string. */ NativeDateAdapter.prototype._stripDirectionalityCharacters = function (s) { return s.replace(/[\u200e\u200f]/g, ''); }; return NativeDateAdapter; }(DateAdapter)); export { NativeDateAdapter }; //# sourceMappingURL=native-date-adapter.js.map