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