| 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 |
use makeover_layout as layout; |
| 81 |
use quasi_router::{Action, Node, RegionKind, Slot}; |
| 82 |
use quasi_webview::Webview; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
const STRIP: &str = "tab-content"; |
| 93 |
|
| 94 |
|
| 95 |
struct Tab { |
| 96 |
label: &'static str, |
| 97 |
|
| 98 |
panel: &'static str, |
| 99 |
|
| 100 |
route: &'static str, |
| 101 |
|
| 102 |
bundles_too: bool, |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
const TABS: &[Tab] = &[ |
| 107 |
Tab { |
| 108 |
label: "Overview", |
| 109 |
panel: "item-overview", |
| 110 |
route: "overview", |
| 111 |
bundles_too: true, |
| 112 |
}, |
| 113 |
Tab { |
| 114 |
label: "Details", |
| 115 |
panel: "item-details", |
| 116 |
route: "details", |
| 117 |
bundles_too: true, |
| 118 |
}, |
| 119 |
Tab { |
| 120 |
label: "Pricing", |
| 121 |
panel: "item-pricing", |
| 122 |
route: "pricing", |
| 123 |
bundles_too: true, |
| 124 |
}, |
| 125 |
Tab { |
| 126 |
label: "Files", |
| 127 |
panel: "item-files", |
| 128 |
route: "files", |
| 129 |
|
| 130 |
bundles_too: false, |
| 131 |
}, |
| 132 |
Tab { |
| 133 |
label: "Sales", |
| 134 |
panel: "item-sales", |
| 135 |
route: "sales", |
| 136 |
bundles_too: true, |
| 137 |
}, |
| 138 |
]; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
#[must_use] |
| 150 |
pub fn shown_at(asked: Option<&str>, is_bundle: bool) -> usize { |
| 151 |
let Some(asked) = asked else { return 0 }; |
| 152 |
visible(is_bundle) |
| 153 |
.iter() |
| 154 |
.position(|tab| tab.route == asked) |
| 155 |
.unwrap_or(0) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
#[must_use] |
| 160 |
pub fn route_at(shown: usize, is_bundle: bool) -> &'static str { |
| 161 |
visible(is_bundle) |
| 162 |
.get(shown) |
| 163 |
.map_or(TABS[0].route, |tab| tab.route) |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
fn visible(is_bundle: bool) -> Vec<&'static Tab> { |
| 168 |
TABS.iter() |
| 169 |
.filter(|tab| tab.bundles_too || !is_bundle) |
| 170 |
.collect() |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
#[must_use] |
| 180 |
pub fn html(item_id: &str, shown: usize, panel: &str, is_bundle: bool) -> String { |
| 181 |
let tabs = visible(is_bundle); |
| 182 |
let shown = shown.min(tabs.len().saturating_sub(1)); |
| 183 |
|
| 184 |
let mut strip = Slot::new(STRIP, RegionKind::TabGroup) |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
.across(layout::Fallback::Menu) |
| 190 |
.showing_one(shown); |
| 191 |
|
| 192 |
for (at, tab) in tabs.iter().enumerate() { |
| 193 |
let mut region = Slot::handover(tab.panel, "item-panel"); |
| 194 |
if at != shown { |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
region = region.fed_by( |
| 200 |
Action::get(format!("/dashboard/item/{item_id}/tabs/{}", tab.route)) |
| 201 |
.awaiting() |
| 202 |
.replacing(tab.panel), |
| 203 |
); |
| 204 |
} |
| 205 |
strip = strip.frame(tab.label, Node::Region(region)); |
| 206 |
} |
| 207 |
|
| 208 |
use quasi_axum::Serves as _; |
| 209 |
|
| 210 |
|
| 211 |
Webview::new() |
| 212 |
.with_fill(tabs[shown].panel, panel) |
| 213 |
.fragment(&Node::Region(strip)) |
| 214 |
} |
| 215 |
|
| 216 |
#[cfg(test)] |
| 217 |
mod tests { |
| 218 |
use super::*; |
| 219 |
|
| 220 |
fn strip(is_bundle: bool) -> String { |
| 221 |
html("itm_1", 0, "<p>the overview</p>", is_bundle) |
| 222 |
} |
| 223 |
|
| 224 |
#[test] |
| 225 |
fn the_page_asks_for_nothing_on_load() { |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
let html = strip(false); |
| 231 |
|
| 232 |
assert!(!html.contains("hx-trigger=\"load\""), "{html}"); |
| 233 |
assert!(html.contains("<p>the overview</p>"), "{html}"); |
| 234 |
assert_eq!(html.matches("hx-get=").count(), 4, "{html}"); |
| 235 |
} |
| 236 |
|
| 237 |
#[test] |
| 238 |
fn every_unshown_tab_says_where_its_answer_lands() { |
| 239 |
|
| 240 |
|
| 241 |
let html = strip(false); |
| 242 |
|
| 243 |
for panel in ["item-details", "item-pricing", "item-files", "item-sales"] { |
| 244 |
assert!(html.contains(&format!("hx-target=\"#{panel}\"")), "{html}"); |
| 245 |
assert!(html.contains(&format!("id=\"{panel}\"")), "{html}"); |
| 246 |
} |
| 247 |
assert!(!html.contains("hx-target=\"#item-overview\""), "{html}"); |
| 248 |
} |
| 249 |
|
| 250 |
#[test] |
| 251 |
fn the_routes_carry_the_item() { |
| 252 |
let html = strip(false); |
| 253 |
|
| 254 |
assert!( |
| 255 |
html.contains("hx-get=\"/dashboard/item/itm_1/tabs/details\""), |
| 256 |
"{html}" |
| 257 |
); |
| 258 |
|
| 259 |
|
| 260 |
assert!(!html.contains("/tabs/embed"), "{html}"); |
| 261 |
} |
| 262 |
|
| 263 |
#[test] |
| 264 |
fn a_bundle_has_no_files_tab() { |
| 265 |
let bundle = strip(true); |
| 266 |
assert!(!bundle.contains(">Files</button>"), "{bundle}"); |
| 267 |
assert!(!bundle.contains("item-files"), "{bundle}"); |
| 268 |
|
| 269 |
assert!(bundle.contains("role=\"tablist\""), "{bundle}"); |
| 270 |
assert!(bundle.contains("<p>the overview</p>"), "{bundle}"); |
| 271 |
assert_eq!(bundle.matches("hx-get=").count(), 3, "{bundle}"); |
| 272 |
|
| 273 |
assert!(strip(false).contains(">Files</button>")); |
| 274 |
} |
| 275 |
|
| 276 |
#[test] |
| 277 |
fn the_strip_says_what_it_does_when_it_runs_out_of_room() { |
| 278 |
assert!(strip(false).contains("run-menu")); |
| 279 |
} |
| 280 |
|
| 281 |
#[test] |
| 282 |
fn a_deep_link_arrives_showing_what_it_asked_for() { |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
let shown = shown_at(Some("files"), false); |
| 288 |
assert_eq!(shown, 3); |
| 289 |
assert_eq!(route_at(shown, false), "files"); |
| 290 |
|
| 291 |
let html = html("itm_1", shown, "<p>the versions</p>", false); |
| 292 |
assert!(html.contains("<p>the versions</p>"), "{html}"); |
| 293 |
|
| 294 |
assert!(!html.contains("hx-target=\"#item-files\""), "{html}"); |
| 295 |
assert!(html.contains("hx-target=\"#item-overview\""), "{html}"); |
| 296 |
assert!(html.contains("data-shows=\"3\""), "{html}"); |
| 297 |
} |
| 298 |
|
| 299 |
#[test] |
| 300 |
fn a_bundle_asking_for_files_lands_on_the_first_tab() { |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
assert_eq!(shown_at(Some("files"), true), 0); |
| 305 |
assert_eq!(route_at(shown_at(Some("files"), true), true), "overview"); |
| 306 |
|
| 307 |
assert_eq!(shown_at(Some("sales"), true), 3); |
| 308 |
} |
| 309 |
|
| 310 |
#[test] |
| 311 |
fn an_unknown_or_absent_tab_is_the_first_one() { |
| 312 |
assert_eq!(shown_at(None, false), 0); |
| 313 |
assert_eq!(shown_at(Some("nonsense"), false), 0); |
| 314 |
assert_eq!(shown_at(Some("embed"), false), 0); |
| 315 |
} |
| 316 |
|
| 317 |
#[test] |
| 318 |
fn a_shown_index_past_the_end_cannot_panic() { |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
let html = html("itm_1", 99, "<p>the overview</p>", true); |
| 323 |
assert!(html.contains("<p>the overview</p>"), "{html}"); |
| 324 |
assert!(html.contains("data-shows=\"3\""), "{html}"); |
| 325 |
} |
| 326 |
} |
| 327 |
|