Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / apps / workbench / app / assets / javascripts / selection.js.erb
index 40724bb566b4f8903687edfc9e2e69bf331d4ff4..e8f21eefd592fe1dcf4567f20ef5dc6a20ea515a 100644 (file)
+<%# Copyright (C) The Arvados Authors. All rights reserved.
+
+SPDX-License-Identifier: AGPL-3.0 %>
+
 //= require jquery
 //= require jquery_ujs
 
-/** Javascript for local persistent selection. */
-
-get_selection_list = null;
-form_selection_sources = {};
+/** Javascript for selection. */
 
 jQuery(function($){
-    var storage = localStorage; // sessionStorage
-
-    get_selection_list = function() {
-        if (!storage.persistentSelection) {
-            storage.persistentSelection = JSON.stringify([]);
-        }
-        return JSON.parse(storage.persistentSelection);
-    }
-
-    var put_storage = function(lst) {
-        storage.persistentSelection = JSON.stringify(lst);
-    }
-
-    var add_selection = function(uuid, name, href, type) {
-        var lst = get_selection_list();
-        lst.push({"uuid": uuid, "name": name, "href": href, "type": type});
-        put_storage(lst);
-        update_count();
-    };
-
-    var remove_selection = function(uuid) {
-        var lst = get_selection_list();
-        for (var i = 0; i < lst.length; i++) {
-            if (lst[i].uuid == uuid) {
-                lst.splice(i, 1);
-                i--;
-            }
-        }
-        put_storage(lst);
-        update_count();
-    };
-
-    var remove_selection_click = function(e) {
-        remove_selection($(this).val());
-    };
-
-    var clear_selections = function() {
-        put_storage([]);
-        update_count();
-    }
-
-    var update_count = function(e) {
-        var html;
-        var this_object_uuid = $('#selection-form-content').
-            closest('form').
-            find('input[name=uuid]').val();
-        var lst = get_selection_list();
-        $("#persistent-selection-count").text(lst.length);
-        if (lst.length > 0) {
-            html = '<li><a href="#" class="btn btn-xs btn-info" id="clear_selections_button"><i class="fa fa-fw fa-ban"></i> Clear selections</a></li>';
-            $('#selection-form-content').html(html);
-
-            for (var i = 0; i < lst.length; i++) {
-                $('#selection-form-content > li > table').append("<tr>"
-                                                       + "<td>"
-                                                       + "<input class='remove-selection' name='selection[]' type='checkbox' value='" + lst[i].uuid + "' checked='true' data-stoppropagation='true' />"
-                                                       + "</td>"
-
-                                                       + "<td>"
-                                                       + "<div style='padding-left: 1em'><a href=\"" + lst[i].href + "\">" + lst[i].name + "</a></div>"
-                                                       + "</td>"
-
-                                                       + "<td style=\"vertical-align: top\">"
-                                                       + "<span style=\"padding-right: 1em\">" + lst[i].type + "</span>"
-                                                       + "</td>"
-
-                                                       + "</tr>");
-            }
-        } else {
-            $('#selection-form-content').html("<li class='notification empty'>No selections.</li>");
-        }
-
-        var checkboxes = $('.persistent-selection:checkbox');
-        for (i = 0; i < checkboxes.length; i++) {
-            for (var j = 0; j < lst.length; j++) {
-                if (lst[j].uuid == $(checkboxes[i]).val()) {
-                    checkboxes[i].checked = true;
-                    break;
-                }
-            }
-            if (j == lst.length) {
-                checkboxes[i].checked = false;
-            }
-        }
-
-        $('.remove-selection').on('click', remove_selection_click);
-        $('#clear_selections_button').on('click', clear_selections);
-        $(document).trigger('selections-updated', [lst]);
-    };
-
     $(document).
         on('change', '.persistent-selection:checkbox', function(e) {
-            var inc = 0;
-            if ($(this).is(":checked")) {
-                add_selection($(this).val(), $(this).attr('friendly_name'), $(this).attr('href'), $(this).attr('friendly_type'));
-            }
-            else {
-                remove_selection($(this).val());
-            }
+            $(document).trigger('selections-updated');
         });
-
-    $(window).on('load', clear_selections);
-    $(window).on('storage', update_count);
-
-    $('#selection-form-content').on("click", function(e) {
-        e.stopPropagation();
-    });
 });
 
