/**
* @license
* Copyright Akveo. All Rights Reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
import {
ChangeDetectionStrategy,
Component,
EventEmitter,
HostBinding,
HostListener,
Input,
Output,
} from '@angular/core';
import { NbCalendarCell, NbCalendarSize, NbCalendarSizeValues } from '../calendar-kit/model';
import { NbDateService } from '../calendar-kit/services/date.service';
import { NbCalendarRange } from './calendar-range.component';
import { NbBaseCalendarRangeCell } from './base-calendar-range-cell';
@Component({
selector: 'nb-calendar-range-month-cell',
template: `
{{ month }}
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NbCalendarRangeMonthCellComponent extends NbBaseCalendarRangeCell
implements NbCalendarCell> {
get month(): string {
return this.dateService.getMonthName(this.date);
}
@Input() date: D;
@Input() visibleDate: D;
@Input() selectedValue: NbCalendarRange;
@Input() min: D;
@Input() max: D;
@Input() size: NbCalendarSize = NbCalendarSize.MEDIUM;
static ngAcceptInputType_size: NbCalendarSizeValues;
@Output() select: EventEmitter = new EventEmitter(true);
@HostBinding('class.month-cell')
monthCellClass = true;
@HostBinding('class.range-cell')
rangeCellClass = true;
@HostBinding('class.selected')
get selected(): boolean {
if (this.inRange) {
return true;
}
if (this.selectedValue) {
return this.dateService.isSameMonthSafe(this.date, this.selectedValue.start);
}
}
@HostBinding('class.in-range')
get inRange(): boolean {
if (this.hasRange) {
return this.isInRage(this.date, this.selectedValue);
}
}
@HostBinding('class.start')
get rangeStart(): boolean {
if (this.hasRange) {
return this.dateService.isSameMonth(this.date, this.selectedValue.start);
}
}
@HostBinding('class.end')
get rangeEnd(): boolean {
if (this.hasRange) {
return this.dateService.isSameMonth(this.date, this.selectedValue.end);
}
}
@HostBinding('class.today')
get today(): boolean {
return this.dateService.isSameMonthSafe(this.date, this.dateService.today());
}
@HostBinding('class.disabled')
get disabled(): boolean {
return this.smallerThanMin() || this.greaterThanMax();
}
@HostBinding('class.size-large')
get isLarge(): boolean {
return this.size === NbCalendarSize.LARGE;
}
@HostListener('click')
onClick() {
if (this.disabled) {
return;
}
this.select.emit(this.date);
}
constructor(protected dateService: NbDateService) {
super();
}
protected smallerThanMin(): boolean {
return this.date && this.min && this.dateService.compareDates(this.monthEnd(), this.min) < 0;
}
protected greaterThanMax(): boolean {
return this.date && this.max && this.dateService.compareDates(this.monthStart(), this.max) > 0;
}
protected monthStart(): D {
return this.dateService.getMonthStart(this.date);
}
protected monthEnd(): D {
return this.dateService.getMonthEnd(this.date);
}
protected isInRage(date: D, range: NbCalendarRange): boolean {
if (range.start && range.end) {
const cellDate = this.dateService.getMonthStart(date);
const start = this.dateService.getMonthStart(range.start);
const end = this.dateService.getMonthStart(range.end);
const isGreaterThanStart = this.dateService.compareDates(cellDate, start) >= 0;
const isLessThanEnd = this.dateService.compareDates(cellDate, end) <= 0;
return isGreaterThanStart && isLessThanEnd;
}
return this.dateService.isSameMonth(date, range.start);
}
}