Fix Canceled -> Cancelled (american vs canadian spelling, canada wins)
[arvados.git] / apps / workbench / app / assets / javascripts / infinite_scroll.js
index 1f9997efa05fc002f8a9907965d73e9dff2b1a46..6b392c48d7042ad16e212214e3b3626f61b7b0a9 100644 (file)
@@ -1,25 +1,32 @@
-function maybe_load_more_content() {
+function maybe_load_more_content(event) {
     var scroller = this;        // element with scroll bars
-    var container;              // element that receives new content
+    var $container;             // element that receives new content
     var src;                    // url for retrieving content
     var scrollHeight;
     var spinner, colspan;
+    var serial = Date.now();
+    var params;
     scrollHeight = scroller.scrollHeight || $('body')[0].scrollHeight;
-    var num_scrollers = $(window).data("arv-num-scrollers");
     if ($(scroller).scrollTop() + $(scroller).height()
         >
         scrollHeight - 50)
     {
-      for (var i = 0; i < num_scrollers; i++) {
-        $container = $($(this).data('infinite-container'+i));
+        $container = $(event.data.container);
+        if (!$container.attr('data-infinite-content-href0')) {
+            // Remember the first page source url, so we can refresh
+            // from page 1 later.
+            $container.attr('data-infinite-content-href0',
+                            $container.attr('data-infinite-content-href'));
+        }
         src = $container.attr('data-infinite-content-href');
         if (!src || !$container.is(':visible'))
-          continue;
+            // Finished
+            return;
 
         // Don't start another request until this one finishes
         $container.attr('data-infinite-content-href', null);
         spinner = '<div class="spinner spinner-32px spinner-h-center"></div>';
-        if ($(container).is('table,tbody,thead,tfoot')) {
+        if ($container.is('table,tbody,thead,tfoot')) {
             // Hack to determine how many columns a new tr should have
             // in order to reach full width.
             colspan = $container.closest('table').
@@ -30,16 +37,62 @@ function maybe_load_more_content() {
                        spinner +
                        '</td></tr>');
         }
+        $container.find(".spinner").detach();
         $container.append(spinner);
+        $container.attr('data-infinite-serial', serial);
+
+        // Combine infiniteContentParams from multiple sources. This
+        // mechanism allows each of several components to set and
+        // update its own set of filters, without having to worry
+        // about stomping on some other component's filters.
+        //
+        // For example, filterable.js writes filters in
+        // infiniteContentParamsFilterable ("search for text foo")
+        // without worrying about clobbering the filters set up by the
+        // tab pane ("only show jobs and pipelines in this tab").
+        params = {};
+        $.each($container.data(), function(datakey, datavalue) {
+            // Note: We attach these data to DOM elements using
+            // <element data-foo-bar="baz">. We store/retrieve them
+            // using $('element').data('foo-bar'), although
+            // .data('fooBar') would also work. The "all data" hash
+            // returned by $('element').data(), however, always has
+            // keys like 'fooBar'. In other words, where we have a
+            // choice, we stick with the 'foo-bar' style to be
+            // consistent with HTML. Here, our only option is
+            // 'fooBar'.
+            if (/^infiniteContentParams/.exec(datakey)) {
+                if (datavalue instanceof Object) {
+                    $.each(datavalue, function(hkey, hvalue) {
+                        if (hvalue instanceof Array) {
+                            params[hkey] = (params[hkey] || []).concat(hvalue);
+                        } else if (hvalue instanceof Object) {
+                            $.extend(params[hkey], hvalue);
+                        } else {
+                            params[hkey] = hvalue;
+                        }
+                    });
+                }
+            }
+        });
+        $.each(params, function(k,v) {
+            if (v instanceof Object) {
+                params[k] = JSON.stringify(v);
+            }
+        });
+
         $.ajax(src,
                {dataType: 'json',
                 type: 'GET',
-                data: {},
-                context: {container: $container, src: src}}).
-            always(function() {
-                $(this.container).find(".spinner").detach();
-            }).
+                data: params,
+                context: {container: $container, src: src, serial: serial}}).
             fail(function(jqxhr, status, error) {
+                var $faildiv;
+                var $container = this.container;
+                if ($container.attr('data-infinite-serial') != this.serial) {
+                    // A newer request is already in progress.
+                    return;
+                }
                 if (jqxhr.readyState == 0 || jqxhr.status == 0) {
                     message = "Cancelled."
                 } else if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
@@ -47,32 +100,72 @@ function maybe_load_more_content() {
                 } else {
                     message = "Request failed.";
                 }
-                // TODO: report this to the user.
+                // TODO: report the message to the user.
                 console.log(message);
-                $(this.container).attr('data-infinite-content-href', this.src);
+                $faildiv = $('<div />').
+                    attr('data-infinite-content-href', this.src).
+                    addClass('infinite-retry').
+                    append('<span class="fa fa-warning" /> Oops, request failed. <button class="btn btn-xs btn-primary">Retry</button>');
+                $container.find('div.spinner').replaceWith($faildiv);
             }).
             done(function(data, status, jqxhr) {
-                $(this.container).append(data.content);
-                $(this.container).attr('data-infinite-content-href', data.next_page_href);
+                if ($container.attr('data-infinite-serial') != this.serial) {
+                    // A newer request is already in progress.
+                    return;
+                }
+                $container.find(".spinner").detach();
+                $container.append(data.content);
+                $container.attr('data-infinite-content-href', data.next_page_href);
             });
-        break;
      }
-   }
+}
+
+function ping_all_scrollers() {
+    // Send a scroll event to all scroll listeners that might need
+    // updating. Adding infinite-scroller class to the window element
+    // doesn't work, so we add it explicitly here.
+    $('.infinite-scroller').add(window).trigger('scroll');
 }
 
 $(document).
