| 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 |
use quasi_router::layout::{FieldKind, Notice, Selector, Tone}; |
| 67 |
use quasi_router::{ |
| 68 |
Act, Action, Cell, Cells, Choice, Column, Field, Node, Outcome, RegionKind, Request, Response, |
| 69 |
Rest, RouteError, Router, Screen, Slot, Tag, |
| 70 |
}; |
| 71 |
|
| 72 |
use super::{Chosen, Panels}; |
| 73 |
|
| 74 |
|
| 75 |
const BODY: &str = "bulk-body"; |
| 76 |
|
| 77 |
|
| 78 |
const TAG: &str = "tag"; |
| 79 |
|
| 80 |
const MODE: &str = "mode"; |
| 81 |
|
| 82 |
const FOLDER: &str = "folder"; |
| 83 |
|
| 84 |
const PATTERN: &str = "pattern"; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
const ROOT: &str = ""; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
const BACK: &str = "/detail"; |
| 97 |
|
| 98 |
|
| 99 |
const START: &str = "{name}"; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
const TOKENS: [&str; 9] = [ |
| 108 |
"{name}", |
| 109 |
"{ext}", |
| 110 |
"{bpm}", |
| 111 |
"{key}", |
| 112 |
"{class}", |
| 113 |
"{duration}", |
| 114 |
"{n}", |
| 115 |
"{nn}", |
| 116 |
"{nnn}", |
| 117 |
]; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
const SHOWN: usize = 50; |
| 134 |
|
| 135 |
|
| 136 |
pub fn routes(router: Router<Panels<'_>>) -> Router<Panels<'_>> { |
| 137 |
router |
| 138 |
.get("/bulk/tag", tag_screen) |
| 139 |
.post("/bulk/tag", tag) |
| 140 |
.get("/bulk/move", move_screen) |
| 141 |
.post("/bulk/move", move_to) |
| 142 |
.get("/bulk/rename", rename_screen) |
| 143 |
.post("/bulk/rename/preview", preview) |
| 144 |
.post("/bulk/rename", rename) |
| 145 |
.post(DONE, done) |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
fn tag_screen(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 150 |
let chosen = state.bulk.chosen(); |
| 151 |
if chosen.samples == 0 { |
| 152 |
return Err(RouteError::not_found("no samples are chosen")); |
| 153 |
} |
| 154 |
Ok(over(tagging(state, &chosen, None, true))) |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
fn tag(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 163 |
let typed = request.payload.get(TAG).unwrap_or_default().trim(); |
| 164 |
let adding = request.payload.get(MODE) != Some("remove"); |
| 165 |
let chosen = state.bulk.chosen(); |
| 166 |
|
| 167 |
if typed.is_empty() { |
| 168 |
return Ok(over(tagging(state, &chosen, Some(""), adding)) |
| 169 |
.toast(Tone::Danger, "Type a tag first.")); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
if !adding && !state.bulk.known_tags().iter().any(|known| known == typed) { |
| 175 |
return Err(RouteError::not_found(UNKNOWN)); |
| 176 |
} |
| 177 |
state.bulk.tag(typed, adding); |
| 178 |
state.bulk.done(); |
| 179 |
Ok(Response::from(leaving()).toast( |
| 180 |
Tone::Success, |
| 181 |
format!( |
| 182 |
"{} \"{typed}\" {} {} samples.", |
| 183 |
if adding { "Adding" } else { "Removing" }, |
| 184 |
if adding { "on" } else { "from" }, |
| 185 |
chosen.samples, |
| 186 |
), |
| 187 |
)) |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
const UNKNOWN: &str = "None of the selected samples have this tag."; |
| 192 |
|
| 193 |
|
| 194 |
fn tagging(state: &Panels<'_>, chosen: &Chosen, typed: Option<&str>, adding: bool) -> Screen { |
| 195 |
let mut body = Slot::new(BODY, RegionKind::Pane) |
| 196 |
.with(Node::page(format!("Tag {} samples", chosen.samples))); |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
body = body.with(Node::Select { |
| 202 |
kind: Selector::Segmented, |
| 203 |
options: vec![ |
| 204 |
(Choice::new("add", "Add tag"), None), |
| 205 |
(Choice::new("remove", "Remove tag"), None), |
| 206 |
], |
| 207 |
chosen: Some(if adding { "add" } else { "remove" }.to_owned()), |
| 208 |
action: None, |
| 209 |
}); |
| 210 |
|
| 211 |
let mut field = Field::new(FieldKind::Text, TAG, "Tag").hint("e.g. genre.electronic"); |
| 212 |
if let Some(typed) = typed { |
| 213 |
field = field.value(typed); |
| 214 |
} |
| 215 |
body = body |
| 216 |
.with(Node::Form { |
| 217 |
fields: vec![field], |
| 218 |
submit: "Apply".to_owned(), |
| 219 |
action: Action::post("/bulk/tag"), |
| 220 |
}) |
| 221 |
.with(Node::text(if adding { |
| 222 |
"Will add to every selected sample that lacks it." |
| 223 |
} else { |
| 224 |
"Will remove from selected samples that have this tag." |
| 225 |
})); |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
body = body.with(Node::section("Known tags")); |
| 238 |
for known in state.bulk.known_tags().iter().take(SHOWN) { |
| 239 |
body = body.with(Node::Token(Tag::badge(known.clone()))); |
| 240 |
} |
| 241 |
|
| 242 |
closing(subjects(body, &chosen.names)) |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
fn move_screen(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 247 |
let chosen = state.bulk.chosen(); |
| 248 |
if chosen.names.is_empty() { |
| 249 |
return Err(RouteError::not_found("nothing is chosen")); |
| 250 |
} |
| 251 |
Ok(over(moving(state, &chosen))) |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
fn move_to(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 259 |
let chosen = request |
| 260 |
.payload |
| 261 |
.get(FOLDER) |
| 262 |
.ok_or_else(|| RouteError::not_found("no destination named"))?; |
| 263 |
let folder = if chosen == ROOT { |
| 264 |
None |
| 265 |
} else { |
| 266 |
let id: i64 = chosen |
| 267 |
.parse() |
| 268 |
.map_err(|_| RouteError::not_found("no such folder"))?; |
| 269 |
if !state.bulk.folders().iter().any(|folder| folder.id == id) { |
| 270 |
return Err(RouteError::not_found("no such folder")); |
| 271 |
} |
| 272 |
Some(id) |
| 273 |
}; |
| 274 |
let count = state.bulk.chosen().names.len(); |
| 275 |
state.bulk.move_to(folder); |
| 276 |
state.bulk.done(); |
| 277 |
Ok(Response::from(leaving()).toast(Tone::Success, format!("Moving {count} items."))) |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
fn moving(state: &Panels<'_>, chosen: &Chosen) -> Screen { |
| 289 |
let folders = state.bulk.folders(); |
| 290 |
let mut rows = vec![row("/", ROOT)]; |
| 291 |
rows.extend( |
| 292 |
folders |
| 293 |
.iter() |
| 294 |
.map(|folder| row(&folder.path, &folder.id.to_string())), |
| 295 |
); |
| 296 |
|
| 297 |
let body = Slot::new(BODY, RegionKind::Pane) |
| 298 |
.with(Node::page(format!("Move {} items", chosen.names.len()))) |
| 299 |
.with(Node::text("Choose where they go.")) |
| 300 |
.with(Node::Table { |
| 301 |
columns: vec![Column::new("Folder")], |
| 302 |
rows, |
| 303 |
|
| 304 |
|
| 305 |
more: None, |
| 306 |
}); |
| 307 |
|
| 308 |
closing(subjects(body, &chosen.names)) |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
fn row(path: &str, value: &str) -> Cells { |
| 313 |
Cells::new(vec![Cell::new(path)]).activate(Action::post("/bulk/move").carrying(FOLDER, value)) |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
fn rename_screen(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 318 |
let chosen = state.bulk.chosen(); |
| 319 |
if chosen.names.is_empty() { |
| 320 |
return Err(RouteError::not_found("nothing is chosen")); |
| 321 |
} |
| 322 |
Ok(over(renaming(state, START))) |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
fn preview(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 336 |
let pattern = request.payload.get(PATTERN).unwrap_or_default(); |
| 337 |
Ok(Response::from(Outcome::Fragment { |
| 338 |
region: PREVIEW.to_owned(), |
| 339 |
node: previewed(state, pattern), |
| 340 |
})) |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
fn rename(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 345 |
let pattern = request.payload.get(PATTERN).unwrap_or_default(); |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
let previews = state |
| 350 |
.bulk |
| 351 |
.previews(pattern) |
| 352 |
.map_err(RouteError::not_found)?; |
| 353 |
if previews.is_empty() { |
| 354 |
return Err(RouteError::not_found("that pattern renames nothing")); |
| 355 |
} |
| 356 |
state.bulk.rename(pattern); |
| 357 |
state.bulk.done(); |
| 358 |
Ok(Response::from(leaving()) |
| 359 |
.toast(Tone::Success, format!("Renaming {} items.", previews.len()))) |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
const DONE: &str = "/bulk/done"; |
| 368 |
|
| 369 |
|
| 370 |
fn done(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 371 |
state.bulk.done(); |
| 372 |
Ok(Response::from(leaving())) |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
const PREVIEW: &str = "bulk-rename-preview"; |
| 377 |
|
| 378 |
|
| 379 |
fn renaming(state: &Panels<'_>, pattern: &str) -> Screen { |
| 380 |
let mut body = Slot::new(BODY, RegionKind::Pane).with(Node::page("Rename pattern")); |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
for token in TOKENS { |
| 395 |
body = body.with(Node::Act( |
| 396 |
Act::new(token, Action::local()).filling(PATTERN, token), |
| 397 |
)); |
| 398 |
} |
| 399 |
|
| 400 |
body = body.with(Node::Form { |
| 401 |
fields: vec![ |
| 402 |
Field::new(FieldKind::Text, PATTERN, "Pattern") |
| 403 |
.value(pattern) |
| 404 |
.hint("{name}_{bpm}") |
| 405 |
.changes(Action::post("/bulk/rename/preview")), |
| 406 |
], |
| 407 |
submit: "Rename".to_owned(), |
| 408 |
action: Action::post("/bulk/rename"), |
| 409 |
}); |
| 410 |
|
| 411 |
closing(body.with(Node::Region( |
| 412 |
Slot::new(PREVIEW, RegionKind::Group).with(previewed(state, pattern)), |
| 413 |
))) |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
fn closing(body: Slot) -> Screen { |
| 422 |
Screen::sidebar_content("Bulk") |
| 423 |
.with(body.with(Node::Act(Act::new("Cancel", Action::post(DONE)).key("esc")))) |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
fn previewed(state: &Panels<'_>, pattern: &str) -> Node { |
| 431 |
let previews = match state.bulk.previews(pattern) { |
| 432 |
Ok(previews) => previews, |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
Err(why) => { |
| 437 |
return Node::Notice { |
| 438 |
kind: Notice::Banner, |
| 439 |
tone: Tone::Danger, |
| 440 |
text: why, |
| 441 |
}; |
| 442 |
} |
| 443 |
}; |
| 444 |
if previews.is_empty() { |
| 445 |
return Node::empty("Nothing to rename."); |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
let mut seen: std::collections::HashMap<&str, usize> = std::collections::HashMap::new(); |
| 452 |
for (_, new) in &previews { |
| 453 |
*seen.entry(new.as_str()).or_insert(0) += 1; |
| 454 |
} |
| 455 |
|
| 456 |
let total = previews.len(); |
| 457 |
Node::Table { |
| 458 |
columns: vec![Column::new("Old"), Column::new("New")], |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
more: (total > SHOWN).then(|| Rest { |
| 465 |
paging: quasi_router::layout::Paging::more(SHOWN).of(total), |
| 466 |
forward: None, |
| 467 |
back: None, |
| 468 |
}), |
| 469 |
rows: previews |
| 470 |
.iter() |
| 471 |
.take(SHOWN) |
| 472 |
.map(|(old, new)| { |
| 473 |
let collides = seen.get(new.as_str()).copied().unwrap_or(0) > 1; |
| 474 |
Cells::new(vec![ |
| 475 |
Cell::new(old), |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
if collides { |
| 481 |
Cell::tag(Tag::badge(new.clone()).tone(Tone::Warning)) |
| 482 |
} else { |
| 483 |
Cell::new(new) |
| 484 |
}, |
| 485 |
]) |
| 486 |
}) |
| 487 |
.collect(), |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
fn subjects(body: Slot, names: &[String]) -> Slot { |
| 498 |
body.with(Node::section(format!("{} chosen", names.len()))) |
| 499 |
.with(Node::List { |
| 500 |
rows: names |
| 501 |
.iter() |
| 502 |
.take(SHOWN) |
| 503 |
.map(quasi_router::Row::new) |
| 504 |
.collect(), |
| 505 |
more: (names.len() > SHOWN).then(|| Rest { |
| 506 |
paging: quasi_router::layout::Paging::more(SHOWN).of(names.len()), |
| 507 |
forward: None, |
| 508 |
back: None, |
| 509 |
}), |
| 510 |
}) |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
fn leaving() -> Outcome { |
| 520 |
Outcome::Goto(Action::get(BACK)) |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
fn over(screen: Screen) -> Response { |
| 525 |
Response::from(Outcome::Over(screen)) |
| 526 |
} |
| 527 |
|