Allow to keep query params in paginate

Fix #349
This commit is contained in:
Trinity Pointard 2018-12-12 00:51:45 +01:00
parent 144f5966dd
commit fcbaf6eee3
3 changed files with 26 additions and 10 deletions

View File

@ -29,13 +29,11 @@ pub struct SearchQuery {
macro_rules! param_to_query {
( $query:ident, $parsed_query:ident; normal: $($field:ident),*; date: $($date:ident),*) => {
$(
if let Some(ref field) = $query.$field {
let mut rest = field.as_str();
while !rest.is_empty() {
let (token, r) = Query::get_first_token(rest);
rest = r;
$parsed_query.$field(token, None);
}
let mut rest = $query.$field.as_ref().map(String::as_str).unwrap_or_default();
while !rest.is_empty() {
let (token, r) = Query::get_first_token(rest);
rest = r;
$parsed_query.$field(token, None);
}
)*
$(

View File

@ -75,18 +75,36 @@ pub fn tabs(links: &[(&str, &str, bool)]) -> Html<String> {
}
pub fn paginate(catalog: &Catalog, page: i32, total: i32) -> Html<String> {
paginate_param(catalog, page, total, None)
}
pub fn paginate_param(catalog: &Catalog, page: i32, total: i32, param: Option<String>) -> Html<String> {
let mut res = String::new();
let param = param.map(|mut p| {p.push('&'); p}).unwrap_or_default();
res.push_str(r#"<div class="pagination">"#);
if page != 1 {
res.push_str(format!(r#"<a href="?page={}">{}</a>"#, page - 1, catalog.gettext("Previous page")).as_str());
res.push_str(format!(r#"<a href="?{}page={}">{}</a>"#, param, page - 1, catalog.gettext("Previous page")).as_str());
}
if page < total {
res.push_str(format!(r#"<a href="?page={}">{}</a>"#, page + 1, catalog.gettext("Next page")).as_str());
res.push_str(format!(r#"<a href="?{}page={}">{}</a>"#, param, page + 1, catalog.gettext("Next page")).as_str());
}
res.push_str("</div>");
Html(res)
}
pub fn encode_query_param(param: &str) -> String {
param.chars().map(|c| match c {
'+' => Ok("%2B"),
' ' => Err('+'),
c => Err(c),
}).fold(String::new(), |mut s,r| {
match r {
Ok(r) => s.push_str(r),
Err(r) => s.push(r),
};
s
})
}
#[macro_export]
macro_rules! icon {
($name:expr) => {

View File

@ -23,5 +23,5 @@
}
</div>
}
@paginate(ctx.1, page, n_pages)
@paginate_param(ctx.1, page, n_pages, Some(format!("q={}", encode_query_param(query_str))))
})