8784: Fix test for latest firefox.
[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"></div>').
103                 show().
104                 children().text(message);
105         }).
106         done(function(data, status, jqxhr) {
107             var event_name = this.action_data.success;
108             this.modal.find('.modal-error').hide();
109             $(document).trigger(event_name!=null ? event_name : 'page-refresh',
110                                 [data, status, jqxhr, this.action_data]);
111         });
112 }).on('click', '.chooser-show-project', function() {
113     var params = {};
114     var project_uuid = $(this).attr('data-project-uuid');
115     $(this).attr('href', '#');  // Skip normal click handler
116     if (project_uuid) {
117         params = {'filters': [['owner_uuid',
118                                '=',
119                                project_uuid]],
120                   'project_uuid': project_uuid
121                  };
122     }
123     $(".modal-dialog-preview-pane").html("");
124     // Use current selection as dropdown button label
125     $(this).
126         closest('.dropdown-menu').
127         prev('button').
128         html($(this).text() + ' <span class="caret"></span>');
129     // Set (or unset) filter params and refresh filterable rows
130     $($(this).closest('[data-filterable-target]').attr('data-filterable-target')).
131         data('infinite-content-params-from-project-dropdown', params).
132         trigger('refresh-content');
133 }).on('ready', function() {
134     $('form[data-search-modal] a').on('click', function() {
135         $(this).closest('form').submit();
136         return false;
137     });
138     $('form[data-search-modal]').on('submit', function() {
139         // Ask the server for a Search modal. When it arrives, copy
140         // the search string from the top nav input into the modal's
141         // search query field.
142         var $form = $(this);
143         var searchq = $form.find('input').val();
144         var is_a_uuid = /^([0-9a-f]{32}(\+\S+)?|[0-9a-z]{5}-[0-9a-z]{5}-[0-9a-z]{15})$/;
145         if (searchq.trim().match(is_a_uuid)) {
146             window.location = '/actions?uuid=' + encodeURIComponent(searchq.trim());
147             // Show the "loading" indicator. TODO: better page transition hook
148             $(document).trigger('ajax:send');
149             return false;
150         }
151         if ($form.find('a[data-remote]').length > 0) {
152             // A search dialog is already loading.
153             return false;
154         }
155         $('<a />').
156             attr('data-remote-href', $form.attr('data-search-modal')).
157             attr('data-remote', 'true').
158             attr('data-method', 'GET').
159             hide().
160             appendTo($form).
161             on('ajax:success', function(data, status, xhr) {
162                 $('body > .modal-container input[type=text]').
163                     val($form.find('input').val()).
164                     focus();
165                 $form.find('input').val('');
166             }).on('ajax:complete', function() {
167                 $(this).detach();
168             }).
169             click();
170         return false;
171     });
172 }).on('page-refresh', function(event, data, status, jqxhr, action_data) {
173     window.location.reload();
174 }).on('tab-refresh', function(event, data, status, jqxhr, action_data) {
175     $(document).trigger('arv:pane:reload:all');
176     $('body > .modal-container .modal').modal('hide');
177 }).on('redirect-to-created-object', function(event, data, status, jqxhr, action_data) {
178     window.location.href = data.href.replace(/^[^\/]*\/\/[^\/]*/, '');
179 }).on('shown.bs.modal', 'body > .modal-container .modal', function() {
180     $('.focus-on-display', this).focus();
181 });