-add_form_selection_sources = null;
-select_form_sources = null;
-
-(function() {
-    var form_selection_sources = {};
-    add_form_selection_sources = function (src) {
-        for (var i = 0; i < src.length; i++) {
-            var t = form_selection_sources[src[i].type];
-            if (!t) {
-                t = form_selection_sources[src[i].type] = {};
-            }
-            if (!t[src[i].uuid]) {
-                t[src[i].uuid] = src[i];
-            }
-        }
-    };
-
-    select_form_sources = function(type) {
-        var ret = [];
-
-        if (get_selection_list) {
-            var lst = get_selection_list();
-            if (lst.length > 0) {
-                var text = "&horbar; Selections &horbar;";
-                var span = document.createElement('span');
-                span.innerHTML = text;
-                ret.push({text: span.innerHTML, value: "***invalid***"});
-
-                for (var i = 0; i < lst.length; i++) {
-                    if (lst[i].type == type) {
-                        var n = lst[i].name;
-                        n = n.replace(/<span[^>]*>/i, "[");
-                        n = n.replace(/<\/span>/i, "]");
-                        ret.push({text: n, value: lst[i].uuid})
-                    }
-                }
-            }
-        }
-
-        var text = "&horbar; Recent &horbar;";
-        var span = document.createElement('span');
-        span.innerHTML = text;
-        ret.push({text: span.innerHTML, value: "***invalid***"});
-
-        var t = form_selection_sources[type];
-        for (var key in t) {
-            if (t.hasOwnProperty(key)) {
-                var obj = t[key];
-                ret.push({text: obj.name, value: obj.uuid})
-            }
-        }
-        return ret;
-    };
-})();
-
 function dispatch_selection_action() {
-    // Build a new "href" attribute for this link by starting with the
-    // "data-href" attribute and appending ?foo[]=bar&foo[]=baz (or
-    // &foo=... as appropriate) to reflect the current object
-    // selections.
-    var data = [];
-    var param_name = $(this).attr('data-selection-param-name');
-    var href = $(this).attr('data-href');
-    if ($(this).closest('.disabled').length > 0) {
-       return false;
+    /* When the user clicks a selection action link, build a form to perform
+       the action on the selected data, and submit it.
+       This is based on handleMethod from rails-ujs, extended to add the
+       selections to the submitted form.
+       Copyright (c) 2007-2010 Contributors at http://github.com/rails/jquery-ujs/contributors
+       */
+    var $container = $(this);
+    if ($container.closest('.disabled').length) {
+        return false;
+    }
+    $container.closest('.dropdown-menu').dropdown('toggle');
+
+    var href = $container.data('href'),
+    method = $container.data('method') || 'GET',
+    paramName = $container.data('selection-param-name'),
+    csrfToken = $('meta[name=csrf-token]').attr('content'),
+    csrfParam = $('meta[name=csrf-param]').attr('content'),
+    form = $('<form method="post" action="' + href + '"></form>'),
+    metadataInput = ('<input name="_method" value="' + method +
+                     '" type="hidden" />');
+
+    if (csrfParam !== undefined && csrfToken !== undefined) {
+        metadataInput += ('<input type="hidden" name="' + csrfParam +
+                          '" value="' + csrfToken + '" />');
     }
-    $(this).
+    $container.
         closest('.selection-action-container').
         find(':checkbox:checked:visible').
-        each(function() {
-            data.push({name: param_name, value: $(this).val()});
+        each(function(index, elem) {
+            metadataInput += ('<input type="hidden" name="' + paramName +
+                              '" value="' + elem.value + '" />');
         });
-    if (href.indexOf('?') >= 0)
-        href += '&';
-    else
-        href += '?';
-    href += $.param(data, true);
-    $(this).attr('href', href);
-    return true;
+
+    form.data('remote', $container.data('remote'));
+    form.hide().append(metadataInput).appendTo('body');
+    form.submit();
+    return false;
 }
 
 function enable_disable_selection_actions() {
-    var $container = $(this).closest('.selection-action-container');
+    var $container = $(this);
     var $checked = $('.persistent-selection:checkbox:checked', $container);
-    $('[data-selection-action]').
+    var collection_lock_classes = $('.lock-collection-btn').attr('class')
+
+    $('[data-selection-action]', $container).
         closest('div.btn-group-sm').
         find('ul li').
         toggleClass('disabled', ($checked.length == 0));
-    $('[data-selection-action=compare]').
+    $('[data-selection-action=compare]', $container).
         closest('li').
         toggleClass('disabled',
                     ($checked.filter('[value*=-d1hrv-]').length < 2) ||
                     ($checked.not('[value*=-d1hrv-]').length > 0));
     <% unless Group.copies_to_projects? %>
-        $('[data-selection-action=copy]').
+        $('[data-selection-action=copy]', $container).
             closest('li').
             toggleClass('disabled',
                         ($checked.filter('[value*=-j7d0g-]').length > 0) ||
                         ($checked.length < 1));
     <% end %>
-    $('[data-selection-action=combine-project-contents]').
+    $('[data-selection-action=combine-project-contents]', $container).
         closest('li').
         toggleClass('disabled',
                     ($checked.filter('[value*=-4zz18-]').length < 1) ||
                     ($checked.length != $checked.filter('[value*=-4zz18-]').length));
+    $('[data-selection-action=remove-selected-files]', $container).
+        closest('li').
+        toggleClass('disabled',
+                    ($checked.length < 0) ||
+                    !($checked.length > 0 && collection_lock_classes && collection_lock_classes.indexOf("fa-unlock") !=-1));
+    $('[data-selection-action=untrash-selected-items]', $container).
+        closest('li').
+        toggleClass('disabled',
+                    ($checked.length < 1));
 }
 
 $(document).
-    on('selections-updated ready ajax:complete', function() {
-        var $btn = $('[data-selection-action]');
-        $btn.click(dispatch_selection_action);
-        enable_disable_selection_actions.call($btn);
+    on('selections-updated', function() {
+        $('.selection-action-container').each(enable_disable_selection_actions);
+    }).
+    on('ready ajax:complete', function() {
+        $('[data-selection-action]').
+            off('click', dispatch_selection_action).
+            on('click', dispatch_selection_action);
+        $(this).trigger('selections-updated');
     });
+
+function select_all_items() {
+  $(".arv-selectable-items :checkbox").filter(":visible").prop("checked", true).trigger("change");
+}
+
+function unselect_all_items() {
+  $(".arv-selectable-items :checkbox").filter(":visible").prop("checked", false).trigger("change");
+}