1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 $(document).on('click', '.selectable', function() {
8 var $container = $(this).closest('.selectable-container');
9 if (!$container.hasClass('multiple')) {
12 removeClass('active');
14 $this.toggleClass('active');
16 if (!$this.hasClass('use-preview-selection')) {
18 find('.selectable.active').length > 0)
21 if (!$container.hasClass('preview-selectable-container')) {
24 find('[data-enable-if-selection]').
25 prop('disabled', !any);
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);
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'),
36 done(function(data, status, jqxhr) {
37 $(".modal-dialog-preview-pane").html(data);
39 fail(function(data, status, jqxhr) {
40 $(".modal-dialog-preview-pane").html(no_preview_available);
45 find('.preview-selectable.active').length > 0)
48 find('[data-enable-if-selection]').
49 prop('disabled', !any);
52 }).on('click', '.modal button[data-action-href]', function() {
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();
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')});
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});
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]});
80 data.push({name: key, value: value});
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});
91 $.ajax($(this).attr('data-action-href'),
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("; ");
103 message = "Request failed.";
105 this.modal.find('.modal-error').
106 html('<div class="alert alert-danger"></div>').
108 children().text(message);
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]);
116 }).on('click', '.chooser-show-project', function() {
118 var project_uuid = $(this).attr('data-project-uuid');
119 $(this).attr('href', '#'); // Skip normal click handler
121 params = {'filters': [['owner_uuid',
124 'project_uuid': project_uuid
127 $(".modal-dialog-preview-pane").html("");
128 // Use current selection as dropdown button label
130 closest('.dropdown-menu').
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();
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.
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');
155 if ($form.find('a[data-remote]').length > 0) {
156 // A search dialog is already loading.
160 attr('data-remote-href', $form.attr('data-search-modal')).
161 attr('data-remote', 'true').
162 attr('data-method', 'GET').
165 on('ajax:success', function(data, status, xhr) {
166 $('body > .modal-container input[type=text]').
167 val($form.find('input').val()).
169 $form.find('input').val('');
170 }).on('ajax:complete', function() {
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();