Merge branch 'master' into 6064-collection-output-of-perf-issue
[arvados.git] / apps / workbench / app / assets / javascripts / permission_toggle.js
1 $(document).
2     on('click', '[data-toggle-permission] input[type=checkbox]', function() {
3         var data = {};
4         var keys = ['data-permission-uuid',
5                     'data-permission-name',
6                     'data-permission-head',
7                     'data-permission-tail'];
8         var attr;
9         for(var i in keys) {
10             attr = keys[i];
11             data[attr] = $(this).closest('[' + attr + ']').attr(attr);
12             if (data[attr] === undefined) {
13                 console.log(["Error: no " + attr + " established here.", this]);
14                 return;
15             }
16         }
17         var is_checked = $(this).prop('checked');
18
19         if (is_checked) {
20             $.ajax('/links',
21                    {dataType: 'json',
22                     type: 'POST',
23                     data: {'link[tail_uuid]': data['data-permission-tail'],
24                            'link[head_uuid]': data['data-permission-head'],
25                            'link[link_class]': 'permission',
26                            'link[name]': data['data-permission-name']},
27                     context: this}).
28                 fail(function(jqxhr, status, error) {
29                     $(this).prop('checked', false);
30                 }).
31                 done(function(data, status, jqxhr) {
32                     $(this).attr('data-permission-uuid', data['uuid']);
33                 }).
34                 always(function() {
35                     $(this).prop('disabled', false);
36                 });
37         }
38         else {
39             $.ajax('/links/' + data['data-permission-uuid'],
40                    {dataType: 'json',
41                     type: 'POST',
42                     data: {'_method': 'DELETE'},
43                     context: this}).
44                 fail(function(jqxhr, status, error) {
45                     $(this).prop('checked', true);
46                 }).
47                 done(function(data, status, jqxhr) {
48                     $(this).attr('data-permission-uuid', 'x');
49                 }).
50                 always(function() {
51                     $(this).prop('disabled', false);
52                 });
53         }
54         $(this).prop('disabled', true);
55     });