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