Merge branch 'master' into 9369-arv-cwl-docs
[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     $('[data-selection-action]', $container).
57         closest('div.btn-group-sm').
58         find('ul li').
59         toggleClass('disabled', ($checked.length == 0));
60     $('[data-selection-action=compare]', $container).
61         closest('li').
62         toggleClass('disabled',
63                     ($checked.filter('[value*=-d1hrv-]').length < 2) ||
64                     ($checked.not('[value*=-d1hrv-]').length > 0));
65     <% unless Group.copies_to_projects? %>
66         $('[data-selection-action=copy]', $container).
67             closest('li').
68             toggleClass('disabled',
69                         ($checked.filter('[value*=-j7d0g-]').length > 0) ||
70                         ($checked.length < 1));
71     <% end %>
72     $('[data-selection-action=combine-project-contents]', $container).
73         closest('li').
74         toggleClass('disabled',
75                     ($checked.filter('[value*=-4zz18-]').length < 1) ||
76                     ($checked.length != $checked.filter('[value*=-4zz18-]').length));
77 }
78
79 $(document).
80     on('selections-updated', function() {
81         $('.selection-action-container').each(enable_disable_selection_actions);
82     }).
83     on('ready ajax:complete', function() {
84         $('[data-selection-action]').
85             off('click', dispatch_selection_action).
86             on('click', dispatch_selection_action);
87         $(this).trigger('selections-updated');
88     });
89
90 function select_all_items() {
91   $(".arv-selectable-items :checkbox").filter(":visible").prop("checked", true).trigger("change");
92 }
93
94 function unselect_all_items() {
95   $(".arv-selectable-items :checkbox").filter(":visible").prop("checked", false).trigger("change");
96 }