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