Merge branch '2800-python-global-state' into 2800-pgs
[arvados.git] / apps / workbench / app / assets / javascripts / infinite_scroll.js
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
5     var scrollHeight;
6     var spinner, colspan;
7     var serial = Date.now();
8     scrollHeight = scroller.scrollHeight || $('body')[0].scrollHeight;
9     if ($(scroller).scrollTop() + $(scroller).height()
10         >
11         scrollHeight - 50)
12     {
13         $container = $(event.data.container);
14         if (!$container.attr('data-infinite-content-href0')) {
15             // Remember the first page source url, so we can refresh
16             // from page 1 later.
17             $container.attr('data-infinite-content-href0',
18                             $container.attr('data-infinite-content-href'));
19         }
20         src = $container.attr('data-infinite-content-href');
21         if (!src || !$container.is(':visible'))
22             // Finished
23             return;
24
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;
33             if (colspan == 0)
34                 colspan = '*';
35             spinner = ('<tr class="spinner"><td colspan="' + colspan + '">' +
36                        spinner +
37                        '</td></tr>');
38         }
39         $container.find(".spinner").detach();
40         $container.append(spinner);
41         $container.attr('data-infinite-serial', serial);
42         $.ajax(src,
43                {dataType: 'json',
44                 type: 'GET',
45                 data: ($container.data('infinite-content-params') || {}),
46                 context: {container: $container, src: src, serial: serial}}).
47             fail(function(jqxhr, status, error) {
48                 var $faildiv;
49                 var $container = this.container;
50                 if ($container.attr('data-infinite-serial') != this.serial) {
51                     // A newer request is already in progress.
52                     return;
53                 }
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("; ");
58                 } else {
59                     message = "Request failed.";
60                 }
61                 // TODO: report the message to the user.
62                 console.log(message);
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);
68             }).
69             done(function(data, status, jqxhr) {
70                 if ($container.attr('data-infinite-serial') != this.serial) {
71                     // A newer request is already in progress.
72                     return;
73                 }
74                 $container.find(".spinner").detach();
75                 $container.append(data.content);
76                 $container.attr('data-infinite-content-href', data.next_page_href);
77             });
78      }
79 }
80
81 $(document).
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');
89     }).
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');
94         if (!first_page_href)
95             first_page_href = $(this).attr('data-infinite-content-href');
96         $(this).
97             html('').
98             attr('data-infinite-content-href', first_page_href);
99         $('.infinite-scroller').
100             trigger('scroll');
101     }).
102     on('ready ajax:complete', function() {
103         $('[data-infinite-scroller]').each(function() {
104             if ($(this).hasClass('infinite-scroller-ready'))
105                 return;
106             $(this).addClass('infinite-scroller-ready');
107
108             var $scroller = $($(this).attr('data-infinite-scroller'));
109             if (!$scroller.hasClass('smart-scroll') &&
110                 'scroll' != $scroller.css('overflow-y'))
111                 $scroller = $(window);
112             $scroller.
113                 addClass('infinite-scroller').
114                 on('scroll resize', { container: this }, maybe_load_more_content).
115                 trigger('scroll');
116         });
117     });