ng-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
643 lines (637 loc) • 24 kB
JavaScript
import { CommonModule } from '@angular/common';
import { Directive, EventEmitter, Component, ViewEncapsulation, ChangeDetectionStrategy, Input, Output, forwardRef, ChangeDetectorRef, ContentChild, TemplateRef, HostBinding, NgModule } from '@angular/core';
import { NG_VALUE_ACCESSOR, FormsModule } from '@angular/forms';
import { NzI18nService, DateHelperService, NzI18nModule } from 'ng-zorro-antd/i18n';
import { NzRadioModule } from 'ng-zorro-antd/radio';
import { NzSelectModule } from 'ng-zorro-antd/select';
import setMonth from 'date-fns/set_month';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import addDays from 'date-fns/add_days';
import differenceInCalendarDays from 'date-fns/difference_in_calendar_days';
import differenceInCalendarMonths from 'date-fns/difference_in_calendar_months';
import differenceInCalendarWeeks from 'date-fns/difference_in_calendar_weeks';
import endOfMonth from 'date-fns/end_of_month';
import isSameDay from 'date-fns/is_same_day';
import isSameMonth from 'date-fns/is_same_month';
import isSameYear from 'date-fns/is_same_year';
import isThisMonth from 'date-fns/is_this_month';
import isThisYear from 'date-fns/is_this_year';
import setYear from 'date-fns/set_year';
import startOfMonth from 'date-fns/start_of_month';
import startOfWeek from 'date-fns/start_of_week';
import startOfYear from 'date-fns/start_of_year';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NzDateCellDirective {
}
NzDateCellDirective.decorators = [
{ type: Directive, args: [{
selector: '[nzDateCell]',
exportAs: 'nzDateCell'
},] }
];
class NzMonthCellDirective {
}
NzMonthCellDirective.decorators = [
{ type: Directive, args: [{
selector: '[nzMonthCell]',
exportAs: 'nzMonthCell'
},] }
];
class NzDateFullCellDirective {
}
NzDateFullCellDirective.decorators = [
{ type: Directive, args: [{
selector: '[nzDateFullCell]',
exportAs: 'nzDateFullCell'
},] }
];
class NzMonthFullCellDirective {
}
NzMonthFullCellDirective.decorators = [
{ type: Directive, args: [{
selector: '[nzMonthFullCell]',
exportAs: 'nzMonthFullCell'
},] }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NzCalendarHeaderComponent {
/**
* @param {?} i18n
* @param {?} dateHelper
*/
constructor(i18n, dateHelper) {
this.i18n = i18n;
this.dateHelper = dateHelper;
this.mode = 'month';
this.modeChange = new EventEmitter();
this.fullscreen = true;
this.yearChange = new EventEmitter();
this.monthChange = new EventEmitter();
this._activeDate = new Date();
this.yearOffset = 10;
this.yearTotal = 20;
}
/**
* @param {?} value
* @return {?}
*/
set activeDate(value) {
this._activeDate = value;
this.setUpYears();
}
/**
* @return {?}
*/
get activeDate() {
return this._activeDate;
}
/**
* @return {?}
*/
get activeYear() {
return this.activeDate.getFullYear();
}
/**
* @return {?}
*/
get activeMonth() {
return this.activeDate.getMonth();
}
/**
* @return {?}
*/
get size() {
return this.fullscreen ? 'default' : 'small';
}
/**
* @return {?}
*/
get yearTypeText() {
return this.i18n.getLocale().Calendar.year;
}
/**
* @return {?}
*/
get monthTypeText() {
return this.i18n.getLocale().Calendar.month;
}
/**
* @return {?}
*/
ngOnInit() {
this.setUpYears();
this.setUpMonths();
}
/**
* @param {?} year
* @return {?}
*/
updateYear(year) {
this.yearChange.emit(year);
this.setUpYears(year);
}
/**
* @private
* @param {?=} year
* @return {?}
*/
setUpYears(year) {
/** @type {?} */
const start = (year || this.activeYear) - this.yearOffset;
/** @type {?} */
const end = start + this.yearTotal;
this.years = [];
for (let i = start; i < end; i++) {
this.years.push({ label: `${i}`, value: i });
}
}
/**
* @private
* @return {?}
*/
setUpMonths() {
this.months = [];
for (let i = 0; i < 12; i++) {
/** @type {?} */
const dateInMonth = setMonth(this.activeDate, i);
/** @type {?} */
const monthText = this.dateHelper.format(dateInMonth, 'MMM');
this.months.push({ label: monthText, value: i });
}
}
}
NzCalendarHeaderComponent.decorators = [
{ type: Component, args: [{
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'nz-calendar-header',
exportAs: 'nzCalendarHeader',
template: "<nz-select class=\"ant-fullcalendar-year-select\" [nzSize]=\"size\" [nzDropdownMatchSelectWidth]=\"false\"\n [ngModel]=\"activeYear\" (ngModelChange)=\"updateYear($event)\">\n <nz-option *ngFor=\"let year of years\" [nzLabel]=\"year.label\" [nzValue]=\"year.value\"></nz-option>\n</nz-select>\n\n<nz-select *ngIf=\"mode === 'month'\" class=\"ant-fullcalendar-month-select\" [nzSize]=\"size\" [nzDropdownMatchSelectWidth]=\"false\"\n [ngModel]=\"activeMonth\" (ngModelChange)=\"monthChange.emit($event)\">\n <nz-option *ngFor=\"let month of months\" [nzLabel]=\"month.label\" [nzValue]=\"month.value\"></nz-option>\n</nz-select>\n\n<nz-radio-group [(ngModel)]=\"mode\" (ngModelChange)=\"modeChange.emit($event)\" [nzSize]=\"size\">\n <label nz-radio-button nzValue=\"month\">{{ monthTypeText }}</label>\n <label nz-radio-button nzValue=\"year\">{{ yearTypeText }}</label>\n</nz-radio-group>\n",
host: {
'[style.display]': `'block'`,
'[class.ant-fullcalendar-header]': `true`
}
}] }
];
/** @nocollapse */
NzCalendarHeaderComponent.ctorParameters = () => [
{ type: NzI18nService },
{ type: DateHelperService }
];
NzCalendarHeaderComponent.propDecorators = {
mode: [{ type: Input }],
modeChange: [{ type: Output }],
fullscreen: [{ type: Input }],
activeDate: [{ type: Input }],
yearChange: [{ type: Output }],
monthChange: [{ type: Output }]
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NzCalendarComponent {
/**
* @param {?} i18n
* @param {?} cdr
* @param {?} dateHelper
*/
constructor(i18n, cdr, dateHelper) {
this.i18n = i18n;
this.cdr = cdr;
this.dateHelper = dateHelper;
this.nzMode = 'month';
this.nzModeChange = new EventEmitter();
this.nzPanelChange = new EventEmitter();
this.nzSelectChange = new EventEmitter();
this.nzValueChange = new EventEmitter();
this.fullscreen = true;
this.daysInWeek = [];
this.monthsInYear = [];
this.dateMatrix = [];
this.activeDate = new Date();
this.currentDateRow = -1;
this.currentDateCol = -1;
this.activeDateRow = -1;
this.activeDateCol = -1;
this.currentMonthRow = -1;
this.currentMonthCol = -1;
this.activeMonthRow = -1;
this.activeMonthCol = -1;
this.dateCell = null;
this.dateFullCell = null;
this.monthCell = null;
this.monthFullCell = null;
this.currentDate = new Date();
this.onChangeFn = (/**
* @return {?}
*/
() => { });
this.onTouchFn = (/**
* @return {?}
*/
() => { });
}
/**
* @param {?} value
* @return {?}
*/
set nzValue(value) {
this.updateDate(value, false);
}
/**
* @param {?} value
* @return {?}
*/
set nzDateCell(value) {
this.dateCell = value;
}
/**
* @param {?} value
* @return {?}
*/
set nzDateFullCell(value) {
this.dateFullCell = value;
}
/**
* @param {?} value
* @return {?}
*/
set nzMonthCell(value) {
this.monthCell = value;
}
/**
* @param {?} value
* @return {?}
*/
set nzMonthFullCell(value) {
this.monthFullCell = value;
}
/**
* @param {?} value
* @return {?}
*/
set nzFullscreen(value) {
this.fullscreen = coerceBooleanProperty(value);
}
/**
* @return {?}
*/
get nzFullscreen() {
return this.fullscreen;
}
/**
* @param {?} value
* @return {?}
*/
set nzCard(value) {
this.fullscreen = !coerceBooleanProperty(value);
}
/**
* @return {?}
*/
get nzCard() {
return !this.fullscreen;
}
/**
* @param {?} value
* @return {?}
*/
set dateCellChild(value) {
if (value) {
this.dateCell = value;
}
}
/**
* @param {?} value
* @return {?}
*/
set dateFullCellChild(value) {
if (value) {
this.dateFullCell = value;
}
}
/**
* @param {?} value
* @return {?}
*/
set monthCellChild(value) {
if (value) {
this.monthCell = value;
}
}
/**
* @param {?} value
* @return {?}
*/
set monthFullCellChild(value) {
if (value) {
this.monthFullCell = value;
}
}
/**
* @private
* @return {?}
*/
get calendarStart() {
return startOfWeek(startOfMonth(this.activeDate), { weekStartsOn: this.dateHelper.getFirstDayOfWeek() });
}
/**
* @return {?}
*/
ngOnInit() {
this.setUpDaysInWeek();
this.setUpMonthsInYear();
this.setUpDateMatrix();
this.calculateCurrentDate();
this.calculateActiveDate();
this.calculateCurrentMonth();
this.calculateActiveMonth();
}
/**
* @param {?} mode
* @return {?}
*/
onModeChange(mode) {
this.nzModeChange.emit(mode);
this.nzPanelChange.emit({ date: this.activeDate, mode });
}
/**
* @param {?} date
* @return {?}
*/
onDateSelect(date) {
this.updateDate(date);
this.nzSelectChange.emit(date);
}
/**
* @param {?} year
* @return {?}
*/
onYearSelect(year) {
/** @type {?} */
const date = setYear(this.activeDate, year);
this.updateDate(date);
this.nzSelectChange.emit(date);
}
/**
* @param {?} month
* @return {?}
*/
onMonthSelect(month) {
/** @type {?} */
const date = setMonth(this.activeDate, month);
this.updateDate(date);
this.nzSelectChange.emit(date);
}
/**
* @param {?} value
* @return {?}
*/
writeValue(value) {
this.updateDate(value || new Date(), false);
this.cdr.markForCheck();
}
/**
* @param {?} fn
* @return {?}
*/
registerOnChange(fn) {
this.onChangeFn = fn;
}
/**
* @param {?} fn
* @return {?}
*/
registerOnTouched(fn) {
this.onTouchFn = fn;
}
/**
* @private
* @param {?} date
* @param {?=} touched
* @return {?}
*/
updateDate(date, touched = true) {
/** @type {?} */
const dayChanged = !isSameDay(date, this.activeDate);
/** @type {?} */
const monthChanged = !isSameMonth(date, this.activeDate);
/** @type {?} */
const yearChanged = !isSameYear(date, this.activeDate);
this.activeDate = date;
if (dayChanged) {
this.calculateActiveDate();
}
if (monthChanged) {
this.setUpDateMatrix();
this.calculateCurrentDate();
this.calculateActiveMonth();
}
if (yearChanged) {
this.calculateCurrentMonth();
}
if (touched) {
this.onChangeFn(date);
this.onTouchFn();
this.nzValueChange.emit(date);
}
}
/**
* @private
* @return {?}
*/
setUpDaysInWeek() {
this.daysInWeek = [];
/** @type {?} */
const weekStart = startOfWeek(this.activeDate, { weekStartsOn: this.dateHelper.getFirstDayOfWeek() });
for (let i = 0; i < 7; i++) {
/** @type {?} */
const date = addDays(weekStart, i);
/** @type {?} */
const title = this.dateHelper.format(date, this.dateHelper.relyOnDatePipe ? 'E' : 'ddd');
/** @type {?} */
const label = this.dateHelper.format(date, this.dateHelper.relyOnDatePipe ? 'EEEEEE' : 'dd');
this.daysInWeek.push({ title, label });
}
}
/**
* @private
* @return {?}
*/
setUpMonthsInYear() {
this.monthsInYear = [];
for (let i = 0; i < 12; i++) {
/** @type {?} */
const date = setMonth(this.activeDate, i);
/** @type {?} */
const title = this.dateHelper.format(date, 'MMM');
/** @type {?} */
const label = this.dateHelper.format(date, 'MMM');
/** @type {?} */
const start = startOfMonth(date);
this.monthsInYear.push({ title, label, start });
}
}
/**
* @private
* @return {?}
*/
setUpDateMatrix() {
this.dateMatrix = [];
/** @type {?} */
const monthStart = startOfMonth(this.activeDate);
/** @type {?} */
const monthEnd = endOfMonth(this.activeDate);
/** @type {?} */
const weekDiff = differenceInCalendarWeeks(monthEnd, monthStart, { weekStartsOn: this.dateHelper.getFirstDayOfWeek() }) + 2;
for (let week = 0; week < weekDiff; week++) {
/** @type {?} */
const row = [];
/** @type {?} */
const weekStart = addDays(this.calendarStart, week * 7);
for (let day = 0; day < 7; day++) {
/** @type {?} */
const date = addDays(weekStart, day);
/** @type {?} */
const monthDiff = differenceInCalendarMonths(date, this.activeDate);
/** @type {?} */
const dateFormat = this.dateHelper.relyOnDatePipe
? 'longDate'
: this.i18n.getLocaleData('DatePicker.lang.dateFormat', 'YYYY-MM-DD');
/** @type {?} */
const title = this.dateHelper.format(date, dateFormat);
/** @type {?} */
const label = this.dateHelper.format(date, this.dateHelper.relyOnDatePipe ? 'dd' : 'DD');
/** @type {?} */
const rel = monthDiff === 0 ? 'current' : monthDiff < 0 ? 'last' : 'next';
row.push({ title, label, rel, value: date });
}
this.dateMatrix.push(row);
}
}
/**
* @private
* @return {?}
*/
calculateCurrentDate() {
if (isThisMonth(this.activeDate)) {
this.currentDateRow = differenceInCalendarWeeks(this.currentDate, this.calendarStart, {
weekStartsOn: this.dateHelper.getFirstDayOfWeek()
});
this.currentDateCol = differenceInCalendarDays(this.currentDate, addDays(this.calendarStart, this.currentDateRow * 7));
}
else {
this.currentDateRow = -1;
this.currentDateCol = -1;
}
}
/**
* @private
* @return {?}
*/
calculateActiveDate() {
this.activeDateRow = differenceInCalendarWeeks(this.activeDate, this.calendarStart, {
weekStartsOn: this.dateHelper.getFirstDayOfWeek()
});
this.activeDateCol = differenceInCalendarDays(this.activeDate, addDays(this.calendarStart, this.activeDateRow * 7));
}
/**
* @private
* @return {?}
*/
calculateCurrentMonth() {
if (isThisYear(this.activeDate)) {
/** @type {?} */
const yearStart = startOfYear(this.currentDate);
/** @type {?} */
const monthDiff = differenceInCalendarMonths(this.currentDate, yearStart);
this.currentMonthRow = Math.floor(monthDiff / 3);
this.currentMonthCol = monthDiff % 3;
}
else {
this.currentMonthRow = -1;
this.currentMonthCol = -1;
}
}
/**
* @private
* @return {?}
*/
calculateActiveMonth() {
this.activeMonthRow = Math.floor(this.activeDate.getMonth() / 3);
this.activeMonthCol = this.activeDate.getMonth() % 3;
}
}
NzCalendarComponent.decorators = [
{ type: Component, args: [{
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'nz-calendar',
exportAs: 'nzCalendar',
template: "<nz-calendar-header [fullscreen]=\"fullscreen\" [activeDate]=\"activeDate\"\n [(mode)]=\"nzMode\" (modeChange)=\"onModeChange($event)\"\n (yearChange)=\"onYearSelect($event)\" (monthChange)=\"onMonthSelect($event)\">\n</nz-calendar-header>\n\n<div class=\"ant-fullcalendar ant-fullcalendar-full\" [ngClass]=\"fullscreen ? 'ant-fullcalendar-fullscreen' : ''\">\n <div class=\"ant-fullcalendar-calendar-body\">\n <ng-container *ngIf=\"nzMode === 'month' then monthModeTable else yearModeTable\"></ng-container>\n </div>\n</div>\n\n<ng-template #monthModeTable>\n <table class=\"ant-fullcalendar-table\" cellspacing=\"0\" role=\"grid\">\n <thead>\n <tr role=\"row\">\n <th *ngFor=\"let day of daysInWeek\" class=\"ant-fullcalendar-column-header\" role=\"columnheader\" [title]=\"day.title\">\n <span class=\"ant-fullcalendar-column-header-inner\">{{ day.label }}</span>\n </th>\n </tr>\n </thead>\n <tbody class=\"ant-fullcalendar-tbody\">\n <tr *ngFor=\"let week of dateMatrix; index as row\"\n [class.ant-fullcalendar-current-week]=\"row === currentDateRow\"\n [class.ant-fullcalendar-active-week]=\"row === activeDateRow\">\n <td *ngFor=\"let day of week; index as col\" role=\"gridcell\" class=\"ant-fullcalendar-cell\" [title]=\"day.title\"\n [class.ant-fullcalendar-today]=\"row === currentDateRow && col === currentDateCol\"\n [class.ant-fullcalendar-selected-day]=\"row === activeDateRow && col === activeDateCol\"\n [class.ant-fullcalendar-last-month-cell]=\"day.rel === 'last'\"\n [class.ant-fullcalendar-next-month-btn-day]=\"day.rel === 'next'\"\n (click)=\"onDateSelect(day.value)\">\n <div class=\"ant-fullcalendar-date\">\n <ng-container *ngIf=\"dateFullCell else defaultCell\">\n <ng-container *ngTemplateOutlet=\"dateFullCell; context: {$implicit: day.value}\"></ng-container>\n </ng-container>\n <ng-template #defaultCell>\n <div class=\"ant-fullcalendar-value\">{{ day.label }}</div>\n <div *ngIf=\"dateCell\" class=\"ant-fullcalendar-content\">\n <ng-container *ngTemplateOutlet=\"dateCell; context: {$implicit: day.value}\"></ng-container>\n </div>\n </ng-template>\n </div>\n </td>\n </tr>\n </tbody>\n </table>\n</ng-template>\n\n<ng-template #yearModeTable>\n <table class=\"ant-fullcalendar-month-panel-table\" cellspacing=\"0\" role=\"grid\">\n <tbody class=\"ant-fullcalendar-month-panel-tbody\">\n <tr *ngFor=\"let row of [0, 1, 2, 3]\" role=\"row\">\n <td *ngFor=\"let col of [0, 1, 2]\" role=\"gridcell\" [title]=\"monthsInYear[row * 3 + col].title\"\n class=\"ant-fullcalendar-month-panel-cell\"\n [class.ant-fullcalendar-month-panel-current-cell]=\"row === currentMonthRow && col === currentMonthCol\"\n [class.ant-fullcalendar-month-panel-selected-cell]=\"row === activeMonthRow && col === activeMonthCol\"\n (click)=\"onMonthSelect(row * 3 + col)\">\n <div class=\"ant-fullcalendar-month\">\n <ng-container *ngIf=\"monthFullCell else defaultCell\">\n <ng-container *ngTemplateOutlet=\"monthFullCell; context: {$implicit: monthsInYear[row * 3 + col].start}\"></ng-container>\n </ng-container>\n <ng-template #defaultCell>\n <div class=\"ant-fullcalendar-value\">{{ monthsInYear[row * 3 + col].label }}</div>\n <div *ngIf=\"monthCell\" class=\"ant-fullcalendar-content\">\n <ng-container *ngTemplateOutlet=\"monthCell; context: {$implicit: monthsInYear[row * 3 + col].start}\"></ng-container>\n </div>\n </ng-template>\n </div>\n </td>\n </tr>\n </tbody>\n </table>\n</ng-template>\n",
providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef((/**
* @return {?}
*/
() => NzCalendarComponent)), multi: true }]
}] }
];
/** @nocollapse */
NzCalendarComponent.ctorParameters = () => [
{ type: NzI18nService },
{ type: ChangeDetectorRef },
{ type: DateHelperService }
];
NzCalendarComponent.propDecorators = {
nzMode: [{ type: Input }],
nzModeChange: [{ type: Output }],
nzPanelChange: [{ type: Output }],
nzSelectChange: [{ type: Output }],
nzValue: [{ type: Input }],
nzValueChange: [{ type: Output }],
nzDateCell: [{ type: Input }],
nzDateFullCell: [{ type: Input }],
nzMonthCell: [{ type: Input }],
nzMonthFullCell: [{ type: Input }],
nzFullscreen: [{ type: Input }],
nzCard: [{ type: Input }],
dateCellChild: [{ type: ContentChild, args: [NzDateCellDirective, { static: false, read: TemplateRef },] }],
dateFullCellChild: [{ type: ContentChild, args: [NzDateFullCellDirective, { static: false, read: TemplateRef },] }],
monthCellChild: [{ type: ContentChild, args: [NzMonthCellDirective, { static: false, read: TemplateRef },] }],
monthFullCellChild: [{ type: ContentChild, args: [NzMonthFullCellDirective, { static: false, read: TemplateRef },] }],
fullscreen: [{ type: HostBinding, args: ['class.ant-fullcalendar--fullscreen',] }]
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NzCalendarModule {
}
NzCalendarModule.decorators = [
{ type: NgModule, args: [{
declarations: [
NzCalendarHeaderComponent,
NzCalendarComponent,
NzDateCellDirective,
NzDateFullCellDirective,
NzMonthCellDirective,
NzMonthFullCellDirective
],
exports: [
NzCalendarComponent,
NzDateCellDirective,
NzDateFullCellDirective,
NzMonthCellDirective,
NzMonthFullCellDirective
],
imports: [CommonModule, FormsModule, NzI18nModule, NzRadioModule, NzSelectModule]
},] }
];
export { NzCalendarComponent, NzCalendarHeaderComponent, NzCalendarModule, NzDateCellDirective, NzDateFullCellDirective, NzMonthCellDirective, NzMonthFullCellDirective };
//# sourceMappingURL=ng-zorro-antd-calendar.js.map