Merge branch 'master' into 3118-docker-fixes
[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('<div class="spinner spinner-32px spinner-h-center spinner-v-center"></div>');
20         $.ajax($this.attr('data-preview-href'),
21                {dataType: "html"}).
22            done(function(data, status, jqxhr) {
23                 $(".modal-dialog-preview-pane").html(data);
24             }).
25             fail(function(data, status, jqxhr) {
26                 $(".modal-dialog-preview-pane").text('Preview load failed.');
27             });
28     }
29
30 }).on('click', '.modal button[data-action-href]', function() {
31     var selection = [];
32     var data = [];
33     var $modal = $(this).closest('.modal');
34     var action_data = $(this).data('action-data');
35     var selection_param = action_data.selection_param;
36     $modal.find('.modal-error').removeClass('hide').hide();
37     $modal.find('.selectable.active[data-object-uuid]').each(function() {
38         var val = $(this).attr('data-object-uuid');
39         data.push({name: selection_param, value: val});
40     });
41     $.each(action_data, function(key, value) {
42         data.push({name: key, value: value});
43     });
44     $.ajax($(this).attr('data-action-href'),
45            {dataType: 'json',
46             type: $(this).attr('data-method'),
47             data: data,
48             traditional: true,
49             context: {modal: $modal, action_data: action_data}}).
50         fail(function(jqxhr, status, error) {
51             if (jqxhr.readyState == 0 || jqxhr.status == 0) {
52                 message = "Cancelled."
53             } else if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
54                 message = jqxhr.responseJSON.errors.join("; ");
55             } else {
56                 message = "Request failed.";
57             }
58             this.modal.find('.modal-error').
59                 html('<div class="alert alert-danger">' + message + '</div>').
60                 show();
61         }).
62         done(function(data, status, jqxhr) {
63             var event_name = this.action_data.success;
64             this.modal.find('.modal-error').hide();
65             $(document).trigger(event_name!=null ? event_name : 'page-refresh',
66                                 [data, status, jqxhr, this.action_data]);
67         });
68 });
69 $(document).on('page-refresh', function(event, data, status, jqxhr, action_data) {
70     window.location.reload();
71 }).on('redirect-to-created-object', function(event, data, status, jqxhr, action_data) {
72     window.location.href = data.href.replace(/^[^\/]*\/\/[^\/]*/, '');
73 });