1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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'),
20 toggle_group: toggle_group,
21 want_state: want_state,
25 done(function(data, status, jqxhr) {
27 // Remove "danger" status in case a previous action failed
28 $('.btn-danger', context.toggle_group).
30 removeClass('btn-danger');
31 // Update last-saved-state
33 attr('data-persistent-state', context.want_state);
35 fail(function(jqxhr, status, error) {
38 // Add a visual indication that something failed
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');
45 toggleClass('active', saved_state == 'persistent').
46 html(saved_state == 'persistent' ? 'Persistent' : 'Cache');
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("; "));
55 window.alert("Request failed.");