bolded search matches / small refactor

This commit is contained in:
Cade Scroggins
2017-06-28 23:42:49 -07:00
parent a0a611dcf6
commit f7379c8651
+25 -21
View File
@@ -118,7 +118,9 @@
} }
input, input,
input:focus { button,
input:focus,
button:focus {
box-sizing: border-box; box-sizing: border-box;
width: 100%; width: 100%;
margin: 0; margin: 0;
@@ -428,7 +430,7 @@
class DuckDuckGo { class DuckDuckGo {
constructor(suggestionsLimit = 0) { constructor(suggestionsLimit = 0) {
this._endpoint = 'https://duckduckgo.com/ac'; this._endpoint = 'https://duckduckgo.com/ac/';
this._callback = 'autocompleteCallback'; this._callback = 'autocompleteCallback';
this._suggestionsLimit = suggestionsLimit; this._suggestionsLimit = suggestionsLimit;
} }
@@ -467,8 +469,6 @@
} }
suggest(input, clickCallback = () => {}) { suggest(input, clickCallback = () => {}) {
input = input.trim();
if (!input) { if (!input) {
this._clearSuggestions(); this._clearSuggestions();
return; return;
@@ -478,15 +478,20 @@
this._suggest(input); this._suggest(input);
} }
_appendSuggestion(suggestion) { _appendSuggestion(suggestion, input) {
const suggestionHtml = suggestion
.replace(new RegExp(input, 'g'), `<b>${input}</b>`);
this._suggestionsEl.insertAdjacentHTML( this._suggestionsEl.insertAdjacentHTML(
'beforeend', 'beforeend',
`<li> `<li>
<input <button
class="js-search-suggestion search-suggestion"
type="button" type="button"
value="${suggestion}" class="js-search-suggestion search-suggestion"
data-suggestion="${suggestion}"
> >
${suggestionHtml}
</button>
</li>` </li>`
); );
@@ -561,7 +566,8 @@
_registerClickEvents() { _registerClickEvents() {
this._suggestionEls.forEach(el => { this._suggestionEls.forEach(el => {
el.addEventListener('click', this._handleClick.bind(null, el.value)); const value = el.getAttribute('data-suggestion');
el.addEventListener('click', this._handleClick.bind(null, value));
}); });
} }
@@ -570,7 +576,7 @@
this._clearSuggestions(); this._clearSuggestions();
this._flattenAndUnique(res) this._flattenAndUnique(res)
.some(item => this._appendSuggestion(item)); .some(item => this._appendSuggestion(item, input));
this._suggestionEls = $.els('.js-search-suggestion'); this._suggestionEls = $.els('.js-search-suggestion');
this._registerClickEvents(); this._registerClickEvents();
@@ -680,6 +686,7 @@
_handleKeypress(event) { _handleKeypress(event) {
const newChar = String.fromCharCode(event.which); const newChar = String.fromCharCode(event.which);
const newQuery = this._inputEl.value + newChar;
const isNotEmpty = newChar.length; const isNotEmpty = newChar.length;
const isEnterKey = event.which !== 13; const isEnterKey = event.which !== 13;
@@ -689,21 +696,18 @@
} }
if (CONFIG.instantRedirect) { if (CONFIG.instantRedirect) {
this._queryParser.instantRedirect( this._queryParser
event, .instantRedirect(event, newQuery, this._submitWithValue);
this._inputEl.value + newChar,
this._submitWithValue
);
} }
} }
_handleKeyup(event) { _handleKeyup(event) {
if ( const oldVal = this._inputElVal;
CONFIG.suggestions && const newVal = this._inputEl.value.trim();
this._inputElVal.trim() !== this._inputEl.value.trim() this._inputElVal = this._inputEl.value;
) {
this._suggester.suggest(this._inputEl.value, this._submitWithValue); if (CONFIG.suggestions && oldVal !== newVal) {
this._inputElVal = this._inputEl.value; this._suggester.suggest(newVal, this._submitWithValue);
} }
} }