| 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 |
use quasi_router::layout::{Token, Tone}; |
| 80 |
use quasi_router::{ |
| 81 |
Act, Action, Node, RegionKind, Request, Response, RouteError, Router, Row, Slot, Tag, |
| 82 |
}; |
| 83 |
|
| 84 |
use super::{Holding, Panels}; |
| 85 |
|
| 86 |
|
| 87 |
const SIDE: &str = "library-side"; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
pub fn routes(router: Router<Panels<'_>>) -> Router<Panels<'_>> { |
| 95 |
router |
| 96 |
.post("/vaults/{id}/open", open_vault) |
| 97 |
.post("/vaults/{id}/delete", delete_vault) |
| 98 |
.post("/tags/{path}/filter", toggle_tag) |
| 99 |
.post("/tags/{path}/remove", remove_tag) |
| 100 |
.post("/collections/{id}/open", open_collection) |
| 101 |
.post("/collections/close", close_collection) |
| 102 |
.post("/collections/{id}/delete", delete_collection) |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
fn open_vault(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 107 |
let id = numbered(&request, "no such vault")?; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
if !state.library.vaults().iter().any(|vault| vault.id == id) { |
| 113 |
return Err(RouteError::not_found("no such vault")); |
| 114 |
} |
| 115 |
state.library.open_vault(id); |
| 116 |
Ok(super::shell::screen(state).into()) |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
fn delete_vault(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 125 |
let id = numbered(&request, "no such vault")?; |
| 126 |
if state.library.vaults().len() < 2 { |
| 127 |
return Err(RouteError::not_found(LAST_VAULT)); |
| 128 |
} |
| 129 |
state.library.delete_vault(id); |
| 130 |
Ok(super::shell::screen(state).into()) |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
fn toggle_tag(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 135 |
let path = request.captures.require("path")?.to_owned(); |
| 136 |
state.library.toggle_tag(&path); |
| 137 |
Ok(super::shell::screen(state).into()) |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
fn remove_tag(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 142 |
let path = request.captures.require("path")?.to_owned(); |
| 143 |
state.library.remove_tag(&path); |
| 144 |
Ok(super::shell::screen(state).into()) |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
fn open_collection(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 149 |
let id = numbered(&request, "no such collection")?; |
| 150 |
state.library.open_collection(id); |
| 151 |
Ok(super::shell::screen(state).into()) |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
fn close_collection(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 156 |
state.library.close_collection(); |
| 157 |
Ok(super::shell::screen(state).into()) |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
fn delete_collection(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 162 |
let id = numbered(&request, "no such collection")?; |
| 163 |
state.library.delete_collection(id); |
| 164 |
Ok(super::shell::screen(state).into()) |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
fn numbered(request: &Request, whats_wrong: &'static str) -> Result<i64, RouteError> { |
| 169 |
request |
| 170 |
.captures |
| 171 |
.require("id")? |
| 172 |
.parse() |
| 173 |
.map_err(|_| RouteError::not_found(whats_wrong)) |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
const LAST_VAULT: &str = "Create another vault first, audiofiles needs at least one."; |
| 178 |
|
| 179 |
|
| 180 |
pub fn body(state: &Panels<'_>) -> Slot { |
| 181 |
let side = Slot::new(SIDE, RegionKind::Sidebar); |
| 182 |
let side = vaults(side, state); |
| 183 |
let side = collections(side, state); |
| 184 |
tags(side, state) |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
fn vaults(side: Slot, state: &Panels<'_>) -> Slot { |
| 189 |
let all = state.library.vaults(); |
| 190 |
let alone = all.len() < 2; |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
let mut side = side |
| 196 |
.with(Node::section("Vaults")) |
| 197 |
.with(Node::Act(Act::new("New vault", Action::get("/vaults/new")))); |
| 198 |
|
| 199 |
let mut rows = Vec::with_capacity(all.len()); |
| 200 |
for vault in &all { |
| 201 |
let mut delete = Act::new( |
| 202 |
"Delete", |
| 203 |
Action::post(format!("/vaults/{}/delete", vault.id)), |
| 204 |
) |
| 205 |
.tone(Tone::Danger) |
| 206 |
|
| 207 |
|
| 208 |
.confirm(format!( |
| 209 |
"Delete vault \"{}\" and all its contents?", |
| 210 |
vault.name |
| 211 |
)); |
| 212 |
if alone { |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
delete = delete.disabled(); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
let mut row = Row::new(&vault.name) |
| 223 |
.activate(Action::post(format!("/vaults/{}/open", vault.id))) |
| 224 |
.offers(Act::new( |
| 225 |
"Rename", |
| 226 |
Action::get(format!("/vaults/{}/rename", vault.id)), |
| 227 |
)) |
| 228 |
.offers(delete); |
| 229 |
row.current = vault.current; |
| 230 |
rows.push(row); |
| 231 |
} |
| 232 |
side = side.with(Node::List { rows, more: None }); |
| 233 |
|
| 234 |
if alone { |
| 235 |
|
| 236 |
|
| 237 |
side = side.with(Node::Text { |
| 238 |
text: LAST_VAULT.to_owned(), |
| 239 |
tone: Tone::Info, |
| 240 |
}); |
| 241 |
} |
| 242 |
side |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
fn collections(side: Slot, state: &Panels<'_>) -> Slot { |
| 247 |
let all = state.library.collections(); |
| 248 |
let mut side = side.with(Node::section("Collections")); |
| 249 |
|
| 250 |
if all.is_empty() { |
| 251 |
return side.with(Node::empty("No collections yet.")); |
| 252 |
} |
| 253 |
|
| 254 |
let mut rows = Vec::with_capacity(all.len()); |
| 255 |
for collection in &all { |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
let mark = match collection.holding { |
| 262 |
Holding::Dynamic => Tag::badge("auto"), |
| 263 |
Holding::Fixed(count) => Tag::badge(count.to_string()), |
| 264 |
}; |
| 265 |
let mut row = Row::new(&collection.name) |
| 266 |
.token(mark) |
| 267 |
.activate(if collection.active { |
| 268 |
Action::post("/collections/close") |
| 269 |
} else { |
| 270 |
Action::post(format!("/collections/{}/open", collection.id)) |
| 271 |
}) |
| 272 |
.offers( |
| 273 |
Act::new( |
| 274 |
"Delete", |
| 275 |
Action::post(format!("/collections/{}/delete", collection.id)), |
| 276 |
) |
| 277 |
.tone(Tone::Danger) |
| 278 |
.confirm(format!("Delete collection \"{}\"?", collection.name)), |
| 279 |
); |
| 280 |
row.current = collection.active; |
| 281 |
rows.push(row); |
| 282 |
} |
| 283 |
|
| 284 |
side = side.with(Node::List { rows, more: None }); |
| 285 |
side |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
fn tags(side: Slot, state: &Panels<'_>) -> Slot { |
| 295 |
let all = state.library.tags(); |
| 296 |
let mut side = side.with(Node::section("Tags")); |
| 297 |
|
| 298 |
if all.is_empty() { |
| 299 |
return side.with(Node::empty("No tags yet.")); |
| 300 |
} |
| 301 |
|
| 302 |
for filter in &all { |
| 303 |
side = side.with(Node::Token(Tag { |
| 304 |
kind: Token::Chip { removable: false }, |
| 305 |
label: filter.path.clone(), |
| 306 |
tone: if filter.on { Tone::Info } else { Tone::Neutral }, |
| 307 |
latched: filter.on, |
| 308 |
action: Some(Action::post(format!("/tags/{}/filter", filter.path))), |
| 309 |
})); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
side.with(Node::section("Remove a tag everywhere")) |
| 317 |
.with(Node::List { |
| 318 |
rows: all |
| 319 |
.iter() |
| 320 |
.map(|filter| { |
| 321 |
Row::new(&filter.path).offers( |
| 322 |
Act::new( |
| 323 |
"Remove", |
| 324 |
Action::post(format!("/tags/{}/remove", filter.path)), |
| 325 |
) |
| 326 |
.tone(Tone::Danger) |
| 327 |
.confirm(format!( |
| 328 |
"Remove tag \"{}\" from every sample that has it?", |
| 329 |
filter.path |
| 330 |
)), |
| 331 |
) |
| 332 |
}) |
| 333 |
.collect(), |
| 334 |
more: None, |
| 335 |
}) |
| 336 |
} |
| 337 |
|