@rifansi/angular-datetime-picker
Version:
Angular Date Time Picker
193 lines (166 loc) • 4.74 kB
text/typescript
/**
* calendar-body.component
*/
import {
ChangeDetectionStrategy,
Component,
ElementRef,
EventEmitter,
Input,
NgZone,
OnInit,
Output
} from '@angular/core';
import { SelectMode } from './date-time.class';
import { take } from 'rxjs/operators';
import { CalendarAgenda } from './calendar-agenda.class';
export class CalendarCell {
constructor(
public value: number,
public displayValue: string,
public ariaLabel: string,
public enabled: boolean,
public out: boolean = false,
public cellClass: string = '',
public agendas: CalendarAgenda[] = []
) {}
}
export class OwlCalendarBodyComponent implements OnInit {
/**
* The cell number of the active cell in the table.
*/
activeCell = 0;
/**
* The cells to display in the table.
* */
rows: CalendarCell[][];
/**
* The number of columns in the table.
* */
numCols = 7;
/**
* The ratio (width / height) to use for the cells in the table.
*/
cellRatio = 1;
/**
* The value in the table that corresponds to today.
* */
todayValue: number;
/**
* The value in the table that is currently selected.
* */
selectedValues: number[];
/**
* Current picker select mode
*/
selectMode: SelectMode;
/**
* Emit when a calendar cell is selected
* */
public readonly select = new EventEmitter<CalendarCell>();
get owlDTCalendarBodyClass(): boolean {
return true;
}
get isInSingleMode(): boolean {
return this.selectMode === 'single';
}
get isInRangeMode(): boolean {
return (
this.selectMode === 'range' ||
this.selectMode === 'rangeFrom' ||
this.selectMode === 'rangeTo'
);
}
constructor(private elmRef: ElementRef, private ngZone: NgZone) {}
public ngOnInit() {}
public selectCell(cell: CalendarCell): void {
this.select.emit(cell);
}
public isActiveCell(rowIndex: number, colIndex: number): boolean {
const cellNumber = rowIndex * this.numCols + colIndex;
return cellNumber === this.activeCell;
}
/**
* Check if the cell is selected
*/
public isSelected(value: number): boolean {
if (!this.selectedValues || this.selectedValues.length === 0) {
return false;
}
if (this.isInSingleMode) {
return value === this.selectedValues[0];
}
if (this.isInRangeMode) {
const fromValue = this.selectedValues[0];
const toValue = this.selectedValues[1];
return value === fromValue || value === toValue;
}
}
/**
* Check if the cell in the range
* */
public isInRange(value: number): boolean {
if (this.isInRangeMode) {
const fromValue = this.selectedValues[0];
const toValue = this.selectedValues[1];
if (fromValue !== null && toValue !== null) {
return value >= fromValue && value <= toValue;
} else {
return value === fromValue || value === toValue;
}
}
}
/**
* Check if the cell is the range from
* */
public isRangeFrom(value: number): boolean {
if (this.isInRangeMode) {
const fromValue = this.selectedValues[0];
return fromValue !== null && value === fromValue;
}
}
/**
* Check if the cell is the range to
* */
public isRangeTo(value: number): boolean {
if (this.isInRangeMode) {
const toValue = this.selectedValues[1];
return toValue !== null && value === toValue;
}
}
/**
* Focus to a active cell
* */
public focusActiveCell(): void {
this.ngZone.runOutsideAngular(() => {
this.ngZone.onStable
.asObservable()
.pipe(take(1))
.subscribe(() => {
this.elmRef.nativeElement
.querySelector('.owl-dt-calendar-cell-active')
.focus();
});
});
}
}