1 $(document).on('click', '.selectable', function() {
4 var $container = $(this).closest('.selectable-container');
5 if (!$container.hasClass('multiple')) {
10 $this.toggleClass('active');
12 if (!$this.hasClass('use-preview-selection')) {
14 find('.selectable.active').length > 0)
17 if (!$container.hasClass('preview-selectable-container')) {
20 find('[data-enable-if-selection]').
21 prop('disabled', !any);
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);
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'),
32 done(function(data, status, jqxhr) {
33 $(".modal-dialog-preview-pane").html(data);
35 fail(function(data, status, jqxhr) {
36 $(".modal-dialog-preview-pane").html(no_preview_available);
41 find('.preview-selectable.active').length > 0)
44 find('[data-enable-if-selection]').
45 prop('disabled', !any);
48 }).on('click', '.modal button[data-action-href]', function() {
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();
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')});
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});
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]});
76 data.push({name: key, value: value});
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});
87 $.ajax($(this).attr('data-action-href'),
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("; ");
99 message = "Request failed.";
101 this.modal.find('.modal-error').
102 html('<div class="alert alert-danger">' + message + '</div>').
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]);
111 }).on('click', '.chooser-show-project', function() {
113 var project_uuid = $(this).attr('data-project-uuid');
114 $(this).attr('href', '#'); // Skip normal click handler
116 params = {'filters': [['owner_uuid',
119 'project_uuid': project_uuid
122 // Use current selection as dropdown button label
124 closest('.dropdown-menu').
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();
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.
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');
149 if ($form.find('a[data-remote]').length > 0) {
150 // A search dialog is already loading.
154 attr('href', $form.attr('data-search-modal')).
155 attr('data-remote', 'true').
156 attr('data-method', 'GET').
159 on('ajax:success', function(data, status, xhr) {
160 $('body > .modal-container input[type=text]').
161 val($form.find('input').val()).
163 $form.find('input').val('');
164 }).on('ajax:complete', function() {
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();