Merge branch 'master' into 3654-combine-selections
[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     any = ($container.
12            find('.selectable.active').length > 0)
13     $this.
14         closest('.modal').
15         find('[data-enable-if-selection]').
16         prop('disabled', !any);
17
18     if ($this.hasClass('active')) {
19         var no_preview_available = '<div class="spinner-h-center spinner-v-center"><center>(No preview available)</center></div>';
20         if (!$this.attr('data-preview-href')) {
21             $(".modal-dialog-preview-pane").html(no_preview_available);
22             return;
23         }
24         $(".modal-dialog-preview-pane").html('<div class="spinner spinner-32px spinner-h-center spinner-v-center"></div>');
25         $.ajax($this.attr('data-preview-href'),
26                {dataType: "html"}).
27             done(function(data, status, jqxhr) {
28                 $(".modal-dialog-preview-pane").html(data);
29             }).
30             fail(function(data, status, jqxhr) {
31                 $(".modal-dialog-preview-pane").html(no_preview_available);
32             });
33     }
34
35 }).on('click', '.modal button[data-action-href]', function() {
36     var selection = [];
37     var data = [];
38     var $modal = $(this).closest('.modal');
39     var action_data = $(this).data('action-data');
40     var action_data_from_params = $(this).data('action-data-from-params');
41     var selection_param = action_data.selection_param;
42     $modal.find('.modal-error').removeClass('hide').hide();
43     $modal.find('.selectable.active[data-object-uuid]').each(function() {
44         var val = $(this).attr('data-object-uuid');
45         data.push({name: selection_param, value: val});
46     });
47     $.each($.extend({}, action_data, action_data_from_params),
48            function(key, value) {
49                if (value instanceof Array && key[-1] != ']') {
50                    for (var i in value) {
51                        data.push({name: key + '[]', value: value[i]});
52                    }
53                } else {
54                    data.push({name: key, value: value});
55                }
56            });
57     $.ajax($(this).attr('data-action-href'),
58            {dataType: 'json',
59             type: $(this).attr('data-method'),
60             data: data,
61             traditional: false,
62             context: {modal: $modal, action_data: action_data}}).
63         fail(function(jqxhr, status, error) {
64             if (jqxhr.readyState == 0 || jqxhr.status == 0) {
65                 message = "Cancelled."
66             } else if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
67                 message = jqxhr.responseJSON.errors.join("; ");
68             } else {
69                 message = "Request failed.";
70             }
71             this.modal.find('.modal-error').
72                 html('<div class="alert alert-danger">' + message + '</div>').
73                 show();
74         }).
75         done(function(data, status, jqxhr) {
76             var event_name = this.action_data.success;
77             this.modal.find('.modal-error').hide();
78             $(document).trigger(event_name!=null ? event_name : 'page-refresh',
79                                 [data, status, jqxhr, this.action_data]);
80         });
81 }).on('click', '.chooser-show-project', function() {
82     var params = {};
83     var project_uuid = $(this).attr('data-project-uuid');
84     $(this).attr('href', '#');  // Skip normal click handler
85     if (project_uuid) {
86         params = {'filters': [['owner_uuid',
87                                '=',
88                                project_uuid]],
89                   'project_uuid': project_uuid
90                  };
91     }
92     // Use current selection as dropdown button label
93     $(this).
94         closest('.dropdown-menu').
95         prev('button').
96         html($(this).text() + ' <span class="caret"></span>');
97     // Set (or unset) filter params and refresh filterable rows
98     $($(this).closest('[data-filterable-target]').attr('data-filterable-target')).
99         data('infinite-content-params-from-project-dropdown', params).
100         trigger('refresh-content');
101 }).on('ready', function() {
102     $('form[data-search-modal] a').on('click', function() {
103         $(this).closest('form').submit();
104         return false;
105     });
106     $('form[data-search-modal]').on('submit', function() {
107         // Ask the server for a Search modal. When it arrives, copy
108         // the search string from the top nav input into the modal's
109         // search query field.
110         var $form = $(this);
111         var searchq = $form.find('input').val();
112         var is_a_uuid = /^([0-9a-f]{32}(\+\S+)?|[0-9a-z]{5}-[0-9a-z]{5}-[0-9a-z]{15})$/;
113         if (searchq.trim().match(is_a_uuid)) {
114             window.location = '/actions?uuid=' + encodeURIComponent(searchq.trim());
115             // Show the "loading" indicator. TODO: better page transition hook
116             $(document).trigger('ajax:send');
117             return false;
118         }
119         if ($form.find('a[data-remote]').length > 0) {
120             // A search dialog is already loading.
121             return false;
122         }
123         $('<a />').
124             attr('href', $form.attr('data-search-modal')).
125             attr('data-remote', 'true').
126             attr('data-method', 'GET').
127             hide().
128             appendTo($form).
129             on('ajax:success', function(data, status, xhr) {
130                 $('body > .modal-container input[type=text]').
131                     val($form.find('input').val()).
132                     focus();
133                 $form.find('input').val('');
134             }).on('ajax:complete', function() {
135                 $(this).detach();
136             }).
137             click();
138         return false;
139     });
140 }).on('page-refresh', function(event, data, status, jqxhr, action_data) {
141     window.location.reload();
142 }).on('tab-refresh', function(event, data, status, jqxhr, action_data) {
143     $(document).trigger('arv:pane:reload:all');
144     $('body > .modal-container .modal').modal('hide');
145 }).on('redirect-to-created-object', function(event, data, status, jqxhr, action_data) {
146     window.location.href = data.href.replace(/^[^\/]*\/\/[^\/]*/, '');
147 }).on('shown.bs.modal', 'body > .modal-container .modal', function() {
148     $('.focus-on-display', this).focus();
149 });