-
Notifications
You must be signed in to change notification settings - Fork 4
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
offline search for mirror drives #1175
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
230b925
add fuse and proof of concept of course search
gumaerc c6711fb
fix styling on search page and add search button
gumaerc cc58ae7
display search icon with proper link on pages that aren't the search …
gumaerc 7725114
make clicking the logo go to the index
gumaerc 3f6407f
organize and comment code
gumaerc 70fec64
only pad the search results area on desktop
gumaerc 3dec40b
use basic string templating instead of custom web component
gumaerc a2ca5ad
lower minimum match characters to 3
gumaerc 3041b46
add placeholders for empty search results
gumaerc 678b591
search button of type button and remove preventdefault
gumaerc 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
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,2 @@ | ||
{{ $siteRootUrl := partial "site_root_url.html" "/" }} | ||
{{ return $siteRootUrl }} |
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 |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import "./css/www-offline.scss" | ||
import Fuse from "fuse.js" | ||
import Popper from "popper.js" | ||
|
||
export interface OCWWindow extends Window { | ||
$: JQueryStatic | ||
jQuery: JQueryStatic | ||
Popper: typeof Popper | ||
Fuse: typeof Fuse | ||
} | ||
declare let window: OCWWindow | ||
|
||
window.jQuery = $ | ||
window.$ = $ | ||
window.Popper = Popper | ||
window.Fuse = Fuse |
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,5 @@ | ||
--- | ||
title: Search | ||
type: search | ||
renderSearchIcon: false | ||
--- |
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,8 @@ | ||
{{ define "extraheader" }} | ||
{{ $renderSearchIcon := index .Params "renderSearchIcon" | default true}} | ||
{{ if $renderSearchIcon}} | ||
<a class="nav-link search-icon pr-6" href="/search.html"> | ||
<i class="material-icons">search</i> | ||
</a> | ||
{{end}} | ||
{{ end }} |
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 @@ | ||
{{ return "/index.html" }} |
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,165 @@ | ||
{{ define "main" }} | ||
{{ $pathToRoot := strings.TrimSuffix "/" (partial "path_to_root.html" .Permalink) }} | ||
{{ $websites := where site.Pages ".Params.content_type" "==" "website" }} | ||
{{ block "header" . }} | ||
{{ partial "header" . }} | ||
{{ end }} | ||
<div class="search-wrapper"> | ||
<div class="search-page row justify-content-center"> | ||
<div class="search-results-area col-12 py-4"> | ||
<div class="container-fluid"> | ||
<main> | ||
<div class="container-fluid p-0"> | ||
<div class="row m-0"> | ||
<div id="search-page" class="w-100"> | ||
<div class="container"> | ||
<div class="search-box py-sm-5 py-md-7 py-lg-5 row"> | ||
<div class="col-lg-3"></div> | ||
<div class="col-lg-6 search-box-inner d-flex flex-column align-items-center mb-2 mb-sm-5 mb-md-4"> | ||
<h1>Explore OpenCourseWare</h1> | ||
<div class="w-100 d-flex flex-column align-items-center search-input-wrapper mt-5"> | ||
<div class="w-100"> | ||
<form role="search" class="search-box d-flex flex-direction-row"> | ||
<input | ||
id="search-input" | ||
class="w-100 pl-2" | ||
type="text" | ||
aria-label="Search" | ||
placeholder="Search OpenCourseWare" | ||
/> | ||
<button id="search-button" type="submit" class="py-2 px-3">Search</button> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="col-lg-3"></div> | ||
</div> | ||
</div> | ||
<section id="search-results" class="m-auto px-lg-8"> | ||
</section> | ||
</div> | ||
</div> | ||
</div> | ||
</main> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<script> | ||
window.onload = function() { | ||
if (window.Fuse) { | ||
// define the search-result web component and add it for use | ||
const searchResultTemplate = document.createElement("template") | ||
searchResultTemplate.innerHTML = ` | ||
<article> | ||
<div class="card learning-resource-card compact-view"> | ||
<div class="card-contents"> | ||
<div class="lr-info search-result"> | ||
<div class="col-2 course-num"> | ||
</div> | ||
<div class="course-title"> | ||
<a class="course-link"></a> | ||
</div> | ||
<div class="col-2 resource-level"> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</article> | ||
` | ||
|
||
class SearchResult extends HTMLElement { | ||
constructor() { | ||
super() | ||
} | ||
|
||
connectedCallback() { | ||
this.innerHTML = searchResultTemplate.innerHTML | ||
this.courseNumberDiv = this.querySelector(".course-num") | ||
this.courseLink = this.querySelector(".course-link") | ||
this.levelDiv = this.querySelector(".resource-level") | ||
this.courseNumberDiv.innerHTML = this.primaryCourseNumber | ||
this.courseLink.setAttribute("href", this.url) | ||
this.courseLink.innerHTML = this.title | ||
this.levelDiv.innerHTML = this.level | ||
} | ||
|
||
get title() { | ||
return this.getAttribute("data-title") | ||
} | ||
|
||
get primaryCourseNumber() { | ||
return this.getAttribute("data-primary-course-number") | ||
} | ||
|
||
get level() { | ||
return this.getAttribute("data-level") | ||
} | ||
|
||
get url() { | ||
return this.getAttribute("data-url") | ||
} | ||
} | ||
|
||
window.customElements.define("search-result", SearchResult) | ||
|
||
// an array of known websites generated at build time | ||
const websites = [ | ||
{{ $websites := where site.Pages ".Params.content_type" "==" "website" }} | ||
{{ range $websites }} | ||
{{ $urlPath := strings.TrimPrefix "/" .Params.url_path }} | ||
{{ $url := printf "%s/%s/index.html" $pathToRoot $urlPath }} | ||
{ | ||
"title": {{- .Title | jsonify -}}, | ||
"primary_course_number": {{- .Params.primary_course_number | jsonify -}}, | ||
"url": {{- $url -}}, | ||
{{ if .Params.level }} | ||
"level": {{ (delimit .Params.level ", ") | jsonify }} | ||
{{ else }} | ||
"level": "" | ||
{{ end }} | ||
}, | ||
{{ end }} | ||
] | ||
|
||
// initialize Fuse and bind event listeners | ||
const Fuse = window.Fuse | ||
const searchOptions = { | ||
shouldSort: true, | ||
threshold: 0.4, | ||
location: 0, | ||
distance: 100, | ||
matchAllTokens: true, | ||
includeScore: true, | ||
maxPatternLength: 32, | ||
minMatchCharLength: 5, | ||
keys: ["title", "primary_course_number"] | ||
} | ||
const fuse = new Fuse(websites, searchOptions) | ||
const searchInput = document.getElementById("search-input") | ||
const searchButton = document.getElementById("search-button") | ||
const performSearch = searchString => { | ||
const searchResults = fuse.search(searchString) | ||
const searchResultComponents = searchResults.map(searchResult => { | ||
return `<search-result data-url="${searchResult.item["url"]}" data-title=${searchResult.item["title"]} data-primary-course-number=${searchResult.item["primary_course_number"]} data-level=${searchResult.item["level"]}></search-result>` | ||
}) | ||
const searchResultsSection = document.getElementById("search-results") | ||
searchResultsSection.innerHTML = searchResultComponents.join("") | ||
} | ||
const onInput = e => { | ||
if (e.target.value) { | ||
const searchString = e.target.value | ||
performSearch(searchString) | ||
} | ||
} | ||
const onSearchButtonClick = e => { | ||
e.preventDefault() | ||
performSearch(searchInput.value) | ||
} | ||
|
||
searchInput.addEventListener("input", onInput) | ||
searchButton.addEventListener("click", onSearchButtonClick) | ||
} | ||
} | ||
</script> | ||
{{ end }} |
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
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.
Then you won't need to prevent default down below.