Merge branch '2883-job-log-viewer' of git.curoverse.com:arvados into origin-2883...
[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 }).on('click', '.modal button[data-action-href]', function() {
18     var selection = [];
19     var data = [];
20     var $modal = $(this).closest('.modal');
21     var action_data = $(this).data('action-data');
22     var selection_param = action_data.selection_param;
23     $modal.find('.modal-error').removeClass('hide').hide();
24     $modal.find('.selectable.active[data-object-uuid]').each(function() {
25         var val = $(this).attr('data-object-uuid');
26         data.push({name: selection_param, value: val});
27     });
28     $.each(action_data, function(key, value) {
29         data.push({name: key, value: value});
30     });
31     $.ajax($(this).attr('data-action-href'),
32            {dataType: 'json',
33             type: $(this).attr('data-method'),
34             data: data,
35             traditional: true,
36             context: {modal: $modal, action_data: action_data}}).
37         fail(function(jqxhr, status, error) {
38             if (jqxhr.readyState == 0 || jqxhr.status == 0) {
39                 message = "Cancelled."
40             } else if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
41                 message = jqxhr.responseJSON.errors.join("; ");
42             } else {
43                 message = "Request failed.";
44             }
45             this.modal.find('.modal-error').
46                 html('<div class="alert alert-danger">' + message + '</div>').
47                 show();
48         }).
49         done(function(data, status, jqxhr) {
50             var event_name = this.action_data.success;
51             this.modal.find('.modal-error').hide();
52             $(document).trigger(event_name!=null ? event_name : 'page-refresh',
53                                 [data, status, jqxhr, this.action_data]);
54         });
55 });
56 $(document).on('page-refresh', function(event, data, status, jqxhr, action_data) {
57     window.location.reload();
58 }).on('redirect-to-created-object', function(event, data, status, jqxhr, action_data) {
59     window.location.href = data.href.replace(/^[^\/]*\/\/[^\/]*/, '');
60 });