Skip to content

Commit 0ffdfd0

Browse files
linyihaiGuillaumeGomez
authored andcommittedNov 24, 2023
add sort by select-box for searching crate
1 parent bd23777 commit 0ffdfd0

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed
 

‎src/web/releases.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ pub(super) struct Search {
418418
#[serde(rename = "releases")]
419419
pub(super) results: Vec<Release>,
420420
pub(super) search_query: Option<String>,
421+
pub(super) search_sort_by: Option<String>,
421422
pub(super) previous_page_link: Option<String>,
422423
pub(super) next_page_link: Option<String>,
423424
/// This should always be `ReleaseType::Search`
@@ -434,6 +435,7 @@ impl Default for Search {
434435
search_query: None,
435436
previous_page_link: None,
436437
next_page_link: None,
438+
search_sort_by: None,
437439
release_type: ReleaseType::Search,
438440
status: http::StatusCode::OK,
439441
}
@@ -507,7 +509,10 @@ pub(crate) async fn search_handler(
507509
.get("query")
508510
.map(|q| q.to_string())
509511
.unwrap_or_else(|| "".to_string());
510-
512+
let sort_by = params
513+
.get("sort")
514+
.map(|q| q.to_string())
515+
.unwrap_or_else(|| "relevance".to_string());
511516
// check if I am feeling lucky button pressed and redirect user to crate page
512517
// if there is a match. Also check for paths to items within crates.
513518
if params.remove("i-am-feeling-lucky").is_some() || query.contains("::") {
@@ -578,6 +583,7 @@ pub(crate) async fn search_handler(
578583
} else if !query.is_empty() {
579584
let query_params: String = form_urlencoded::Serializer::new(String::new())
580585
.append_pair("q", &query)
586+
.append_pair("sort", &sort_by)
581587
.append_pair("per_page", &RELEASES_IN_RELEASES.to_string())
582588
.finish();
583589

@@ -598,6 +604,7 @@ pub(crate) async fn search_handler(
598604
title,
599605
results: search_result.results,
600606
search_query: Some(executed_query),
607+
search_sort_by: Some(sort_by),
601608
next_page_link: search_result
602609
.next_page
603610
.map(|params| format!("/releases/search?paginate={}", b64.encode(params))),

‎static/keyboard.js

+12
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@
110110
}
111111
}
112112

113+
function handleSortByChange() {
114+
const inputSearch = document.getElementById("nav-search");
115+
const searchForm = document.getElementById("nav-search-form");
116+
if (inputSearch.value && searchForm) {
117+
searchForm.submit()
118+
}
119+
}
120+
const searchSortBySel = document.getElementById("nav-sort");
121+
if (searchSortBySel) {
122+
searchSortBySel.addEventListener("change", handleSortByChange)
123+
}
124+
113125
document.onkeypress = handleKey;
114126
document.onkeydown = handleKey;
115127
})();

‎templates/header/topbar_begin.html

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<div class="pure-menu pure-menu-horizontal" role="navigation" aria-label="Main navigation">
99
<form action="/releases/search"
1010
method="GET"
11+
id="nav-search-form"
1112
class="landing-search-form-nav {%
1213
if is_latest_version is defined and not is_latest_version %}not-latest{% endif
1314
%} {% if metadata.yanked %}yanked{% endif %}">

‎templates/header/topbar_end.html

+13
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@
6767
<input id="nav-search" name="query" type="text" aria-label="Find crate by search query" tabindex="-1"
6868
placeholder="Find crate" {%- if search_query %} value="{{ search_query }}" {%- endif %}>
6969
</div>
70+
{# The sort by select-box #}
71+
<div id="search-select-nav">
72+
<label for="nav-sort">
73+
{{ "list" | fas }}
74+
</label>
75+
<select name="sort" id="nav-sort" aria-label="Find crate by the sort by select-box" tabindex="-1">
76+
<option value="relevance" {%- if search_sort_by and search_sort_by == "relevance" %} selected="selected" {%- endif %}>Relevance</option>
77+
<option value="downloads" {%- if search_sort_by and search_sort_by == "downloads" %} selected="selected" {%- endif %}>All-Time Downloads</option>
78+
<option value="recent-downloads" {%- if search_sort_by and search_sort_by == "recent-downloads" %} selected="selected" {%- endif %}>Recent Downloads</option>
79+
<option value="recent-updates" {%- if search_sort_by and search_sort_by == "recent-updates" %} selected="selected" {%- endif %}>Recent Updates</option>
80+
<option value="new" {%- if search_sort_by and search_sort_by == "new" %} selected="selected" {%- endif %}>Newly Added</option>
81+
</select>
82+
</div>
7083
</form>
7184
</div>
7285
</div>

‎templates/style/_navbar.scss

+4-4
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ div.nav-container {
9999
display: flex;
100100
flex-direction: row;
101101

102-
#search-input-nav {
102+
#search-input-nav, #search-select-nav {
103103
max-width: 150px;
104104
display: none;
105105
border-left: 1px solid var(--color-border);
@@ -120,7 +120,7 @@ div.nav-container {
120120
font-size: 12.8px;
121121
}
122122

123-
input {
123+
input, select {
124124
border: none;
125125
margin: 0 1em 0 0;
126126
font-size: 12.8px;
@@ -130,7 +130,7 @@ div.nav-container {
130130
}
131131
}
132132

133-
input.search-input-nav:focus {
133+
input.search-input-nav:focus, select.search-select-nav:focus {
134134
outline: unset;
135135
}
136136

@@ -363,7 +363,7 @@ div.nav-container {
363363
}
364364
}
365365

366-
#nav-search {
366+
#nav-search, #nav-sort {
367367
color: var(--color-navbar-standard);
368368
}
369369

0 commit comments

Comments
 (0)