Merge branch 'master' into 3118-docker-fixes
[arvados.git] / apps / workbench / app / assets / javascripts / select_modal.js
index 85d97c998222a5b9449884b2653d26b59bd1fdd2..0a58213eb9ffdea8d16efca470d59e1cc796a294 100644 (file)
@@ -1,4 +1,5 @@
 $(document).on('click', '.selectable', function() {
+    var any;
     var $this = $(this);
     if (!$this.hasClass('multiple')) {
         $this.closest('.selectable-container').
@@ -6,20 +7,46 @@ $(document).on('click', '.selectable', function() {
             removeClass('active');
     }
     $this.toggleClass('active');
+    any = ($this.
+           closest('.selectable-container').
+           find('.selectable.active').length > 0)
+    $this.
+        closest('.modal').
+        find('[data-enable-if-selection]').
+        prop('disabled', !any);
+
+    if ($this.hasClass('active')) {
+        $(".modal-dialog-preview-pane").html('<div class="spinner spinner-32px spinner-h-center spinner-v-center"></div>');
+        $.ajax($this.attr('data-preview-href'),
+               {dataType: "html"}).
+           done(function(data, status, jqxhr) {
+                $(".modal-dialog-preview-pane").html(data);
+            }).
+            fail(function(data, status, jqxhr) {
+                $(".modal-dialog-preview-pane").text('Preview load failed.');
+            });
+    }
+
 }).on('click', '.modal button[data-action-href]', function() {
     var selection = [];
-    var data = {};
+    var data = [];
     var $modal = $(this).closest('.modal');
+    var action_data = $(this).data('action-data');
+    var selection_param = action_data.selection_param;
     $modal.find('.modal-error').removeClass('hide').hide();
     $modal.find('.selectable.active[data-object-uuid]').each(function() {
-        selection.push($(this).attr('data-object-uuid'));
+        var val = $(this).attr('data-object-uuid');
+        data.push({name: selection_param, value: val});
+    });
+    $.each(action_data, function(key, value) {
+        data.push({name: key, value: value});
     });
-    data[$(this).data('action-data').selection_param] = selection[0];
     $.ajax($(this).attr('data-action-href'),
            {dataType: 'json',
             type: $(this).attr('data-method'),
             data: data,
-            context: {modal: $modal}}).
+            traditional: true,
+            context: {modal: $modal, action_data: action_data}}).
         fail(function(jqxhr, status, error) {
             if (jqxhr.readyState == 0 || jqxhr.status == 0) {
                 message = "Cancelled."
@@ -32,8 +59,15 @@ $(document).on('click', '.selectable', function() {
                 html('<div class="alert alert-danger">' + message + '</div>').
                 show();
         }).
-        success(function() {
+        done(function(data, status, jqxhr) {
+            var event_name = this.action_data.success;
             this.modal.find('.modal-error').hide();
-            window.location.reload();
+            $(document).trigger(event_name!=null ? event_name : 'page-refresh',
+                                [data, status, jqxhr, this.action_data]);
         });
 });
+$(document).on('page-refresh', function(event, data, status, jqxhr, action_data) {
+    window.location.reload();
+}).on('redirect-to-created-object', function(event, data, status, jqxhr, action_data) {
+    window.location.href = data.href.replace(/^[^\/]*\/\/[^\/]*/, '');
+});