Skip to content
This repository has been archived by the owner on Jan 1, 2022. It is now read-only.

Commit

Permalink
Global search (#268)
Browse files Browse the repository at this point in the history
Global search

Co-authored-by: Dirk Kelly <github@dirkkelly.com>
  • Loading branch information
jmickey and dirkkelly committed May 16, 2019
2 parents 4521f8a + 2296d16 commit c455503
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 22 deletions.
2 changes: 1 addition & 1 deletion data/videos/meanstv/8uaInDOjYyw.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ title: "Capitalism and Me"
channel: meanstv channel: meanstv
description: "Mond Sankara explains what alienation of labor is and why it makes you feel like shit all the time. description: "Mond Sankara explains what alienation of labor is and why it makes you feel like shit all the time.
We need your help to build entertainment for all of us. We can't do it without you. SUPPORT: https://actionnetwork.org/fundraising/lets-build-means-tv" We need your help to build entertainment for all of us. We can't do it without you."
series: series:
source: youtube source: youtube
1 change: 1 addition & 0 deletions layouts/_default/baseof.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
{{ end }} {{ end }}
</main> </main>
{{ partial "base/footer.html" . }} {{ partial "base/footer.html" . }}
{{ partial "search/code.html" . }}
</body> </body>
</html> </html>
4 changes: 0 additions & 4 deletions layouts/channels/list.html
Original file line number Original file line Diff line number Diff line change
@@ -1,9 +1,5 @@
{{ define "main" }} {{ define "main" }}
<div class="header-action" style="margin-top: 10px">
{{ partial "search/bar.html" . }}
</div>
{{ partial "channels/row.html" . }} {{ partial "channels/row.html" . }}
{{ partial "search/code.html" . }}
<div class="header-action" style="margin-top: 10px"> <div class="header-action" style="margin-top: 10px">
<a href="/channels/new/" class="header-add">Add Channel</a> <a href="/channels/new/" class="header-add">Add Channel</a>
</div> </div>
Expand Down
5 changes: 4 additions & 1 deletion layouts/partials/base/navigation.html
Original file line number Original file line Diff line number Diff line change
@@ -1,8 +1,11 @@
<nav class="nav"> <nav class="nav">
<div class="header-action" style="margin-top: 10px">
{{ partial "search/bar.html" . }}
</div>
<ul class="nav-list plain-list"> <ul class="nav-list plain-list">
{{- $currentPage := . -}} {{- $currentPage := . -}}
{{ range .Site.Menus.main -}} {{ range .Site.Menus.main -}}
<li> <li class="nav-item">
<a href="{{ .URL | absLangURL }}" class="nav-link {{ if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }} is-active{{ end }}">{{ .Name }}</a> <a href="{{ .URL | absLangURL }}" class="nav-link {{ if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }} is-active{{ end }}">{{ .Name }}</a>
</li> </li>
{{- end }} {{- end }}
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/playlists/row.html
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
<h2 class="list-heading"> <h2 class="list-heading heading">
<a href="{{ .Permalink }}">{{ .Title }}</a> <a href="{{ .Permalink }}">{{ .Title }}</a>
</h2> </h2>
{{ partial "playlists/videos.html" . }} {{ partial "playlists/videos.html" . }}
4 changes: 2 additions & 2 deletions layouts/partials/search/bar.html
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
<div> <div>
<label for="search" class="visually-hidden">Search Channels</label> <label for="search" class="visually-hidden">Search</label>
<input id="search" type="search" placeholder="Search Channels" class="header-search" autofocus /> <input id="search" type="search" placeholder="Search" class="header-search" autofocus />
</div> </div>
50 changes: 39 additions & 11 deletions layouts/partials/search/code.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@
'description': {{ .description }}, 'description': {{ .description }},
}, },
{{ end }} {{ end }}
] ].concat([
{{- range $channelSlug, $videos := .Site.Data.videos -}}
{{- range $videoID, $video := $videos -}}
{
'name': "{{ $video.title }}",
'slug': "{{ $video.id }}",
'tags': {{ $video.tags }},
'channel': "{{ $video.channel }}",
'description': {{ $video.description }},
},
{{- end -}}
{{ end }}
])

var options = { var options = {
shouldSort: true, shouldSort: true,
threshold: 0.4, threshold: 0.4,
Expand All @@ -18,26 +31,30 @@
minMatchCharLength: 1, minMatchCharLength: 1,
id: "slug", id: "slug",
keys: [ keys: [
"name", "slug", "tags", "description" "name", "slug", "tags", "channel", "description"
] ]
}; };
var fuse = new Fuse(list, options); var fuse = new Fuse(list, options);
var search = document.getElementById("search"); var search = document.getElementById("search");
var list = document.getElementsByClassName("list")[0]; var lists = document.getElementsByClassName("list");
var items = list.children;


function listSearch(query) { function listSearch(query) {
list.classList.toggle('searching', query.length > 0); if(search.value.length == 0) {
search.value = query.replace("%20"," ");
}


window.location.hash = 'search:' + query.toLowerCase();; window.location.hash = 'search:' + query.toLowerCase();;
var results = fuse.search(query); var results = fuse.search(query);
for (var i = 0; i < items.length; i++) {
var item = items[i];
item.classList.toggle('found', results.indexOf(item.id) > -1);
}


if(search.value.length == 0) { for (var i = 0; i < lists.length; i++) {
search.value = query.replace("%20"," "); var list = lists[i];
var items = list.children;
list.classList.toggle('searching', query.length > 0);

for (var j = 0; j < items.length; j++) {
var item = items[j];
item.classList.toggle('found', results.indexOf(item.id) > -1);
}
} }
} }


Expand All @@ -46,6 +63,17 @@
listSearch(query); listSearch(query);
}); });


var navlinks = document.getElementsByClassName("nav-link");

/* Persist search in nav links */
for (var i = 0; i < navlinks.length; i++) {
var link = navlinks[i];
link.addEventListener("click", function(event) {
event.preventDefault();
window.location = event.target.href + location.hash;
});
}

if(window.location.hash && window.location.hash.indexOf('search') > -1) { if(window.location.hash && window.location.hash.indexOf('search') > -1) {
listSearch(window.location.hash.replace("#search:","")); listSearch(window.location.hash.replace("#search:",""));
} }
Expand Down
4 changes: 2 additions & 2 deletions layouts/partials/videos/card.html
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ if .videoIframe }} {{ if .videoIframe }}
<li class="channel-video"> <li id="{{ .video.id }}" class="channel-video list-item">
<ul> <ul>
<li class="video"> <li class="video">
<div class="video-item"> <div class="video-item">
Expand All @@ -18,7 +18,7 @@
</ul> </ul>
</li> </li>
{{ else }} {{ else }}
<li class="video-item"> <li id="{{ .video.id }}" class="video-item list-item">
<div class="video"> <div class="video">
<div class="video-link-container"> <div class="video-link-container">
<div class="video-image"> <div class="video-image">
Expand Down

0 comments on commit c455503

Please sign in to comment.