This repository has been archived by the owner on May 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.js
413 lines (364 loc) · 15.4 KB
/
util.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// ***************************
// declare global variables
// ***************************
var GLOBAL_confirmHandlerData = -1; // data value of element
var GLOBAL_confirmHandlerReference = -1; // data value of reference element (i.e. container, parent, etc.)
var GLOBAL_util_showConfirmBox = null; // hack to enable passing of JS values between fxns in different files
// ***************************
// Listeners
// ***************************
$(document).ready(function () {
// create container to hold ajax messages; hide #page_alert_div
// note: dkc removed dismiss button from screen alerts to standardize user behaviour
// <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
$('#content_container').prepend('<div id="page_alert_div" class="alert alert-dismissible small" role="alert"><span id="page_alert_message"></span></div>');
$('#page_alert_div').hide();
// PrintArea: Print any specific div (the div is assigned a unique class called: "wms_print_XYZ"; eg, wms_print_CalSetup, wms_print_EditOne, wms_print_OpeningSignup)
$(".wmsPrintArea").click(function () {
var print_this_area = $(this).data("what-area-to-print");
var options = {
mode: "popup", // avoid mode: "iframe" (bug): big spaces created if multiple pages are to be printed
standard: "html5"
};
// CSS overrides pre-existing max-height, removes scrollbar; otherwise, PrintArea jQuery plugin outputs to print only what is visible within visible boundary
$("#openings-list-container, #container-my-signups, #container-others-signups").addClass("printareaPatch");
$("div." + print_this_area).printArea(options);
$("#openings-list-container, #container-my-signups, #container-others-signups").removeClass("printareaPatch");
});
// CSV output: download file
function exportTableToCSV(csvText, filename) {
csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csvText);
$(this)
.attr({
'download': filename,
'href': csvData,
'target': '_blank'
});
}
// This must be a hyperlink
$(".wmsExportCSV").on('click', function (event) {
//csv = '"sample1","sample2","cat","dog"';
csv = $(".wms_export_CSV").text();
//console.log(csv);
// IF CSV, don't do event.preventDefault() or return false (we actually need this to be a typical hyperlink)
exportTableToCSV.apply(this, [csv, 'export.csv']);
});
});
// ***************************
// helper functions
// ***************************
// BootBox jQuery confirm box (helper function)
function showConfirmBox(ary) {
// CPH: Moved this code to the callback function below.
// console.dir(ary); // debugging
//if (ary['ajax_action'] == 'sus-delete-opening') {
// var custom_data = $("input[name='custom_user_value']:checked").val();
// }
// else
// if (ary['ajax_action'] == 'send-email-to-participants-for-opening-id') {
//var custom_data = ary['subject_message_json'];
//console.log("custom_data = "+JSON.parse(custom_data));
// issue: showConfirmBox dialog naturally removes scrollbar from the layer below; if that layer is also a dialog, this can be bad for UI
// solution: reintroduce scrollbar to modal
//$("#modal-edit-opening").css("cssText", "overflow-x: hidden !important; overflow-y: auto !important; display: block !important;");
//}
bootbox.dialog({
title: ary['title'],
message: ary['message'],
buttons: {
success: {
label: ary['label'],
className: ary['class'],
callback: function () {
if (ary['ajax_action'] == 'delete-opening') {
// console.log("callback function array is delete-opening");
var custom_data = $("input[name='custom_user_value']:checked").val();
// console.log("callback function = "+ custom_data);
}
if (ary['ajax_action'] == 'send-email-to-participants-for-opening-id') {
var custom_data = ary['subject_message_json'];
// issue: showConfirmBox dialog naturally removes scrollbar from the layer below; if that layer is also a dialog, this can be bad for UI
// solution: reintroduce scrollbar to modal
$("#modal-edit-opening").css("cssText", "overflow-x: hidden !important; overflow-y: auto !important; display: block !important;");
// show button loading text (bootstrap) only after clicking "Send" button
$("#notifyParticipantsButton").button('loading');
// console.log("callback function = "+ custom_data);
}
// show status
susUtil_setTransientAlert('progress', 'Working...');
$.ajax({
type: 'GET',
url: ary['url'],
cache: false,
data: {
'ajax_Action': ary['ajax_action'],
'ajax_Primary_ID': ary['ajax_id'],
'ajax_Custom_Data': custom_data // see above for how this custom value is set
},
dataType: 'json',
error: function (data) {
// console.log("error section"); console.dir(data);
updateDOM(ary, false, data);
},
success: function (data) {
// console.log("success section"); console.dir(data);
if (data.status == 'success') {
// remove element
updateDOM(ary, true, data);
}
else {
// error message
updateDOM(ary, false, data);
}
}
});
}
},
cancel: {
label: "Cancel",
className: "btn btn-link btn-cancel",
callback: function () {
this.dismiss = "modal";
}
}
},
// modal options
animate: false,
backdrop: "static",
onEscape: true
});
}
GLOBAL_util_showConfirmBox = showConfirmBox;
function helper_Remove_DOM_Elements(openingID) {
// check to see if this the last opening on this date
var countRemainingOpenings = $('.list-opening-id-' + openingID).siblings(".list-opening").length;
if (countRemainingOpenings == 0) {
// this is the last opening on this date!
// remove the list container from DOM for both: "Calendar View" overlay AND calendar "List View"
$('.list-opening-id-' + openingID).parent().parent(".calendar-cell-openings").remove();
$('#tabOpeningsListView .list-opening-id-' + openingID).parent(".opening-list-for-date").remove();
}
else {
// additional openings still exist on this date...
// remove single opening from DOM for both: "Calendar View" overlay AND calendar "List View"
$('.list-opening-id-' + openingID).remove();
$('#tabOpeningsListView .list-opening-id-' + openingID).remove();
}
}
function updateDOM(action_ary, ret, data) {
//console.dir(action_ary);
//console.log(ret);
//console.log(data);
if (action_ary.ajax_action == 'copy-sheet') {
if (ret) {
// show status
susUtil_setTransientAlert('success', 'Copied');
// redirect: to newly copied sheet
location.href = data["url_redirect"];
}
else {
// error message
susUtil_setTransientAlert('error', 'Failed: No action taken: ' + data.notes);
}
}
else if (action_ary.ajax_action == 'delete-sheetgroup') {
if (ret) {
// show status
susUtil_setTransientAlert('success', 'Deleted');
// remove element
$('#btn-edit-sheetgroup-id-' + GLOBAL_confirmHandlerData).closest('TABLE').remove();
}
else {
// error message
susUtil_setTransientAlert('error', 'Failed: No action taken: ' + data.notes);
}
}
else if (action_ary.ajax_action == 'delete-sheet') {
if (ret) {
// show status
susUtil_setTransientAlert('success', 'Deleted');
// remove element
$('#btn-edit-sheet-id-' + GLOBAL_confirmHandlerData).closest('TR').remove();
}
else {
// error message
susUtil_setTransientAlert('error', 'Failed: No action taken: ' + data.notes);
}
}
else if (action_ary.ajax_action == 'delete-opening') {
// console.log('Data: ' + parseInt(data.customData));
if (ret) {
// show status
susUtil_setTransientAlert('success', 'Deleted');
switch (parseInt(data.customData)) {
case 0:
// delete only this opening
//console.log('reached case 0. customData = ' + parseInt(data.customData));
//console.dir(data.updateIDs_ary);
var openingID = GLOBAL_confirmHandlerData;
helper_Remove_DOM_Elements(openingID);
break;
case 1:
// delete all openings for this single day
//console.log('reached case 1. customData = ' + parseInt(data.customData));
//console.dir(data.updateIDs_ary);
var openingID = GLOBAL_confirmHandlerData;
for (i = 0; i < data.updateIDs_ary.length; i++) {
// loop through returned IDs, and remove each from DOM
openingID = parseInt(data.updateIDs_ary[i]);
helper_Remove_DOM_Elements(openingID);
}
break;
case 2:
// delete this and all future openings in this series
//console.log('reached case 2. customData = ' + parseInt(data.customData));
//console.dir(data.updateIDs_ary);
var openingID = GLOBAL_confirmHandlerData;
for (i = 0; i < data.updateIDs_ary.length; i++) {
// loop through returned IDs, and remove each from DOM
openingID = parseInt(data.updateIDs_ary[i]);
helper_Remove_DOM_Elements(openingID);
}
break;
case 3:
// delete this and all past and future openings in this series
//console.log('reached case 3. customData = ' + parseInt(data.customData));
//console.dir(data.updateIDs_ary);
var openingID = GLOBAL_confirmHandlerData;
for (i = 0; i < data.updateIDs_ary.length; i++) {
// loop through returned IDs, and remove each from DOM
openingID = parseInt(data.updateIDs_ary[i]);
helper_Remove_DOM_Elements(openingID);
}
break;
default:
// default condition (failsafe)
var openingID = GLOBAL_confirmHandlerData;
helper_Remove_DOM_Elements(openingID);
break;
}
}
else {
// error message
susUtil_setTransientAlert('error', 'Failed: No action taken: ' + data.notes);
}
}
else if (action_ary.ajax_action == 'delete-signup') {
if (ret) {
// show status
susUtil_setTransientAlert('success', 'Deleted');
// count remaining signups within this opening
var countMySignupsRemaining = $('#tabMySignups .list-signup-id-' + GLOBAL_confirmHandlerData).siblings(".list-signups").length;
var countOthersSignupsRemaining = $('#tabOthersSignups .list-signup-id-' + GLOBAL_confirmHandlerData).siblings(".list-signups").length;
// console.log('countMySignupsRemaining = ' + countMySignupsRemaining + ', countOthersSignupsRemaining = ' + countOthersSignupsRemaining);
// My Signups: determine if signup_id exists in DOM
if ($('#tabMySignups .list-signup-id-' + GLOBAL_confirmHandlerData).length > 0) {
if (countMySignupsRemaining == 0) {
// is this the last opening on this date?
if ($('#tabMySignups .list-opening-id-' + GLOBAL_confirmHandlerReference).siblings(".list-openings").length == 0) {
// this is the last opening on this date: remove this date from DOM
$('#tabMySignups .list-opening-id-' + GLOBAL_confirmHandlerReference).parent('.opening-list-for-date').remove();
}
else {
// this is the last signup on this opening: remove this opening from DOM
$('#tabMySignups .list-opening-id-' + GLOBAL_confirmHandlerReference).remove();
}
}
else {
// additional signups still exist on this opening: remove only this signup from DOM
$('#tabMySignups .list-signup-id-' + GLOBAL_confirmHandlerData).remove();
}
}
// Sign-ups on my Sheets: determine if signup_id exists in DOM
if ($('#tabOthersSignups .list-signup-id-' + GLOBAL_confirmHandlerData).length > 0) {
if (countOthersSignupsRemaining == 0) {
// is this the last opening on this date?
if ($('#tabOthersSignups .list-opening-id-' + GLOBAL_confirmHandlerReference).siblings(".list-openings").length == 0) {
// this is the last opening on this date: remove this date from DOM
$('#tabOthersSignups .list-opening-id-' + GLOBAL_confirmHandlerReference).parent('.opening-list-for-date').remove();
}
else {
// this is the last signup on this opening: remove this opening from DOM
$('#tabOthersSignups .list-opening-id-' + GLOBAL_confirmHandlerReference).remove();
}
}
else {
// additional signups still exist on this opening: remove only this signup from DOM
$('#tabOthersSignups .list-signup-id-' + GLOBAL_confirmHandlerData).remove();
}
}
// restore default text if no signups remain in either container
if ($('#container-my-signups .list-signups').length == 0) {
$('#container-my-signups').html("<div class='bg-info'>You have not yet signed up for any sheet openings.<br />To sign-up, click on <strong>"Available Openings"</strong> (above).</div>");
}
if ($('#container-others-signups .list-signups').length == 0) {
$('#container-others-signups').html("<div class='bg-info'>No one has signed up on your sheets.<br />To see your sheets, click on <strong>"Sheets"</strong> (above).</div>");
}
// console.log('container-my-signups = ' + $('#container-my-signups .list-signups').length + ', container-others-signups = ' + $('#container-others-signups .list-signups').length);
}
else {
// error message
susUtil_setTransientAlert('error', 'Failed: No action taken: ' + data.notes);
}
}
else if (action_ary.ajax_action == 'delete-signup-from-edit-opening-modal') {
if (ret) {
// show status
susUtil_setTransientAlert('success', 'Deleted');
// fetch count of remaining LI elements within this UL
GLOBAL_calendar_fetchSignupsforOpening(GLOBAL_confirmHandlerReference);
}
else {
// error message
susUtil_setTransientAlert('error', 'Failed: No action taken: ' + data.notes);
}
}
else if (action_ary.ajax_action == 'send-email-to-participants-for-opening-id') {
if (ret) {
// show status
susUtil_setTransientAlert('success', 'Sent');
// hide button, show confirmation text
$('#notifyParticipantsButton').hide();
$('#btnConfirmationText').html(data.html_output);
}
else {
// error message
susUtil_setTransientAlert('error', 'Failed: No action taken: ' + data.notes);
// hide button, show confirmation text
$('#notifyParticipantsButton').hide();
$('#btnConfirmationText').html(data.html_output);
}
}
}
function appRootPath() {
return "/GITHUB/lti/lti-signup-sheets";
}
// REQUIRES: a div of id page_alert_div
function susUtil_setTransientAlert(alertType, alertMessage) {
// show the pre-existing alert button in DOM
$('#page_alert_div').show();
if (alertType == 'progress') {
$('#page_alert_div').addClass('alert-success').removeClass('alert-danger').removeClass("alert-info");
$('#page_alert_message').html('<i class="glyphicon glyphicon-time"></i> ' + alertMessage);
}
else if (alertType == 'success') {
// alert('INSIDE SUCCESS: ' + alertType + ',' + alertMessage);
$('#page_alert_div').addClass('alert-success').removeClass('alert-danger').removeClass("alert-info");
$('#page_alert_message').html('<i class="glyphicon glyphicon-ok"></i> ' + alertMessage);
}
else if (alertType == 'error') {
$('#page_alert_div').removeClass('alert-success').addClass('alert-danger').removeClass("alert-info");
$('#page_alert_message').html('<i class="glyphicon glyphicon-exclamation-sign"></i> ' + alertMessage);
}
// pause for user to read the alert, then hide alert button
setTimeout(function () {
$('#page_alert_div').hide();
}, 3000);
}
function randomString(strSize) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < strSize; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}