-
Notifications
You must be signed in to change notification settings - Fork 2
/
Event.js
171 lines (155 loc) · 5.17 KB
/
Event.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { getDayIndex, generateId, dateString } from "./helper.js";
export const MODE = {
VIEW: 1,
UPDATE: 2,
CREATE: 3,
};
export class Event {
constructor(data) {
this.id = data.id || generateId();
this.title = data.title;
this.start = data.start;
this.end = data.end;
this.date = data.date;
this.prevDate = this.date;
this.description = data.description;
this.color = data.color;
}
get dayIndex() {
return getDayIndex(new Date(this.date));
}
get duration() {
return (
(new Date(`${this.date}T${this.end}`).getTime() -
new Date(`${this.date}T${this.start}`).getTime()) /
(1000 * 60)
);
}
get startHour() {
return parseInt(this.start.substring(0, 2));
}
get startMinutes() {
return parseInt(this.start.substring(3, 5));
}
get endHour() {
return parseInt(this.end.substring(0, 2));
}
get endMinutes() {
return parseInt(this.end.substring(3, 5));
}
saveIn(calendar) {
if (this.prevDate && this.date != this.prevDate) {
delete calendar.events[this.prevDate][this.id];
if (Object.values(calendar.events[this.prevDate]).length == 0) {
delete calendar.events[this.prevDate];
}
}
if (!calendar.events[this.date]) {
calendar.events[this.date] = {};
}
calendar.events[this.date][this.id] = this;
calendar.saveEvents();
}
showIn(calendar) {
if (
this.date < dateString(calendar.weekStart) ||
this.date > dateString(calendar.weekEnd)
) {
$(`#${this.id}`).remove();
return;
}
let eventSlot;
if ($(`#${this.id}`).length) {
eventSlot = $(`#${this.id}`);
} else {
eventSlot = $("<div></div>")
.addClass("event")
.attr("id", this.id)
.click(() => this.clickIn(calendar));
}
const h = calendar.slotHeight;
eventSlot
.text(this.title)
.css("top", (this.startHour + this.startMinutes / 60) * h + 2 + "px")
.css("bottom", 24 * h - (this.endHour + this.endMinutes / 60) * h + 1 + "px")
.css("backgroundColor", `var(--color-${this.color})`)
.appendTo(`.day[data-dayIndex=${this.dayIndex}] .slots`);
const duration = this.duration;
if (duration < 45) {
eventSlot.removeClass("shortEvent").addClass("veryShortEvent");
} else if (duration < 59) {
eventSlot.removeClass("veryShortEvent").addClass("shortEvent");
} else {
eventSlot.removeClass("shortEvent").removeClass("veryShortEvent");
}
}
clickIn(calendar) {
if (calendar.mode != MODE.VIEW) return;
calendar.mode = MODE.UPDATE;
calendar.openModal(this);
}
updateIn(calendar) {
this.prevDate = this.date;
this.title = $("#eventTitle").val();
this.start = $("#eventStart").val();
this.end = $("#eventEnd").val();
this.date = $("#eventDate").val();
this.description = $("#eventDescription").val();
this.color = $(".color.active").attr("data-color");
this.saveIn(calendar);
this.showIn(calendar);
}
copyIn(calendar) {
if (calendar.mode != MODE.UPDATE) return;
calendar.closeModal();
calendar.mode = MODE.CREATE;
const copy = new Event({
title: "Copy of " + this.title,
start: this.start,
end: this.end,
date: this.date,
description: this.description,
color: this.color,
});
calendar.openModal(copy);
}
deleteIn(calendar) {
calendar.closeModal();
$(`#${this.id}`).remove();
delete calendar.events[this.date][this.id];
if (Object.values(calendar.events[this.date]).length == 0) {
delete calendar.events[this.date];
}
calendar.saveEvents();
}
isValidIn(calendar) {
const newStart = $("#eventStart").val();
const newEnd = $("#eventEnd").val();
const newDate = $("#eventDate").val();
if (calendar.events[newDate]) {
const event = Object.values(calendar.events[newDate]).find(
(event) =>
event.id != this.id && event.end > newStart && event.start < newEnd
);
if (event) {
$("#errors").text(
`This collides with the event '${event.title}'
(${event.start} - ${event.end}).`
);
return false;
}
}
const duration =
(new Date(`${newDate}T${newEnd}`).getTime() -
new Date(`${newDate}T${newStart}`).getTime()) /
(1000 * 60);
if (duration < 0) {
$("#errors").text("The start cannot be after the end.");
return false;
} else if (duration < 30) {
$("#errors").text("Events should be at least 30 minutes.");
return false;
}
return true;
}
}