+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
jQuery(function($){
- $(document).on('change', '.toggle-persist input[name=wants]', function() {
+ $(document).on('click', '.toggle-persist button', function() {
var toggle_group = $(this).parents('[data-remote-href]').first();
- if (toggle_group.attr('data-persistent-state') == $(this).val()) {
- // When the user clicks the already-selected choice, or
- // the fail() handler below reverts state to the existing
- // state, don't start an AJAX request.
- return;
- }
+ var want_persist = !toggle_group.find('button').hasClass('active');
+ var want_state = want_persist ? 'persistent' : 'cache';
+ toggle_group.find('button').
+ toggleClass('active', want_persist).
+ html(want_persist ? 'Persistent' : 'Cache');
$.ajax(toggle_group.attr('data-remote-href'),
{dataType: 'json',
type: 'POST',
data: {
- value: $(this).val()
+ value: want_state
},
context: {
toggle_group: toggle_group,
- input: this
+ want_state: want_state,
+ button: this
}
}).
done(function(data, status, jqxhr) {
var context = this;
- $(document).trigger('ajax:complete');
// Remove "danger" status in case a previous action failed
- $('label.btn-danger', context.toggle_group).
+ $('.btn-danger', context.toggle_group).
addClass('btn-info').
removeClass('btn-danger');
// Update last-saved-state
context.toggle_group.
- attr('data-persistent-state', $(context.input).val());
+ attr('data-persistent-state', context.want_state);
}).
fail(function(jqxhr, status, error) {
var context = this;
- $(document).trigger('ajax:complete');
+ var saved_state;
// Add a visual indication that something failed
- $('label.btn', context.toggle_group).
+ $(context.button).
addClass('btn-danger').
removeClass('btn-info');
- // Select the button reflecting the last-saved-state
- $('label.btn input[value=' +
- context.toggle_group.attr('data-persistent-state') +
- ']', context.toggle_group).
- button('toggle');
- if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
+ // Change to the last-saved-state
+ saved_state = context.toggle_group.attr('data-persistent-state');
+ $(context.button).
+ toggleClass('active', saved_state == 'persistent').
+ html(saved_state == 'persistent' ? 'Persistent' : 'Cache');
+
+ if (jqxhr.readyState == 0 || jqxhr.status == 0) {
+ // Request cancelled due to page reload.
+ // Displaying an alert would be rather annoying.
+ } else if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
window.alert("Request failed: " +
jqxhr.responseJSON.errors.join("; "));
} else {
window.alert("Request failed.");
}
});
- $(document).trigger('ajax:send');
});
});