2884: AJAX load a preview panel in collection and pipeline template picker.
[arvados.git] / apps / workbench / app / assets / javascripts / select_modal.js
1 $(document).on('click', '.selectable', function() {
2     var any;
3     var $this = $(this);
4     if (!$this.hasClass('multiple')) {
5         $this.closest('.selectable-container').
6             find('.selectable').
7             removeClass('active');
8     }
9     $this.toggleClass('active');
10     any = ($this.
11            closest('.selectable-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         $(".modal-dialog-preview-pane").html('<img src="/assets/ajax-loader.gif"></img>');
20         $.ajax($(this).attr('data-preview-href'),
21                {dataType: "html"}).done(function(data, status, jqxhr) {
22                    $(".modal-dialog-preview-pane").html(data);
23                });
24     }
25
26 }).on('click', '.modal button[data-action-href]', function() {
27     var selection = [];
28     var data = [];
29     var $modal = $(this).closest('.modal');
30     var action_data = $(this).data('action-data');
31     var selection_param = action_data.selection_param;
32     $modal.find('.modal-error').removeClass('hide').hide();
33     $modal.find('.selectable.active[data-object-uuid]').each(function() {
34         var val = $(this).attr('data-object-uuid');
35         data.push({name: selection_param, value: val});
36     });
37     $.each(action_data, function(key, value) {
38         data.push({name: key, value: value});
39     });
40     $.ajax($(this).attr('data-action-href'),
41            {dataType: 'json',
42             type: $(this).attr('data-method'),
43             data: data,
44             traditional: true,
45             context: {modal: $modal, action_data: action_data}}).
46         fail(function(jqxhr, status, error) {
47             if (jqxhr.readyState == 0 || jqxhr.status == 0) {
48                 message = "Cancelled."
49             } else if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
50                 message = jqxhr.responseJSON.errors.join("; ");
51             } else {
52                 message = "Request failed.";
53             }
54             this.modal.find('.modal-error').
55                 html('<div class="alert alert-danger">' + message + '</div>').
56                 show();
57         }).
58         done(function(data, status, jqxhr) {
59             var event_name = this.action_data.success;
60             this.modal.find('.modal-error').hide();
61             $(document).trigger(event_name!=null ? event_name : 'page-refresh',
62                                 [data, status, jqxhr, this.action_data]);
63         });
64 });
65 $(document).on('page-refresh', function(event, data, status, jqxhr, action_data) {
66     window.location.reload();
67 }).on('redirect-to-created-object', function(event, data, status, jqxhr, action_data) {
68     window.location.href = data.href.replace(/^[^\/]*\/\/[^\/]*/, '');
69 });