diff options
author | Michael Große <grosse@cosmocode.de> | 2018-03-20 16:14:00 +0100 |
---|---|---|
committer | Michael Große <grosse@cosmocode.de> | 2018-03-20 16:17:28 +0100 |
commit | bb8ef86758882cd38a7bbafff62a3bc807ffe056 (patch) | |
tree | ff41345d776e63b5dcb0455d087292c31c2cc268 /lib/scripts | |
parent | 427ed988282f72cb160d09b0830f843d462cc93a (diff) | |
download | dokuwiki-bb8ef86758882cd38a7bbafff62a3bc807ffe056.tar.gz dokuwiki-bb8ef86758882cd38a7bbafff62a3bc807ffe056.zip |
feat(search): add search assistance for simple queries
This add some search assistance to simple, single-word search queries
which may be restricted to a single namespace.
Further improvements:
* better styling
* trigger events for other plugins
* set namespaces directly from fulltext search results
* some more config options
Diffstat (limited to 'lib/scripts')
-rw-r--r-- | lib/scripts/search.js | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/lib/scripts/search.js b/lib/scripts/search.js new file mode 100644 index 000000000..0c9dca76a --- /dev/null +++ b/lib/scripts/search.js @@ -0,0 +1,67 @@ +jQuery(function () { + 'use strict'; + + const $searchForm = jQuery('.search-results-form'); + if (!$searchForm.length) { + return; + } + if (!$searchForm.find('#search-results-form__show-assistance-button').length){ + return; + } + const $toggleAssistanceButton = $searchForm.find('#search-results-form__show-assistance-button'); + const $queryInput = $searchForm.find('[name="id"]'); + const $termInput = $searchForm.find('[name="searchTerm"]'); + + $toggleAssistanceButton.on('click', function () { + jQuery('.js-advancedSearchOptions').toggle(); + $queryInput.toggle(); + $termInput.toggle(); + }); + + + const $matchTypeSwitcher = $searchForm.find('[name="matchType"]'); + const $namespaceSwitcher = $searchForm.find('[name="namespace"]'); + const $refiningElements = $termInput.add($matchTypeSwitcher).add($namespaceSwitcher); + $refiningElements.on('input change', function () { + $queryInput.val( + rebuildQuery( + $termInput.val(), + $matchTypeSwitcher.filter(':checked').val(), + $namespaceSwitcher.filter(':checked').val() + ) + ); + }); + + /** + * Rebuild the search query from the parts + * + * @param {string} searchTerm the word which is to be searched + * @param {enum} matchType the type of matching that is to be done + * @param {string} namespace list of namespaces to which to limit the search + * + * @return {string} the query string for the actual search + */ + function rebuildQuery(searchTerm, matchType, namespace) { + let query = ''; + + switch (matchType) { + case 'contains': + query = '*' + searchTerm + '*'; + break; + case 'starts': + query = '*' + searchTerm; + break; + case 'ends': + query = searchTerm + '*'; + break; + default: + query = searchTerm; + } + + if (namespace && namespace.length) { + query += ' @' + namespace; + } + + return query; + } +}); |