8784: Fix test for latest firefox.
[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     /* When the user clicks a selection action link, build a form to perform
15        the action on the selected data, and submit it.
16        This is based on handleMethod from rails-ujs, extended to add the
17        selections to the submitted form.
18        Copyright (c) 2007-2010 Contributors at http://github.com/rails/jquery-ujs/contributors
19        */
20     var $container = $(this);
21     if ($container.closest('.disabled').length) {
22         return false;
23     }
24     $container.closest('.dropdown-menu').dropdown('toggle');
25
26     var href = $container.data('href'),
27     method = $container.data('method') || 'GET',
28     paramName = $container.data('selection-param-name'),
29     csrfToken = $('meta[name=csrf-token]').attr('content'),
30     csrfParam = $('meta[name=csrf-param]').attr('content'),
31     form = $('<form method="post" action="' + href + '"></form>'),
32     metadataInput = ('<input name="_method" value="' + method +
33                      '" type="hidden" />');
34
35     if (csrfParam !== undefined && csrfToken !== undefined) {
36         metadataInput += ('<input type="hidden" name="' + csrfParam +
37                           '" value="' + csrfToken + '" />');
38     }
39     $container.
40         closest('.selection-action-container').
41         find(':checkbox:checked:visible').
42         each(function(index, elem) {
43             metadataInput += ('<input type="hidden" name="' + paramName +
44                               '" value="' + elem.value + '" />');
45         });
46
47     form.data('remote', $container.data('remote'));
48     form.hide().append(metadataInput).appendTo('body');
49     form.submit();
50     return false;
51 }
52
53 function enable_disable_selection_actions() {
54     var $container = $(this);
55     var $checked = $('.persistent-selection:checkbox:checked', $container);
56     var collection_lock_classes = $('.lock-collection-btn').attr('class')
57
58     $('[data-selection-action]', $container).
59         closest('div.btn-group-sm').
60         find('ul li').
61         toggleClass('disabled', ($checked.length == 0));
62     $('[data-selection-action=compare]', $container).
63         closest('li').
64         toggleClass('disabled',
65                     ($checked.filter('[value*=-d1hrv-]').length < 2) ||
66                     ($checked.not('[value*=-d1hrv-]').length > 0));
67     <% unless Group.copies_to_projects? %>
68         $('[data-selection-action=copy]', $container).
69             closest('li').
70             toggleClass('disabled',
71                         ($checked.filter('[value*=-j7d0g-]').length > 0) ||
72                         ($checked.length < 1));
73     <% end %>
74     $('[data-selection-action=combine-project-contents]', $container).
75         closest('li').
76         toggleClass('disabled',
77                     ($checked.filter('[value*=-4zz18-]').length < 1) ||
78                     ($checked.length != $checked.filter('[value*=-4zz18-]').length));
79     $('[data-selection-action=remove-selected-files]', $container).
80         closest('li').
81         toggleClass('disabled',
82                     ($checked.length < 0) ||
83                     !($checked.length > 0 && collection_lock_classes && collection_lock_classes.indexOf("fa-unlock") !=-1));
84     $('[data-selection-action=untrash-selected-items]', $container).
85         closest('li').
86         toggleClass('disabled',
87                     ($checked.length < 1));
88 }
89
90 $(document).
91     on('selections-updated', function() {
92         $('.selection-action-container').each(enable_disable_selection_actions);
93     }).
94     on('ready ajax:complete', function() {
95         $('[data-selection-action]').
96             off('click', dispatch_selection_action).
97             on('click', dispatch_selection_action);
98         $(this).trigger('selections-updated');
99     });
100
101 function select_all_items() {
102   $(".arv-selectable-items :checkbox").filter(":visible").prop("checked", true).trigger("change");
103 }
104
105 function unselect_all_items() {
106   $(".arv-selectable-items :checkbox").filter(":visible").prop("checked", false).trigger("change");
107 }