-
Notifications
You must be signed in to change notification settings - Fork 112
/
permission_toggle.js
59 lines (57 loc) · 2.21 KB
/
permission_toggle.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
// Copyright (C) The Arvados Authors. All rights reserved.
//
// SPDX-License-Identifier: AGPL-3.0
$(document).
on('click', '[data-toggle-permission] input[type=checkbox]', function() {
var data = {};
var keys = ['data-permission-uuid',
'data-permission-name',
'data-permission-head',
'data-permission-tail'];
var attr;
for(var i in keys) {
attr = keys[i];
data[attr] = $(this).closest('[' + attr + ']').attr(attr);
if (data[attr] === undefined) {
console.log(["Error: no " + attr + " established here.", this]);
return;
}
}
var is_checked = $(this).prop('checked');
if (is_checked) {
$.ajax('/links',
{dataType: 'json',
type: 'POST',
data: {'link[tail_uuid]': data['data-permission-tail'],
'link[head_uuid]': data['data-permission-head'],
'link[link_class]': 'permission',
'link[name]': data['data-permission-name']},
context: this}).
fail(function(jqxhr, status, error) {
$(this).prop('checked', false);
}).
done(function(data, status, jqxhr) {
$(this).attr('data-permission-uuid', data['uuid']);
}).
always(function() {
$(this).prop('disabled', false);
});
}
else {
$.ajax('/links/' + data['data-permission-uuid'],
{dataType: 'json',
type: 'POST',
data: {'_method': 'DELETE'},
context: this}).
fail(function(jqxhr, status, error) {
$(this).prop('checked', true);
}).
done(function(data, status, jqxhr) {
$(this).attr('data-permission-uuid', 'x');
}).
always(function() {
$(this).prop('disabled', false);
});
}
$(this).prop('disabled', true);
});