Merge branch '19954-permission-dedup-doc'
[arvados.git] / apps / workbench / app / assets / javascripts / select_modal.js
index d84037763c77b8a33e0478511c0e92c4f8e84170..19cf3cd927f04d5b7c22f41fba15d5a86c83af89 100644 (file)
@@ -1,3 +1,7 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
 $(document).on('click', '.selectable', function() {
     var any;
     var $this = $(this);
@@ -9,16 +13,18 @@ $(document).on('click', '.selectable', function() {
     }
     $this.toggleClass('active');
 
-    if (!$this.hasClass('use-checkbox-selection')) {
+    if (!$this.hasClass('use-preview-selection')) {
       any = ($container.
            find('.selectable.active').length > 0)
     }
-    $this.
+
+    if (!$container.hasClass('preview-selectable-container')) {
+      $this.
         closest('.modal').
         find('[data-enable-if-selection]').
         prop('disabled', !any);
 
-    if ($this.hasClass('active')) {
+      if ($this.hasClass('active')) {
         var no_preview_available = '<div class="spinner-h-center spinner-v-center"><center>(No preview available)</center></div>';
         if (!$this.attr('data-preview-href')) {
             $(".modal-dialog-preview-pane").html(no_preview_available);
@@ -33,38 +39,32 @@ $(document).on('click', '.selectable', function() {
             fail(function(data, status, jqxhr) {
                 $(".modal-dialog-preview-pane").html(no_preview_available);
             });
-    }
-}).on('click', '.persistent-selection', function() {
-    var checked_status = this.checked;
-    var $modal = $(this).closest('.modal');
-    $checked_selections = $modal.find('.persistent-selection:checked');
-
-    if (checked_status && ($checked_selections.length > 1)) {
-      $(this).prop('checked', false);
+      }
+    } else {
+      any = ($container.
+           find('.preview-selectable.active').length > 0)
+      $(this).
+          closest('.modal').
+          find('[data-enable-if-selection]').
+          prop('disabled', !any);
     }
 
-    any = ($checked_selections.length > 0);
-    $(this).
-        closest('.modal').
-        find('[data-enable-if-selection]').
-        prop('disabled', !any);
 }).on('click', '.modal button[data-action-href]', function() {
     var selection = [];
     var data = [];
     var $modal = $(this).closest('.modal');
+    var http_method = $(this).attr('data-method').toUpperCase();
     var action_data = $(this).data('action-data');
     var action_data_from_params = $(this).data('action-data-from-params');
     var selection_param = action_data.selection_param;
     $modal.find('.modal-error').removeClass('hide').hide();
 
-    $checked_selections = $modal.find('.persistent-selection:checked');
-    if ($checked_selections) {
-      $checked_selections.each(function() {
-          data.push({name: selection_param, value: $(this).attr('value')});
-      });
+    var $preview_selections = $modal.find('.preview-selectable.active');
+    if ($preview_selections.length > 0) {
+      data.push({name: selection_param, value: $preview_selections.first().attr('href')});
     }
 
-    if (data.length == 0) {   // no checked persistent selection
+    if (data.length == 0) {   // not using preview selection option
       $modal.find('.selectable.active[data-object-uuid]').each(function() {
         var val = $(this).attr('data-object-uuid');
         data.push({name: selection_param, value: val});
@@ -80,9 +80,17 @@ $(document).on('click', '.selectable', function() {
                    data.push({name: key, value: value});
                }
            });
+    if (http_method === 'PATCH') {
+        // Some user agents do not support HTTP PATCH (notably,
+        // phantomjs silently ignores our "data" and sends an empty
+        // request body) so we use POST instead, and supply a
+        // _method=PATCH param to tell Rails what we really want.
+        data.push({name: '_method', value: http_method});
+        http_method = 'POST';
+    }
     $.ajax($(this).attr('data-action-href'),
            {dataType: 'json',
-            type: $(this).attr('data-method'),
+            type: http_method,
             data: data,
             traditional: false,
             context: {modal: $modal, action_data: action_data}}).
@@ -95,8 +103,9 @@ $(document).on('click', '.selectable', function() {
                 message = "Request failed.";
             }
             this.modal.find('.modal-error').
-                html('<div class="alert alert-danger">' + message + '</div>').
-                show();
+                html('<div class="alert alert-danger"></div>').
+                show().
+                children().text(message);
         }).
         done(function(data, status, jqxhr) {
             var event_name = this.action_data.success;
@@ -115,6 +124,7 @@ $(document).on('click', '.selectable', function() {
                   'project_uuid': project_uuid
                  };
     }
+    $(".modal-dialog-preview-pane").html("");
     // Use current selection as dropdown button label
     $(this).
         closest('.dropdown-menu').
@@ -147,7 +157,7 @@ $(document).on('click', '.selectable', function() {
             return false;
         }
         $('<a />').
-            attr('href', $form.attr('data-search-modal')).
+            attr('data-remote-href', $form.attr('data-search-modal')).
             attr('data-remote', 'true').
             attr('data-method', 'GET').
             hide().