Merge branch '3099-spinner-assets' closes #3099
[arvados.git] / apps / workbench / app / assets / javascripts / infinite_scroll.js
1 function maybe_load_more_content() {
2     var scroller = this;        // element with scroll bars
3     var container;              // element that receives new content
4     var src;                    // url for retrieving content
5     var scrollHeight;
6     var spinner, colspan;
7     scrollHeight = scroller.scrollHeight || $('body')[0].scrollHeight;
8     if ($(scroller).scrollTop() + $(scroller).height()
9         >
10         scrollHeight - 50) {
11         container = $(this).data('infinite-container');
12         src = $(container).attr('data-infinite-content-href');
13         if (!src)
14             // Finished
15             return;
16         // Don't start another request until this one finishes
17         $(container).attr('data-infinite-content-href', null);
18         spinner = '<div class="spinner spinner-32px spinner-h-center"></div>';
19         if ($(container).is('table,tbody,thead,tfoot')) {
20             // Hack to determine how many columns a new tr should have
21             // in order to reach full width.
22             colspan = $(container).closest('table').
23                 find('tr').eq(0).find('td,th').length;
24             if (colspan == 0)
25                 colspan = '*';
26             spinner = ('<tr class="spinner"><td colspan="' + colspan + '">' +
27                        spinner +
28                        '</td></tr>');
29         }
30         $(container).append(spinner);
31         $.ajax(src,
32                {dataType: 'json',
33                 type: 'GET',
34                 data: {},
35                 context: {container: container, src: src}}).
36             always(function() {
37                 $(this.container).find(".spinner").detach();
38             }).
39             fail(function(jqxhr, status, error) {
40                 if (jqxhr.readyState == 0 || jqxhr.status == 0) {
41                     message = "Cancelled."
42                 } else if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
43                     message = jqxhr.responseJSON.errors.join("; ");
44                 } else {
45                     message = "Request failed.";
46                 }
47                 // TODO: report this to the user.
48                 console.log(message);
49                 $(this.container).attr('data-infinite-content-href', this.src);
50             }).
51             done(function(data, status, jqxhr) {
52                 $(this.container).append(data.content);
53                 $(this.container).attr('data-infinite-content-href', data.next_page_href);
54             });
55     }
56 }
57 $(document).
58     on('ready ajax:complete', function() {
59         $('[data-infinite-scroller]').each(function() {
60             var $scroller = $($(this).attr('data-infinite-scroller'));
61             if (!$scroller.hasClass('smart-scroll') &&
62                 'scroll' != $scroller.css('overflow-y'))
63                 $scroller = $(window);
64             $scroller.
65                 addClass('infinite-scroller').
66                 data('infinite-container', this).
67                 on('scroll', maybe_load_more_content);
68         });
69     });