2019-10-07 15:40:45 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
/**
|
2020-02-14 15:35:10 +00:00
|
|
|
* Options is a set of options used for VDatePicker.vue.
|
2019-10-07 15:40:45 +01:00
|
|
|
*/
|
|
|
|
export class Options {
|
|
|
|
public constructor(
|
|
|
|
public mondayFirstWeek: string[] = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'],
|
|
|
|
public sundayFirstWeek: string[] = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
|
|
|
|
public month: string[] = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
|
|
|
|
public color = {
|
|
|
|
checked: '#2683FF',
|
|
|
|
header: '#2683FF',
|
|
|
|
headerText: '#444C63',
|
|
|
|
},
|
|
|
|
public inputStyle = {
|
|
|
|
'visibility': 'hidden',
|
|
|
|
'width': '0',
|
|
|
|
},
|
|
|
|
public overlayOpacity: number = 0.5,
|
|
|
|
public dismissible: boolean = true,
|
|
|
|
) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-14 15:35:10 +00:00
|
|
|
* DayItem is used to store information about day cell in calendar.
|
2019-10-07 15:40:45 +01:00
|
|
|
*/
|
|
|
|
export class DayItem {
|
|
|
|
public constructor(
|
|
|
|
public value: number,
|
|
|
|
public inMonth: boolean,
|
|
|
|
public unavailable: boolean,
|
|
|
|
public checked: boolean,
|
|
|
|
public moment: Date,
|
|
|
|
public action: DayAction = DayAction.Default,
|
|
|
|
public today: boolean = false,
|
|
|
|
) {}
|
|
|
|
|
|
|
|
public equals(dateToCompare: Date): boolean {
|
|
|
|
const isDayEqual = this.moment.getDate() === dateToCompare.getDate();
|
|
|
|
const isMonthEqual = this.moment.getMonth() === dateToCompare.getMonth();
|
|
|
|
const isYearEqual = this.moment.getFullYear() === dateToCompare.getFullYear();
|
|
|
|
|
|
|
|
return isDayEqual && isMonthEqual && isYearEqual;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-14 15:35:10 +00:00
|
|
|
* DayAction is enum represents month change direction on day click.
|
2019-10-07 15:40:45 +01:00
|
|
|
*/
|
|
|
|
export enum DayAction {
|
|
|
|
Next,
|
|
|
|
Previous,
|
|
|
|
Default,
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-14 15:35:10 +00:00
|
|
|
* DateStamp is cozy representation of Date for view.
|
2019-10-07 15:40:45 +01:00
|
|
|
*/
|
|
|
|
export class DateStamp {
|
|
|
|
public constructor(
|
|
|
|
public year: number,
|
|
|
|
public month: number,
|
|
|
|
public day: number,
|
|
|
|
) {}
|
|
|
|
|
|
|
|
public fromDate(date: Date): void {
|
|
|
|
this.year = date.getFullYear();
|
|
|
|
this.month = date.getMonth();
|
|
|
|
this.day = date.getDate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-14 15:35:10 +00:00
|
|
|
* DisplayedType is enum represents view type to show in calendar to check.
|
2019-10-07 15:40:45 +01:00
|
|
|
*/
|
|
|
|
export enum DisplayedType {
|
|
|
|
Day,
|
|
|
|
Month,
|
|
|
|
Year,
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-14 15:35:10 +00:00
|
|
|
* DateGenerator is utility class used for generating DayItem and year lists for calendar.
|
2019-10-07 15:40:45 +01:00
|
|
|
*/
|
|
|
|
export class DateGenerator {
|
|
|
|
private current: DateStamp;
|
|
|
|
private isSundayFirst: boolean;
|
2020-01-02 14:17:57 +00:00
|
|
|
private now = new Date();
|
2019-10-07 15:40:45 +01:00
|
|
|
|
|
|
|
public populateDays(current: DateStamp, isSundayFirst: boolean): DayItem[] {
|
|
|
|
this.current = current;
|
|
|
|
this.isSundayFirst = isSundayFirst;
|
|
|
|
|
|
|
|
const days: DayItem[] = [];
|
|
|
|
|
|
|
|
this.populateSelectedMonthDays(days);
|
|
|
|
this.populatePreviousMonthDays(days);
|
|
|
|
this.populateNextMonthDays(days);
|
|
|
|
this.markToday(days);
|
|
|
|
|
|
|
|
return days;
|
|
|
|
}
|
|
|
|
|
|
|
|
public populateYears(): number[] {
|
|
|
|
const year = new Date().getFullYear();
|
|
|
|
const years: number[] = [];
|
|
|
|
for (let i = year - 99; i <= year; i++) {
|
|
|
|
years.unshift(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return years;
|
|
|
|
}
|
|
|
|
|
|
|
|
private populateSelectedMonthDays(days: DayItem[]): void {
|
|
|
|
const daysInSelectedMonth = new Date(this.current.year, this.current.month + 1, 0).getDate();
|
2020-01-02 14:17:57 +00:00
|
|
|
const currentMonth = this.now.getMonth();
|
2019-10-07 15:40:45 +01:00
|
|
|
|
|
|
|
for (let i = 1; i <= daysInSelectedMonth; i++) {
|
2020-01-02 14:17:57 +00:00
|
|
|
const moment = new Date(this.current.year, this.current.month, this.current.day, 23, 59);
|
2019-10-07 15:40:45 +01:00
|
|
|
moment.setDate(i);
|
|
|
|
|
|
|
|
days.push(
|
|
|
|
new DayItem(
|
|
|
|
i,
|
2020-01-02 14:17:57 +00:00
|
|
|
this.current.month !== currentMonth || (this.current.month === currentMonth && i <= this.now.getDate()),
|
2019-10-07 15:40:45 +01:00
|
|
|
false,
|
|
|
|
false,
|
|
|
|
moment,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private populatePreviousMonthDays(days: DayItem[]): void {
|
|
|
|
const previousMonth = new Date(this.current.year, this.current.month, this.current.day);
|
|
|
|
previousMonth.setMonth(previousMonth.getMonth() - 1);
|
|
|
|
|
|
|
|
const firstDate = new Date(this.current.year, this.current.month, this.current.day);
|
|
|
|
firstDate.setDate(1);
|
|
|
|
let firstDay = firstDate.getDay();
|
|
|
|
|
|
|
|
if (firstDay === 0) firstDay = 7;
|
|
|
|
const daysInPreviousMonth = new Date(previousMonth.getFullYear(), previousMonth.getMonth() + 1, 0).getDate();
|
|
|
|
|
|
|
|
for (let i = 0; i < firstDay - (this.isSundayFirst ? 0 : 1); i++) {
|
2020-01-02 14:17:57 +00:00
|
|
|
const moment = new Date(this.current.year, this.current.month, this.current.day, 23, 59);
|
2019-10-07 15:40:45 +01:00
|
|
|
moment.setDate(1);
|
|
|
|
moment.setMonth(moment.getMonth() - 1);
|
|
|
|
moment.setDate(new Date(moment.getFullYear(), moment.getMonth() + 1, 0).getDate() - i);
|
|
|
|
|
|
|
|
days.unshift(
|
|
|
|
new DayItem(
|
|
|
|
daysInPreviousMonth - i,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
moment,
|
|
|
|
DayAction.Previous,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private populateNextMonthDays(days: DayItem[]): void {
|
|
|
|
const passiveDaysAtFinal = 42 - days.length;
|
|
|
|
|
|
|
|
for (let i = 1; i <= passiveDaysAtFinal; i++) {
|
2020-01-02 14:17:57 +00:00
|
|
|
const moment = new Date(this.current.year, this.current.month, this.current.day, 23, 59);
|
2019-10-07 15:40:45 +01:00
|
|
|
moment.setMonth(moment.getMonth() + 1);
|
|
|
|
moment.setDate(i);
|
|
|
|
|
|
|
|
days.push(
|
|
|
|
new DayItem(
|
|
|
|
i,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
moment,
|
|
|
|
DayAction.Next,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private markToday(days: DayItem[]): void {
|
|
|
|
const daysCount = days.length;
|
|
|
|
|
|
|
|
for (let i = 0; i < daysCount; i++) {
|
|
|
|
const day: DayItem = days[i];
|
|
|
|
|
2020-01-02 14:17:57 +00:00
|
|
|
if (day.equals(this.now)) {
|
2019-10-07 15:40:45 +01:00
|
|
|
day.today = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-08 16:47:45 +00:00
|
|
|
|
|
|
|
/**
|
2020-02-14 15:35:10 +00:00
|
|
|
* DateFormat is utils class for date formatting to string.
|
2019-11-08 16:47:45 +00:00
|
|
|
*/
|
|
|
|
export class DateFormat {
|
|
|
|
|
|
|
|
/**
|
2020-02-14 15:35:10 +00:00
|
|
|
* getUSDate transforms date into US date format string.
|
2019-11-08 16:47:45 +00:00
|
|
|
* @param date - Date to format
|
|
|
|
* @param separator - symbol for joining date string
|
|
|
|
* @returns formatted date string
|
|
|
|
*/
|
2020-03-13 18:14:41 +00:00
|
|
|
public static getUTCDate(date: Date, separator: string): string {
|
2020-01-02 14:17:57 +00:00
|
|
|
const month = date.getUTCMonth() + 1;
|
|
|
|
const day = date.getUTCDate();
|
|
|
|
const year = date.getUTCFullYear();
|
2019-11-08 16:47:45 +00:00
|
|
|
|
|
|
|
return [month, day, year].join(separator);
|
|
|
|
}
|
|
|
|
}
|