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

Commit

Permalink
Allow search results to be clicked before blur events happen (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Ratcliffe authored and aciccarello committed Aug 25, 2018
1 parent f992ea3 commit c71e7dc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
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();
}
});
}
}

0 comments on commit c71e7dc

Please sign in to comment.