Skip to content
This repository has been archived by the owner on Aug 14, 2021. It is now read-only.

Allow search results to be clicked before blur events happen #59

Merged
merged 1 commit into from
Aug 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/default/assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ var typedoc;
var hasFocus = false;
var preventPress = false;
var index;
var resultClicked = false;
function createIndex() {
index = new lunr.Index();
index.pipeline.add(lunr.trimmer);
Expand Down Expand Up @@ -499,10 +500,22 @@ var typedoc;
$field.blur();
}
}
$results
.on('mousedown', function () {
resultClicked = true;
})
.on('mouseup', function () {
resultClicked = false;
setHasFocus(false);
});
$field.on('focusin', function () {
setHasFocus(true);
loadIndex();
}).on('focusout', function () {
if (resultClicked) {
resultClicked = false;
return;
}
setTimeout(function () { return setHasFocus(false); }, 100);
}).on('input', function () {
setQuery($.trim($field.val()));
Expand Down
29 changes: 28 additions & 1 deletion src/default/assets/js/src/typedoc/components/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ module typedoc.search
*/
var index:lunr.Index;

/**
* Has a search result been clicked?
* Used to stop the results hiding before a user can fully click on a result.
*/
var resultClicked:boolean = false;


/**
* Instantiate the lunr index.
Expand Down Expand Up @@ -223,6 +229,19 @@ module typedoc.search
}
}

/**
* Intercept mousedown and mouseup events so we can correctly
* handle clicking on search results
*/
$results
.on('mousedown', () => {
resultClicked = true;
})
.on('mouseup', () => {
resultClicked = false;
setHasFocus(false);
});


/**
* Bind all required events on the input field.
Expand All @@ -231,6 +250,14 @@ module typedoc.search
setHasFocus(true);
loadIndex();
}).on('focusout', () => {
// If the user just clicked on a search result, then
// don't lose the focus straight away, as this prevents
// them from clicking the result and following the link
if(resultClicked) {
resultClicked = false;
return;
}

setTimeout(() => setHasFocus(false), 100);
}).on('input', () => {
setQuery($.trim($field.val()));
Expand Down Expand Up @@ -265,4 +292,4 @@ module typedoc.search
$field.focus();
}
});
}
}