Skip to content

Commit

Permalink
add sort by select-box for searching crate
Browse files Browse the repository at this point in the history
  • Loading branch information
linyihai committed Oct 31, 2023
1 parent 4514194 commit 20971eb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/web/releases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ pub(super) struct Search {
#[serde(rename = "releases")]
pub(super) results: Vec<Release>,
pub(super) search_query: Option<String>,
pub(super) search_sort_by: Option<String>,
pub(super) previous_page_link: Option<String>,
pub(super) next_page_link: Option<String>,
/// This should always be `ReleaseType::Search`
Expand All @@ -451,6 +452,7 @@ impl Default for Search {
search_query: None,
previous_page_link: None,
next_page_link: None,
search_sort_by: None,
release_type: ReleaseType::Search,
status: http::StatusCode::OK,
}
Expand Down Expand Up @@ -528,7 +530,10 @@ pub(crate) async fn search_handler(
.get("query")
.map(|q| q.to_string())
.unwrap_or_else(|| "".to_string());

let sort_by = params
.get("sort")
.map(|q| q.to_string())
.unwrap_or_else(|| "relevance".to_string());
// check if I am feeling lucky button pressed and redirect user to crate page
// if there is a match. Also check for paths to items within crates.
if params.remove("i-am-feeling-lucky").is_some() || query.contains("::") {
Expand Down Expand Up @@ -599,6 +604,7 @@ pub(crate) async fn search_handler(
} else if !query.is_empty() {
let query_params: String = form_urlencoded::Serializer::new(String::new())
.append_pair("q", &query)
.append_pair("sort", &sort_by)
.append_pair("per_page", &RELEASES_IN_RELEASES.to_string())
.finish();

Expand All @@ -619,6 +625,7 @@ pub(crate) async fn search_handler(
title,
results: search_result.results,
search_query: Some(executed_query),
search_sort_by: Some(sort_by),
next_page_link: search_result
.next_page
.map(|params| format!("/releases/search?paginate={}", b64.encode(params))),
Expand Down
14 changes: 13 additions & 1 deletion static/keyboard.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function() {
(function () {
function focusSearchInput() {
// On the index page, we have a "#search" input. If we are on this page, we want to go back
// to this one and not the one in the header navbar.
Expand Down Expand Up @@ -110,6 +110,18 @@
}
}

function handleSortByChange() {
var inputSearch = document.getElementById("nav-search");
var searchForm = document.getElementById("nav-search-form");
if (inputSearch.value && searchForm) {
searchForm.submit()
}
}
var searchSortBySel = document.getElementById("nav-sort");
if (searchSortBySel) {
searchSortBySel.addEventListener("change", handleSortByChange)
}

document.onkeypress = handleKey;
document.onkeydown = handleKey;
})();
1 change: 1 addition & 0 deletions templates/header/topbar_begin.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<div class="pure-menu pure-menu-horizontal" role="navigation" aria-label="Main navigation">
<form action="/releases/search"
method="GET"
id = "nav-search-form"
class="landing-search-form-nav {%
if is_latest_version is defined and not is_latest_version %}not-latest{% endif
%} {% if metadata.yanked %}yanked{% endif %}">
Expand Down
13 changes: 13 additions & 0 deletions templates/header/topbar_end.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@
<input id="nav-search" name="query" type="text" aria-label="Find crate by search query" tabindex="-1"
placeholder="Find crate" {%- if search_query %} value="{{ search_query }}" {%- endif %}>
</div>
{# The Search Sort by dropdown menu#}
<div id="search-select-nav">
<label for="nav-sort">
{{ "list" | fas }}
</label>
<select name="sort" id="nav-sort">
<option value="relevance" {%- if search_sort_by and search_sort_by == "relevance" %} selected="selected" {%- endif %}>Relevance</option>
<option value="downloads" {%- if search_sort_by and search_sort_by == "downloads" %} selected="selected" {%- endif %}>All-Time Downloads</option>
<option value="recent-downloads" {%- if search_sort_by and search_sort_by == "recent-downloads" %} selected="selected" {%- endif %}>Recent Downloads</option>
<option value="recent-updates" {%- if search_sort_by and search_sort_by == "recent-updates" %} selected="selected" {%- endif %}>Recent Updates</option>
<option value="new" {%- if search_sort_by and search_sort_by == "new" %} selected="selected" {%- endif %}>Newly Added</option>
</select>
</div>
</form>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions templates/style/_navbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ div.nav-container {
display: flex;
flex-direction: row;

#search-input-nav {
#search-input-nav, #search-select-nav {
max-width: 150px;
display: none;
border-left: 1px solid var(--color-border);
Expand All @@ -120,7 +120,7 @@ div.nav-container {
font-size: 12.8px;
}

input {
input, select {
border: none;
margin: 0 1em 0 0;
font-size: 12.8px;
Expand Down Expand Up @@ -363,7 +363,7 @@ div.nav-container {
}
}

#nav-search {
#nav-search, #nav-sort {
color: var(--color-navbar-standard);
}

Expand Down

0 comments on commit 20971eb

Please sign in to comment.