11454: Do federated logins by requesting the current user object
[arvados.git] / apps / workbench / app / assets / javascripts / select_modal.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 $(document).on('click', '.selectable', function() {
6     var any;
7     var $this = $(this);
8     var $container = $(this).closest('.selectable-container');
9     if (!$container.hasClass('multiple')) {
10         $container.
11             find('.selectable').
12             removeClass('active');
13     }
14     $this.toggleClass('active');
15
16     if (!$this.hasClass('use-preview-selection')) {
17       any = ($container.
18            find('.selectable.active').length > 0)
19     }
20
21     if (!$container.hasClass('preview-selectable-container')) {
22       $this.
23         closest('.modal').
24         find('[data-enable-if-selection]').
25         prop('disabled', !any);
26
27       if ($this.hasClass('active')) {
28         var no_preview_available = '<div class="spinner-h-center spinner-v-center"><center>(No preview available)</center></div>';
29         if (!$this.attr('data-preview-href')) {
30             $(".modal-dialog-preview-pane").html(no_preview_available);
31             return;
32         }
33         $(".modal-dialog-preview-pane").html('<div class="spinner spinner-32px spinner-h-center spinner-v-center"></div>');
34         $.ajax($this.attr('data-preview-href'),
35                {dataType: "html"}).
36             done(function(data, status, jqxhr) {
37                 $(".modal-dialog-preview-pane").html(data);
38             }).
39             fail(function(data, status, jqxhr) {
40                 $(".modal-dialog-preview-pane").html(no_preview_available);
41             });
42       }
43     } else {
44       any = ($container.
45            find('.preview-selectable.active').length > 0)
46       $(this).
47           closest('.modal').
48           find('[data-enable-if-selection]').
49           prop('disabled', !any);
50     }
51
52 }).on('click', '.modal button[data-action-href]', function() {
53     var selection = [];
54     var data = [];
55     var $modal = $(this).closest('.modal');
56     var http_method = $(this).attr('data-method').toUpperCase();
57     var action_data = $(this).data('action-data');
58     var action_data_from_params = $(this).data('action-data-from-params');
59     var selection_param = action_data.selection_param;
60     $modal.find('.modal-error').removeClass('hide').hide();
61
62     var $preview_selections = $modal.find('.preview-selectable.active');
63     if ($preview_selections.length > 0) {
64       data.push({name: selection_param, value: $preview_selections.first().attr('href')});
65     }
66
67     if (data.length == 0) {   // not using preview selection option
68       $modal.find('.selectable.active[data-object-uuid]').each(function() {
69         var val = $(this).attr('data-object-uuid');
70         data.push({name: selection_param, value: val});
71       });
72     }
73     $.each($.extend({}, action_data, action_data_from_params),
74            function(key, value) {
75                if (value instanceof Array && key[-1] != ']') {
76                    for (var i in value) {
77                        data.push({name: key + '[]', value: value[i]});
78                    }
79                } else {
80                    data.push({name: key, value: value});
81                }
82            });
83     if (http_method === 'PATCH') {
84         // Some user agents do not support HTTP PATCH (notably,
85         // phantomjs silently ignores our "data" and sends an empty
86         // request body) so we use POST instead, and supply a
87         // _method=PATCH param to tell Rails what we really want.
88         data.push({name: '_method', value: http_method});
89         http_method = 'POST';
90     }
91     $.ajax($(this).attr('data-action-href'),
92            {dataType: 'json',
93             type: http_method,
94             data: data,
95             traditional: false,
96             context: {modal: $modal, action_data: action_data}}).
97         fail(function(jqxhr, status, error) {
98             if (jqxhr.readyState == 0 || jqxhr.status == 0) {
99                 message = "Cancelled."
100             } else if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
101                 message = jqxhr.responseJSON.errors.join("; ");
102             } else {
103                 message = "Request failed.";
104             }
105             this.modal.find('.modal-error').
106                 html('<div class="alert alert-danger"></div>').
107                 show().
108                 children().text(message);
109         }).
110         done(function(data, status, jqxhr) {
111             var event_name = this.action_data.success;
112             this.modal.find('.modal-error').hide();
113             $(document).trigger(event_name!=null ? event_name : 'page-refresh',
114                                 [data, status, jqxhr, this.action_data]);
115         });
116 }).on('click', '.chooser-show-project', function() {
117     var params = {};
118     var project_uuid = $(this).attr('data-project-uuid');
119     $(this).attr('href', '#');  // Skip normal click handler
120     if (project_uuid) {
121         params = {'filters': [['owner_uuid',
122                                '=',
123                                project_uuid]],
124                   'project_uuid': project_uuid
125                  };
126     }
127     $(".modal-dialog-preview-pane").html("");
128     // Use current selection as dropdown button label
129     $(this).
130         closest('.dropdown-menu').
131         prev('button').
132         html($(this).text() + ' <span class="caret"></span>');
133     // Set (or unset) filter params and refresh filterable rows
134     $($(this).closest('[data-filterable-target]').attr('data-filterable-target')).
135         data('infinite-content-params-from-project-dropdown', params).
136         trigger('refresh-content');
137 }).on('ready', function() {
138     $('form[data-search-modal] a').on('click', function() {
139         $(this).closest('form').submit();
140         return false;
141     });
142     $('form[data-search-modal]').on('submit', function() {
143         // Ask the server for a Search modal. When it arrives, copy
144         // the search string from the top nav input into the modal's
145         // search query field.
146         var $form = $(this);
147         var searchq = $form.find('input').val();
148         var is_a_uuid = /^([0-9a-f]{32}(\+\S+)?|[0-9a-z]{5}-[0-9a-z]{5}-[0-9a-z]{15})$/;
149         if (searchq.trim().match(is_a_uuid)) {
150             window.location = '/actions?uuid=' + encodeURIComponent(searchq.trim());
151             // Show the "loading" indicator. TODO: better page transition hook
152             $(document).trigger('ajax:send');
153             return false;
154         }
155         if ($form.find('a[data-remote]').length > 0) {
156             // A search dialog is already loading.
157             return false;
158         }
159         $('<a />').
160             attr('data-remote-href', $form.attr('data-search-modal')).
161             attr('data-remote', 'true').
162             attr('data-method', 'GET').
163             hide().
164             appendTo($form).
165             on('ajax:success', function(data, status, xhr) {
166                 $('body > .modal-container input[type=text]').
167                     val($form.find('input').val()).
168                     focus();
169                 $form.find('input').val('');
170             }).on('ajax:complete', function() {
171                 $(this).detach();
172             }).
173             click();
174         return false;
175     });
176 }).on('page-refresh', function(event, data, status, jqxhr, action_data) {
177     window.location.reload();
178 }).on('tab-refresh', function(event, data, status, jqxhr, action_data) {
179     $(document).trigger('arv:pane:reload:all');
180     $('body > .modal-container .modal').modal('hide');
181 }).on('redirect-to-created-object', function(event, data, status, jqxhr, action_data) {
182     window.location.href = data.href.replace(/^[^\/]*\/\/[^\/]*/, '');
183 }).on('shown.bs.modal', 'body > .modal-container .modal', function() {
184     $('.focus-on-display', this).focus();
185 });