1 function maybe_load_more_content(event) {
2 var scroller = this; // element with scroll bars
3 var $container; // element that receives new content
4 var src; // url for retrieving content
7 var serial = Date.now();
8 scrollHeight = scroller.scrollHeight || $('body')[0].scrollHeight;
9 if ($(scroller).scrollTop() + $(scroller).height()
13 $container = $(event.data.container);
14 if (!$container.attr('data-infinite-content-href0')) {
15 // Remember the first page source url, so we can refresh
17 $container.attr('data-infinite-content-href0',
18 $container.attr('data-infinite-content-href'));
20 src = $container.attr('data-infinite-content-href');
21 if (!src || !$container.is(':visible'))
25 // Don't start another request until this one finishes
26 $container.attr('data-infinite-content-href', null);
27 spinner = '<div class="spinner spinner-32px spinner-h-center"></div>';
28 if ($container.is('table,tbody,thead,tfoot')) {
29 // Hack to determine how many columns a new tr should have
30 // in order to reach full width.
31 colspan = $container.closest('table').
32 find('tr').eq(0).find('td,th').length;
35 spinner = ('<tr class="spinner"><td colspan="' + colspan + '">' +
39 $container.find(".spinner").detach();
40 $container.append(spinner);
41 $container.attr('data-infinite-serial', serial);
45 data: ($container.data('infinite-content-params') || {}),
46 context: {container: $container, src: src, serial: serial}}).
47 fail(function(jqxhr, status, error) {
49 var $container = this.container;
50 if ($container.attr('data-infinite-serial') != this.serial) {
51 // A newer request is already in progress.
54 if (jqxhr.readyState == 0 || jqxhr.status == 0) {
55 message = "Cancelled."
56 } else if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
57 message = jqxhr.responseJSON.errors.join("; ");
59 message = "Request failed.";
61 // TODO: report the message to the user.
63 $faildiv = $('<div />').
64 attr('data-infinite-content-href', this.src).
65 addClass('infinite-retry').
66 append('<span class="fa fa-warning" /> Oops, request failed. <button class="btn btn-xs btn-primary">Retry</button>');
67 $container.find('div.spinner').replaceWith($faildiv);
69 done(function(data, status, jqxhr) {
70 if ($container.attr('data-infinite-serial') != this.serial) {
71 // A newer request is already in progress.
74 $container.find(".spinner").detach();
75 $container.append(data.content);
76 $container.attr('data-infinite-content-href', data.next_page_href);
82 on('click', 'div.infinite-retry button', function() {
83 var $retry_div = $(this).closest('.infinite-retry');
84 var $scroller = $(this).closest('.infinite-scroller')
85 $scroller.attr('data-infinite-content-href',
86 $retry_div.attr('data-infinite-content-href'));
87 $retry_div.replaceWith('<div class="spinner spinner-32px spinner-h-center" />');
88 $scroller.trigger('scroll');
90 on('refresh-content', '[data-infinite-scroller]', function() {
91 // Clear all rows, reset source href to initial state, and
92 // (if the container is visible) start loading content.
93 var first_page_href = $(this).attr('data-infinite-content-href0');
95 first_page_href = $(this).attr('data-infinite-content-href');
98 attr('data-infinite-content-href', first_page_href);
99 $('.infinite-scroller').
102 on('ready ajax:complete', function() {
103 $('[data-infinite-scroller]').each(function() {
104 if ($(this).hasClass('infinite-scroller-ready'))
106 $(this).addClass('infinite-scroller-ready');
108 var $scroller = $($(this).attr('data-infinite-scroller'));
109 if (!$scroller.hasClass('smart-scroll') &&
110 'scroll' != $scroller.css('overflow-y'))
111 $scroller = $(window);
113 addClass('infinite-scroller').
114 on('scroll resize', { container: this }, maybe_load_more_content).