Merge branch 'master' into origin-2883-job-log-viewer
[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     scrollHeight = scroller.scrollHeight || $('body')[0].scrollHeight;
7     if ($(scroller).scrollTop() + $(scroller).height()
8         >
9         scrollHeight - 50) {
10         container = $(this).data('infinite-container');
11         src = $(container).attr('data-infinite-content-href');
12         if (!src)
13             // Finished
14             return;
15         // Don't start another request until this one finishes
16         $(container).attr('data-infinite-content-href', null);
17         $.ajax(src,
18                {dataType: 'json',
19                 type: 'GET',
20                 data: {},
21                 context: {container: container, src: src}}).
22             fail(function(jqxhr, status, error) {
23                 if (jqxhr.readyState == 0 || jqxhr.status == 0) {
24                     message = "Cancelled."
25                 } else if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
26                     message = jqxhr.responseJSON.errors.join("; ");
27                 } else {
28                     message = "Request failed.";
29                 }
30                 // TODO: report this to the user.
31                 console.log(message);
32                 $(this.container).attr('data-infinite-content-href', this.src);
33             }).
34             done(function(data, status, jqxhr) {
35                 $(this.container).append(data.content);
36                 $(this.container).attr('data-infinite-content-href', data.next_page_href);
37                 $(document).trigger('ajax:complete');
38             });
39     }
40 }
41 $(document).
42     on('ready ajax:complete', function() {
43         $('[data-infinite-scroller]').each(function() {
44             var $scroller = $($(this).attr('data-infinite-scroller'));
45             if (!$scroller.hasClass('smart-scroll') &&
46                 'scroll' != $scroller.css('overflow-y'))
47                 $scroller = $(window);
48             $scroller.
49                 addClass('infinite-scroller').
50                 data('infinite-container', this).
51                 on('scroll', maybe_load_more_content);
52         });
53     });