Skip to content

Add some keyboard interactions with the website first pages #324

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

Merged
merged 1 commit into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 77 additions & 1 deletion templates/releases.hbs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
{{> header}}

<script type="text/javascript" charset="utf-8">
function getKey(ev) {
if ("key" in ev && typeof ev.key != "undefined") {
return ev.key;
}
return String.fromCharCode(ev.charCode || ev.keyCode);
}
</script>

{{#if varsb.show_search_form}}
<div class="container landing">
<h1 class="brand"><i class="fa fa-cubes fa-fw"></i> Docs.rs</h1>

<form action="/releases/search" method="GET" class="landing-search-form">
<div><input class="search-input" name="query" type="text" placeholder="Search" autofocus></div>
<div><input class="search-input" id="search" name="query" type="text" placeholder="Click or press 'S' to search" autofocus></div>
<div class="buttons">
<button type="submit" class="pure-button pure-button-normal">Search</button>
<button type="submit" class="pure-button pure-button-normal" id="i-am-feeling-lucky-button">I'm Feeling Lucky</button>
Expand All @@ -21,6 +30,20 @@
document.getElementsByClassName("landing-search-form")[0].appendChild(input);
return true;
};
function handleShortcut(ev) {
if (ev.ctrlKey || ev.altKey || ev.metaKey || document.activeElement.tagName === "INPUT") {
return;
}
switch (getKey(ev)) {
case "s":
case "S":
ev.preventDefault();
document.getElementById("search").focus();
break;
}
}
document.onkeypress = handleShortcut;
document.onkeydown = handleShortcut;
</script>
</div>
{{/if}}
Expand All @@ -31,6 +54,59 @@
<div class="release">
<a href="/releases"><strong>Recent Releases</strong></a> <a href="/releases/feed" title="Atom feed"><i class="fa fa-rss-square"></i></a>
</div>
{{else}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the JS that registers the key handlers in the else branch of the template? Wouldn't that make it not appear on the front page?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the whole point! I want to be able to navigate through my search results, not on the front page.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But doesn't this also register the "S" shortcut?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the "S" shortcut is "registered" through handleShortcut whereas the up and down arrows are "registered" through handleKey.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, i see it now. I was confused because this also registered "S", but that's probably because the handler overwrites document.onkeypress/document.onkeydown. This looks fine, then.

<script type="text/javascript" charset="utf-8">
var active = null;

function handleKey(ev) {
if (ev.ctrlKey || ev.altKey || ev.metaKey || document.activeElement.tagName === "INPUT") {
return;
}
if (ev.which === 38) { // Down arrow
if (active === null) {
active = document.getElementsByClassName("recent-releases-container")[0].getElementsByTagName("li")[0];
} else if (active.nextElementSibling) {
active.classList.remove("selected");
active = active.nextElementSibling;
}
active.classList.add("selected");
} else if (ev.which === 40) { // Up arrow
if (active === null) {
active = document.getElementsByClassName("recent-releases-container")[0].getElementsByTagName("li")[0];
} else if (active.previousElementSibling) {
active.classList.remove("selected");
active = active.previousElementSibling;
}
active.classList.add("selected");
active.focus();
} else if (e.which === 13) { // Return
if (active !== null) {
document.location.href = active.getElementsByTagName("a")[0].href;
}
} else {
switch (getKey(ev)) {
case "s":
case "S":
ev.preventDefault();
document.getElementsByClassName("search-input-nav")[0].focus();
break;
}
}
}
document.onkeypress = handleKey;
document.onkeydown = handleKey;
var crates = Array.prototype.slice.call(document.getElementsByClassName("recent-releases-container")[0].getElementsByTagName("li"));
for (var i = 0; i < crates.length; ++i) {
crates[i].addEventListener("mouseover", function(event) {
this.classList.remove("selected");
active = null;
});
crates[i].addEventListener("mouseout", function(event) {
this.classList.remove("selected");
active = null;
});
}
</script>
{{/if}}

<ul>
Expand Down
2 changes: 1 addition & 1 deletion templates/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ div.recent-releases-container {
}
}

.release:hover {
.release:hover, li.selected > .release {
background-color: $color-background-code;
}

Expand Down