Add 'apps/arv-web/' from commit 'f9732ad8460d013c2f28363655d0d1b91894dca5'
[arvados.git] / apps / workbench / app / assets / javascripts / selection.js.erb
1 //= require jquery
2 //= require jquery_ujs
3
4 /** Javascript for selection. */
5
6 jQuery(function($){
7     $(document).
8         on('change', '.persistent-selection:checkbox', function(e) {
9             $(document).trigger('selections-updated');
10         });
11 });
12
13 function dispatch_selection_action() {
14     // Build a new "href" attribute for this link by starting with the
15     // "data-href" attribute and appending ?foo[]=bar&foo[]=baz (or
16     // &foo=... as appropriate) to reflect the current object
17     // selections.
18     var data = [];
19     var param_name = $(this).attr('data-selection-param-name');
20     var href = $(this).attr('data-href');
21     if ($(this).closest('.disabled').length > 0) {
22         return false;
23     }
24     $(this).
25         closest('.selection-action-container').
26         find(':checkbox:checked:visible').
27         each(function() {
28             data.push({name: param_name, value: $(this).val()});
29         });
30     if (href.indexOf('?') >= 0)
31         href += '&';
32     else
33         href += '?';
34     href += $.param(data, true);
35     $(this).attr('href', href);
36     return true;
37 }
38
39 function enable_disable_selection_actions() {
40     var $container = $(this);
41     var $checked = $('.persistent-selection:checkbox:checked', $container);
42     $('[data-selection-action]', $container).
43         closest('div.btn-group-sm').
44         find('ul li').
45         toggleClass('disabled', ($checked.length == 0));
46     $('[data-selection-action=compare]', $container).
47         closest('li').
48         toggleClass('disabled',
49                     ($checked.filter('[value*=-d1hrv-]').length < 2) ||
50                     ($checked.not('[value*=-d1hrv-]').length > 0));
51     <% unless Group.copies_to_projects? %>
52         $('[data-selection-action=copy]', $container).
53             closest('li').
54             toggleClass('disabled',
55                         ($checked.filter('[value*=-j7d0g-]').length > 0) ||
56                         ($checked.length < 1));
57     <% end %>
58     $('[data-selection-action=combine-project-contents]', $container).
59         closest('li').
60         toggleClass('disabled',
61                     ($checked.filter('[value*=-4zz18-]').length < 1) ||
62                     ($checked.length != $checked.filter('[value*=-4zz18-]').length));
63 }
64
65 $(document).
66     on('selections-updated', function() {
67         $('.selection-action-container').each(enable_disable_selection_actions);
68     }).
69     on('ready ajax:complete', function() {
70         $('[data-selection-action]').
71             off('click', dispatch_selection_action).
72             on('click', dispatch_selection_action);
73         $(this).trigger('selections-updated');
74     });