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