2872: Fix bugs, tweak formatting
[arvados.git] / apps / workbench / app / assets / javascripts / selection.js
index 5d0900aa768972f48728c486e19f6e98d9ac037a..a313c8b78e17da310ce011745b59599ef92c51d4 100644 (file)
@@ -3,27 +3,32 @@
 
 /** Javascript for local persistent selection. */
 
-(function($){
-    var get_storage = function() {
-        if (!sessionStorage.persistentSelection) {
-            sessionStorage.persistentSelection = JSON.stringify([]);
+get_selection_list = null;
+form_selection_sources = {};
+
+jQuery(function($){
+    var storage = localStorage; // sessionStorage
+
+    get_selection_list = function() {
+        if (!storage.persistentSelection) {
+            storage.persistentSelection = JSON.stringify([]);
         }
-        return JSON.parse(sessionStorage.persistentSelection);
+        return JSON.parse(storage.persistentSelection);
     }
 
     var put_storage = function(lst) {
-        sessionStorage.persistentSelection = JSON.stringify(lst);
+        storage.persistentSelection = JSON.stringify(lst);
     }
 
     var add_selection = function(uuid, name, href, type) {
-        var lst = get_storage();
+        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_storage();
+        var lst = get_selection_list();
         for (var i = 0; i < lst.length; i++) {
             if (lst[i].uuid == uuid) {
                 lst.splice(i, 1);
     };
 
     var remove_selection_click = function(e) {
-        remove_selection($(this).attr('name'));
+        remove_selection($(this).val());
     };
 
+    var clear_selections = function() {
+        put_storage([]);
+        update_count();
+    }
+
     var update_count = function(e) {
-        var lst = get_storage();
+        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);
-
-        $('#persistent-selection-list > li > table').empty();
         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>';
+            if (this_object_uuid.match('-j7d0g-')) {
+                html += '<li><button class="btn btn-xs btn-info" type="submit" name="copy_selections_into_project" id="copy_selections_into_project"><i class="fa fa-fw fa-copy"></i> Copy selections into this project</button></li>';
+                html += '<li><button class="btn btn-xs btn-info" type="submit" name="move_selections_into_project" id="move_selections_into_project"><i class="fa fa-fw fa-truck"></i> Move selections into this project</button></li>';
+           }
+            html += '<li><button class="btn btn-xs btn-info" type="submit" name="combine_selected_files_into_collection" '
+                + ' id="combine_selected_files_into_collection">'
+                + '<i class="fa fa-fw fa-archive"></i> Combine selected collections and files into a new collection</button></li>'
+                + '<li class="notification"><table style="width: 100%"></table></li>';
+            $('#selection-form-content').html(html);
+
             for (var i = 0; i < lst.length; i++) {
-                $('#persistent-selection-list > li > table').append("<tr>"
-                                                       + "<td style=\"vertical-align: top\">"
-                                                       + "<span style=\"padding-right: 1em\">" + lst[i].type + "</span>"
+                $('#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>"
-                                                       + "<span><a href=\"" + lst[i].href + "\">" + lst[i].name + "</a></span>"
+                                                       + "<div style='padding-left: 1em'><a href=\"" + lst[i].href + "\">" + lst[i].name + "</a></div>"
                                                        + "</td>"
 
-                                                       + "<td>"
-                                                       + "<a href=\"#\" class=\"remove-selection\" name=\"" + lst[i].uuid + "\">" 
-                                                       + "<span class=\"glyphicon glyphicon-trash pull-right\"></span>"
-                                                       + "</a></span>"
+                                                       + "<td style=\"vertical-align: top\">"
+                                                       + "<span style=\"padding-right: 1em\">" + lst[i].type + "</span>"
                                                        + "</td>"
+
                                                        + "</tr>");
             }
         } else {
-            $('#persistent-selection-list > li > table').html("<tr><td>No selections.</td></tr>");
+            $('#selection-form-content').html("<li class='notification empty'>No selections.</li>");
         }
 
         var checkboxes = $('.persistent-selection:checkbox');
                 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) {
-            //console.log($(this));
-            //console.log($(this).val());
-            
             var inc = 0;
             if ($(this).is(":checked")) {
                 add_selection($(this).val(), $(this).attr('friendly_name'), $(this).attr('href'), $(this).attr('friendly_type'));
             }
         });
 
-
     $(window).on('load storage', update_count);
-})(jQuery);
\ No newline at end of file
+
+    $('#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;
+    }
+    $('.persistent-selection:checkbox:checked').each(function() {
+        data.push({name: param_name, value: $(this).val()});
+    });
+    if (href.indexOf('?') >= 0)
+        href += '&';
+    else
+        href += '?';
+    href += $.param(data, true);
+    $(this).attr('href', href);
+    return true;
+}
+
+function enable_disable_selection_actions() {
+    var $checked = $('.persistent-selection:checkbox:checked');
+    $('[data-selection-action]').
+        closest('div.btn-group-sm').
+        find('ul li').
+        toggleClass('disabled', ($checked.length == 0));
+    $('[data-selection-action=compare]').
+        closest('li').
+        toggleClass('disabled',
+                    ($checked.filter('[value*=-d1hrv-]').length < 2) ||
+                    ($checked.not('[value*=-d1hrv-]').length > 0));
+}
+
+$(document).
+    on('selections-updated ready ajax:complete', function() {
+        $('[data-selection-action]').click(dispatch_selection_action);
+        enable_disable_selection_actions();
+    });