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 $modal = $(this).closest('.modal');
39 $checked_selections = $modal.find('.persistent-selection:checked');
40 any = ($checked_selections.length > 0);
43 find('[data-enable-if-selection]').
44 prop('disabled', !any);
45 }).on('click', '.modal button[data-action-href]', function() {
48 var $modal = $(this).closest('.modal');
49 var action_data = $(this).data('action-data');
50 var action_data_from_params = $(this).data('action-data-from-params');
51 var selection_param = action_data.selection_param;
52 $modal.find('.modal-error').removeClass('hide').hide();
54 $checked_selections = $modal.find('.persistent-selection:checked');
55 if ($checked_selections) {
56 $checked_selections.each(function() {
57 data.push({name: selection_param, value: $(this).attr('value')});
61 if (data.length == 0) { // no checked persistent selection
62 $modal.find('.selectable.active[data-object-uuid]').each(function() {
63 var val = $(this).attr('data-object-uuid');
64 data.push({name: selection_param, value: val});
67 $.each($.extend({}, action_data, action_data_from_params),
68 function(key, value) {
69 if (value instanceof Array && key[-1] != ']') {
70 for (var i in value) {
71 data.push({name: key + '[]', value: value[i]});
74 data.push({name: key, value: value});
77 $.ajax($(this).attr('data-action-href'),
79 type: $(this).attr('data-method'),
82 context: {modal: $modal, action_data: action_data}}).
83 fail(function(jqxhr, status, error) {
84 if (jqxhr.readyState == 0 || jqxhr.status == 0) {
85 message = "Cancelled."
86 } else if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
87 message = jqxhr.responseJSON.errors.join("; ");
89 message = "Request failed.";
91 this.modal.find('.modal-error').
92 html('<div class="alert alert-danger">' + message + '</div>').
95 done(function(data, status, jqxhr) {
96 var event_name = this.action_data.success;
97 this.modal.find('.modal-error').hide();
98 $(document).trigger(event_name!=null ? event_name : 'page-refresh',
99 [data, status, jqxhr, this.action_data]);
101 }).on('click', '.chooser-show-project', function() {
103 var project_uuid = $(this).attr('data-project-uuid');
104 $(this).attr('href', '#'); // Skip normal click handler
106 params = {'filters': [['owner_uuid',
109 'project_uuid': project_uuid
112 // Use current selection as dropdown button label
114 closest('.dropdown-menu').
116 html($(this).text() + ' <span class="caret"></span>');
117 // Set (or unset) filter params and refresh filterable rows
118 $($(this).closest('[data-filterable-target]').attr('data-filterable-target')).
119 data('infinite-content-params-from-project-dropdown', params).
120 trigger('refresh-content');
121 }).on('ready', function() {
122 $('form[data-search-modal] a').on('click', function() {
123 $(this).closest('form').submit();
126 $('form[data-search-modal]').on('submit', function() {
127 // Ask the server for a Search modal. When it arrives, copy
128 // the search string from the top nav input into the modal's
129 // search query field.
131 var searchq = $form.find('input').val();
132 var is_a_uuid = /^([0-9a-f]{32}(\+\S+)?|[0-9a-z]{5}-[0-9a-z]{5}-[0-9a-z]{15})$/;
133 if (searchq.trim().match(is_a_uuid)) {
134 window.location = '/actions?uuid=' + encodeURIComponent(searchq.trim());
135 // Show the "loading" indicator. TODO: better page transition hook
136 $(document).trigger('ajax:send');
139 if ($form.find('a[data-remote]').length > 0) {
140 // A search dialog is already loading.
144 attr('href', $form.attr('data-search-modal')).
145 attr('data-remote', 'true').
146 attr('data-method', 'GET').
149 on('ajax:success', function(data, status, xhr) {
150 $('body > .modal-container input[type=text]').
151 val($form.find('input').val()).
153 $form.find('input').val('');
154 }).on('ajax:complete', function() {
160 }).on('page-refresh', function(event, data, status, jqxhr, action_data) {
161 window.location.reload();
162 }).on('tab-refresh', function(event, data, status, jqxhr, action_data) {
163 $(document).trigger('arv:pane:reload:all');
164 $('body > .modal-container .modal').modal('hide');
165 }).on('redirect-to-created-object', function(event, data, status, jqxhr, action_data) {
166 window.location.href = data.href.replace(/^[^\/]*\/\/[^\/]*/, '');
167 }).on('shown.bs.modal', 'body > .modal-container .modal', function() {
168 $('.focus-on-display', this).focus();