forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeslot_edit.js
More file actions
257 lines (229 loc) · 9.47 KB
/
timeslot_edit.js
File metadata and controls
257 lines (229 loc) · 9.47 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// create a namespace for local JS
timeslotEdit = (function () {
let deleteModal;
let timeslotTableBody = document.querySelector('#timeslot-table tbody');
function initializeDeleteModal() {
deleteModal = jQuery('#delete-modal');
deleteModal.eltsToDelete = null; // PK of TimeSlot that modal 'Delete' button should delete
let spans = deleteModal.find('span');
deleteModal.elts = {
unofficialUseWarning: deleteModal.find('.unofficial-use-warning'),
officialUseWarning: deleteModal.find('.official-use-warning'),
timeslotNameSpans: spans.filter('.ts-name'),
timeslotDateSpans: spans.filter('.ts-date'),
timeslotTimeSpans: spans.filter('.ts-time'),
timeslotLocSpans: spans.filter('.ts-location'),
timeslotCountSpans: spans.filter('.ts-count'),
pluralSpans: spans.filter('.ts-plural'),
singularSpans: spans.filter('.ts-singular')
};
document.getElementById('confirm-delete-button')
.addEventListener(
'click',
() => timeslotEdit.handleDeleteButtonClick()
);
function uniqueArray(a) {
let s = new Set();
a.forEach(item => s.add(item));
return Array.from(s);
}
deleteModal.openModal = function (eltsToDelete) {
let eltArray = Array.from(eltsToDelete); // make sure this is an array
if (eltArray.length > 1) {
deleteModal.elts.pluralSpans.show();
deleteModal.elts.singularSpans.hide();
} else {
deleteModal.elts.pluralSpans.hide();
deleteModal.elts.singularSpans.show();
}
deleteModal.elts.timeslotCountSpans.text(String(eltArray.length));
let names = uniqueArray(eltArray.map(elt => elt.dataset.timeslotName));
if (names.length === 1) {
names = names[0];
} else {
names.sort();
names = names.join(', ');
}
deleteModal.elts.timeslotNameSpans.text(names);
let dates = uniqueArray(eltArray.map(elt => elt.dataset.timeslotDate));
if (dates.length === 1) {
dates = dates[0];
} else {
dates = 'Multiple';
}
deleteModal.elts.timeslotDateSpans.text(dates);
let times = uniqueArray(eltArray.map(elt => elt.dataset.timeslotTime));
if (times.length === 1) {
times = times[0];
} else {
times = 'Multiple';
}
deleteModal.elts.timeslotTimeSpans.text(times);
let locs = uniqueArray(eltArray.map(elt => elt.dataset.timeslotLocation));
if (locs.length === 1) {
locs = locs[0];
} else {
locs = 'Multiple';
}
deleteModal.elts.timeslotLocSpans.text(locs);
// Check whether any of the elts are used in official / unofficial schedules
let unofficialUse = eltArray.some(elt => elt.dataset.unofficialUse === 'true');
let officialUse = eltArray.some(elt => elt.dataset.officialUse === 'true');
deleteModal.elts.unofficialUseWarning.hide();
deleteModal.elts.officialUseWarning.hide();
if (officialUse) {
deleteModal.elts.officialUseWarning.show();
} else if (unofficialUse) {
deleteModal.elts.unofficialUseWarning.show();
}
deleteModal.eltsToDelete = eltsToDelete;
deleteModal.modal('show');
};
/**
* Handle deleting a single timeslot
*
* clicked arg is the clicked element, which must be a child of the timeslot element
*/
function deleteSingleTimeSlot(clicked) {
deleteModal.openModal([clicked.closest('.timeslot')]);
}
/**
* Handle deleting an entire day worth of timeslots
*
* clicked arg is the clicked element, which must be a child of the day header element
*/
function deleteDay(clicked) {
// Find all timeslots for this day
let dateId = clicked.dataset.dateId;
let timeslots = timeslotTableBody.querySelectorAll(
':scope .timeslot[data-date-id="' + dateId + '"]' // :scope prevents picking up results outside table body
);
if (timeslots.length > 0) {
deleteModal.openModal(timeslots);
}
}
/**
* Handle deleting an entire column worth of timeslots
*
* clicked arg is the clicked element, which must be a child of the column header element
*/
function deleteColumn(clicked) {
let colId = clicked.dataset.colId;
let timeslots = timeslotTableBody.querySelectorAll(
':scope .timeslot[data-col-id="' + colId + '"]' // :scope prevents picking up results outside table body
);
if (timeslots.length > 0) {
deleteModal.openModal(timeslots);
}
}
/**
* Event handler for clicks on the timeslot table
*
* Handles clicks on all the delete buttons to avoid large numbers of event handlers.
*/
document.getElementById('timeslot-table')
.addEventListener('click', function (event) {
let clicked = event.target; // find out what was clicked
if (clicked.dataset.deleteScope) {
switch (clicked.dataset.deleteScope) {
case 'timeslot':
deleteSingleTimeSlot(clicked);
break;
case 'column':
deleteColumn(clicked);
break;
case 'day':
deleteDay(clicked);
break;
default:
throw new Error('Unexpected deleteScope "' + clicked.dataset.deleteScope + '"');
}
}
});
}
// Update timeslot classes when DOM changes
function tstableObserveCallback(mutationList) {
mutationList.forEach(mutation => {
if (mutation.type === 'childList' && mutation.target.classList.contains('tscell')) {
const tscell = mutation.target;
// mark collisions
if (tscell.getElementsByClassName('timeslot')
.length > 1) {
tscell.classList.add('timeslot-collision');
} else {
tscell.classList.remove('timeslot-collision');
}
// remove timeslot type classes for any removed timeslots
mutation.removedNodes.forEach(node => {
if (node.classList.contains('timeslot') && node.dataset.timeslotType) {
tscell.classList.remove('tstype_' + node.dataset.timeslotType);
}
});
// now add timeslot type classes for any remaining timeslots
Array.from(tscell.getElementsByClassName('timeslot'))
.forEach(elt => {
if (elt.dataset.timeslotType) {
tscell.classList.add('tstype_' + elt.dataset.timeslotType);
}
});
}
});
}
function initializeTsTableObserver() {
const observer = new MutationObserver(tstableObserveCallback);
observer.observe(timeslotTableBody, { childList: true, subtree: true });
}
window.addEventListener('load', function () {
initializeTsTableObserver();
initializeDeleteModal();
});
// function removeTimeslotElement(elt) {
// if (elt.parentNode) {
// elt.parentNode.removeChild(elt);
// }
// }
function handleDeleteButtonClick() {
if (!deleteModal || !deleteModal.eltsToDelete) {
return; // do nothing if not yet initialized
}
let timeslotElts = Array.from(deleteModal.eltsToDelete); // make own copy as Array so we have .map()
ajaxDeleteTimeSlot(timeslotElts.map(elt => elt.dataset.timeslotPk))
.fail(function (jqXHR) {
displayError('Error deleting timeslot: ' + jqXHR.responseText);
})
.done(function () {
timeslotElts.forEach(
tse => {
tse.closest('td.tscell')
.querySelector('.new-timeslot-link')
.classList.remove('hidden');
tse.parentNode.removeChild(tse);
}
);
})
.always(function () { deleteModal.modal('hide'); });
}
/**
* Make an AJAX request to delete a TimeSlot
*
* @param pkArray array of PKs of timeslots to delete
* @returns jqXHR object corresponding to jQuery ajax request
*/
function ajaxDeleteTimeSlot(pkArray) {
return jQuery.ajax({
method: 'post',
timeout: 5 * 1000,
data: {
action: 'delete',
slot_id: pkArray.join(',')
}
});
}
function displayError(msg) {
window.alert(msg);
}
// export callable methods
return {
handleDeleteButtonClick: handleDeleteButtonClick,
};
})();