70 lines
2.6 KiB
TypeScript
70 lines
2.6 KiB
TypeScript
import { DateTime, Settings } from "luxon";
|
|
import { DescriptorSet } from "./descriptors";
|
|
|
|
export const defaultAvailable = [{ start_day_sec: 8 * 3600, duration_sec: 8 * 3600, onSaturday: false, onSunOrHoliday: false }];
|
|
|
|
|
|
export abstract class GanttViewType {
|
|
start: DateTime; // start date of this gantt view
|
|
viewDuration: number; // duration in seconds between start and end of this gantt view
|
|
cellWidth: number; // width of a single cell (e.g. a day in the week view)
|
|
|
|
pixelPerSecond: number = 0; // width in pixel of one second of the gantt view
|
|
globalBaseFontSize: number = 16; // font size that is used to calculate tick display
|
|
|
|
constructor() {
|
|
// defining time settings
|
|
Settings.defaultZone = 'utc';
|
|
|
|
// default view is week
|
|
let date: DateTime = DateTime.now();
|
|
|
|
this.start = date.startOf('week').startOf('day');
|
|
this.viewDuration = this.start.endOf('week').endOf('day').diff(this.start).as('seconds');
|
|
this.cellWidth = this.viewDuration / 7;
|
|
}
|
|
|
|
// update view (e.g. resizing, interval change)
|
|
update(opts?: { width?: number, action?: 'next' | 'back', today?: boolean }): DescriptorSet {
|
|
// was panel resized?
|
|
if (opts?.width)
|
|
this.pixelPerSecond = opts.width / this.viewDuration;
|
|
// did the interval change?
|
|
if (opts?.action)
|
|
this.calculateIntervalShift(opts.action)
|
|
|
|
// recreate descriptors
|
|
return this.createDescriptorSet();
|
|
}
|
|
|
|
// convert duration to width in pixels
|
|
durationToWidth(seconds: number, opts?: { offsetCorrection?: number }): string {
|
|
let correction = opts?.offsetCorrection ?? 0;
|
|
return `${this.pixelPerSecond * seconds + correction}px`;
|
|
}
|
|
|
|
// get offset in pixels
|
|
getOffset(date: DateTime | number, opts?: { dstCorrection?: boolean, offsetCorrection?: number }): string {
|
|
// generate date from timestamp if necessary
|
|
if (typeof date === 'number')
|
|
date = DateTime.fromSeconds(date);
|
|
// calculate offset
|
|
let offset = date.diff(this.start).as('seconds') * this.pixelPerSecond;
|
|
let correction = opts?.offsetCorrection ?? 0;
|
|
|
|
return `${offset + correction}px`;
|
|
}
|
|
|
|
|
|
isDaySunOrHoliday(date: DateTime): boolean {
|
|
return date.weekday === 7
|
|
}
|
|
isDayOnWeekend(date: DateTime): boolean {
|
|
return date.weekday > 5;
|
|
}
|
|
|
|
createDescriptorSet(): DescriptorSet { return { interval: [], cell: [], tick: [] } };
|
|
calculateIntervalShift(action: 'next' | 'back'): void { }
|
|
setToday(): void { }
|
|
|
|
} |