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