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