-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventManager.js
190 lines (152 loc) · 5.72 KB
/
EventManager.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// EventManager.js
class EventManager {
constructor() {
this.dbName = 'ZInfinityCalendarDB';
this.dbVersion = 1;
this.storeName = 'events';
this.db = null;
this.initDB();
}
initDB() {
const request = indexedDB.open(this.dbName, this.dbVersion);
request.onerror = (event) => {
console.error("IndexedDB error:", event.target.error);
};
request.onsuccess = (event) => {
this.db = event.target.result;
console.log("IndexedDB opened successfully");
};
request.onupgradeneeded = (event) => {
this.db = event.target.result;
const objectStore = this.db.createObjectStore(this.storeName, { keyPath: "id" });
objectStore.createIndex("start", "start", { unique: false });
objectStore.createIndex("end", "end", { unique: false });
};
}
addEvent(event) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction([this.storeName], "readwrite");
const objectStore = transaction.objectStore(this.storeName);
const request = objectStore.add(event);
request.onerror = (event) => {
console.error("Error adding event:", event.target.error);
reject("Error adding event: " + event.target.error);
};
request.onsuccess = (event) => {
console.log("Event added successfully:", event.target.result);
resolve(event.target.result);
};
});
}
removeEvent(eventId) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction([this.storeName], "readwrite");
const objectStore = transaction.objectStore(this.storeName);
const request = objectStore.delete(eventId);
request.onerror = (event) => {
reject("Error removing event: " + event.target.error);
};
request.onsuccess = (event) => {
resolve();
};
});
}
updateEvent(eventId, updatedEvent) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction([this.storeName], "readwrite");
const objectStore = transaction.objectStore(this.storeName);
const request = objectStore.put(updatedEvent);
request.onerror = (event) => {
reject("Error updating event: " + event.target.error);
};
request.onsuccess = (event) => {
resolve();
};
});
}
getEventsForDate(date) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction([this.storeName], "readonly");
const objectStore = transaction.objectStore(this.storeName);
const index = objectStore.index("start");
const startOfDay = new Date(date);
startOfDay.setHours(0, 0, 0, 0);
const endOfDay = new Date(date);
endOfDay.setHours(23, 59, 59, 999);
const range = IDBKeyRange.bound(startOfDay, endOfDay);
const request = index.getAll(range);
request.onerror = (event) => {
console.error("Error getting events:", event.target.error);
reject("Error getting events: " + event.target.error);
};
request.onsuccess = (event) => {
console.log("Events retrieved successfully:", event.target.result);
resolve(event.target.result);
};
});
}
clearEvents() {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction([this.storeName], "readwrite");
const objectStore = transaction.objectStore(this.storeName);
const request = objectStore.clear();
request.onerror = (event) => {
console.error("Error clearing events:", event.target.error);
reject("Error clearing events: " + event.target.error);
};
request.onsuccess = (event) => {
console.log("Events cleared successfully");
resolve();
};
});
}
getEventsForYear(year) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction([this.storeName], "readonly");
const objectStore = transaction.objectStore(this.storeName);
const index = objectStore.index("start");
const startOfYear = new Date(year, 0, 1);
const endOfYear = new Date(year, 11, 31, 23, 59, 59, 999);
const range = IDBKeyRange.bound(startOfYear, endOfYear);
const request = index.getAll(range);
request.onerror = (event) => {
reject("Error getting events for year: " + event.target.error);
};
request.onsuccess = (event) => {
resolve(event.target.result);
};
});
}
getEventsForMonth(year, month) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction([this.storeName], "readonly");
const objectStore = transaction.objectStore(this.storeName);
const index = objectStore.index("start");
const startOfMonth = new Date(year, month, 1);
const endOfMonth = new Date(year, month + 1, 0, 23, 59, 59, 999);
const range = IDBKeyRange.bound(startOfMonth, endOfMonth);
const request = index.getAll(range);
request.onerror = (event) => {
reject("Error getting events for month: " + event.target.error);
};
request.onsuccess = (event) => {
resolve(event.target.result);
};
});
}
getEventsForDateRange(startDate, endDate) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction([this.storeName], "readonly");
const objectStore = transaction.objectStore(this.storeName);
const index = objectStore.index("start");
const range = IDBKeyRange.bound(startDate, endDate);
const request = index.getAll(range);
request.onerror = (event) => {
reject("Error getting events for date range: " + event.target.error);
};
request.onsuccess = (event) => {
resolve(event.target.result);
};
});
}
}