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();
9 scrollHeight = scroller.scrollHeight || $('body')[0].scrollHeight;
10 if ($(scroller).scrollTop() + $(scroller).height()
14 $container = $(event.data.container);
15 if (!$container.attr('data-infinite-content-href0')) {
16 // Remember the first page source url, so we can refresh
18 $container.attr('data-infinite-content-href0',
19 $container.attr('data-infinite-content-href'));
21 src = $container.attr('data-infinite-content-href');
22 if (!src || !$container.is(':visible'))
26 // Don't start another request until this one finishes
27 $container.attr('data-infinite-content-href', null);
28 spinner = '<div class="spinner spinner-32px spinner-h-center"></div>';
29 if ($container.is('table,tbody,thead,tfoot')) {
30 // Hack to determine how many columns a new tr should have
31 // in order to reach full width.
32 colspan = $container.closest('table').
33 find('tr').eq(0).find('td,th').length;
36 spinner = ('<tr class="spinner"><td colspan="' + colspan + '">' +
40 $container.find(".spinner").detach();
41 $container.append(spinner);
42 $container.attr('data-infinite-serial', serial);
44 if (src == $container.attr('data-infinite-content-href0')) {
45 // If we're loading the first page, collect filters from
47 params = mergeInfiniteContentParams($container);
48 $.each(params, function(k,v) {
49 if (v instanceof Object) {
50 params[k] = JSON.stringify(v);
54 // If we're loading page >1, ignore other filtering
55 // mechanisms and just use the "next page" URI from the
56 // previous page's response. Aside from avoiding race
57 // conditions (where page 2 could have different filters
58 // than page 1), this allows the server to use filters in
59 // the "next page" URI to achieve paging. (To apply any
60 // new filters effectively, we need to load page 1 again
69 context: {container: $container, src: src, serial: serial}}).
70 fail(function(jqxhr, status, error) {
72 var $container = this.container;
73 if ($container.attr('data-infinite-serial') != this.serial) {
74 // A newer request is already in progress.
77 if (jqxhr.readyState == 0 || jqxhr.status == 0) {
78 message = "Cancelled."
79 } else if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
80 message = jqxhr.responseJSON.errors.join("; ");
82 message = "Request failed.";
84 // TODO: report the message to the user.
86 $faildiv = $('<div />').
87 attr('data-infinite-content-href', this.src).
88 addClass('infinite-retry').
89 append('<span class="fa fa-warning" /> Oops, request failed. <button class="btn btn-xs btn-primary">Retry</button>');
90 $container.find('div.spinner').replaceWith($faildiv);
92 done(function(data, status, jqxhr) {
93 if ($container.attr('data-infinite-serial') != this.serial) {
94 // A newer request is already in progress.
97 $container.find(".spinner").detach();
98 $container.append(data.content);
99 $container.attr('data-infinite-content-href', data.next_page_href);
104 function ping_all_scrollers() {
105 // Send a scroll event to all scroll listeners that might need
106 // updating. Adding infinite-scroller class to the window element
107 // doesn't work, so we add it explicitly here.
108 $('.infinite-scroller').add(window).trigger('scroll');
111 function mergeInfiniteContentParams($container) {
113 // Combine infiniteContentParams from multiple sources. This
114 // mechanism allows each of several components to set and
115 // update its own set of filters, without having to worry
116 // about stomping on some other component's filters.
118 // For example, filterable.js writes filters in
119 // infiniteContentParamsFilterable ("search for text foo")
120 // without worrying about clobbering the filters set up by the
121 // tab pane ("only show jobs and pipelines in this tab").
122 $.each($container.data(), function(datakey, datavalue) {
123 // Note: We attach these data to DOM elements using
124 // <element data-foo-bar="baz">. We store/retrieve them
125 // using $('element').data('foo-bar'), although
126 // .data('fooBar') would also work. The "all data" hash
127 // returned by $('element').data(), however, always has
128 // keys like 'fooBar'. In other words, where we have a
129 // choice, we stick with the 'foo-bar' style to be
130 // consistent with HTML. Here, our only option is
132 if (/^infiniteContentParams/.exec(datakey)) {
133 if (datavalue instanceof Object) {
134 $.each(datavalue, function(hkey, hvalue) {
135 if (hvalue instanceof Array) {
136 params[hkey] = (params[hkey] || []).
138 } else if (hvalue instanceof Object) {
139 $.extend(params[hkey], hvalue);
141 params[hkey] = hvalue;
150 function setColumnSort( $container, $header, direction ) {
151 // $container should be the tbody or whatever has all the infinite table data attributes
152 // $header should be the th with a preset data-sort-order attribute
153 // direction should be "asc" or "desc"
154 // This function returns the order by clause for this column header as a string
156 // First reset all sort directions
157 $('th[data-sort-order]').removeData('sort-order-direction');
158 // set the current one
159 $header.data('sort-order-direction', direction);
160 // change the ordering parameter
161 var paramsAttr = 'infinite-content-params-' + $container.data('infinite-content-params-attr');
162 var params = $container.data(paramsAttr) || {};
163 params.order = $header.data('sort-order').split(",").join( ' ' + direction + ', ' ) + ' ' + direction;
164 $container.data(paramsAttr, params);
165 // show the correct icon next to the column header
166 $container.trigger('sort-icons');
172 on('click', 'div.infinite-retry button', function() {
173 var $retry_div = $(this).closest('.infinite-retry');
174 var $container = $(this).closest('.infinite-scroller-ready')
175 $container.attr('data-infinite-content-href',
176 $retry_div.attr('data-infinite-content-href'));
178 replaceWith('<div class="spinner spinner-32px spinner-h-center" />');
179 ping_all_scrollers();
181 on('refresh-content', '[data-infinite-scroller]', function() {
182 // Clear all rows, reset source href to initial state, and
183 // (if the container is visible) start loading content.
184 var first_page_href = $(this).attr('data-infinite-content-href0');
185 if (!first_page_href)
186 first_page_href = $(this).attr('data-infinite-content-href');
189 attr('data-infinite-content-href', first_page_href);
190 ping_all_scrollers();
192 on('ready ajax:complete', function() {
193 $('[data-infinite-scroller]').each(function() {
194 if ($(this).hasClass('infinite-scroller-ready'))
196 $(this).addClass('infinite-scroller-ready');
198 // deal with sorting if there is any, and if it was set on this page for this tab already
199 if( $('th[data-sort-order]').length ) {
200 var tabId = $(this).closest('div.tab-pane').attr('id');
201 if( hasHTML5History() && history.state !== undefined && history.state !== null && history.state.order !== undefined && history.state.order[tabId] !== undefined ) {
202 // we will use the list of one or more table columns associated with this header to find the right element
203 // see sortable_columns as it is passed to render_pane in the various tab .erbs (e.g. _show_jobs_and_pipelines.html.erb)
204 var strippedColumns = history.state.order[tabId].replace(/\s|\basc\b|\bdesc\b/g,'');
205 var sortDirection = history.state.order[tabId].split(" ")[1].replace(/,/,'');
206 $columnHeader = $(this).closest('table').find('[data-sort-order="'+ strippedColumns +'"]');
207 setColumnSort( $(this), $columnHeader, sortDirection );
209 // otherwise just reset the sort icons
210 $(this).trigger('sort-icons');
214 // $scroller is the DOM element that hears "scroll"
215 // events: sometimes it's a div, sometimes it's
216 // window. Here, "this" is the DOM element containing the
217 // result rows. We pass it to maybe_load_more_content in
219 var $scroller = $($(this).attr('data-infinite-scroller'));
220 if (!$scroller.hasClass('smart-scroll') &&
221 'scroll' != $scroller.css('overflow-y'))
222 $scroller = $(window);
224 addClass('infinite-scroller').
225 on('scroll resize', { container: this }, maybe_load_more_content).
229 on('click', 'th[data-sort-order]', function() {
230 var direction = $(this).data('sort-order-direction');
231 // reverse the current direction, or do ascending if none
232 if( direction === undefined || direction === 'desc' ) {
238 var $container = $(this).closest('table').find('[data-infinite-content-params-attr]');
240 var order = setColumnSort( $container, $(this), direction );
242 // put it in the browser history state if browser allows it
243 if( hasHTML5History() ) {
244 var tabId = $(this).closest('div.tab-pane').attr('id');
245 var state = history.state;
246 if( state.order === undefined) {
249 state.order[tabId] = order;
250 history.replaceState( state, null, null );
253 $container.trigger('refresh-content');
255 on('sort-icons', function() {
256 // set or reset the icon next to each sortable column header according to the current direction attribute
257 $('th[data-sort-order]').each(function() {
258 $(this).find('i').remove();
259 var direction = $(this).data('sort-order-direction');
260 if( direction !== undefined ) {
261 $(this).append('<i class="fa fa-sort-' + direction + '"/>');
263 $(this).append('<i class="fa fa-sort"/>');