Fix drive search throttle

pull/1/head
yflory 4 years ago
parent d4fa64a1bf
commit 6f399eb298

@ -3259,6 +3259,7 @@ define([
var $spinnerContainer = $(h('div.cp-app-drive-search-spinner')); var $spinnerContainer = $(h('div.cp-app-drive-search-spinner'));
var spinner = UI.makeSpinner($spinnerContainer); var spinner = UI.makeSpinner($spinnerContainer);
var searching = true;
var $input = APP.Search.$input = $('<input>', { var $input = APP.Search.$input = $('<input>', {
id: 'cp-app-drive-search-input', id: 'cp-app-drive-search-input',
placeholder: Messages.fm_searchName, placeholder: Messages.fm_searchName,
@ -3266,21 +3267,19 @@ define([
draggable: false, draggable: false,
tabindex: 1, tabindex: 1,
}).keyup(function (e) { }).keyup(function (e) {
var lastValue = search.value; if (searching) {
search.value = $input.val().trim(); e.preventDefault();
if (lastValue === search.value) { return; } e.stopPropagation();
if (search.to) { window.clearTimeout(search.to); }
if (search.value === "") {
search.cursor = 0;
APP.displayDirectory([SEARCH]);
return; return;
} }
spinner.spin(); var currentValue = $input.val().trim();
if (search.to) { window.clearTimeout(search.to); }
if (e.which === 13) { if (e.which === 13) {
spinner.spin();
var newLocation = [SEARCH, $input.val()]; var newLocation = [SEARCH, $input.val()];
search.cursor = $input[0].selectionStart; search.cursor = $input[0].selectionStart;
if (!manager.comparePath(newLocation, currentPath.slice())) { if (!manager.comparePath(newLocation, currentPath.slice())) {
searching = true;
APP.displayDirectory(newLocation); APP.displayDirectory(newLocation);
} }
return; return;
@ -3288,27 +3287,30 @@ define([
if (e.which === 27) { if (e.which === 27) {
$input.val(''); $input.val('');
search.cursor = 0; search.cursor = 0;
searching = true;
APP.displayDirectory([SEARCH]); APP.displayDirectory([SEARCH]);
return; return;
} }
if ($input.val()) {
if (!$input.hasClass('cp-app-drive-search-active')) { if (currentValue === "") {
$input.addClass('cp-app-drive-search-active'); search.cursor = 0;
} APP.displayDirectory([SEARCH]);
} else { return;
$input.removeClass('cp-app-drive-search-active');
} }
if (currentValue.length < 2) { return; } // Don't autosearch 1 character
search.to = window.setTimeout(function () { search.to = window.setTimeout(function () {
var newLocation = [SEARCH, $input.val()]; var newLocation = [SEARCH, $input.val()];
search.cursor = $input[0].selectionStart; search.cursor = $input[0].selectionStart;
if (currentValue === search.value) { return; }
if (!manager.comparePath(newLocation, currentPath.slice())) { if (!manager.comparePath(newLocation, currentPath.slice())) {
searching = true;
APP.displayDirectory(newLocation); APP.displayDirectory(newLocation);
} }
}, 500); }, 500);
}).on('click mousedown mouseup', function (e) { }).on('click mousedown mouseup', function (e) {
e.stopPropagation(); e.stopPropagation();
}).val(value || '').appendTo($div); }).val(value || '').appendTo($div);
if (value) { $input.addClass('cp-app-drive-search-active'); }
$input[0].selectionStart = search.cursor || 0; $input[0].selectionStart = search.cursor || 0;
$input[0].selectionEnd = search.cursor || 0; $input[0].selectionEnd = search.cursor || 0;
@ -3329,6 +3331,7 @@ define([
if (typeof(value) === "string" && value.trim()) { if (typeof(value) === "string" && value.trim()) {
spinner.spin(); spinner.spin();
} else { } else {
searching = false;
return; return;
} }
@ -3402,6 +3405,7 @@ define([
}); });
setTimeout(collapseDrivePath); setTimeout(collapseDrivePath);
spinner.hide(); spinner.hide();
searching = false;
}); });
}; };

Loading…
Cancel
Save