-
Notifications
You must be signed in to change notification settings - Fork 318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proof of concept for client-side search support (WIP) #221
Draft
JonUK
wants to merge
6
commits into
stefanjudis:master
Choose a base branch
from
JonUK:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7c63d10
Added a "skip to content" link (web accessibility improvement)
JonUK e3014a6
Merge pull request #1 from stefanjudis/master
JonUK de80d54
Working POF for client-side search
JonUK d40010e
Merge branch 'master' of https://github.com/stefanjudis/tiny-helpers
JonUK b71f10f
Merge remote-tracking branch 'origin/master'
JonUK 6fce128
Used an IIFE for better browser compatibility
JonUK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,7 +112,8 @@ static/screenshots | |
static/*.js | ||
static/*.xml | ||
static/*.html | ||
static/*.json | ||
TODO.txt | ||
|
||
#WebStorm IDE | ||
.idea | ||
.idea |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,77 @@ | ||
// This JavaScript is bundled together with the variable 'searchItems' from the | ||
// file 'static/searchData.json'. This variable is an array and contains items with | ||
// 'name' and 'desc' properties that are lowercase. | ||
|
||
(function () { | ||
const helpers = searchItems; | ||
|
||
// Get the search term from the querystring | ||
const urlParams = new URLSearchParams(window.location.search); | ||
const hasSearchTerm = urlParams.has('q'); | ||
const querySearchTerm = hasSearchTerm ? urlParams.get('q').toLowerCase() : null; | ||
|
||
const searchElement = document.getElementById('search'); | ||
|
||
// If no search input then not showing all items / filtering by tag so do nothing | ||
if (searchElement) { | ||
// Add the search input event listener with debounced handler and set value | ||
const debounceSearchFunc = debounce(filterItems, 250); | ||
searchElement.addEventListener('keydown', debounceSearchFunc); | ||
searchElement.value = querySearchTerm; | ||
|
||
// If there is a querystring search term then perform the initial filter | ||
if (querySearchTerm) { | ||
filterItems(); | ||
} | ||
} | ||
|
||
// Unhide the helpers list | ||
const helpersList = document.getElementById('helpers-list'); | ||
helpersList.classList.remove('js-helpers-hidden'); | ||
|
||
function filterItems() { | ||
const searchTerm = searchElement.value.toLowerCase(); | ||
|
||
console.time('Filtering helpers'); | ||
|
||
helpers.forEach(helper => { | ||
const element = document.getElementById(helper.id); | ||
const match = !searchTerm || helper.name.includes(searchTerm) || helper.desc.includes(searchTerm); | ||
|
||
if (!match) { | ||
element.style.display = 'none'; | ||
} else { | ||
element.style.display = 'flex'; | ||
} | ||
}); | ||
|
||
console.timeEnd('Filtering helpers'); | ||
} | ||
|
||
// Returns a function, that, as long as it continues to be invoked, will not | ||
// be triggered. The function will be called after it stops being called for | ||
// N milliseconds. If `immediate` is passed, trigger the function on the | ||
// leading edge, instead of the trailing. | ||
function debounce(func, wait, immediate) { | ||
let timeout; | ||
|
||
return function executedFunction() { | ||
const context = this; | ||
const args = arguments; | ||
|
||
const later = function() { | ||
timeout = null; | ||
if (!immediate) func.apply(context, args); | ||
}; | ||
|
||
const callNow = immediate && !timeout; | ||
|
||
clearTimeout(timeout); | ||
|
||
timeout = setTimeout(later, wait); | ||
|
||
if (callNow) func.apply(context, args); | ||
}; | ||
} | ||
|
||
})(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
permalink: search.js | ||
eleventyExcludeFromCollections: true | ||
--- | ||
const searchItems = {% include "../static/searchData.json" %}; | ||
|
||
{% set js %} | ||
{% include "js/search.js" %} | ||
{% endset %} | ||
{{ js | safe }} | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can do
jsmin
here to reduce the JS size down.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we should def do that. :)