+    on('click', 'div.infinite-retry button', function() {
+        var $retry_div = $(this).closest('.infinite-retry');
+        var $container = $(this).closest('.infinite-scroller-ready')
+        $container.attr('data-infinite-content-href',
+                        $retry_div.attr('data-infinite-content-href'));
+        $retry_div.
+            replaceWith('<div class="spinner spinner-32px spinner-h-center" />');
+        ping_all_scrollers();
+    }).
+    on('refresh-content', '[data-infinite-scroller]', function() {
+        // Clear all rows, reset source href to initial state, and
+        // (if the container is visible) start loading content.
+        var first_page_href = $(this).attr('data-infinite-content-href0');
+        if (!first_page_href)
+            first_page_href = $(this).attr('data-infinite-content-href');
+        $(this).
+            html('').
+            attr('data-infinite-content-href', first_page_href);
+        ping_all_scrollers();
+    }).
     on('ready ajax:complete', function() {
-        var num_scrollers = 0;
         $('[data-infinite-scroller]').each(function() {
+            if ($(this).hasClass('infinite-scroller-ready'))
+                return;
+            $(this).addClass('infinite-scroller-ready');
+
+            // $scroller is the DOM element that hears "scroll"
+            // events: sometimes it's a div, sometimes it's
+            // window. Here, "this" is the DOM element containing the
+            // result rows. We pass it to maybe_load_more_content in
+            // event.data.
             var $scroller = $($(this).attr('data-infinite-scroller'));
             if (!$scroller.hasClass('smart-scroll') &&
                 'scroll' != $scroller.css('overflow-y'))
                 $scroller = $(window);
             $scroller.
                 addClass('infinite-scroller').
-                data('infinite-container'+num_scrollers, this).
-                on('scroll', maybe_load_more_content);
-            num_scrollers++;
+                on('scroll resize', { container: this }, maybe_load_more_content).
+                trigger('scroll');
         });
-        $(window).data("arv-num-scrollers", num_scrollers);
     });