primeng
Version:
[](https://opensource.org/licenses/MIT) [](https://gitter.im/primefaces/primeng?ut
1,275 lines • 78.3 kB
JavaScript
"use strict";
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);
};
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var animations_1 = require("@angular/animations");
var common_1 = require("@angular/common");
var button_1 = require("../button/button");
var domhandler_1 = require("../dom/domhandler");
var shared_1 = require("../common/shared");
var forms_1 = require("@angular/forms");
exports.CALENDAR_VALUE_ACCESSOR = {
provide: forms_1.NG_VALUE_ACCESSOR,
useExisting: core_1.forwardRef(function () { return Calendar; }),
multi: true
};
var Calendar = /** @class */ (function () {
function Calendar(el, domHandler, renderer, cd) {
this.el = el;
this.domHandler = domHandler;
this.renderer = renderer;
this.cd = cd;
this.dateFormat = 'mm/dd/yy';
this.inline = false;
this.showOtherMonths = true;
this.icon = 'pi pi-calendar';
this.shortYearCutoff = '+10';
this.hourFormat = '24';
this.stepHour = 1;
this.stepMinute = 1;
this.stepSecond = 1;
this.showSeconds = false;
this.showOnFocus = true;
this.dataType = 'date';
this.selectionMode = 'single';
this.todayButtonStyleClass = 'ui-button-secondary';
this.clearButtonStyleClass = 'ui-button-secondary';
this.autoZIndex = true;
this.baseZIndex = 0;
this.keepInvalid = false;
this.hideOnDateTimeSelect = false;
this.numberOfMonths = 1;
this.view = 'date';
this.showTransitionOptions = '225ms ease-out';
this.hideTransitionOptions = '195ms ease-in';
this.onFocus = new core_1.EventEmitter();
this.onBlur = new core_1.EventEmitter();
this.onClose = new core_1.EventEmitter();
this.onSelect = new core_1.EventEmitter();
this.onInput = new core_1.EventEmitter();
this.onTodayClick = new core_1.EventEmitter();
this.onClearClick = new core_1.EventEmitter();
this.onMonthChange = new core_1.EventEmitter();
this.onYearChange = new core_1.EventEmitter();
this._locale = {
firstDayOfWeek: 0,
dayNames: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
dayNamesShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
dayNamesMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
monthNames: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
monthNamesShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
today: 'Today',
clear: 'Clear'
};
this.onModelChange = function () { };
this.onModelTouched = function () { };
this.inputFieldValue = null;
}
Object.defineProperty(Calendar.prototype, "utc", {
get: function () {
return this._utc;
},
set: function (_utc) {
this._utc = _utc;
console.log("Setting utc has no effect as built-in UTC support is dropped.");
},
enumerable: true,
configurable: true
});
Object.defineProperty(Calendar.prototype, "minDate", {
get: function () {
return this._minDate;
},
set: function (date) {
this._minDate = date;
if (this.currentMonth != undefined && this.currentMonth != null && this.currentYear) {
this.createMonths(this.currentMonth, this.currentYear);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(Calendar.prototype, "maxDate", {
get: function () {
return this._maxDate;
},
set: function (date) {
this._maxDate = date;
if (this.currentMonth != undefined && this.currentMonth != null && this.currentYear) {
this.createMonths(this.currentMonth, this.currentYear);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(Calendar.prototype, "disabledDates", {
get: function () {
return this._disabledDates;
},
set: function (disabledDates) {
this._disabledDates = disabledDates;
if (this.currentMonth != undefined && this.currentMonth != null && this.currentYear) {
this.createMonths(this.currentMonth, this.currentYear);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(Calendar.prototype, "disabledDays", {
get: function () {
return this._disabledDays;
},
set: function (disabledDays) {
this._disabledDays = disabledDays;
if (this.currentMonth != undefined && this.currentMonth != null && this.currentYear) {
this.createMonths(this.currentMonth, this.currentYear);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(Calendar.prototype, "showTime", {
get: function () {
return this._showTime;
},
set: function (showTime) {
this._showTime = showTime;
if (this.currentHour === undefined) {
this.initTime(this.value || new Date());
}
this.updateInputfield();
},
enumerable: true,
configurable: true
});
Object.defineProperty(Calendar.prototype, "locale", {
get: function () {
return this._locale;
},
set: function (newLocale) {
this._locale = newLocale;
if (this.view === 'date') {
this.createWeekDays();
this.createMonths(this.currentMonth, this.currentYear);
}
else if (this.view === 'month') {
this.createMonthPickerValues();
}
},
enumerable: true,
configurable: true
});
Calendar.prototype.ngOnInit = function () {
var date = this.defaultDate || new Date();
this.currentMonth = date.getMonth();
this.currentYear = date.getFullYear();
if (this.yearNavigator && this.yearRange) {
var years = this.yearRange.split(':');
var yearStart = parseInt(years[0]);
var yearEnd = parseInt(years[1]);
this.populateYearOptions(yearStart, yearEnd);
}
if (this.view === 'date') {
this.createWeekDays();
this.initTime(date);
this.createMonths(this.currentMonth, this.currentYear);
this.ticksTo1970 = (((1970 - 1) * 365 + Math.floor(1970 / 4) - Math.floor(1970 / 100) + Math.floor(1970 / 400)) * 24 * 60 * 60 * 10000000);
}
else if (this.view === 'month') {
this.createMonthPickerValues();
}
};
Calendar.prototype.ngAfterContentInit = function () {
var _this = this;
this.templates.forEach(function (item) {
switch (item.getType()) {
case 'date':
_this.dateTemplate = item.template;
break;
default:
_this.dateTemplate = item.template;
break;
}
});
};
Calendar.prototype.populateYearOptions = function (start, end) {
this.yearOptions = [];
for (var i = start; i <= end; i++) {
this.yearOptions.push(i);
}
};
Calendar.prototype.createWeekDays = function () {
this.weekDays = [];
var dayIndex = this.locale.firstDayOfWeek;
for (var i = 0; i < 7; i++) {
this.weekDays.push(this.locale.dayNamesMin[dayIndex]);
dayIndex = (dayIndex == 6) ? 0 : ++dayIndex;
}
};
Calendar.prototype.createMonthPickerValues = function () {
this.monthPickerValues = [];
for (var i = 0; i <= 11; i++) {
this.monthPickerValues.push(this.locale.monthNamesShort[i]);
}
};
Calendar.prototype.createMonths = function (month, year) {
this.months = this.months = [];
for (var i = 0; i < this.numberOfMonths; i++) {
var m = month + i;
var y = year;
if (m > 11) {
m = m % 11 - 1;
y = year + 1;
}
this.months.push(this.createMonth(m, y));
}
};
Calendar.prototype.createMonth = function (month, year) {
var dates = [];
var firstDay = this.getFirstDayOfMonthIndex(month, year);
var daysLength = this.getDaysCountInMonth(month, year);
var prevMonthDaysLength = this.getDaysCountInPrevMonth(month, year);
var sundayIndex = this.getSundayIndex();
var dayNo = 1;
var today = new Date();
for (var i = 0; i < 6; i++) {
var week = [];
if (i == 0) {
for (var j = (prevMonthDaysLength - firstDay + 1); j <= prevMonthDaysLength; j++) {
var prev = this.getPreviousMonthAndYear(month, year);
week.push({ day: j, month: prev.month, year: prev.year, otherMonth: true,
today: this.isToday(today, j, prev.month, prev.year), selectable: this.isSelectable(j, prev.month, prev.year, true) });
}
var remainingDaysLength = 7 - week.length;
for (var j = 0; j < remainingDaysLength; j++) {
week.push({ day: dayNo, month: month, year: year, today: this.isToday(today, dayNo, month, year),
selectable: this.isSelectable(dayNo, month, year, false) });
dayNo++;
}
}
else {
for (var j = 0; j < 7; j++) {
if (dayNo > daysLength) {
var next = this.getNextMonthAndYear(month, year);
week.push({ day: dayNo - daysLength, month: next.month, year: next.year, otherMonth: true,
today: this.isToday(today, dayNo - daysLength, next.month, next.year),
selectable: this.isSelectable((dayNo - daysLength), next.month, next.year, true) });
}
else {
week.push({ day: dayNo, month: month, year: year, today: this.isToday(today, dayNo, month, year),
selectable: this.isSelectable(dayNo, month, year, false) });
}
dayNo++;
}
}
dates.push(week);
}
return {
month: month,
year: year,
dates: dates
};
};
Calendar.prototype.initTime = function (date) {
this.pm = date.getHours() > 11;
if (this.showTime) {
this.currentMinute = date.getMinutes();
this.currentSecond = date.getSeconds();
if (this.hourFormat == '12')
this.currentHour = date.getHours() == 0 ? 12 : date.getHours() % 12;
else
this.currentHour = date.getHours();
}
else if (this.timeOnly) {
this.currentMinute = 0;
this.currentHour = 0;
this.currentSecond = 0;
}
};
Calendar.prototype.navBackward = function (event) {
if (this.disabled) {
event.preventDefault();
return;
}
if (this.view === 'month') {
this.decrementYear();
}
else {
if (this.currentMonth === 0) {
this.currentMonth = 11;
this.decrementYear();
}
else {
this.currentMonth--;
}
this.onMonthChange.emit({ month: this.currentMonth + 1, year: this.currentYear });
this.createMonths(this.currentMonth, this.currentYear);
}
event.preventDefault();
};
Calendar.prototype.navForward = function (event) {
if (this.disabled) {
event.preventDefault();
return;
}
if (this.view === 'month') {
this.incrementYear();
}
else {
if (this.currentMonth === 11) {
this.currentMonth = 0;
this.incrementYear();
}
else {
this.currentMonth++;
}
this.onMonthChange.emit({ month: this.currentMonth + 1, year: this.currentYear });
this.createMonths(this.currentMonth, this.currentYear);
}
event.preventDefault();
};
Calendar.prototype.decrementYear = function () {
this.currentYear--;
if (this.yearNavigator && this.currentYear < this.yearOptions[0]) {
var difference = this.yearOptions[this.yearOptions.length - 1] - this.yearOptions[0];
this.populateYearOptions(this.yearOptions[0] - difference, this.yearOptions[this.yearOptions.length - 1] - difference);
}
};
Calendar.prototype.incrementYear = function () {
this.currentYear++;
if (this.yearNavigator && this.currentYear > this.yearOptions[this.yearOptions.length - 1]) {
var difference = this.yearOptions[this.yearOptions.length - 1] - this.yearOptions[0];
this.populateYearOptions(this.yearOptions[0] + difference, this.yearOptions[this.yearOptions.length - 1] + difference);
}
};
Calendar.prototype.onDateSelect = function (event, dateMeta) {
var _this = this;
if (this.disabled || !dateMeta.selectable) {
event.preventDefault();
return;
}
if (this.isMultipleSelection() && this.isSelected(dateMeta)) {
this.value = this.value.filter(function (date, i) {
return !_this.isDateEquals(date, dateMeta);
});
this.updateModel(this.value);
}
else {
if (this.shouldSelectDate(dateMeta)) {
if (dateMeta.otherMonth) {
this.currentMonth = dateMeta.month;
this.currentYear = dateMeta.year;
this.createMonths(this.currentMonth, this.currentYear);
this.selectDate(dateMeta);
}
else {
this.selectDate(dateMeta);
}
}
}
if (this.isSingleSelection() && (!this.showTime || this.hideOnDateTimeSelect)) {
setTimeout(function () {
event.preventDefault();
_this.overlayVisible = false;
if (_this.mask) {
_this.disableModality();
}
_this.cd.markForCheck();
}, 150);
}
this.updateInputfield();
event.preventDefault();
};
Calendar.prototype.shouldSelectDate = function (dateMeta) {
if (this.isMultipleSelection())
return !this.maxDateCount || !this.value || this.maxDateCount > this.value.length;
else
return true;
};
Calendar.prototype.onMonthSelect = function (event, index) {
this.onDateSelect(event, { year: this.currentYear, month: index, day: 1, selectable: true });
};
Calendar.prototype.updateInputfield = function () {
var formattedValue = '';
if (this.value) {
if (this.isSingleSelection()) {
formattedValue = this.formatDateTime(this.value);
}
else if (this.isMultipleSelection()) {
for (var i = 0; i < this.value.length; i++) {
var dateAsString = this.formatDateTime(this.value[i]);
formattedValue += dateAsString;
if (i !== (this.value.length - 1)) {
formattedValue += ', ';
}
}
}
else if (this.isRangeSelection()) {
if (this.value && this.value.length) {
var startDate = this.value[0];
var endDate = this.value[1];
formattedValue = this.formatDateTime(startDate);
if (endDate) {
formattedValue += ' - ' + this.formatDateTime(endDate);
}
}
}
}
this.inputFieldValue = formattedValue;
this.updateFilledState();
if (this.inputfieldViewChild && this.inputfieldViewChild.nativeElement) {
this.inputfieldViewChild.nativeElement.value = this.inputFieldValue;
}
};
Calendar.prototype.formatDateTime = function (date) {
var formattedValue = null;
if (date) {
if (this.timeOnly) {
formattedValue = this.formatTime(date);
}
else {
formattedValue = this.formatDate(date, this.dateFormat);
if (this.showTime) {
formattedValue += ' ' + this.formatTime(date);
}
}
}
return formattedValue;
};
Calendar.prototype.selectDate = function (dateMeta) {
var date = new Date(dateMeta.year, dateMeta.month, dateMeta.day);
if (this.showTime) {
if (this.hourFormat === '12' && this.pm && this.currentHour != 12)
date.setHours(this.currentHour + 12);
else
date.setHours(this.currentHour);
date.setMinutes(this.currentMinute);
date.setSeconds(this.currentSecond);
}
if (this.minDate && this.minDate > date) {
date = this.minDate;
this.currentHour = date.getHours();
this.currentMinute = date.getMinutes();
this.currentSecond = date.getSeconds();
}
if (this.maxDate && this.maxDate < date) {
date = this.maxDate;
this.currentHour = date.getHours();
this.currentMinute = date.getMinutes();
this.currentSecond = date.getSeconds();
}
if (this.isSingleSelection()) {
this.updateModel(date);
}
else if (this.isMultipleSelection()) {
this.updateModel(this.value ? this.value.concat([date]) : [date]);
}
else if (this.isRangeSelection()) {
if (this.value && this.value.length) {
var startDate = this.value[0];
var endDate = this.value[1];
if (!endDate && date.getTime() >= startDate.getTime()) {
endDate = date;
}
else {
startDate = date;
endDate = null;
}
this.updateModel([startDate, endDate]);
}
else {
this.updateModel([date, null]);
}
}
this.onSelect.emit(date);
};
Calendar.prototype.updateModel = function (value) {
var _this = this;
this.value = value;
if (this.dataType == 'date') {
this.onModelChange(this.value);
}
else if (this.dataType == 'string') {
if (this.isSingleSelection()) {
this.onModelChange(this.formatDateTime(this.value));
}
else {
var stringArrValue = null;
if (this.value) {
stringArrValue = this.value.map(function (date) { return _this.formatDateTime(date); });
}
this.onModelChange(stringArrValue);
}
}
};
Calendar.prototype.getFirstDayOfMonthIndex = function (month, year) {
var day = new Date();
day.setDate(1);
day.setMonth(month);
day.setFullYear(year);
var dayIndex = day.getDay() + this.getSundayIndex();
return dayIndex >= 7 ? dayIndex - 7 : dayIndex;
};
Calendar.prototype.getDaysCountInMonth = function (month, year) {
return 32 - this.daylightSavingAdjust(new Date(year, month, 32)).getDate();
};
Calendar.prototype.getDaysCountInPrevMonth = function (month, year) {
var prev = this.getPreviousMonthAndYear(month, year);
return this.getDaysCountInMonth(prev.month, prev.year);
};
Calendar.prototype.getPreviousMonthAndYear = function (month, year) {
var m, y;
if (month === 0) {
m = 11;
y = year - 1;
}
else {
m = month - 1;
y = year;
}
return { 'month': m, 'year': y };
};
Calendar.prototype.getNextMonthAndYear = function (month, year) {
var m, y;
if (month === 11) {
m = 0;
y = year + 1;
}
else {
m = month + 1;
y = year;
}
return { 'month': m, 'year': y };
};
Calendar.prototype.getSundayIndex = function () {
return this.locale.firstDayOfWeek > 0 ? 7 - this.locale.firstDayOfWeek : 0;
};
Calendar.prototype.isSelected = function (dateMeta) {
if (this.value) {
if (this.isSingleSelection()) {
return this.isDateEquals(this.value, dateMeta);
}
else if (this.isMultipleSelection()) {
var selected = false;
for (var _i = 0, _a = this.value; _i < _a.length; _i++) {
var date = _a[_i];
selected = this.isDateEquals(date, dateMeta);
if (selected) {
break;
}
}
return selected;
}
else if (this.isRangeSelection()) {
if (this.value[1])
return this.isDateEquals(this.value[0], dateMeta) || this.isDateEquals(this.value[1], dateMeta) || this.isDateBetween(this.value[0], this.value[1], dateMeta);
else
return this.isDateEquals(this.value[0], dateMeta);
}
}
else {
return false;
}
};
Calendar.prototype.isMonthSelected = function (month) {
return this.value ? (this.value.getMonth() === month && this.value.getFullYear() === this.currentYear) : false;
};
Calendar.prototype.isDateEquals = function (value, dateMeta) {
if (value)
return value.getDate() === dateMeta.day && value.getMonth() === dateMeta.month && value.getFullYear() === dateMeta.year;
else
return false;
};
Calendar.prototype.isDateBetween = function (start, end, dateMeta) {
var between = false;
if (start && end) {
var date = new Date(dateMeta.year, dateMeta.month, dateMeta.day);
return start.getTime() <= date.getTime() && end.getTime() >= date.getTime();
}
return between;
};
Calendar.prototype.isSingleSelection = function () {
return this.selectionMode === 'single';
};
Calendar.prototype.isRangeSelection = function () {
return this.selectionMode === 'range';
};
Calendar.prototype.isMultipleSelection = function () {
return this.selectionMode === 'multiple';
};
Calendar.prototype.isToday = function (today, day, month, year) {
return today.getDate() === day && today.getMonth() === month && today.getFullYear() === year;
};
Calendar.prototype.isSelectable = function (day, month, year, otherMonth) {
var validMin = true;
var validMax = true;
var validDate = true;
var validDay = true;
if (otherMonth && !this.selectOtherMonths) {
return false;
}
if (this.minDate) {
if (this.minDate.getFullYear() > year) {
validMin = false;
}
else if (this.minDate.getFullYear() === year) {
if (this.minDate.getMonth() > month) {
validMin = false;
}
else if (this.minDate.getMonth() === month) {
if (this.minDate.getDate() > day) {
validMin = false;
}
}
}
}
if (this.maxDate) {
if (this.maxDate.getFullYear() < year) {
validMax = false;
}
else if (this.maxDate.getFullYear() === year) {
if (this.maxDate.getMonth() < month) {
validMax = false;
}
else if (this.maxDate.getMonth() === month) {
if (this.maxDate.getDate() < day) {
validMax = false;
}
}
}
}
if (this.disabledDates) {
validDate = !this.isDateDisabled(day, month, year);
}
if (this.disabledDays) {
validDay = !this.isDayDisabled(day, month, year);
}
return validMin && validMax && validDate && validDay;
};
Calendar.prototype.isDateDisabled = function (day, month, year) {
if (this.disabledDates) {
for (var _i = 0, _a = this.disabledDates; _i < _a.length; _i++) {
var disabledDate = _a[_i];
if (disabledDate.getFullYear() === year && disabledDate.getMonth() === month && disabledDate.getDate() === day) {
return true;
}
}
}
return false;
};
Calendar.prototype.isDayDisabled = function (day, month, year) {
if (this.disabledDays) {
var weekday = new Date(year, month, day);
var weekdayNumber = weekday.getDay();
return this.disabledDays.indexOf(weekdayNumber) !== -1;
}
return false;
};
Calendar.prototype.onInputFocus = function (event) {
this.focus = true;
if (this.showOnFocus) {
this.showOverlay();
}
this.onFocus.emit(event);
};
Calendar.prototype.onInputClick = function (event) {
this.datepickerClick = true;
if (this.overlay && this.autoZIndex) {
this.overlay.style.zIndex = String(this.baseZIndex + (++domhandler_1.DomHandler.zindex));
}
};
Calendar.prototype.onInputBlur = function (event) {
this.focus = false;
this.onBlur.emit(event);
if (!this.keepInvalid) {
this.updateInputfield();
}
this.onModelTouched();
};
Calendar.prototype.onButtonClick = function (event, inputfield) {
if (!this.overlayVisible) {
inputfield.focus();
this.showOverlay();
}
else {
this.overlayVisible = false;
}
this.datepickerClick = true;
};
Calendar.prototype.onInputKeydown = function (event) {
this.isKeydown = true;
if (event.keyCode === 9) {
this.overlayVisible = false;
}
};
Calendar.prototype.onMonthDropdownChange = function (m) {
this.currentMonth = parseInt(m);
this.onMonthChange.emit({ month: this.currentMonth + 1, year: this.currentYear });
this.createMonths(this.currentMonth, this.currentYear);
};
Calendar.prototype.onYearDropdownChange = function (y) {
this.currentYear = parseInt(y);
this.onYearChange.emit({ month: this.currentMonth + 1, year: this.currentYear });
this.createMonths(this.currentMonth, this.currentYear);
};
Calendar.prototype.incrementHour = function (event) {
var prevHour = this.currentHour;
var newHour = this.currentHour + this.stepHour;
if (this.validateHour(newHour)) {
if (this.hourFormat == '24')
this.currentHour = (newHour >= 24) ? (newHour - 24) : newHour;
else if (this.hourFormat == '12') {
// Before the AM/PM break, now after
if (prevHour < 12 && newHour > 11) {
this.pm = !this.pm;
}
this.currentHour = (newHour >= 13) ? (newHour - 12) : newHour;
}
this.updateTime();
}
event.preventDefault();
};
Calendar.prototype.decrementHour = function (event) {
var newHour = this.currentHour - this.stepHour;
if (this.validateHour(newHour)) {
if (this.hourFormat == '24')
this.currentHour = (newHour < 0) ? (24 + newHour) : newHour;
else if (this.hourFormat == '12') {
// If we were at noon/midnight, then switch
if (this.currentHour === 12) {
this.pm = !this.pm;
}
this.currentHour = (newHour <= 0) ? (12 + newHour) : newHour;
}
this.updateTime();
}
event.preventDefault();
};
Calendar.prototype.validateHour = function (hour) {
var valid = true;
var value = this.value;
if (this.isRangeSelection()) {
value = this.value[1] || this.value[0];
}
if (this.isMultipleSelection()) {
value = this.value[this.value.length - 1];
}
var valueDateString = value ? value.toDateString() : null;
if (this.minDate && valueDateString && this.minDate.toDateString() === valueDateString) {
if (this.minDate.getHours() > hour) {
valid = false;
}
}
if (this.maxDate && valueDateString && this.maxDate.toDateString() === valueDateString) {
if (this.maxDate.getHours() < hour) {
valid = false;
}
}
return valid;
};
Calendar.prototype.incrementMinute = function (event) {
var newMinute = this.currentMinute + this.stepMinute;
if (this.validateMinute(newMinute)) {
this.currentMinute = (newMinute > 59) ? newMinute - 60 : newMinute;
this.updateTime();
}
event.preventDefault();
};
Calendar.prototype.decrementMinute = function (event) {
var newMinute = this.currentMinute - this.stepMinute;
if (this.validateMinute(newMinute)) {
this.currentMinute = (newMinute < 0) ? 60 + newMinute : newMinute;
this.updateTime();
}
event.preventDefault();
};
Calendar.prototype.validateMinute = function (minute) {
var valid = true;
var value = this.value;
if (this.isRangeSelection()) {
value = this.value[1] || this.value[0];
}
if (this.isMultipleSelection()) {
value = this.value[this.value.length - 1];
}
var valueDateString = value ? value.toDateString() : null;
if (this.minDate && valueDateString && this.minDate.toDateString() === valueDateString) {
if (value.getHours() == this.minDate.getHours()) {
if (this.minDate.getMinutes() > minute) {
valid = false;
}
}
}
if (this.maxDate && valueDateString && this.maxDate.toDateString() === valueDateString) {
if (value.getHours() == this.maxDate.getHours()) {
if (this.maxDate.getMinutes() < minute) {
valid = false;
}
}
}
return valid;
};
Calendar.prototype.incrementSecond = function (event) {
var newSecond = this.currentSecond + this.stepSecond;
if (this.validateSecond(newSecond)) {
this.currentSecond = (newSecond > 59) ? newSecond - 60 : newSecond;
this.updateTime();
}
event.preventDefault();
};
Calendar.prototype.decrementSecond = function (event) {
var newSecond = this.currentSecond - this.stepSecond;
if (this.validateSecond(newSecond)) {
this.currentSecond = (newSecond < 0) ? 60 + newSecond : newSecond;
this.updateTime();
}
event.preventDefault();
};
Calendar.prototype.validateSecond = function (second) {
var valid = true;
var value = this.value;
if (this.isRangeSelection()) {
value = this.value[1] || this.value[0];
}
if (this.isMultipleSelection()) {
value = this.value[this.value.length - 1];
}
var valueDateString = value ? value.toDateString() : null;
if (this.minDate && valueDateString && this.minDate.toDateString() === valueDateString) {
if (this.minDate.getSeconds() > second) {
valid = false;
}
}
if (this.maxDate && valueDateString && this.maxDate.toDateString() === valueDateString) {
if (this.maxDate.getSeconds() < second) {
valid = false;
}
}
return valid;
};
Calendar.prototype.updateTime = function () {
var value = this.value;
if (this.isRangeSelection()) {
value = this.value[1] || this.value[0];
}
if (this.isMultipleSelection()) {
value = this.value[this.value.length - 1];
}
value = value ? new Date(value.getTime()) : new Date();
if (this.hourFormat == '12') {
if (this.currentHour === 12)
value.setHours(this.pm ? 12 : 0);
else
value.setHours(this.pm ? this.currentHour + 12 : this.currentHour);
}
else {
value.setHours(this.currentHour);
}
value.setMinutes(this.currentMinute);
value.setSeconds(this.currentSecond);
if (this.isRangeSelection()) {
if (this.value[1])
value = [this.value[0], value];
else
value = [value, null];
}
if (this.isMultipleSelection()) {
value = this.value.slice(0, -1).concat([value]);
}
this.updateModel(value);
this.onSelect.emit(value);
this.updateInputfield();
};
Calendar.prototype.toggleAMPM = function (event) {
this.pm = !this.pm;
this.updateTime();
event.preventDefault();
};
Calendar.prototype.onUserInput = function (event) {
// IE 11 Workaround for input placeholder : https://github.com/primefaces/primeng/issues/2026
if (!this.isKeydown) {
return;
}
this.isKeydown = false;
var val = event.target.value;
try {
var value = this.parseValueFromString(val);
if (this.isSelectable(value.getDate(), value.getMonth(), value.getFullYear(), false)) {
this.updateModel(value);
this.updateUI();
}
}
catch (err) {
//invalid date
this.updateModel(null);
}
this.filled = val != null && val.length;
this.onInput.emit(event);
};
Calendar.prototype.parseValueFromString = function (text) {
if (!text || text.trim().length === 0) {
return null;
}
var value;
if (this.isSingleSelection()) {
value = this.parseDateTime(text);
}
else if (this.isMultipleSelection()) {
var tokens = text.split(',');
value = [];
for (var _i = 0, tokens_1 = tokens; _i < tokens_1.length; _i++) {
var token = tokens_1[_i];
value.push(this.parseDateTime(token.trim()));
}
}
else if (this.isRangeSelection()) {
var tokens = text.split(' - ');
value = [];
for (var i = 0; i < tokens.length; i++) {
value[i] = this.parseDateTime(tokens[i].trim());
}
}
return value;
};
Calendar.prototype.parseDateTime = function (text) {
var date;
var parts = text.split(' ');
if (this.timeOnly) {
date = new Date();
this.populateTime(date, parts[0], parts[1]);
}
else {
if (this.showTime) {
date = this.parseDate(parts[0], this.dateFormat);
this.populateTime(date, parts[1], parts[2]);
}
else {
date = this.parseDate(text, this.dateFormat);
}
}
return date;
};
Calendar.prototype.populateTime = function (value, timeString, ampm) {
if (this.hourFormat == '12' && !ampm) {
throw 'Invalid Time';
}
this.pm = (ampm === 'PM' || ampm === 'pm');
var time = this.parseTime(timeString);
value.setHours(time.hour);
value.setMinutes(time.minute);
value.setSeconds(time.second);
};
Calendar.prototype.updateUI = function () {
var val = this.value || this.defaultDate || new Date();
if (Array.isArray(val)) {
val = val[0];
}
this.currentMonth = val.getMonth();
this.currentYear = val.getFullYear();
this.createMonths(this.currentMonth, this.currentYear);
if (this.showTime || this.timeOnly) {
var hours = val.getHours();
if (this.hourFormat == '12') {
this.pm = hours > 11;
if (hours >= 12) {
this.currentHour = (hours == 12) ? 12 : hours - 12;
}
else {
this.currentHour = (hours == 0) ? 12 : hours;
}
}
else {
this.currentHour = val.getHours();
}
this.currentMinute = val.getMinutes();
this.currentSecond = val.getSeconds();
}
};
Calendar.prototype.onDatePickerClick = function (event) {
this.datepickerClick = true;
};
Calendar.prototype.showOverlay = function () {
this.overlayVisible = true;
};
Calendar.prototype.onOverlayAnimationStart = function (event) {
switch (event.toState) {
case 'visible':
case 'visibleTouchUI':
if (!this.inline) {
this.overlay = event.element;
this.appendOverlay();
if (this.autoZIndex) {
this.overlay.style.zIndex = String(this.baseZIndex + (++domhandler_1.DomHandler.zindex));
}
this.alignOverlay();
this.bindDocumentClickListener();
}
break;
case 'void':
this.onOverlayHide();
break;
}
};
Calendar.prototype.appendOverlay = function () {
if (this.appendTo) {
if (this.appendTo === 'body')
document.body.appendChild(this.overlay);
else
this.domHandler.appendChild(this.overlay, this.appendTo);
}
};
Calendar.prototype.restoreOverlayAppend = function () {
if (this.overlay && this.appendTo) {
this.el.nativeElement.appendChild(this.overlay);
}
};
Calendar.prototype.alignOverlay = function () {
if (this.touchUI) {
this.enableModality(this.overlay);
}
else {
if (this.appendTo)
this.domHandler.absolutePosition(this.overlay, this.inputfieldViewChild.nativeElement);
else
this.domHandler.relativePosition(this.overlay, this.inputfieldViewChild.nativeElement);
}
};
Calendar.prototype.enableModality = function (element) {
var _this = this;
if (!this.mask) {
this.mask = document.createElement('div');
this.mask.style.zIndex = String(parseInt(element.style.zIndex) - 1);
var maskStyleClass = 'ui-widget-overlay ui-datepicker-mask ui-datepicker-mask-scrollblocker';
this.domHandler.addMultipleClasses(this.mask, maskStyleClass);
this.maskClickListener = this.renderer.listen(this.mask, 'click', function (event) {
_this.disableModality();
});
document.body.appendChild(this.mask);
this.domHandler.addClass(document.body, 'ui-overflow-hidden');
}
};
Calendar.prototype.disableModality = function () {
if (this.mask) {
document.body.removeChild(this.mask);
var bodyChildren = document.body.children;
var hasBlockerMasks = void 0;
for (var i = 0; i < bodyChildren.length; i++) {
var bodyChild = bodyChildren[i];
if (this.domHandler.hasClass(bodyChild, 'ui-datepicker-mask-scrollblocker')) {
hasBlockerMasks = true;
break;
}
}
if (!hasBlockerMasks) {
this.domHandler.removeClass(document.body, 'ui-overflow-hidden');
}
this.overlayVisible = false;
this.unbindMaskClickListener();
this.mask = null;
}
};
Calendar.prototype.unbindMaskClickListener = function () {
if (this.maskClickListener) {
this.maskClickListener();
this.maskClickListener = null;
}
};
Calendar.prototype.writeValue = function (value) {
this.value = value;
if (this.value && typeof this.value === 'string') {
this.value = this.parseValueFromString(this.value);
}
this.updateInputfield();
this.updateUI();
};
Calendar.prototype.registerOnChange = function (fn) {
this.onModelChange = fn;
};
Calendar.prototype.registerOnTouched = function (fn) {
this.onModelTouched = fn;
};
Calendar.prototype.setDisabledState = function (val) {
this.disabled = val;
};
// Ported from jquery-ui datepicker formatDate
Calendar.prototype.formatDate = function (date, format) {
if (!date) {
return '';
}
var iFormat;
var lookAhead = function (match) {
var matches = (iFormat + 1 < format.length && format.charAt(iFormat + 1) === match);
if (matches) {
iFormat++;
}
return matches;
}, formatNumber = function (match, value, len) {
var num = '' + value;
if (lookAhead(match)) {
while (num.length < len) {
num = '0' + num;
}
}
return num;
}, formatName = function (match, value, shortNames, longNames) {
return (lookAhead(match) ? longNames[value] : shortNames[value]);
};
var output = '';
var literal = false;
if (date) {
for (iFormat = 0; iFormat < format.length; iFormat++) {
if (literal) {
if (format.charAt(iFormat) === '\'' && !lookAhead('\'')) {
literal = false;
}
else {
output += format.charAt(iFormat);
}
}
else {
switch (format.charAt(iFormat)) {
case 'd':
output += formatNumber('d', date.getDate(), 2);
break;
case 'D':
output += formatName('D', date.getDay(), this.locale.dayNamesShort, this.locale.dayNames);
break;
case 'o':
output += formatNumber('o', Math.round((new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime() -
new Date(date.getFullYear(), 0, 0).getTime()) / 86400000), 3);
break;
case 'm':
output += formatNumber('m', date.getMonth() + 1, 2);
break;
case 'M':
output += formatName('M', date.getMonth(), this.locale.monthNamesShort, this.locale.monthNames);
break;
case 'y':
output += lookAhead('y') ? date.getFullYear() : (date.getFullYear() % 100 < 10 ? '0' : '') + (date.getFullYear() % 100);
break;
case '@':
output += date.getTime();
break;
case '!':
output += date.getTime() * 10000 + this.ticksTo1970;
break;
case '\'':
if (lookAhead('\'')) {
output += '\'';
}
else {
literal = true;
}
break;
default:
output += format.charAt(iFormat);
}
}
}
}
return output;
};
Calendar.prototype.formatTime = function (date) {
if (!date) {
return '';
}
var output = '';
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
if (this.hourFormat == '12' && hours > 11 && hours != 12) {
hours -= 12;
}
if (this.hourFormat == '12') {
output += hours === 0 ? 12 : (hours < 10) ? '0' + hours : hours;
}
else {
output += (hours < 10) ? '0' + hours : hours;
}
output += ':';
output += (minutes < 10) ? '0' + minutes : minutes;
if (this.showSeconds) {
output += ':';
output += (seconds < 10) ? '0' + seconds : seconds;
}
if (this.hourFormat == '12') {
output += date.getHours() > 11 ? ' PM' : ' AM';
}
return output;
};
Calendar.prototype.parseTime = function (value) {
var tokens = value.split(':');
var validTokenLength = this.showSeconds ? 3 : 2;
if (tokens.length !== validTokenLength) {
throw "Invalid time";
}
var h = parseInt(tokens[0]);
var m = parseInt(tokens[1]);
var s = this.showSeconds ? parseInt(tokens[2]) : null;
if (isNaN(h) || isNaN(m) || h > 23 || m > 59 || (this.hourFormat == '12' && h > 12) || (this.showSeconds && (isNaN(s) || s > 59))) {
throw "Invalid time";
}
else {
if (this.hourFormat == '12' && h !== 12 && this.pm) {
h += 12;
}
return { hour: h, minute: m, second: s };
}
};
// Ported from jquery-ui datepicker parseDate
Calendar.prototype.parseDate = function (value, format) {
if (format == null || value == null) {
throw "Invalid arguments";
}
value = (typeof value === "object" ? value.toString() : value + "");
if (value === "") {
return null;
}
var iFormat, dim, extra, iValue = 0, shortYearCutoff = (typeof this.shortYearCutoff !== "string" ? this.shortYearCutoff : new Date().getFullYear() % 100 + parseInt(this.shortYearCutoff, 10)), year = -1, month = -1, day = -1, doy = -1, literal = false, date, lookAhead = function (match) {
var matches = (iFormat + 1 < format.length && format.charAt(iFormat + 1) === match);
if (matches) {
iFormat++;
}
return matches;
}, getNumber = function (match) {
var isDoubled = lookAhead(match), size = (match === "@" ? 14 : (match === "!" ? 20 :
(match === "y" && isDoubled ? 4 : (match === "o" ? 3 : 2)))), minSize = (match === "y" ? size : 1), digits = new RegExp("^\\d{" + minSize + "," + size + "}"), num = value.substring(iValue).match(digits);
if (!num) {
throw "Missing number at position " + iValue;
}
iValue += num[0].length;
return parseInt(num[0], 10);
}, getName = function (match, shortNames, longNames) {
var index = -1;
var arr = lookAhead(match) ? longNames : shortNames;
var names = [];
for (var i = 0; i < arr.length; i++) {
names.push([i, arr[i]]);
}
names.sort(function (a, b) {
return -(a[1].length - b[1].length);
});
for (var i = 0; i < names.length; i++) {
var name_1 = names[i][1];
if (value.substr(iValue, name_1.length).toLowerCase() === name_1.toLowerCase()) {
index = names[i][0];
iValue += name_1.length;
break;
}
}
if (index !== -1) {
return index + 1;
}
else {
throw "Unknown name at position " + iValue;
}
}, checkLiteral = function () {
if (value.charA