forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalerts.js
More file actions
362 lines (338 loc) · 14 KB
/
Copy pathalerts.js
File metadata and controls
362 lines (338 loc) · 14 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
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
/**
* Copyright (C) 2011 Instructure, Inc.
*
* This file is part of Canvas.
*
* Canvas is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, version 3 of the License.
*
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
define([
'i18n!alerts',
'jquery', // $
'str/htmlEscape',
'jquery.ajaxJSON', // ajaxJSON
'jquery.instructure_forms', // validateForm, formErrors, errorBox
'jquery.instructure_misc_helpers', // replaceTags
'vendor/jquery.ba-tinypubsub', // /\.publish/
'jqueryui/button' // /\.button/
], function(I18n, $, htmlEscape) {
$(function () {
var $list = $('.alerts_list');
var getAlertData = function($alert) {
var criteria = [];
$alert.find('ul.criteria li').each(function() {
criteria.push({
id: $(this).find('input[name="alert[criteria][][id]"]').attr('value'),
criterion_type: $(this).data('value'),
threshold: $(this).find('span').text()
});
});
var recipients = [];
$alert.find('ul.recipients li').each(function() {
recipients.push($(this).data('value'));
});
var repetition = $alert.find('input[name="repetition"]:checked').attr('value');
if(repetition == "value") {
repetition = $alert.find('input[name="alert[repetition]"]').attr('value');
} else {
repetition = null;
}
return {criteria: criteria, recipients: recipients, repetition: repetition};
}
var addRecipientInOrder = function($node, $item) {
$node.append($item);
return $item;
}
var createElement = function(key, element, value, lookup) {
// xsslint safeString.identifier element
var $element = $("<" + element + " />");
$element.data('value', key);
var contentHtml = htmlEscape(lookup[key][value]).toString();
// see placeholder in _alerts.html.erb
contentHtml = contentHtml.replace("%{count}", "<span class='displaying' /><input type='text' name='alert[criteria][][threshold]' class='editing' size='2' />");
$element.html(contentHtml);
if(element == 'li') {
$element.append(' ');
$element.append($list.find('>.delete_item_link').clone().toggle());
} else {
$element.attr('value', key);
}
return $element;
}
// xsslint jqueryObject.function createRecipient createCriterion
var createRecipient = function(recipient, element) {
var $element = createElement(recipient, element, 'label', ENV.ALERTS.POSSIBLE_RECIPIENTS);
if(element == 'li') {
$element.prepend($("<input type='hidden' name='alert[recipients][]' />").attr('value', recipient));
}
return $element;
}
var createCriterion = function(criterion, element) {
var criterion_type = criterion, threshold, id;
if(typeof criterion == "object") {
criterion_type = criterion.criterion_type;
threshold = criterion.threshold;
id = criterion.id;
}
var $element = createElement(criterion_type, element, element == 'li' ? 'label' : 'option', ENV.ALERTS.POSSIBLE_CRITERIA)
if (element == 'li') {
if (!threshold) {
threshold = ENV.ALERTS.POSSIBLE_CRITERIA[criterion_type].default_threshold;
}
$element.find('span').text(threshold);
$element.find('input').attr('value', threshold).attr('title', ENV.ALERTS.POSSIBLE_CRITERIA[criterion_type].title);
$element.prepend($("<input type='hidden' name='alert[criteria][][criterion_type]' />").attr('value', criterion_type));
if(id) {
$element.prepend($("<input type='hidden' name='alert[criteria][][id]' />").attr('value', id));
}
}
return $element;
}
var restoreAlert = function($alert, data) {
var $criteria = $alert.find('.criteria');
$criteria.empty();
for(var idx in data.criteria) {
$criteria.append(createCriterion(data.criteria[idx], 'li'));
}
var $recipients = $alert.find('.recipients');
$recipients.empty();
for(var idx in data.recipients) {
if (ENV.ALERTS.POSSIBLE_RECIPIENTS[data.recipients[idx]]) {
$recipients.append(createRecipient(data.recipients[idx], 'li'));
}
}
if(data.repetition) {
$alert.find('input[name="repetition"][value="value"]').attr('checked', true);
$alert.find('input[name="alert[repetition]"]').attr('value', data.repetition);
$alert.find('.repetition_group .no_repetition').toggle(false);
$alert.find('.repetition_group .repetition').toggle(true).find('span').text(data.repetition);
} else {
$alert.find('input[name="repetition"][value="none"]').attr('checked', true);
$alert.find('.repetition_group .no_repetition').toggle(true);
$alert.find('.repetition_group .repetition').toggle(false);
}
}
for(var idx in ENV.ALERTS.DATA) {
var alert = ENV.ALERTS.DATA[idx];
restoreAlert($('#edit_alert_' + alert.id), alert);
}
$('.add_alert_link').click(function(event) {
event.preventDefault();
var $blank = $('.alert.blank');
var $alert = $blank.clone();
$alert.removeClass('blank');
$alert.addClass('new');
if($list.find('.alert:visible').length != 0) {
$('<div class="alert_separator"></div>').insertBefore($blank);
}
var rand = Math.floor(Math.random() * 100000000);
$alert.find('input').each(function() {
$(this).attr('id', $.replaceTags($(this).attr('id'), 'id', rand));
});
$alert.find('label').each(function() {
$(this).attr('for', $.replaceTags($(this).attr('for'), 'id', rand));
});
$alert.insertBefore($blank);
$alert.find('.edit_link').trigger('click');
$alert.toggle(false);
$alert.slideDown();
});
$list.delegate('.edit_link', 'click', function() {
var $alert = $(this).parents('.alert');
var data = getAlertData($alert);
$alert.data('data', data);
var $criteria_select = $alert.find('.add_criterion_link').prev();
$criteria_select.empty();
var count = 0;
for(var idx in ENV.ALERTS.POSSIBLE_CRITERIA_ORDER) {
var criterion = ENV.ALERTS.POSSIBLE_CRITERIA_ORDER[idx];
var found = -1;
for(var jdx in data.criteria) {
if(data.criteria[jdx].criterion_type == criterion) {
found = jdx;
break;
}
}
if(found == -1) {
$criteria_select.append(createCriterion(criterion, 'option'));
count = count + 1;
}
}
if(count == 0) {
$alert.find('.add_criteria_line').toggle(false);
}
var $recipients_select = $alert.find('.add_recipient_link').prev();
$recipients_select.empty();
count = 0;
for(var idx in ENV.ALERTS.POSSIBLE_RECIPIENTS_ORDER) {
var recipient = ENV.ALERTS.POSSIBLE_RECIPIENTS_ORDER[idx];
if($.inArray(recipient, data.recipients) == -1) {
$recipients_select.append(createRecipient(recipient, 'option'));
count = count + 1;
}
}
if(count == 0) {
$alert.find('.add_recipients_line').toggle(false);
}
$alert.find('.repetition_group label').toggle(true);
$alert.toggleClass('editing');
$alert.toggleClass('displaying');
return false;
}).delegate('.delete_link', 'click', function() {
var $alert = $(this).parents('.alert');
if(!$alert.hasClass('new')) {
$alert.find('input[name="_method"]').attr('value', 'DELETE');
$.ajaxJSON($alert.attr('action'), 'POST', $alert.serialize(), function(data) {
$alert.slideUp(function() {
$alert.remove();
$list.find('.alert:first').prev('.alert_separator').remove();
$list.find('.alert_separator + .alert_separator').remove();
$list.find('.alert:visible:last').next('.alert_separator').remove();
});
});
} else {
$alert.slideUp(function() {
$alert.remove();
$list.find('.alert:first').prev('.alert_separator').remove();
$list.find('.alert_separator + .alert_separator').remove();
$list.find('.alert:visible:last').next('.alert_separator').remove();
});
}
return false;
}).delegate('.cancel_button', 'click', function() {
$(this).parent().hideErrors();
var $alert = $(this).parents('.alert');
if($alert.hasClass('new')) {
$alert.slideUp(function() {
$alert.remove();
$list.find('.alert:first').prev('.alert_separator').remove();
$list.find('.alert_separator + .alert_separator').remove();
$list.find('.alert:visible:last').next('.alert_separator').remove();
});
} else {
var data = $alert.data('data');
restoreAlert($alert, data);
$alert.toggleClass('editing', false);
$alert.toggleClass('displaying', true);
}
return false;
}).delegate('.alert', 'submit', function() {
var $alert = $(this);
// Validation (validateForm doesn't support arrays, and formErrors
// wouldn't be able to locate the correct elements)
var errors = [];
if($alert.find('.criteria li').length == 0) {
errors.push([$alert.find('.add_criterion_link').prev(), I18n.t('errors.criteria_required', "At least one trigger is required")]);
}
$alert.find('.criteria input.editing').each(function() {
var val = $(this).attr('value');
if(!val || isNaN(val) || parseFloat(val) < 0) {
errors.push([$(this), I18n.t('errors.threshold_should_be_numeric', "This should be a positive number")]);
}
});
if($alert.find('.recipients li').length == 0) {
errors.push([$alert.find('.add_recipient_link').prev(), I18n.t('errors.recipients_required', "At least one recipient is required")]);
}
if($alert.find('input[name="repetition"]:checked').attr('value') == 'none') {
$alert.find('input[name="alert[repetition]"]').attr('value', '');
} else {
var $repetition = $alert.find('input[name="alert[repetition]"]');
var val = $repetition.attr('value');
if(!val || isNaN(val) || parseFloat(val) < 0) {
errors.push([$repetition, I18n.t('errors.threshold_should_be_numeric', "This should be a positive number")]);
}
}
if(errors.length != 0) {
$alert.formErrors(errors);
return false;
}
$.ajaxJSON($alert.attr('action'), 'POST', $alert.serialize(), function(data, xhr) {
$alert.removeClass('new');
$alert.attr('action', xhr.getResponseHeader('Location'));
var $method = $alert.find('input[name="_method"]');
if($method.length == 0) {
$alert.append($('<input type="hidden" name="_method" value="put" />'));
}
$alert.toggleClass('editing', false);
$alert.toggleClass('displaying', true);
restoreAlert($alert, data);
}, function(data) {
$alert.formErrors(data);
});
return false;
}).delegate('.recipients .delete_item_link', 'click', function() {
var $li = $(this).parents('li');
var $add_link = $(this).parents('.alert').find('.add_recipient_link');
addRecipientInOrder($add_link.prev(), createRecipient($li.data('value'), 'option'));
$li.slideUp(function() {
$li.remove();
});
$add_link.parent().slideDown(function() {
$add_link.parent().css('display', '');
});
return false;
}).delegate('.add_recipient_link', 'click', function() {
var $recipients = $(this).parents('.alert').find('.recipients');
var $select = $(this).prev();
var recipient = $select.attr('value');
addRecipientInOrder($recipients, createRecipient(recipient, 'li')).toggle().slideDown();
var $errorBox = $select.data('associated_error_box');
if($errorBox) {
$errorBox.fadeOut('slow', function() {
$errorBox.remove();
});
}
$select.find('option[value="' + recipient + '"]').remove();
if($select.find('*').length == 0) {
$(this).parent().slideUp();
}
return false;
}).delegate('.criteria .delete_item_link', 'click', function() {
var $li = $(this).parents('li');
var $add_link = $(this).parents('.alert').find('.add_criterion_link');
addRecipientInOrder($add_link.prev(), createCriterion($li.data('value'), 'option'));
$li.slideUp(function(){
$li.remove();
});
$add_link.parent().slideDown(function() {
$add_link.parent().css('display', '');
});
return false;
}).delegate('.add_criterion_link', 'click', function() {
var $criteria = $(this).parents('.alert').find('.criteria');
var $select = $(this).prev();
var criterion = $select.attr('value');
addRecipientInOrder($criteria, createCriterion(criterion, 'li')).toggle().slideDown();
var $errorBox = $select.data('associated_error_box');
if($errorBox) {
$errorBox.fadeOut('slow', function() {
$errorBox.remove();
});
}
$select.find('option[value="' + criterion + '"]').remove();
if($select.find('*').length == 0) {
$(this).parent().slideUp();
}
return false;
}).delegate('input[name="repetition"]', 'click', function() {
var $error_box = $(this).parents('.alert').find('input[name="alert[repetition]"]').data('associated_error_box');
if($error_box) {
$error_box.fadeOut('slow', function() {
$error_box.remove();
});
}
}).delegate('label.repetition', 'click', function(event){
event.preventDefault();
$(this).parents('.alert').find('input[name="repetition"]').prop('checked', true);
});
});
});