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-checkbox-selection')) {
14 find('.selectable.active').length > 0)
18 find('[data-enable-if-selection]').
19 prop('disabled', !any);
21 if ($this.hasClass('active')) {
22 var no_preview_available = '<div class="spinner-h-center spinner-v-center"><center>(No preview available)</center></div>';
23 if (!$this.attr('data-preview-href')) {
24 $(".modal-dialog-preview-pane").html(no_preview_available);
27 $(".modal-dialog-preview-pane").html('<div class="spinner spinner-32px spinner-h-center spinner-v-center"></div>');
28 $.ajax($this.attr('data-preview-href'),
30 done(function(data, status, jqxhr) {
31 $(".modal-dialog-preview-pane").html(data);
33 fail(function(data, status, jqxhr) {
34 $(".modal-dialog-preview-pane").html(no_preview_available);
37 }).on('click', '.persistent-selection', function() {
38 var checked_status = this.checked;
39 var $modal = $(this).closest('.modal');
40 $checked_selections = $modal.find('.persistent-selection:checked');
42 if (checked_status && ($checked_selections.length > 1)) {
43 $(this).prop('checked', false);
46 any = ($checked_selections.length > 0);
49 find('[data-enable-if-selection]').
50 prop('disabled', !any);
51 }).on('click', '.modal button[data-action-href]', function() {
54 var $modal = $(this).closest('.modal');
55 var action_data = $(this).data('action-data');
56 var action_data_from_params = $(this).data('action-data-from-params');
57 var selection_param = action_data.selection_param;
58 $modal.find('.modal-error').removeClass('hide').hide();
60 $checked_selections = $modal.find('.persistent-selection:checked');
61 if ($checked_selections) {
62 $checked_selections.each(function() {
63 data.push({name: selection_param, value: $(this).attr('value')});
67 if (data.length == 0) { // no checked persistent selection
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 $.ajax($(this).attr('data-action-href'),
85 type: $(this).attr('data-method'),
88 context: {modal: $modal, action_data: action_data}}).
89 fail(function(jqxhr, status, error) {
90 if (jqxhr.readyState == 0 || jqxhr.status == 0) {
91 message = "Cancelled."
92 } else if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
93 message = jqxhr.responseJSON.errors.join("; ");
95 message = "Request failed.";
97 this.modal.find('.modal-error').
98 html('<div class="alert alert-danger">' + message + '</div>').
101 done(function(data, status, jqxhr) {
102 var event_name = this.action_data.success;
103 this.modal.find('.modal-error').hide();
104 $(document).trigger(event_name!=null ? event_name : 'page-refresh',
105 [data, status, jqxhr, this.action_data]);
107 }).on('click', '.chooser-show-project', function() {
109 var project_uuid = $(this).attr('data-project-uuid');
110 $(this).attr('href', '#'); // Skip normal click handler
112 params = {'filters': [['owner_uuid',
115 'project_uuid': project_uuid
118 // Use current selection as dropdown button label
120 closest('.dropdown-menu').
122 html($(this).text() + ' <span class="caret"></span>');
123 // Set (or unset) filter params and refresh filterable rows
124 $($(this).closest('[data-filterable-target]').attr('data-filterable-target')).
125 data('infinite-content-params-from-project-dropdown', params).
126 trigger('refresh-content');
127 }).on('ready', function() {
128 $('form[data-search-modal] a').on('click', function() {
129 $(this).closest('form').submit();
132 $('form[data-search-modal]').on('submit', function() {
133 // Ask the server for a Search modal. When it arrives, copy
134 // the search string from the top nav input into the modal's
135 // search query field.
137 var searchq = $form.find('input').val();
138 var is_a_uuid = /^([0-9a-f]{32}(\+\S+)?|[0-9a-z]{5}-[0-9a-z]{5}-[0-9a-z]{15})$/;
139 if (searchq.trim().match(is_a_uuid)) {
140 window.location = '/actions?uuid=' + encodeURIComponent(searchq.trim());
141 // Show the "loading" indicator. TODO: better page transition hook
142 $(document).trigger('ajax:send');
145 if ($form.find('a[data-remote]').length > 0) {
146 // A search dialog is already loading.
150 attr('href', $form.attr('data-search-modal')).
151 attr('data-remote', 'true').
152 attr('data-method', 'GET').
155 on('ajax:success', function(data, status, xhr) {
156 $('body > .modal-container input[type=text]').
157 val($form.find('input').val()).
159 $form.find('input').val('');
160 }).on('ajax:complete', function() {
166 }).on('page-refresh', function(event, data, status, jqxhr, action_data) {
167 window.location.reload();
168 }).on('tab-refresh', function(event, data, status, jqxhr, action_data) {
169 $(document).trigger('arv:pane:reload:all');
170 $('body > .modal-container .modal').modal('hide');
171 }).on('redirect-to-created-object', function(event, data, status, jqxhr, action_data) {
172 window.location.href = data.href.replace(/^[^\/]*\/\/[^\/]*/, '');
173 }).on('shown.bs.modal', 'body > .modal-container .modal', function() {
174 $('.focus-on-display', this).focus();