Merge branch '2986-arv-edit' closes #2986
[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         $(container).append('<img src="/assets/ajax-loader.gif" class="infinite-scroller-spinner"></img>');
18         $.ajax(src,
19                {dataType: 'json',
20                 type: 'GET',
21                 data: {},
22                 context: {container: container, src: src}}).
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                 // TODO: report this to the user.
32                 console.log(message);
33                 $(this.container).attr('data-infinite-content-href', this.src);
34             }).
35             done(function(data, status, jqxhr) {
36                 $(this.container).find(".infinite-scroller-spinner").detach();
37                 $(this.container).append(data.content);
38                 $(this.container).attr('data-infinite-content-href', data.next_page_href);
39             });
40     }
41 }
42 $(document).
43     on('ready ajax:complete', function() {
44         $('[data-infinite-scroller]').each(function() {
45             var $scroller = $($(this).attr('data-infinite-scroller'));
46             if (!$scroller.hasClass('smart-scroll') &&
47                 'scroll' != $scroller.css('overflow-y'))
48                 $scroller = $(window);
49             $scroller.
50                 addClass('infinite-scroller').
51                 data('infinite-container', this).
52                 on('scroll', maybe_load_more_content);
53         });
54     });