| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
use quasi_router::layout::{FieldKind, Priority, Selector, Tone, Width}; |
| 89 |
use quasi_router::{ |
| 90 |
Act, Action, Choice, Field, Node, RegionKind, Request, Response, RouteError, Router, Screen, |
| 91 |
Slot, |
| 92 |
}; |
| 93 |
|
| 94 |
use super::{Panel, Panels, Where}; |
| 95 |
|
| 96 |
|
| 97 |
const BAR: &str = "toolbar-bar"; |
| 98 |
|
| 99 |
|
| 100 |
const QUERY: &str = "query"; |
| 101 |
|
| 102 |
const NAME: &str = "name"; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
pub fn routes(router: Router<Panels<'_>>) -> Router<Panels<'_>> { |
| 109 |
router |
| 110 |
.post("/search", search) |
| 111 |
.post("/search/scope", scope) |
| 112 |
.post("/search/save", save) |
| 113 |
.get("/search/save", saving) |
| 114 |
.post("/undo", undo) |
| 115 |
.post("/panels/{panel}", toggle) |
| 116 |
.post("/here/root", root) |
| 117 |
.post("/here/{id}/{depth}", go) |
| 118 |
.post("/here/leave", leave) |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
fn search(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 123 |
let query = request.payload.get(QUERY).unwrap_or_default(); |
| 124 |
state.bar.search(query); |
| 125 |
Ok(super::shell::screen(state).into()) |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
fn scope(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 130 |
let chosen = request.payload.get(Node::SELECTED).unwrap_or_default(); |
| 131 |
match chosen { |
| 132 |
"all" => state.bar.set_scope(true), |
| 133 |
"folder" => state.bar.set_scope(false), |
| 134 |
_ => return Err(RouteError::not_found("no such scope")), |
| 135 |
} |
| 136 |
Ok(super::shell::screen(state).into()) |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
fn saving(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 145 |
let searching = state.bar.searching(); |
| 146 |
if !searching.filtered { |
| 147 |
return Err(RouteError::not_found("nothing is filtered")); |
| 148 |
} |
| 149 |
Ok(Response::over( |
| 150 |
Screen::sidebar_content("Save as collection").with( |
| 151 |
Slot::new("save-collection", RegionKind::Pane) |
| 152 |
.with(Node::page("Save as collection")) |
| 153 |
.with(Node::text( |
| 154 |
"A dynamic collection re-applies these filters, so it updates itself as samples match.", |
| 155 |
)) |
| 156 |
.with(Node::Form { |
| 157 |
fields: vec![ |
| 158 |
Field::new(FieldKind::Text, NAME, "Name") |
| 159 |
.required() |
| 160 |
.value(searching.describes) |
| 161 |
.hint("e.g. Kicks Under 120 BPM"), |
| 162 |
], |
| 163 |
submit: "Save collection".to_owned(), |
| 164 |
action: Action::post("/search/save"), |
| 165 |
}), |
| 166 |
), |
| 167 |
)) |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
fn save(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 172 |
let name = request.payload.get(NAME).unwrap_or_default().trim(); |
| 173 |
if name.is_empty() { |
| 174 |
return Err(RouteError::not_found("a collection needs a name")); |
| 175 |
} |
| 176 |
state.bar.save_collection(name); |
| 177 |
Ok(super::shell::screen(state).into()) |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
fn undo(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 190 |
if !state.bar.undoable() { |
| 191 |
return Err(RouteError::not_found("there is nothing to undo")); |
| 192 |
} |
| 193 |
state.bar.undo(); |
| 194 |
Ok(super::shell::screen(state).into()) |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
fn toggle(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 199 |
let named = request.captures.require("panel")?; |
| 200 |
let panel = Panel::from_key(named).ok_or_else(|| RouteError::not_found("no such panel"))?; |
| 201 |
state.bar.toggle(panel); |
| 202 |
Ok(super::shell::screen(state).into()) |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
fn root(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 207 |
state.bar.go_root(); |
| 208 |
Ok(super::shell::screen(state).into()) |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
fn go(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 218 |
let id: i64 = request |
| 219 |
.captures |
| 220 |
.require("id")? |
| 221 |
.parse() |
| 222 |
.map_err(|_| RouteError::not_found("no such folder"))?; |
| 223 |
let depth: usize = request |
| 224 |
.captures |
| 225 |
.require("depth")? |
| 226 |
.parse() |
| 227 |
.map_err(|_| RouteError::not_found("no such place in the trail"))?; |
| 228 |
state.bar.go_to(id, depth); |
| 229 |
Ok(super::shell::screen(state).into()) |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
fn leave(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 234 |
state.bar.leave(); |
| 235 |
Ok(super::shell::screen(state).into()) |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
pub fn body(state: &Panels<'_>) -> Slot { |
| 240 |
let bar = Slot::new(BAR, RegionKind::Band); |
| 241 |
let bar = here(bar, state); |
| 242 |
let bar = looking(bar, state); |
| 243 |
panels(bar, state) |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
fn here(bar: Slot, state: &Panels<'_>) -> Slot { |
| 254 |
match state.bar.place() { |
| 255 |
Where::Folder { trail } => { |
| 256 |
let mut bar = bar.with(Node::Link { |
| 257 |
text: "/".to_owned(), |
| 258 |
action: Action::post("/here/root"), |
| 259 |
}); |
| 260 |
for (depth, crumb) in trail.iter().enumerate() { |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
if depth + 1 == trail.len() { |
| 265 |
bar = bar.with(Node::Text { |
| 266 |
text: crumb.name.clone(), |
| 267 |
tone: Tone::Info, |
| 268 |
}); |
| 269 |
} else { |
| 270 |
bar = bar.with(Node::Link { |
| 271 |
text: crumb.name.clone(), |
| 272 |
action: Action::post(format!("/here/{}/{}", crumb.id, depth + 1)), |
| 273 |
}); |
| 274 |
} |
| 275 |
} |
| 276 |
bar |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
Where::Collection { name } => { |
| 283 |
leaving(bar, format!("Collection: {name}"), "Back to browsing") |
| 284 |
} |
| 285 |
Where::Similar { name } => leaving(bar, format!("Similar to: {name}"), "Back to browsing") |
| 286 |
.with(Node::text( |
| 287 |
"Results are ranked by similarity, so column sort is off.", |
| 288 |
)), |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
fn leaving(bar: Slot, says: String, out: &str) -> Slot { |
| 294 |
bar.with(Node::Text { |
| 295 |
text: says, |
| 296 |
tone: Tone::Info, |
| 297 |
}) |
| 298 |
.with(Node::Act(Act::new(out, Action::post("/here/leave")))) |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
fn looking(bar: Slot, state: &Panels<'_>) -> Slot { |
| 303 |
let searching = state.bar.searching(); |
| 304 |
|
| 305 |
let mut bar = bar |
| 306 |
.with(Node::Field(Box::new( |
| 307 |
Field::new(FieldKind::Text, QUERY, "Search") |
| 308 |
.value(&searching.query) |
| 309 |
.hint("Search samples...") |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
.width(Width::Fill) |
| 316 |
.changes(Action::post("/search")), |
| 317 |
))) |
| 318 |
.with(Node::Select { |
| 319 |
kind: Selector::Segmented, |
| 320 |
options: vec![ |
| 321 |
(Choice::new("folder", "This folder"), None), |
| 322 |
(Choice::new("all", "Everywhere"), None), |
| 323 |
], |
| 324 |
chosen: Some( |
| 325 |
if searching.everywhere { |
| 326 |
"all" |
| 327 |
} else { |
| 328 |
"folder" |
| 329 |
} |
| 330 |
.to_owned(), |
| 331 |
), |
| 332 |
action: Some(Action::post("/search/scope")), |
| 333 |
}); |
| 334 |
|
| 335 |
if searching.filtered { |
| 336 |
bar = bar |
| 337 |
.with(Node::Figure(quasi_router::Figure::new( |
| 338 |
searching.results.to_string(), |
| 339 |
"results", |
| 340 |
))) |
| 341 |
.with(Node::Act(Act::new( |
| 342 |
"Save as collection", |
| 343 |
Action::get("/search/save"), |
| 344 |
))); |
| 345 |
} |
| 346 |
|
| 347 |
let mut undo = Act::new("Undo", Action::post("/undo")).key("ctrl+z"); |
| 348 |
if !state.bar.undoable() { |
| 349 |
undo = undo.disabled(); |
| 350 |
} |
| 351 |
bar.with(Node::Act(undo)) |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
fn panels(bar: Slot, state: &Panels<'_>) -> Slot { |
| 361 |
use quasi_router::Tag; |
| 362 |
use quasi_router::layout::Token; |
| 363 |
|
| 364 |
let showing = state.bar.showing(); |
| 365 |
let mut bar = bar; |
| 366 |
|
| 367 |
for panel in Panel::ALL { |
| 368 |
let on = showing.contains(&panel); |
| 369 |
let mut chip = Tag { |
| 370 |
kind: Token::Chip { removable: false }, |
| 371 |
label: panel.label().to_owned(), |
| 372 |
tone: if on { Tone::Info } else { Tone::Neutral }, |
| 373 |
latched: on, |
| 374 |
action: Some(Action::post(format!("/panels/{}", panel.as_str()))), |
| 375 |
}; |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
if panel == Panel::Filters && state.bar.searching().filters > 0 { |
| 380 |
chip.label = format!("{} ({})", chip.label, state.bar.searching().filters); |
| 381 |
} |
| 382 |
bar = bar.with_ranked(Node::Token(chip), panel.worth()); |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
let bar = bar |
| 390 |
.with(Node::Act(Act::new("Import", Action::get("/import/open")))) |
| 391 |
.with_ranked( |
| 392 |
Node::Act(Act::new("Export", Action::post("/export/begin"))), |
| 393 |
Priority::Secondary, |
| 394 |
); |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
bar.with_ranked( |
| 401 |
Node::Act(Act::new("Settings", Action::get("/settings"))), |
| 402 |
Priority::Secondary, |
| 403 |
) |
| 404 |
.with_ranked( |
| 405 |
Node::Act(Act::new("Cloud Sync", Action::get("/sync"))), |
| 406 |
Priority::Secondary, |
| 407 |
) |
| 408 |
.with_ranked( |
| 409 |
Node::Act(Act::new("Help", Action::get("/help")).key("f1")), |
| 410 |
Priority::Optional, |
| 411 |
) |
| 412 |
} |
| 413 |
|