2760: Merge branch 'master' into 2760-folder-hierarchy
[arvados.git] / apps / workbench / app / assets / javascripts / select_modal.js
1 $(document).on('click', '.selectable', function() {
2     var $this = $(this);
3     if (!$this.hasClass('multiple')) {
4         $this.closest('.selectable-container').
5             find('.selectable').
6             removeClass('active');
7     }
8     $this.toggleClass('active');
9 }).on('click', '.modal button[data-action-href]', function() {
10     var selection = [];
11     var data = {};
12     var $modal = $(this).closest('.modal');
13     $modal.find('.modal-error').removeClass('hide').hide();
14     $modal.find('.selectable.active[data-object-uuid]').each(function() {
15         selection.push($(this).attr('data-object-uuid'));
16     });
17     data[$(this).data('action-data').selection_param] = selection[0];
18     $.ajax($(this).attr('data-action-href'),
19            {dataType: 'json',
20             type: $(this).attr('data-method'),
21             data: data,
22             context: {modal: $modal}}).
23         fail(function(jqxhr, status, error) {
24             if (jqxhr.readyState == 0 || jqxhr.status == 0) {
25                 message = "Cancelled."
26             } else if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
27                 message = jqxhr.responseJSON.errors.join("; ");
28             } else {
29                 message = "Request failed.";
30             }
31             this.modal.find('.modal-error').
32                 html('<div class="alert alert-danger">' + message + '</div>').
33                 show();
34         }).
35         success(function() {
36             this.modal.find('.modal-error').hide();
37             window.location.reload();
38         });
39 });