use cancelable timeout, on scroll only fetch next page if last query had any results

This commit is contained in:
Jörn Friedrich Dreyer 2015-01-06 15:18:41 +01:00
parent 357fbd88be
commit 04e2f59f5d
1 changed files with 25 additions and 21 deletions

View File

@ -84,7 +84,8 @@
var lastInApps = []; var lastInApps = [];
var lastPage = 0; var lastPage = 0;
var lastSize = 30; var lastSize = 30;
var lastResults = {}; var lastResults = [];
var timeoutID = null;
this.getLastQuery = function() { this.getLastQuery = function() {
return lastQuery; return lastQuery;
@ -94,7 +95,7 @@
* Do a search query and display the results * Do a search query and display the results
* @param {string} query the search query * @param {string} query the search query
*/ */
this.search = _.debounce(function(query, inApps, page, size) { this.search = function(query, inApps, page, size) {
if (query) { if (query) {
OC.addStyle('search','results'); OC.addStyle('search','results');
if (typeof page !== 'number') { if (typeof page !== 'number') {
@ -115,6 +116,8 @@
if ($searchResults && query === lastQuery && page === lastPage && size === lastSize) { if ($searchResults && query === lastQuery && page === lastPage && size === lastSize) {
return; return;
} }
window.clearTimeout(timeoutID);
timeoutID = window.setTimeout(function() {
lastQuery = query; lastQuery = query;
lastInApps = inApps; lastInApps = inApps;
lastPage = page; lastPage = page;
@ -133,8 +136,9 @@
addResults(results); addResults(results);
} }
}); });
}
}, 500); }, 500);
}
};
//TODO should be a core method, see https://github.com/owncloud/core/issues/12557 //TODO should be a core method, see https://github.com/owncloud/core/issues/12557
function getCurrentApp() { function getCurrentApp() {
@ -243,7 +247,7 @@
* This appends/renders the next page of entries when reaching the bottom. * This appends/renders the next page of entries when reaching the bottom.
*/ */
function onScroll(e) { function onScroll(e) {
if ($searchResults && lastQuery !== false) { if ($searchResults && lastQuery !== false && lastResults.length > 0) {
var resultsBottom = $searchResults.offset().top + $searchResults.height(); var resultsBottom = $searchResults.offset().top + $searchResults.height();
var containerBottom = $searchResults.offsetParent().offset().top + $searchResults.offsetParent().height(); var containerBottom = $searchResults.offsetParent().offset().top + $searchResults.offsetParent().height();
if ( resultsBottom < containerBottom * 1.2 ) { if ( resultsBottom < containerBottom * 1.2 ) {