-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Krzysztof Słomka <[email protected]> Co-authored-by: Łukasz Ostafin <[email protected]>
- Loading branch information
1 parent
5be86ac
commit 61a6687
Showing
21 changed files
with
622 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/bundle/Resources/public/js/scripts/admin.search.autocomplete.content.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
(function (global, doc, ibexa, Routing) { | ||
const globalSearch = doc.querySelector('.ibexa-global-search'); | ||
|
||
if (!globalSearch) { | ||
return; | ||
} | ||
|
||
const { escapeHTML } = ibexa.helpers.text; | ||
const { highlightText } = ibexa.helpers.highlight; | ||
const { getContentTypeIconUrl, getContentTypeName } = ibexa.helpers.contentType; | ||
const autocompleteListNode = globalSearch.querySelector('.ibexa-global-search__autocomplete-list'); | ||
const autocompleteContentTemplateNode = globalSearch.querySelector('.ibexa-global-search__autocomplete-content-template'); | ||
const renderItem = (result, searchText) => { | ||
const { locationId, contentId, name, contentTypeIdentifier, pathString, parentLocations } = result; | ||
const pathArray = pathString.split('/').filter((id) => id); | ||
|
||
const breadcrumb = pathArray.reduce((total, pathLocationId, index) => { | ||
const parentLocation = parentLocations.find((parent) => parent.locationId === parseInt(pathLocationId, 10)); | ||
|
||
if (parseInt(pathLocationId, 10) === locationId) { | ||
return total; | ||
} | ||
|
||
return index === 0 ? parentLocation.name : `${total} / ${parentLocation.name}`; | ||
}, ''); | ||
|
||
const autocompleteItemTemplate = autocompleteContentTemplateNode.dataset.templateItem; | ||
const autocompleteHighlightTemplate = autocompleteListNode.dataset.templateHighlight; | ||
const renderedTemplate = autocompleteItemTemplate | ||
.replace('{{ contentName }}', highlightText(searchText, name, autocompleteHighlightTemplate)) | ||
.replace('{{ iconHref }}', getContentTypeIconUrl(contentTypeIdentifier)) | ||
.replace('{{ contentTypeName }}', escapeHTML(getContentTypeName(contentTypeIdentifier))) | ||
.replaceAll('{{ contentBreadcrumbs }}', breadcrumb) | ||
.replace('{{ contentHref }}', Routing.generate('ibexa.content.view', { contentId, locationId })); | ||
|
||
return renderedTemplate; | ||
}; | ||
|
||
ibexa.addConfig('autocomplete.renderers.content', renderItem, true); | ||
})(window, document, window.ibexa, window.Routing); |
148 changes: 148 additions & 0 deletions
148
src/bundle/Resources/public/js/scripts/admin.search.autocomplete.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
(function (global, doc, ibexa, Routing, Translator) { | ||
const globalSearch = doc.querySelector('.ibexa-global-search'); | ||
const { getJsonFromResponse } = ibexa.helpers.request; | ||
const { showErrorNotification } = ibexa.helpers.notification; | ||
const { minQueryLength, resultLimit } = ibexa.adminUiConfig.suggestions; | ||
|
||
if (!globalSearch) { | ||
return; | ||
} | ||
|
||
const globalSearchInput = globalSearch.querySelector('.ibexa-global-search__input'); | ||
const clearBtn = globalSearch.querySelector(' .ibexa-input-text-wrapper__action-btn--clear'); | ||
const autocompleteNode = globalSearch.querySelector('.ibexa-global-search__autocomplete'); | ||
const autocompleteListNode = globalSearch.querySelector('.ibexa-global-search__autocomplete-list'); | ||
let searchAbortController; | ||
const showResults = (searchText, results) => { | ||
const { renderers } = ibexa.autocomplete; | ||
const fragment = doc.createDocumentFragment(); | ||
|
||
results.forEach((result) => { | ||
const container = doc.createElement('ul'); | ||
const renderer = renderers[result.type]; | ||
|
||
if (!renderer) { | ||
return; | ||
} | ||
|
||
const renderedTemplate = renderer(result, searchText); | ||
|
||
container.insertAdjacentHTML('beforeend', renderedTemplate); | ||
|
||
const listItemNode = container.querySelector('li'); | ||
|
||
fragment.append(listItemNode); | ||
}); | ||
|
||
addClickOutsideEventListener(); | ||
addKeyboardEventListener(); | ||
|
||
autocompleteListNode.innerHTML = ''; | ||
autocompleteListNode.append(fragment); | ||
|
||
window.ibexa.helpers.ellipsis.middle.parse(autocompleteListNode); | ||
|
||
autocompleteNode.classList.remove('ibexa-global-search__autocomplete--hidden'); | ||
autocompleteNode.classList.toggle('ibexa-global-search__autocomplete--results-empty', results.length === 0); | ||
}; | ||
const getAutocompleteList = (searchText) => { | ||
const url = Routing.generate('ibexa.search.suggestion', { query: searchText, limit: resultLimit }); | ||
const request = new Request(url, { | ||
mode: 'same-origin', | ||
credentials: 'same-origin', | ||
}); | ||
|
||
searchAbortController = new AbortController(); | ||
|
||
const { signal } = searchAbortController; | ||
|
||
fetch(request, { signal }) | ||
.then(getJsonFromResponse) | ||
.then(showResults.bind(this, searchText)) | ||
.catch((error) => { | ||
if (error.name === 'AbortError') { | ||
return; | ||
} | ||
|
||
showErrorNotification( | ||
Translator.trans(/*@Desc("Cannot load suggestions")*/ 'autocomplete.request.error', {}, 'ibexa_search'), | ||
); | ||
}); | ||
}; | ||
const handleTyping = (event) => { | ||
const searchText = event.currentTarget.value.trim(); | ||
|
||
searchAbortController?.abort(); | ||
|
||
if (searchText.length <= minQueryLength) { | ||
hideAutocomplete(); | ||
|
||
return; | ||
} | ||
|
||
getAutocompleteList(searchText); | ||
}; | ||
const handleClickOutside = ({ target }) => { | ||
if (target.closest('.ibexa-global-search')) { | ||
return; | ||
} | ||
|
||
hideAutocomplete(); | ||
}; | ||
const addClickOutsideEventListener = () => { | ||
doc.body.addEventListener('click', handleClickOutside, false); | ||
}; | ||
const removeClickOutsideEventListener = () => { | ||
doc.body.removeEventListener('click', handleClickOutside, false); | ||
}; | ||
const handleKeyboard = ({ code }) => { | ||
const keyboardDispatcher = { | ||
ArrowDown: handleArrowDown, | ||
ArrowUp: handleArrowUp, | ||
}; | ||
|
||
keyboardDispatcher[code]?.(); | ||
}; | ||
const handleArrowDown = () => { | ||
const focusedItemElement = autocompleteListNode.querySelector('.ibexa-global-search__autocomplete-item-link:focus'); | ||
const focusedViewAllElement = autocompleteNode.querySelector('.ibexa-global-search__autocomplete-view-all .ibexa-btn:focus'); | ||
|
||
if (!focusedItemElement && !focusedViewAllElement) { | ||
autocompleteListNode.firstElementChild.firstElementChild.focus(); | ||
|
||
return; | ||
} | ||
|
||
if (focusedItemElement?.parentElement?.nextElementSibling) { | ||
focusedItemElement.parentElement.nextElementSibling.firstElementChild.focus(); | ||
} else { | ||
autocompleteNode.querySelector('.ibexa-global-search__autocomplete-view-all .ibexa-btn').focus(); | ||
} | ||
}; | ||
const handleArrowUp = () => { | ||
const focusedItemElement = autocompleteListNode.querySelector('.ibexa-global-search__autocomplete-item-link:focus'); | ||
const focusedViewAllElement = autocompleteNode.querySelector('.ibexa-global-search__autocomplete-view-all .ibexa-btn:focus'); | ||
|
||
if (focusedViewAllElement) { | ||
autocompleteListNode.lastElementChild.firstElementChild.focus(); | ||
|
||
return; | ||
} | ||
|
||
focusedItemElement?.parentElement?.previousElementSibling.firstElementChild.focus(); | ||
}; | ||
const addKeyboardEventListener = () => { | ||
doc.body.addEventListener('keydown', handleKeyboard, false); | ||
}; | ||
const removeKeyboardEventListener = () => { | ||
doc.body.removeEventListener('keydown', handleKeyboard, false); | ||
}; | ||
const hideAutocomplete = () => { | ||
autocompleteNode.classList.add('ibexa-global-search__autocomplete--hidden'); | ||
removeClickOutsideEventListener(); | ||
removeKeyboardEventListener(); | ||
}; | ||
|
||
globalSearchInput.addEventListener('keyup', handleTyping, false); | ||
clearBtn.addEventListener('click', hideAutocomplete, false); | ||
})(window, document, window.ibexa, window.Routing, window.Translator); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/bundle/Resources/public/js/scripts/helpers/highlight.helper.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
(function (global, doc, ibexa) { | ||
const { escapeHTML } = ibexa.helpers.text; | ||
const highlightText = (searchText, string, template) => { | ||
const stringLowerCase = string.toLowerCase(); | ||
const searchTextLowerCase = searchText.toLowerCase(); | ||
const matches = stringLowerCase.matchAll(searchTextLowerCase); | ||
const stringArray = []; | ||
let previousIndex = 0; | ||
|
||
for (const match of matches) { | ||
const endOfSearchTextIndex = match.index + searchText.length; | ||
const renderedTemplate = template.replace('{{ highlightText }}', escapeHTML(string.slice(match.index, endOfSearchTextIndex))); | ||
|
||
stringArray.push(escapeHTML(string.slice(previousIndex, match.index))); | ||
stringArray.push(renderedTemplate); | ||
|
||
previousIndex = match.index + searchText.length; | ||
} | ||
|
||
stringArray.push(escapeHTML(string.slice(previousIndex))); | ||
|
||
return stringArray.join(''); | ||
}; | ||
|
||
ibexa.addConfig('helpers.highlight', { | ||
highlightText, | ||
}); | ||
})(window, window.document, window.ibexa); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.