11454: When adding a uuid prefix on the add session box, append
[arvados.git] / apps / workbench / app / assets / javascripts / collections.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 jQuery(function($){
6     $(document).on('click', '.toggle-persist button', function() {
7         var toggle_group = $(this).parents('[data-remote-href]').first();
8         var want_persist = !toggle_group.find('button').hasClass('active');
9         var want_state = want_persist ? 'persistent' : 'cache';
10         toggle_group.find('button').
11             toggleClass('active', want_persist).
12             html(want_persist ? 'Persistent' : 'Cache');
13         $.ajax(toggle_group.attr('data-remote-href'),
14                {dataType: 'json',
15                 type: 'POST',
16                 data: {
17                     value: want_state
18                 },
19                 context: {
20                     toggle_group: toggle_group,
21                     want_state: want_state,
22                     button: this
23                 }
24                }).
25             done(function(data, status, jqxhr) {
26                 var context = this;
27                 // Remove "danger" status in case a previous action failed
28                 $('.btn-danger', context.toggle_group).
29                     addClass('btn-info').
30                     removeClass('btn-danger');
31                 // Update last-saved-state
32                 context.toggle_group.
33                     attr('data-persistent-state', context.want_state);
34             }).
35             fail(function(jqxhr, status, error) {
36                 var context = this;
37                 var saved_state;
38                 // Add a visual indication that something failed
39                 $(context.button).
40                     addClass('btn-danger').
41                     removeClass('btn-info');
42                 // Change to the last-saved-state
43                 saved_state = context.toggle_group.attr('data-persistent-state');
44                 $(context.button).
45                     toggleClass('active', saved_state == 'persistent').
46                     html(saved_state == 'persistent' ? 'Persistent' : 'Cache');
47
48                 if (jqxhr.readyState == 0 || jqxhr.status == 0) {
49                     // Request cancelled due to page reload.
50                     // Displaying an alert would be rather annoying.
51                 } else if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
52                     window.alert("Request failed: " +
53                                  jqxhr.responseJSON.errors.join("; "));
54                 } else {
55                     window.alert("Request failed.");
56                 }
57             });
58     });
59 });