| 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 |
use std::collections::HashMap; |
| 28 |
use std::fmt::Write as _; |
| 29 |
|
| 30 |
use makeover_layout as layout; |
| 31 |
|
| 32 |
|
| 33 |
use makeover_layout::Intent as _; |
| 34 |
use makeover_webview::figure::figure_html_into; |
| 35 |
use makeover_webview::form::{Filling, Markup, Value, escape_into, field_html_into}; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
use makeover_webview::{Emit, class, option_class}; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
use makeover_webview::list::{ |
| 47 |
Cell as Emitted, cell_part_class, cells_html_into, part_class, push_column_classes, |
| 48 |
}; |
| 49 |
use makeover_webview::meter::meter_html_into; |
| 50 |
use makeover_webview::placeholder::placeholder_html_into; |
| 51 |
use quasi_router::screen::{Act, Cell, Cells, Destination, Field, Node, Row, Slot, Tag}; |
| 52 |
use quasi_router::{Action, Method, Params}; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
pub(crate) fn class_into(name: &str, opts: &Emit, out: &mut String) { |
| 67 |
escape_into(opts.class_prefix, out); |
| 68 |
escape_into(name, out); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
fn class_attr(names: &[&str], opts: &Emit, out: &mut String) { |
| 73 |
out.push_str(" class=\""); |
| 74 |
for (i, name) in names.iter().enumerate() { |
| 75 |
if i > 0 { |
| 76 |
out.push(' '); |
| 77 |
} |
| 78 |
class_into(name, opts, out); |
| 79 |
} |
| 80 |
out.push('"'); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
fn tone_attr(tone: layout::Tone, out: &mut String) { |
| 97 |
if matches!(tone, layout::Tone::Neutral) { |
| 98 |
return; |
| 99 |
} |
| 100 |
out.push_str(" data-tone=\""); |
| 101 |
out.push_str(tone.token()); |
| 102 |
out.push('"'); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
fn fit_attr(fit: layout::Fit, out: &mut String) { |
| 111 |
let value = match fit { |
| 112 |
layout::Fit::Natural => return, |
| 113 |
layout::Fit::Cover => "cover", |
| 114 |
layout::Fit::Contain => "contain", |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
_ => return, |
| 120 |
}; |
| 121 |
out.push_str(" data-fit=\""); |
| 122 |
out.push_str(value); |
| 123 |
out.push('"'); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
fn refill(field: &Field) -> Value<'_> { |
| 139 |
if field.kind == layout::FieldKind::Secret { |
| 140 |
return Value::Absent; |
| 141 |
} |
| 142 |
match field.value.as_deref() { |
| 143 |
None => Value::Absent, |
| 144 |
Some(_) if field.kind == layout::FieldKind::Checkbox => Value::On(true), |
| 145 |
Some(value) => Value::Text(value), |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
fn field_group_html(field: &Field, morphs: bool, opts: &Emit, out: &mut String) { |
| 162 |
let filling = Filling::of(refill(field)); |
| 163 |
let writes = field.changes.as_ref(); |
| 164 |
|
| 165 |
if let Some(action) = writes { |
| 166 |
out.push_str("<div"); |
| 167 |
class_attr(&["field-writes"], opts, out); |
| 168 |
action_attrs(action, Fires::ChangeInside, None, morphs, None, out); |
| 169 |
out.push('>'); |
| 170 |
} |
| 171 |
|
| 172 |
field.with_layout(|borrowed| { |
| 173 |
field_html_into(&borrowed, &filling, opts, out); |
| 174 |
}); |
| 175 |
|
| 176 |
if writes.is_some() { |
| 177 |
out.push_str("</div>"); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
fn rich_html(source: &str) -> String { |
| 196 |
docengine::render_strict(source) |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
fn json_string_attr(text: &str, out: &mut String) { |
| 217 |
out.push_str("""); |
| 218 |
for ch in text.chars() { |
| 219 |
match ch { |
| 220 |
|
| 221 |
'"' => out.push_str("\\""), |
| 222 |
'\\' => out.push_str("\\\\"), |
| 223 |
'\n' => out.push_str("\\n"), |
| 224 |
'\r' => out.push_str("\\r"), |
| 225 |
'\t' => out.push_str("\\t"), |
| 226 |
c if (c as u32) < 0x20 => { |
| 227 |
let _ = write!(out, "\\u{:04x}", c as u32); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
'&' => out.push_str("&"), |
| 233 |
'<' => out.push_str("<"), |
| 234 |
'>' => out.push_str(">"), |
| 235 |
'\'' => out.push_str("'"), |
| 236 |
c => out.push(c), |
| 237 |
} |
| 238 |
} |
| 239 |
out.push_str("""); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
fn json_object_attr(params: &Params, out: &mut String) { |
| 244 |
out.push('{'); |
| 245 |
for (i, (name, value)) in params.iter().enumerate() { |
| 246 |
if i > 0 { |
| 247 |
out.push(','); |
| 248 |
} |
| 249 |
json_string_attr(name, out); |
| 250 |
out.push(':'); |
| 251 |
json_string_attr(value, out); |
| 252 |
} |
| 253 |
out.push('}'); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 262 |
pub(crate) enum Fires<'a> { |
| 263 |
|
| 264 |
Click, |
| 265 |
|
| 266 |
|
| 267 |
Change, |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
ChangeInside, |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
Key(&'a str), |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
ClickBeside, |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
pub(crate) fn action_attrs( |
| 316 |
action: &Action, |
| 317 |
fires: Fires, |
| 318 |
confirm: Option<&str>, |
| 319 |
morphs: bool, |
| 320 |
gathers: Option<&str>, |
| 321 |
out: &mut String, |
| 322 |
) { |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
if let Destination::External(url) = &action.destination { |
| 328 |
out.push_str(" href=\""); |
| 329 |
escape_into(url, out); |
| 330 |
out.push_str("\" target=\"_blank\" rel=\"noopener noreferrer\""); |
| 331 |
return; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
let url = quasi_http::route_url(action.destination.as_str(), &action.carried); |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
if matches!(action.destination, Destination::Route(_)) && !action.method.mutates() { |
| 347 |
out.push_str(" href=\""); |
| 348 |
escape_into(&url, out); |
| 349 |
out.push('"'); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
if let Some(selector) = gathers { |
| 361 |
out.push_str(" hx-include=\""); |
| 362 |
escape_into(selector, out); |
| 363 |
out.push('"'); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
if let Some(region) = &action.replaces { |
| 370 |
out.push_str(" hx-target=\"#"); |
| 371 |
escape_into(region, out); |
| 372 |
out.push('"'); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
if let Some(filename) = &action.saves { |
| 383 |
if is_link(action) { |
| 384 |
out.push_str(" download=\""); |
| 385 |
} else { |
| 386 |
out.push_str(" data-saves=\""); |
| 387 |
} |
| 388 |
escape_into(filename, out); |
| 389 |
out.push('"'); |
| 390 |
} |
| 391 |
|
| 392 |
let verb = match action.method { |
| 393 |
Method::Get => " hx-get=\"", |
| 394 |
Method::Post => " hx-post=\"", |
| 395 |
Method::Delete => " hx-delete=\"", |
| 396 |
Method::Put => " hx-put=\"", |
| 397 |
}; |
| 398 |
out.push_str(verb); |
| 399 |
escape_into(&url, out); |
| 400 |
out.push('"'); |
| 401 |
|
| 402 |
if !action.params.is_empty() { |
| 403 |
out.push_str(" hx-vals=\""); |
| 404 |
json_object_attr(&action.params, out); |
| 405 |
out.push('"'); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
match fires { |
| 411 |
Fires::Click => {} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
Fires::ClickBeside => out.push_str(concat!( |
| 416 |
" hx-trigger=\"click[!event.target.closest(", |
| 417 |
"'[data-act]')]\"" |
| 418 |
)), |
| 419 |
Fires::Change => out.push_str(" hx-trigger=\"change\""), |
| 420 |
Fires::Key(filter) => { |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
out.push_str(" hx-trigger=\"keydown["); |
| 425 |
escape_into(filter, out); |
| 426 |
out.push_str("] from:body\""); |
| 427 |
} |
| 428 |
Fires::ChangeInside => { |
| 429 |
out.push_str(" hx-trigger=\"change\""); |
| 430 |
out.push_str(" hx-include=\"find input, find select, find textarea\""); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
if let Some(prompt) = confirm { |
| 439 |
out.push_str(" hx-confirm=\""); |
| 440 |
escape_into(prompt, out); |
| 441 |
out.push('"'); |
| 442 |
} |
| 443 |
|
| 444 |
if morphs { |
| 445 |
|
| 446 |
|
| 447 |
out.push_str(" hx-swap=\"morph\""); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
const fn is_link(action: &Action) -> bool { |
| 461 |
action.destination.is_external() || !action.method.mutates() |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
const fn control_tag(action: &Action) -> (&'static str, &'static str) { |
| 476 |
if is_link(action) { |
| 477 |
("<a", "</a>") |
| 478 |
} else { |
| 479 |
("<button type=\"button\"", "</button>") |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
pub(crate) fn act_html(act: &Act, morphs: bool, opts: &Emit, out: &mut String) { |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
let gathers = act |
| 497 |
.over |
| 498 |
.as_ref() |
| 499 |
.map(|_| format!(".{}", class("row-select", opts))); |
| 500 |
let gathers = gathers.as_deref(); |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
let (open, close) = control_tag(&act.action); |
| 508 |
out.push_str(open); |
| 509 |
class_attr(&["button"], opts, out); |
| 510 |
tone_attr(act.tone, out); |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
out.push_str(" data-act"); |
| 516 |
|
| 517 |
match act.state { |
| 518 |
Some(layout::State::Disabled) => { |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
if is_link(&act.action) { |
| 529 |
out.push_str(" aria-disabled=\"true\""); |
| 530 |
} else { |
| 531 |
out.push_str(" disabled"); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
_ => action_attrs( |
| 543 |
&act.action, |
| 544 |
Fires::Click, |
| 545 |
act.confirm.as_deref(), |
| 546 |
morphs, |
| 547 |
gathers, |
| 548 |
out, |
| 549 |
), |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
if let Some(key) = &act.key { |
| 557 |
out.push_str(" accesskey=\""); |
| 558 |
escape_into(key, out); |
| 559 |
out.push('"'); |
| 560 |
} |
| 561 |
|
| 562 |
out.push('>'); |
| 563 |
escape_into(&act.label, out); |
| 564 |
out.push_str(close); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
fn row_html(row: &Row, morphs: bool, opts: &Emit, out: &mut String) { |
| 569 |
let mut classes = vec!["row"]; |
| 570 |
if row.current { |
| 571 |
classes.push("row-current"); |
| 572 |
} |
| 573 |
if row.selected == Some(true) { |
| 574 |
classes.push("row-selected"); |
| 575 |
} |
| 576 |
|
| 577 |
out.push_str("<li"); |
| 578 |
class_attr(&classes, opts, out); |
| 579 |
if row.current { |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
out.push_str(" aria-current=\"true\""); |
| 584 |
} |
| 585 |
out.push('>'); |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
if let Some(ticked) = row.selected { |
| 592 |
out.push_str("<input type=\"checkbox\""); |
| 593 |
class_attr(&["row-select"], opts, out); |
| 594 |
if ticked { |
| 595 |
out.push_str(" checked"); |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
if let Some(value) = &row.value { |
| 606 |
out.push_str(" name=\""); |
| 607 |
escape_into(quasi_router::Node::TICKED, out); |
| 608 |
out.push_str("\" value=\""); |
| 609 |
escape_into(value, out); |
| 610 |
out.push('"'); |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
if let Some(action) = &row.toggle { |
| 618 |
action_attrs(action, Fires::Change, None, morphs, None, out); |
| 619 |
} |
| 620 |
out.push_str(" aria-label=\"Select\">"); |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
let mut rest = row.parts.as_slice(); |
| 633 |
while let Some(head) = rest.first() { |
| 634 |
let role = head.role; |
| 635 |
let taken = rest.iter().take_while(|part| part.role == role).count(); |
| 636 |
let (group, tail) = rest.split_at(taken); |
| 637 |
row_part_html(row, role, group, morphs, opts, out); |
| 638 |
rest = tail; |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
if !row.menu.is_empty() { |
| 646 |
out.push_str("<div"); |
| 647 |
class_attr(&["row-menu"], opts, out); |
| 648 |
|
| 649 |
|
| 650 |
out.push_str(" data-menu=\"row\" hidden>"); |
| 651 |
for act in &row.menu { |
| 652 |
act_html(act, morphs, opts, out); |
| 653 |
} |
| 654 |
out.push_str("</div>"); |
| 655 |
} |
| 656 |
|
| 657 |
out.push_str("</li>"); |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
fn row_part_html( |
| 662 |
row: &Row, |
| 663 |
role: layout::RowPart, |
| 664 |
group: &[quasi_router::Part], |
| 665 |
morphs: bool, |
| 666 |
opts: &Emit, |
| 667 |
out: &mut String, |
| 668 |
) { |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
let activates = role == layout::RowPart::Primary && row.activate.is_some(); |
| 675 |
|
| 676 |
let close = if let (true, Some(action)) = (activates, row.activate.as_ref()) { |
| 677 |
let (open, close) = control_tag(action); |
| 678 |
out.push_str(open); |
| 679 |
class_attr(&["row-activate"], opts, out); |
| 680 |
action_attrs(action, Fires::Click, None, morphs, None, out); |
| 681 |
out.push('>'); |
| 682 |
close |
| 683 |
} else { |
| 684 |
out.push_str("<span"); |
| 685 |
class_attr(&[part_class(role)], opts, out); |
| 686 |
out.push('>'); |
| 687 |
"</span>" |
| 688 |
}; |
| 689 |
|
| 690 |
for part in group { |
| 691 |
row_inline_html(&part.node, morphs, opts, out); |
| 692 |
} |
| 693 |
|
| 694 |
out.push_str(close); |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
fn row_inline_html(node: &Node, morphs: bool, opts: &Emit, out: &mut String) { |
| 717 |
match node { |
| 718 |
Node::Text { text, .. } => escape_into(text, out), |
| 719 |
Node::Rich { source } => out.push_str(&docengine::render_phrase(source)), |
| 720 |
Node::Token(tag) => tag_html(tag, morphs, opts, out), |
| 721 |
Node::Act(act) => act_html(act, morphs, opts, out), |
| 722 |
Node::Meter(meter) => meter_html_into(&meter.as_layout(), opts, out), |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
other => node_html(other, morphs, opts, &HashMap::new(), out), |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
fn cell_run_html(cell: &Cell, morphs: bool, opts: &Emit, out: &mut String) { |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
if let [Node::Text { text, .. }] = cell.parts.as_slice() { |
| 761 |
escape_into(text, out); |
| 762 |
return; |
| 763 |
} |
| 764 |
|
| 765 |
let mut rest = cell.parts.as_slice(); |
| 766 |
while let Some((head, tail)) = rest.split_first() { |
| 767 |
match head { |
| 768 |
Node::Text { text, .. } => { |
| 769 |
out.push_str("<span"); |
| 770 |
class_attr(&[cell_part_class(layout::CellPart::Value)], opts, out); |
| 771 |
out.push('>'); |
| 772 |
escape_into(text, out); |
| 773 |
out.push_str("</span>"); |
| 774 |
rest = tail; |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
Node::Link { text, action } => { |
| 785 |
let (open, close) = control_tag(action); |
| 786 |
out.push_str(open); |
| 787 |
class_attr(&[cell_part_class(layout::CellPart::Link)], opts, out); |
| 788 |
out.push_str(" data-act"); |
| 789 |
action_attrs(action, Fires::Click, None, morphs, None, out); |
| 790 |
out.push('>'); |
| 791 |
escape_into(text, out); |
| 792 |
out.push_str(close); |
| 793 |
rest = tail; |
| 794 |
} |
| 795 |
Node::Token(_) => { |
| 796 |
let run = rest.iter().take_while(|p| matches!(p, Node::Token(_))); |
| 797 |
out.push_str("<span"); |
| 798 |
class_attr(&[cell_part_class(layout::CellPart::Tokens)], opts, out); |
| 799 |
out.push('>'); |
| 800 |
let mut taken = 0; |
| 801 |
for part in run { |
| 802 |
if let Node::Token(tag) = part { |
| 803 |
tag_html(tag, morphs, opts, out); |
| 804 |
} |
| 805 |
taken += 1; |
| 806 |
} |
| 807 |
out.push_str("</span>"); |
| 808 |
rest = &rest[taken..]; |
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
Node::Act(_) => { |
| 815 |
let run = rest.iter().take_while(|p| matches!(p, Node::Act(_))); |
| 816 |
out.push_str("<span"); |
| 817 |
class_attr(&[cell_part_class(layout::CellPart::Actions)], opts, out); |
| 818 |
out.push('>'); |
| 819 |
let mut taken = 0; |
| 820 |
for part in run { |
| 821 |
if let Node::Act(act) = part { |
| 822 |
act_html(act, morphs, opts, out); |
| 823 |
} |
| 824 |
taken += 1; |
| 825 |
} |
| 826 |
out.push_str("</span>"); |
| 827 |
rest = &rest[taken..]; |
| 828 |
} |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
other => { |
| 834 |
|
| 835 |
|
| 836 |
node_html(other, morphs, opts, &HashMap::new(), out); |
| 837 |
rest = tail; |
| 838 |
} |
| 839 |
} |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
#[derive(Default)] |
| 853 |
struct RowBuffers { |
| 854 |
|
| 855 |
filled: Vec<String>, |
| 856 |
|
| 857 |
|
| 858 |
parts: Vec<Option<layout::CellPart>>, |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
fn cells_row_html( |
| 867 |
cells: &Cells, |
| 868 |
columns: &[layout::Column<'_>], |
| 869 |
buffers: &mut RowBuffers, |
| 870 |
morphs: bool, |
| 871 |
opts: &Emit, |
| 872 |
out: &mut String, |
| 873 |
) { |
| 874 |
let mut classes = vec!["table-row"]; |
| 875 |
if cells.current { |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
classes.push("table-row-current"); |
| 880 |
} |
| 881 |
|
| 882 |
out.push_str("<div role=\"row\""); |
| 883 |
class_attr(&classes, opts, out); |
| 884 |
if cells.current { |
| 885 |
out.push_str(" aria-current=\"true\""); |
| 886 |
} |
| 887 |
if let Some(action) = &cells.activate { |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
let carries_control = cells.values.iter().any(Cell::carries_control); |
| 893 |
let fires = if carries_control { |
| 894 |
Fires::ClickBeside |
| 895 |
} else { |
| 896 |
Fires::Click |
| 897 |
}; |
| 898 |
action_attrs(action, fires, None, morphs, None, out); |
| 899 |
} |
| 900 |
out.push('>'); |
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
let RowBuffers { filled, parts } = buffers; |
| 908 |
filled.truncate(cells.values.len()); |
| 909 |
for (at, cell) in cells.values.iter().enumerate() { |
| 910 |
match filled.get_mut(at) { |
| 911 |
Some(buffer) => buffer.clear(), |
| 912 |
None => filled.push(String::new()), |
| 913 |
} |
| 914 |
cell_run_html(cell, morphs, opts, &mut filled[at]); |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
parts.clear(); |
| 923 |
parts.extend(cells.values.iter().map(|cell| { |
| 924 |
matches!(cell.parts.as_slice(), [Node::Text { .. }]).then_some(layout::CellPart::Value) |
| 925 |
})); |
| 926 |
let emitted: Vec<Emitted<'_>> = columns |
| 927 |
.iter() |
| 928 |
.zip(filled.iter()) |
| 929 |
.zip(parts.iter().copied()) |
| 930 |
.map(|((column, value), part)| Emitted { |
| 931 |
column: column.name, |
| 932 |
part, |
| 933 |
content: Markup(value), |
| 934 |
}) |
| 935 |
.collect(); |
| 936 |
cells_html_into(columns, &emitted, opts, out); |
| 937 |
|
| 938 |
out.push_str("</div>"); |
| 939 |
} |
| 940 |
|
| 941 |
|
| 942 |
pub(crate) fn node_html( |
| 943 |
node: &Node, |
| 944 |
morphs: bool, |
| 945 |
opts: &Emit, |
| 946 |
fills: &HashMap<String, String>, |
| 947 |
out: &mut String, |
| 948 |
) { |
| 949 |
match node { |
| 950 |
Node::Heading { level, text } => { |
| 951 |
let tag = match level { |
| 952 |
layout::Heading::Page => "h1", |
| 953 |
layout::Heading::Section => "h2", |
| 954 |
layout::Heading::Subsection => "h3", |
| 955 |
}; |
| 956 |
out.push('<'); |
| 957 |
out.push_str(tag); |
| 958 |
class_attr(&["heading"], opts, out); |
| 959 |
out.push('>'); |
| 960 |
escape_into(text, out); |
| 961 |
out.push_str("</"); |
| 962 |
out.push_str(tag); |
| 963 |
out.push('>'); |
| 964 |
} |
| 965 |
|
| 966 |
Node::Text { text, tone } => { |
| 967 |
out.push_str("<p"); |
| 968 |
class_attr(&["text"], opts, out); |
| 969 |
tone_attr(*tone, out); |
| 970 |
out.push('>'); |
| 971 |
escape_into(text, out); |
| 972 |
out.push_str("</p>"); |
| 973 |
} |
| 974 |
|
| 975 |
Node::StandIn { |
| 976 |
state, |
| 977 |
message, |
| 978 |
act, |
| 979 |
} => { |
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
let mut way_out = String::new(); |
| 985 |
if let Some(act) = act { |
| 986 |
act_html(act, morphs, opts, &mut way_out); |
| 987 |
} |
| 988 |
placeholder_html_into( |
| 989 |
*state, |
| 990 |
message, |
| 991 |
(!way_out.is_empty()).then_some(Markup(way_out.as_str())), |
| 992 |
opts, |
| 993 |
out, |
| 994 |
); |
| 995 |
} |
| 996 |
|
| 997 |
Node::Rich { source } => { |
| 998 |
out.push_str("<div"); |
| 999 |
class_attr(&["rich"], opts, out); |
| 1000 |
out.push('>'); |
| 1001 |
out.push_str(&rich_html(source)); |
| 1002 |
out.push_str("</div>"); |
| 1003 |
} |
| 1004 |
|
| 1005 |
Node::Act(act) => act_html(act, morphs, opts, out), |
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
Node::Link { text, action } => { |
| 1011 |
let (open, close) = control_tag(action); |
| 1012 |
out.push_str(open); |
| 1013 |
class_attr(&["link"], opts, out); |
| 1014 |
out.push_str(" data-act"); |
| 1015 |
action_attrs(action, Fires::Click, None, morphs, None, out); |
| 1016 |
out.push('>'); |
| 1017 |
escape_into(text, out); |
| 1018 |
out.push_str(close); |
| 1019 |
} |
| 1020 |
|
| 1021 |
Node::Token(tag) => tag_html(tag, morphs, opts, out), |
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
Node::Figure(figure) => { |
| 1026 |
figure_html_into(&figure.as_layout(), opts, out); |
| 1027 |
} |
| 1028 |
|
| 1029 |
Node::Image(picture) => { |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
let captioned = picture.caption.is_some(); |
| 1035 |
if captioned { |
| 1036 |
out.push_str("<figure"); |
| 1037 |
class_attr(&["picture"], opts, out); |
| 1038 |
out.push('>'); |
| 1039 |
} |
| 1040 |
|
| 1041 |
out.push_str("<img"); |
| 1042 |
class_attr(&["picture-img"], opts, out); |
| 1043 |
out.push_str(" src=\""); |
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
escape_into(&picture.src, out); |
| 1050 |
out.push_str("\" alt=\""); |
| 1051 |
escape_into(&picture.alt, out); |
| 1052 |
out.push('"'); |
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
if let Some(e) = picture.intrinsic { |
| 1061 |
let _ = write!(out, " width=\"{}\" height=\"{}\"", e.width, e.height); |
| 1062 |
} |
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
if matches!(picture.loading, layout::Loading::Lazy) { |
| 1070 |
out.push_str(" loading=\"lazy\""); |
| 1071 |
} |
| 1072 |
fit_attr(picture.fit, out); |
| 1073 |
out.push('>'); |
| 1074 |
|
| 1075 |
if let Some(caption) = &picture.caption { |
| 1076 |
out.push_str("<figcaption"); |
| 1077 |
class_attr(&["picture-caption"], opts, out); |
| 1078 |
out.push('>'); |
| 1079 |
escape_into(caption, out); |
| 1080 |
out.push_str("</figcaption></figure>"); |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
Node::Notice { kind, tone, text } => { |
| 1085 |
out.push_str("<div"); |
| 1086 |
class_attr( |
| 1087 |
&[match kind { |
| 1088 |
layout::Notice::Toast => "toast", |
| 1089 |
layout::Notice::Banner => "banner", |
| 1090 |
}], |
| 1091 |
opts, |
| 1092 |
out, |
| 1093 |
); |
| 1094 |
tone_attr(*tone, out); |
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
let assertive = matches!(tone, layout::Tone::Danger | layout::Tone::Warning); |
| 1099 |
if assertive { |
| 1100 |
out.push_str(" role=\"alert\""); |
| 1101 |
} else { |
| 1102 |
out.push_str(" role=\"status\" aria-live=\"polite\""); |
| 1103 |
} |
| 1104 |
out.push('>'); |
| 1105 |
escape_into(text, out); |
| 1106 |
out.push_str("</div>"); |
| 1107 |
} |
| 1108 |
|
| 1109 |
Node::Field(field) => field_group_html(field, morphs, opts, out), |
| 1110 |
|
| 1111 |
Node::Form { |
| 1112 |
action, |
| 1113 |
submit, |
| 1114 |
fields, |
| 1115 |
} => { |
| 1116 |
out.push_str("<form"); |
| 1117 |
class_attr(&["form"], opts, out); |
| 1118 |
action_attrs(action, Fires::Click, None, morphs, None, out); |
| 1119 |
out.push('>'); |
| 1120 |
for field in fields { |
| 1121 |
field_group_html(field, morphs, opts, out); |
| 1122 |
} |
| 1123 |
out.push_str("<button type=\"submit\""); |
| 1124 |
class_attr(&["button", "act-submit"], opts, out); |
| 1125 |
out.push('>'); |
| 1126 |
escape_into(submit, out); |
| 1127 |
out.push_str("</button></form>"); |
| 1128 |
} |
| 1129 |
|
| 1130 |
Node::List { rows, more } => { |
| 1131 |
out.push_str("<ul"); |
| 1132 |
class_attr(&["list"], opts, out); |
| 1133 |
out.push('>'); |
| 1134 |
for row in rows { |
| 1135 |
row_html(row, morphs, opts, out); |
| 1136 |
} |
| 1137 |
out.push_str("</ul>"); |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
if let Some(rest) = more { |
| 1144 |
out.push_str("<div"); |
| 1145 |
class_attr(&["rest"], opts, out); |
| 1146 |
out.push('>'); |
| 1147 |
|
| 1148 |
let (open, close) = control_tag(&rest.action); |
| 1149 |
out.push_str(open); |
| 1150 |
class_attr(&["button", "rest-more"], opts, out); |
| 1151 |
action_attrs(&rest.action, Fires::Click, None, morphs, None, out); |
| 1152 |
out.push('>'); |
| 1153 |
match rest.remaining { |
| 1154 |
Some(n) => { |
| 1155 |
out.push_str("Show more ("); |
| 1156 |
let _ = write!(out, "{n}"); |
| 1157 |
out.push_str(" remaining)"); |
| 1158 |
} |
| 1159 |
None => out.push_str("Show more"), |
| 1160 |
} |
| 1161 |
out.push_str(close); |
| 1162 |
out.push_str("</div>"); |
| 1163 |
} |
| 1164 |
} |
| 1165 |
|
| 1166 |
Node::Timeline { |
| 1167 |
track, |
| 1168 |
entries, |
| 1169 |
focus, |
| 1170 |
} => { |
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
out.push_str("<div"); |
| 1175 |
class_attr(&["track"], opts, out); |
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
if let Some(minute) = focus { |
| 1181 |
let _ = write!(out, " data-focus=\"{minute}\""); |
| 1182 |
} |
| 1183 |
out.push('>'); |
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
let slots = track.slots(); |
| 1189 |
let tick_every = if track.slot == 0 || track.tick == 0 { |
| 1190 |
0 |
| 1191 |
} else { |
| 1192 |
track.tick / track.slot |
| 1193 |
}; |
| 1194 |
for slot in 0..slots { |
| 1195 |
out.push_str("<div"); |
| 1196 |
class_attr(&["track-slot"], opts, out); |
| 1197 |
out.push('>'); |
| 1198 |
if tick_every > 0 && slot % tick_every == 0 { |
| 1199 |
let minute = track.span.from() + slot * track.slot; |
| 1200 |
out.push_str("<span"); |
| 1201 |
class_attr(&["track-tick"], opts, out); |
| 1202 |
out.push('>'); |
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
let _ = write!(out, "{:02}:{:02}", (minute / 60) % 24, minute % 60); |
| 1208 |
out.push_str("</span>"); |
| 1209 |
} |
| 1210 |
out.push_str("</div>"); |
| 1211 |
} |
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
let mut lanes: Vec<usize> = Vec::with_capacity(entries.len()); |
| 1225 |
for (i, entry) in entries.iter().enumerate() { |
| 1226 |
let mut lane = 0; |
| 1227 |
loop { |
| 1228 |
let taken = entries[..i] |
| 1229 |
.iter() |
| 1230 |
.zip(&lanes) |
| 1231 |
.any(|(other, &l)| l == lane && entry.overlaps(other)); |
| 1232 |
if !taken { |
| 1233 |
break; |
| 1234 |
} |
| 1235 |
lane += 1; |
| 1236 |
} |
| 1237 |
lanes.push(lane); |
| 1238 |
} |
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
let width = lanes.iter().copied().max().map_or(1, |m| m + 1); |
| 1244 |
|
| 1245 |
for (entry, lane) in entries.iter().zip(&lanes) { |
| 1246 |
let at = track.fraction(entry.placement.at()); |
| 1247 |
let end = track.fraction(entry.placement.end()); |
| 1248 |
out.push_str("<div"); |
| 1249 |
class_attr(&["track-entry"], opts, out); |
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
let _ = write!( |
| 1255 |
out, |
| 1256 |
" style=\"--track-at:{:.4}%;--track-for:{:.4}%;--track-lane:{lane};--track-lanes:{width}\"", |
| 1257 |
at * 100.0, |
| 1258 |
(end - at) * 100.0 |
| 1259 |
); |
| 1260 |
out.push('>'); |
| 1261 |
row_html(&entry.row, morphs, opts, out); |
| 1262 |
out.push_str("</div>"); |
| 1263 |
} |
| 1264 |
|
| 1265 |
out.push_str("</div>"); |
| 1266 |
} |
| 1267 |
|
| 1268 |
Node::Table { columns, rows } => { |
| 1269 |
let borrowed: Vec<layout::Column<'_>> = columns |
| 1270 |
.iter() |
| 1271 |
.map(quasi_router::screen::Column::as_layout) |
| 1272 |
.collect(); |
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
out.push_str("<div role=\"table\""); |
| 1283 |
class_attr(&["table"], opts, out); |
| 1284 |
out.push('>'); |
| 1285 |
|
| 1286 |
out.push_str("<div role=\"row\""); |
| 1287 |
class_attr(&["table-head"], opts, out); |
| 1288 |
out.push('>'); |
| 1289 |
for (column, described) in borrowed.iter().zip(columns) { |
| 1290 |
out.push_str("<span role=\"columnheader\""); |
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
out.push_str(" class=\""); |
| 1297 |
class_into("table-heading", opts, out); |
| 1298 |
out.push(' '); |
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
push_column_classes(out, column, opts); |
| 1308 |
out.push('"'); |
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
if let Some(sort) = column.sorted { |
| 1313 |
out.push_str(" aria-sort=\""); |
| 1314 |
out.push_str(sort.as_str()); |
| 1315 |
out.push('"'); |
| 1316 |
} |
| 1317 |
if column.sortable { |
| 1318 |
out.push_str(" data-sortable"); |
| 1319 |
} |
| 1320 |
out.push('>'); |
| 1321 |
match &described.reorder { |
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
Some(action) => { |
| 1327 |
let (open, close) = control_tag(action); |
| 1328 |
out.push_str(open); |
| 1329 |
class_attr(&["table-sort"], opts, out); |
| 1330 |
action_attrs(action, Fires::Click, None, morphs, None, out); |
| 1331 |
out.push('>'); |
| 1332 |
escape_into(column.name, out); |
| 1333 |
out.push_str(close); |
| 1334 |
} |
| 1335 |
None => escape_into(column.name, out), |
| 1336 |
} |
| 1337 |
out.push_str("</span>"); |
| 1338 |
} |
| 1339 |
out.push_str("</div>"); |
| 1340 |
|
| 1341 |
let mut buffers = RowBuffers::default(); |
| 1342 |
for cells in rows { |
| 1343 |
cells_row_html(cells, &borrowed, &mut buffers, morphs, opts, out); |
| 1344 |
} |
| 1345 |
out.push_str("</div>"); |
| 1346 |
} |
| 1347 |
|
| 1348 |
Node::Select { |
| 1349 |
kind, |
| 1350 |
options, |
| 1351 |
chosen, |
| 1352 |
action, |
| 1353 |
} => select_html( |
| 1354 |
*kind, |
| 1355 |
options, |
| 1356 |
chosen.as_deref(), |
| 1357 |
action.as_ref(), |
| 1358 |
morphs, |
| 1359 |
opts, |
| 1360 |
out, |
| 1361 |
), |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
Node::Meter(meter) => meter_html_into(&meter.as_layout(), opts, out), |
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
Node::Stats { figures } => { |
| 1374 |
out.push_str("<div"); |
| 1375 |
class_attr(&["figures"], opts, out); |
| 1376 |
out.push('>'); |
| 1377 |
for (figure, action) in figures { |
| 1378 |
match action { |
| 1379 |
Some(action) => { |
| 1380 |
let (open, close) = control_tag(action); |
| 1381 |
out.push_str(open); |
| 1382 |
class_attr(&["figure-act"], opts, out); |
| 1383 |
action_attrs(action, Fires::Click, None, morphs, None, out); |
| 1384 |
out.push('>'); |
| 1385 |
figure_html_into(&figure.as_layout(), opts, out); |
| 1386 |
out.push_str(close); |
| 1387 |
} |
| 1388 |
None => figure_html_into(&figure.as_layout(), opts, out), |
| 1389 |
} |
| 1390 |
} |
| 1391 |
out.push_str("</div>"); |
| 1392 |
} |
| 1393 |
|
| 1394 |
Node::Region(slot) => slot_html(slot, morphs, opts, fills, out), |
| 1395 |
} |
| 1396 |
} |
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
fn tag_html(tag: &Tag, morphs: bool, opts: &Emit, out: &mut String) { |
| 1404 |
let Tag { |
| 1405 |
kind, |
| 1406 |
label, |
| 1407 |
tone, |
| 1408 |
latched, |
| 1409 |
action, |
| 1410 |
} = tag; |
| 1411 |
let (kind, tone, latched) = (*kind, *tone, *latched); |
| 1412 |
let action = action.as_ref(); |
| 1413 |
|
| 1414 |
let mut classes = vec![match kind { |
| 1415 |
layout::Token::Badge => "badge", |
| 1416 |
layout::Token::Chip { .. } => "chip", |
| 1417 |
}]; |
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
if latched { |
| 1423 |
classes.push("latched"); |
| 1424 |
} |
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
let interactive = kind.interactive() && action.is_some(); |
| 1430 |
let close = if interactive { |
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
let (open, close) = action.map_or(("<span", "</span>"), control_tag); |
| 1435 |
out.push_str(open); |
| 1436 |
close |
| 1437 |
} else { |
| 1438 |
out.push_str("<span"); |
| 1439 |
"</span>" |
| 1440 |
}; |
| 1441 |
class_attr(&classes, opts, out); |
| 1442 |
tone_attr(tone, out); |
| 1443 |
|
| 1444 |
if interactive { |
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
out.push_str(" data-act"); |
| 1450 |
if latched { |
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
if action.is_some_and(is_link) { |
| 1458 |
out.push_str(" aria-current=\"true\""); |
| 1459 |
} else { |
| 1460 |
out.push_str(" aria-pressed=\"true\""); |
| 1461 |
} |
| 1462 |
} |
| 1463 |
if let Some(action) = action { |
| 1464 |
action_attrs(action, Fires::Click, None, morphs, None, out); |
| 1465 |
} |
| 1466 |
} |
| 1467 |
|
| 1468 |
out.push('>'); |
| 1469 |
escape_into(label, out); |
| 1470 |
if matches!(kind, layout::Token::Chip { removable: true }) { |
| 1471 |
out.push_str("<span"); |
| 1472 |
class_attr(&["chip-remove"], opts, out); |
| 1473 |
out.push_str(" aria-hidden=\"true\"></span>"); |
| 1474 |
} |
| 1475 |
out.push_str(close); |
| 1476 |
} |
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
fn select_html( |
| 1497 |
kind: layout::Selector, |
| 1498 |
options: &[(quasi_router::screen::Choice, Option<Action>)], |
| 1499 |
chosen: Option<&str>, |
| 1500 |
action: Option<&Action>, |
| 1501 |
morphs: bool, |
| 1502 |
opts: &Emit, |
| 1503 |
out: &mut String, |
| 1504 |
) { |
| 1505 |
out.push_str("<div"); |
| 1506 |
class_attr(&["selector"], opts, out); |
| 1507 |
out.push_str(" data-selector=\""); |
| 1508 |
out.push_str(option_class(kind)); |
| 1509 |
out.push('"'); |
| 1510 |
|
| 1511 |
|
| 1512 |
if matches!(kind, layout::Selector::Tabs) { |
| 1513 |
out.push_str(" role=\"tablist\""); |
| 1514 |
} else { |
| 1515 |
out.push_str(" role=\"group\""); |
| 1516 |
} |
| 1517 |
out.push('>'); |
| 1518 |
|
| 1519 |
for (option, own) in options { |
| 1520 |
let picked = chosen.is_some_and(|value| value == option.value); |
| 1521 |
|
| 1522 |
let mut classes = vec![option_class(kind)]; |
| 1523 |
if picked { |
| 1524 |
classes.push("chosen"); |
| 1525 |
} |
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
let (open, close) = own |
| 1532 |
.as_ref() |
| 1533 |
.map_or(("<button type=\"button\"", "</button>"), |action| { |
| 1534 |
control_tag(action) |
| 1535 |
}); |
| 1536 |
out.push_str(open); |
| 1537 |
class_attr(&classes, opts, out); |
| 1538 |
if matches!(kind, layout::Selector::Tabs) { |
| 1539 |
out.push_str(" role=\"tab\" aria-selected=\""); |
| 1540 |
out.push_str(if picked { "true\"" } else { "false\"" }); |
| 1541 |
} else if picked { |
| 1542 |
out.push_str(" aria-pressed=\"true\""); |
| 1543 |
} |
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
if let Some(own) = own { |
| 1550 |
action_attrs(own, Fires::Click, None, morphs, None, out); |
| 1551 |
} else if let Some(action) = action { |
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
let carrying = action.clone().with(Node::SELECTED, option.value.clone()); |
| 1556 |
action_attrs(&carrying, Fires::Click, None, morphs, None, out); |
| 1557 |
} |
| 1558 |
|
| 1559 |
out.push('>'); |
| 1560 |
escape_into(&option.label, out); |
| 1561 |
out.push_str(close); |
| 1562 |
} |
| 1563 |
|
| 1564 |
out.push_str("</div>"); |
| 1565 |
} |
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
pub(crate) fn oob_html( |
| 1589 |
region: &str, |
| 1590 |
node: &Node, |
| 1591 |
morphs: bool, |
| 1592 |
opts: &Emit, |
| 1593 |
fills: &HashMap<String, String>, |
| 1594 |
out: &mut String, |
| 1595 |
) { |
| 1596 |
out.push_str("<div hx-swap-oob=\"innerHTML:#"); |
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
escape_into(region, out); |
| 1601 |
out.push_str("\">"); |
| 1602 |
node_html(node, morphs, opts, fills, out); |
| 1603 |
out.push_str("</div>"); |
| 1604 |
} |
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
fn frame_class_attr(current: bool, opts: &Emit, out: &mut String) { |
| 1613 |
out.push_str(" class=\""); |
| 1614 |
class_into("showing-frame", opts, out); |
| 1615 |
if current { |
| 1616 |
out.push_str(" current"); |
| 1617 |
} |
| 1618 |
out.push('"'); |
| 1619 |
} |
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
fn showing_html(slot: &Slot, opts: &Emit, out: &mut String) { |
| 1654 |
let labels = slot.labels(); |
| 1655 |
let current = slot.current(); |
| 1656 |
let total = slot.body.len(); |
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
if slot.showing.dismissible() |
| 1662 |
&& total == 1 |
| 1663 |
&& let [label] = labels.as_slice() |
| 1664 |
{ |
| 1665 |
out.push_str("<button type=\"button\""); |
| 1666 |
|
| 1667 |
|
| 1668 |
class_attr(&["button"], opts, out); |
| 1669 |
out.push_str(" data-shows=\"0\" aria-expanded=\""); |
| 1670 |
out.push_str(if current.is_some() { |
| 1671 |
"true\"" |
| 1672 |
} else { |
| 1673 |
"false\"" |
| 1674 |
}); |
| 1675 |
out.push('>'); |
| 1676 |
escape_into(label, out); |
| 1677 |
out.push_str("</button>"); |
| 1678 |
return; |
| 1679 |
} |
| 1680 |
|
| 1681 |
if !labels.is_empty() { |
| 1682 |
let tab = option_class(layout::Selector::Tabs); |
| 1683 |
out.push_str("<div"); |
| 1684 |
class_attr(&["selector"], opts, out); |
| 1685 |
out.push_str(" data-selector=\""); |
| 1686 |
out.push_str(tab); |
| 1687 |
out.push_str("\" role=\"tablist\">"); |
| 1688 |
for (at, label) in labels.iter().enumerate() { |
| 1689 |
let picked = current == Some(at); |
| 1690 |
out.push_str("<button type=\"button\" class=\""); |
| 1691 |
class_into(tab, opts, out); |
| 1692 |
if picked { |
| 1693 |
out.push_str(" chosen"); |
| 1694 |
} |
| 1695 |
let _ = write!(out, "\" data-shows=\"{at}\" role=\"tab\" aria-selected=\""); |
| 1696 |
out.push_str(if picked { "true\"" } else { "false\"" }); |
| 1697 |
out.push('>'); |
| 1698 |
escape_into(label, out); |
| 1699 |
out.push_str("</button>"); |
| 1700 |
} |
| 1701 |
out.push_str("</div>"); |
| 1702 |
return; |
| 1703 |
} |
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
out.push_str("<div"); |
| 1710 |
class_attr(&["showing"], opts, out); |
| 1711 |
out.push('>'); |
| 1712 |
|
| 1713 |
out.push_str("<button type=\"button\""); |
| 1714 |
class_attr(&["button"], opts, out); |
| 1715 |
out.push_str(" data-shows=\"previous\">Prev</button>"); |
| 1716 |
|
| 1717 |
out.push('<'); |
| 1718 |
out.push_str("span"); |
| 1719 |
class_attr(&["showing-position"], opts, out); |
| 1720 |
|
| 1721 |
|
| 1722 |
let _ = write!(out, ">{} / {total}</span>", current.map_or(0, |at| at + 1)); |
| 1723 |
|
| 1724 |
out.push_str("<button type=\"button\""); |
| 1725 |
class_attr(&["button"], opts, out); |
| 1726 |
out.push_str(" data-shows=\"next\">Next</button>"); |
| 1727 |
|
| 1728 |
out.push_str("</div>"); |
| 1729 |
} |
| 1730 |
|
| 1731 |
|
| 1732 |
pub(crate) fn slot_html( |
| 1733 |
slot: &Slot, |
| 1734 |
morphs: bool, |
| 1735 |
opts: &Emit, |
| 1736 |
fills: &HashMap<String, String>, |
| 1737 |
out: &mut String, |
| 1738 |
) { |
| 1739 |
let kind = match &slot.kind { |
| 1740 |
quasi_router::RegionKind::Band => "band", |
| 1741 |
quasi_router::RegionKind::Sidebar => "sidebar", |
| 1742 |
quasi_router::RegionKind::Pane => "pane", |
| 1743 |
quasi_router::RegionKind::Split => "split", |
| 1744 |
quasi_router::RegionKind::Columns => "columns", |
| 1745 |
quasi_router::RegionKind::TabGroup => "tabgroup", |
| 1746 |
quasi_router::RegionKind::Modal => "modal", |
| 1747 |
quasi_router::RegionKind::Bespoke { .. } => "bespoke", |
| 1748 |
quasi_router::RegionKind::Widget { .. } => "widget", |
| 1749 |
}; |
| 1750 |
|
| 1751 |
out.push_str("<div id=\""); |
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
escape_into(&slot.id, out); |
| 1756 |
out.push('"'); |
| 1757 |
class_attr(&["region", kind], opts, out); |
| 1758 |
|
| 1759 |
if let quasi_router::RegionKind::Bespoke { name } = &slot.kind { |
| 1760 |
|
| 1761 |
|
| 1762 |
out.push_str(" data-bespoke=\""); |
| 1763 |
escape_into(name, out); |
| 1764 |
out.push('"'); |
| 1765 |
} |
| 1766 |
|
| 1767 |
if let quasi_router::RegionKind::Widget { name } = &slot.kind { |
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
out.push_str(" data-widget=\""); |
| 1780 |
escape_into(name, out); |
| 1781 |
out.push('"'); |
| 1782 |
} |
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
match slot.showing { |
| 1793 |
layout::Showing::One => out.push_str(" data-showing=\"one\""), |
| 1794 |
layout::Showing::AtMostOne => out.push_str(" data-showing=\"at-most-one\""), |
| 1795 |
_ => {} |
| 1796 |
} |
| 1797 |
|
| 1798 |
if matches!(slot.readiness, layout::Readiness::Pending) { |
| 1799 |
out.push_str(" aria-busy=\"true\""); |
| 1800 |
} |
| 1801 |
|
| 1802 |
if matches!(slot.kind, quasi_router::RegionKind::Modal) { |
| 1803 |
|
| 1804 |
|
| 1805 |
out.push_str(" role=\"dialog\" aria-modal=\"true\""); |
| 1806 |
} |
| 1807 |
|
| 1808 |
out.push('>'); |
| 1809 |
if slot.showing.selective() { |
| 1810 |
let current = slot.current(); |
| 1811 |
let labelled = !slot.labels().is_empty(); |
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
if labelled { |
| 1818 |
showing_html(slot, opts, out); |
| 1819 |
} |
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
for (at, node) in slot.body.iter().enumerate() { |
| 1827 |
out.push_str("<div"); |
| 1828 |
frame_class_attr(current == Some(at), opts, out); |
| 1829 |
out.push('>'); |
| 1830 |
node_html(node, morphs, opts, fills, out); |
| 1831 |
out.push_str("</div>"); |
| 1832 |
} |
| 1833 |
|
| 1834 |
if !labelled { |
| 1835 |
showing_html(slot, opts, out); |
| 1836 |
} |
| 1837 |
} else { |
| 1838 |
for node in &slot.body { |
| 1839 |
node_html(node, morphs, opts, fills, out); |
| 1840 |
} |
| 1841 |
} |
| 1842 |
|
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
if matches!(slot.kind, quasi_router::RegionKind::Bespoke { .. }) |
| 1851 |
&& let Some(fill) = fills.get(&slot.id) |
| 1852 |
{ |
| 1853 |
out.push_str(fill); |
| 1854 |
} |
| 1855 |
|
| 1856 |
out.push_str("</div>"); |
| 1857 |
} |
| 1858 |
|