Merge branch 'master' into 3654-combine-selections
[arvados.git] / apps / workbench / app / assets / javascripts / selection.js
index 1e32c635642ef3fd9f109df931aebc23f8a8013b..bcc33c46bc1b1b67a355b96361b9b9ff30874697 100644 (file)
@@ -57,12 +57,6 @@ jQuery(function($){
         $("#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>';
-            if (this_object_uuid.match('-j7d0g-'))
-                html += '<li><button class="btn btn-xs btn-info" type="submit" name="copy_selections_into_folder" id="copy_selections_into_folder"><i class="fa fa-fw fa-folder-open"></i> Copy selections into this folder</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++) {
@@ -100,13 +94,11 @@ jQuery(function($){
 
         $('.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'));
@@ -116,7 +108,6 @@ jQuery(function($){
             }
         });
 
-
     $(window).on('load storage', update_count);
 
     $('#selection-form-content').on("click", function(e) {
@@ -178,3 +169,60 @@ select_form_sources = null;
         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;
+    }
+    $(this).
+        closest('.selection-action-container').
+        find(':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 $container = $(this).closest('.selection-action-container');
+    var $checked = $('.persistent-selection:checkbox:checked', $container);
+    $('[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));
+    $('[data-selection-action=copy]').
+        closest('li').
+        toggleClass('disabled',
+                    ($checked.filter('[value*=-j7d0g-]').length > 0) ||
+                    ($checked.length < 1));
+    $('[data-selection-action=combine-project-contents]').
+        closest('li').
+        toggleClass('disabled',
+                    ($checked.filter('[value*=-4zz18-]').length < 1) ||
+                    ($checked.length != $checked.filter('[value*=-4zz18-]').length));
+}
+
+$(document).
+    on('selections-updated ready ajax:complete', function() {
+        var $btn = $('[data-selection-action]');
+        $btn.click(dispatch_selection_action);
+        enable_disable_selection_actions.call($btn);
+    });