| 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 |
use crate::form::Markup; |
| 34 |
use crate::{Emit, push_class}; |
| 35 |
use makeover_layout::{CellPart, Column, Priority, RowPart, Width}; |
| 36 |
use std::fmt::Write as _; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
#[derive(Debug, Clone, Copy, Default)] |
| 49 |
pub struct Sizing<'a> { |
| 50 |
|
| 51 |
|
| 52 |
pub lengths: &'a [(&'a str, &'a str)], |
| 53 |
|
| 54 |
pub fallback: &'a str, |
| 55 |
} |
| 56 |
|
| 57 |
impl Sizing<'_> { |
| 58 |
|
| 59 |
fn length_for(&self, name: &str) -> &str { |
| 60 |
self.lengths |
| 61 |
.iter() |
| 62 |
.find(|(column, _)| *column == name) |
| 63 |
.map_or_else( |
| 64 |
|| { |
| 65 |
if self.fallback.is_empty() { |
| 66 |
"auto" |
| 67 |
} else { |
| 68 |
self.fallback |
| 69 |
} |
| 70 |
}, |
| 71 |
|(_, length)| *length, |
| 72 |
) |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
fn track(&self, column: &Column<'_>) -> String { |
| 77 |
match column.width { |
| 78 |
Width::Content => "max-content".to_owned(), |
| 79 |
Width::Fixed => self.length_for(column.name).to_owned(), |
| 80 |
Width::Fill => format!("minmax({}, 1fr)", self.length_for(column.name)), |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
_ => "auto".to_owned(), |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
#[must_use] |
| 95 |
pub fn column_class(column: &Column<'_>, opts: &Emit) -> String { |
| 96 |
let mut out = String::new(); |
| 97 |
push_column_class(&mut out, column, opts); |
| 98 |
out |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
pub fn push_column_class(out: &mut String, column: &Column<'_>, opts: &Emit) { |
| 108 |
out.push_str(opts.class_prefix); |
| 109 |
out.push_str("col-"); |
| 110 |
out.push_str(column.name); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
fn width_class(width: Width) -> &'static str { |
| 121 |
match width { |
| 122 |
Width::Content => "cell-content", |
| 123 |
Width::Fixed => "cell-fixed", |
| 124 |
_ => "cell-fill", |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
fn drop_class(priority: Priority) -> &'static str { |
| 136 |
match priority { |
| 137 |
Priority::Optional => "cell-drops-first", |
| 138 |
Priority::Secondary => "cell-drops-next", |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
_ => "cell-keeps", |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
#[must_use] |
| 154 |
pub fn column_classes(column: &Column<'_>, opts: &Emit) -> String { |
| 155 |
let mut out = String::new(); |
| 156 |
push_column_classes(&mut out, column, opts); |
| 157 |
out |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
pub fn push_column_classes(out: &mut String, column: &Column<'_>, opts: &Emit) { |
| 166 |
push_column_class(out, column, opts); |
| 167 |
out.push(' '); |
| 168 |
push_class(out, width_class(column.width), opts); |
| 169 |
out.push(' '); |
| 170 |
push_class(out, drop_class(column.priority), opts); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
#[must_use] |
| 181 |
pub fn grid_template_columns( |
| 182 |
columns: &[Column<'_>], |
| 183 |
sizing: &Sizing<'_>, |
| 184 |
cutoff: Priority, |
| 185 |
) -> String { |
| 186 |
columns |
| 187 |
.iter() |
| 188 |
.filter(|column| column.kept_at(cutoff)) |
| 189 |
.map(|column| sizing.track(column)) |
| 190 |
.collect::<Vec<_>>() |
| 191 |
.join(" ") |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
#[must_use] |
| 205 |
pub fn narrowing_css( |
| 206 |
columns: &[Column<'_>], |
| 207 |
selector: &str, |
| 208 |
sizing: &Sizing<'_>, |
| 209 |
cutoff: Priority, |
| 210 |
opts: &Emit, |
| 211 |
) -> String { |
| 212 |
let parts: Vec<&str> = selector.split(',').map(str::trim).collect(); |
| 213 |
|
| 214 |
let mut css = format!( |
| 215 |
"{} {{\n grid-template-columns: {};\n}}\n", |
| 216 |
parts.join(", "), |
| 217 |
grid_template_columns(columns, sizing, cutoff) |
| 218 |
); |
| 219 |
|
| 220 |
for column in columns.iter().filter(|c| !c.kept_at(cutoff)) { |
| 221 |
let class = column_class(column, opts); |
| 222 |
let targets: Vec<String> = parts |
| 223 |
.iter() |
| 224 |
.map(|part| format!("{part} > .{class}")) |
| 225 |
.collect(); |
| 226 |
let _ = write!(css, "{} {{\n display: none;\n}}\n", targets.join(",\n")); |
| 227 |
} |
| 228 |
css |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
#[derive(Debug, Clone, Copy)] |
| 238 |
pub struct Cell<'a> { |
| 239 |
|
| 240 |
pub column: &'a str, |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
pub part: Option<CellPart>, |
| 258 |
|
| 259 |
pub content: Markup<'a>, |
| 260 |
} |
| 261 |
|
| 262 |
impl<'a> Cell<'a> { |
| 263 |
|
| 264 |
#[must_use] |
| 265 |
pub const fn new(column: &'a str, content: Markup<'a>) -> Self { |
| 266 |
Self { |
| 267 |
column, |
| 268 |
part: None, |
| 269 |
content, |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
pub const ROW_PART_CLASSES: &[&str] = &[ |
| 301 |
"row-primary", |
| 302 |
"row-secondary", |
| 303 |
"row-meta", |
| 304 |
"row-actions", |
| 305 |
"row-tokens", |
| 306 |
"row-proportion", |
| 307 |
"row-part", |
| 308 |
]; |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
pub const CELL_PART_CLASSES: &[&str] = &[ |
| 314 |
"cell-value", |
| 315 |
"cell-tokens", |
| 316 |
"cell-actions", |
| 317 |
"cell-link", |
| 318 |
"cell-part", |
| 319 |
]; |
| 320 |
|
| 321 |
#[must_use] |
| 322 |
pub fn part_class(part: RowPart) -> &'static str { |
| 323 |
match part { |
| 324 |
RowPart::Primary => "row-primary", |
| 325 |
RowPart::Secondary => "row-secondary", |
| 326 |
RowPart::Meta => "row-meta", |
| 327 |
RowPart::Actions => "row-actions", |
| 328 |
RowPart::Tokens => "row-tokens", |
| 329 |
RowPart::Proportion => "row-proportion", |
| 330 |
_ => "row-part", |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
#[must_use] |
| 343 |
pub fn cell_part_class(part: CellPart) -> &'static str { |
| 344 |
match part { |
| 345 |
CellPart::Value => "cell-value", |
| 346 |
CellPart::Tokens => "cell-tokens", |
| 347 |
CellPart::Actions => "cell-actions", |
| 348 |
CellPart::Link => "cell-link", |
| 349 |
_ => "cell-part", |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
#[must_use] |
| 378 |
pub fn cells_html(columns: &[Column<'_>], cells: &[Cell<'_>], opts: &Emit) -> String { |
| 379 |
let mut html = String::new(); |
| 380 |
cells_html_into(columns, cells, opts, &mut html); |
| 381 |
html |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
pub fn cells_html_into(columns: &[Column<'_>], cells: &[Cell<'_>], opts: &Emit, out: &mut String) { |
| 390 |
for column in columns { |
| 391 |
let found = cells.iter().find(|cell| cell.column == column.name); |
| 392 |
out.push_str("<div class=\""); |
| 393 |
push_class(out, "cell", opts); |
| 394 |
out.push(' '); |
| 395 |
push_column_classes(out, column, opts); |
| 396 |
if let Some(part) = found.and_then(|cell| cell.part) { |
| 397 |
out.push(' '); |
| 398 |
push_class(out, cell_part_class(part), opts); |
| 399 |
} |
| 400 |
out.push_str("\">"); |
| 401 |
out.push_str(found.map_or("", |cell| cell.content.0)); |
| 402 |
out.push_str("</div>"); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
#[cfg(test)] |
| 407 |
mod tests { |
| 408 |
use super::*; |
| 409 |
|
| 410 |
fn columns() -> Vec<Column<'static>> { |
| 411 |
vec![ |
| 412 |
Column { |
| 413 |
width: Width::Fill, |
| 414 |
priority: Priority::Essential, |
| 415 |
..Column::new("description") |
| 416 |
}, |
| 417 |
Column { |
| 418 |
width: Width::Fixed, |
| 419 |
priority: Priority::Secondary, |
| 420 |
..Column::new("due") |
| 421 |
}, |
| 422 |
Column { |
| 423 |
width: Width::Fixed, |
| 424 |
priority: Priority::Optional, |
| 425 |
..Column::new("progress") |
| 426 |
}, |
| 427 |
] |
| 428 |
} |
| 429 |
|
| 430 |
fn sizing() -> Sizing<'static> { |
| 431 |
Sizing { |
| 432 |
lengths: &[ |
| 433 |
("description", "200px"), |
| 434 |
("due", "110px"), |
| 435 |
("progress", "100px"), |
| 436 |
], |
| 437 |
fallback: "", |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
#[test] |
| 442 |
fn a_fill_column_gets_a_floor_and_the_slack() { |
| 443 |
let tracks = grid_template_columns(&columns(), &sizing(), Priority::Optional); |
| 444 |
assert_eq!(tracks, "minmax(200px, 1fr) 110px 100px"); |
| 445 |
} |
| 446 |
|
| 447 |
#[test] |
| 448 |
fn a_column_with_no_length_makes_no_claim() { |
| 449 |
let sizing = Sizing::default(); |
| 450 |
let tracks = grid_template_columns(&columns(), &sizing, Priority::Optional); |
| 451 |
assert_eq!(tracks, "minmax(auto, 1fr) auto auto"); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
#[test] |
| 459 |
fn raising_the_cutoff_drops_columns_and_their_tracks_together() { |
| 460 |
let columns = columns(); |
| 461 |
|
| 462 |
let wide = grid_template_columns(&columns, &sizing(), Priority::Optional); |
| 463 |
assert_eq!(wide.split(' ').count(), 4); |
| 464 |
|
| 465 |
let narrow = grid_template_columns(&columns, &sizing(), Priority::Secondary); |
| 466 |
assert_eq!(narrow, "minmax(200px, 1fr) 110px"); |
| 467 |
|
| 468 |
let narrowest = grid_template_columns(&columns, &sizing(), Priority::Essential); |
| 469 |
assert_eq!(narrowest, "minmax(200px, 1fr)"); |
| 470 |
} |
| 471 |
|
| 472 |
#[test] |
| 473 |
fn narrowing_hides_a_dropped_column_by_its_own_class_not_its_position() { |
| 474 |
let css = narrowing_css( |
| 475 |
&columns(), |
| 476 |
".ui-mode-mobile .task-row", |
| 477 |
&sizing(), |
| 478 |
Priority::Secondary, |
| 479 |
&Emit::default(), |
| 480 |
); |
| 481 |
assert!( |
| 482 |
css.contains("grid-template-columns: minmax(200px, 1fr) 110px;"), |
| 483 |
"{css}" |
| 484 |
); |
| 485 |
assert!( |
| 486 |
css.contains(".ui-mode-mobile .task-row > .col-progress {"), |
| 487 |
"{css}" |
| 488 |
); |
| 489 |
assert!(!css.contains("nth-child"), "{css}"); |
| 490 |
|
| 491 |
assert!(!css.contains(".col-due {\n display: none"), "{css}"); |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
#[test] |
| 498 |
fn a_selector_list_distributes_the_hidden_column() { |
| 499 |
let css = narrowing_css( |
| 500 |
&columns(), |
| 501 |
".task-header-row, .task-row", |
| 502 |
&sizing(), |
| 503 |
Priority::Secondary, |
| 504 |
&Emit::default(), |
| 505 |
); |
| 506 |
assert!( |
| 507 |
css.contains(".task-header-row > .col-progress,\n.task-row > .col-progress {"), |
| 508 |
"{css}" |
| 509 |
); |
| 510 |
|
| 511 |
assert!( |
| 512 |
!css.contains(".task-header-row {\n display: none"), |
| 513 |
"{css}" |
| 514 |
); |
| 515 |
assert!( |
| 516 |
css.contains(".task-header-row, .task-row {\n grid-template-columns:"), |
| 517 |
"{css}" |
| 518 |
); |
| 519 |
} |
| 520 |
|
| 521 |
#[test] |
| 522 |
fn cells_follow_the_columns_and_carry_their_column_class() { |
| 523 |
let cells = [ |
| 524 |
Cell { |
| 525 |
column: "due", |
| 526 |
part: Some(CellPart::Value), |
| 527 |
content: Markup("tomorrow"), |
| 528 |
}, |
| 529 |
Cell::new("description", Markup("<span>Ship it</span>")), |
| 530 |
]; |
| 531 |
let html = cells_html(&columns(), &cells, &Emit::default()); |
| 532 |
|
| 533 |
|
| 534 |
let description = html.find("Ship it").expect("description cell"); |
| 535 |
let due = html.find("tomorrow").expect("due cell"); |
| 536 |
assert!(description < due, "{html}"); |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
assert!( |
| 542 |
html.contains(r#"<div class="cell col-description cell-fill cell-keeps">"#), |
| 543 |
"{html}" |
| 544 |
); |
| 545 |
assert!( |
| 546 |
html.contains(r#"<div class="cell col-due cell-fixed cell-drops-next cell-value">"#), |
| 547 |
"{html}" |
| 548 |
); |
| 549 |
|
| 550 |
|
| 551 |
assert!( |
| 552 |
html.contains(r#"<div class="cell col-progress cell-fixed cell-drops-first"></div>"#), |
| 553 |
"{html}" |
| 554 |
); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
#[test] |
| 560 |
fn streamed_cells_are_the_cells_the_other_form_returns() { |
| 561 |
let opts = Emit { |
| 562 |
class_prefix: "mk-", |
| 563 |
..Emit::default() |
| 564 |
}; |
| 565 |
let cells = [ |
| 566 |
Cell { |
| 567 |
column: "due", |
| 568 |
part: Some(CellPart::Value), |
| 569 |
content: Markup("tomorrow"), |
| 570 |
}, |
| 571 |
Cell::new("description", Markup("<span>Ship it</span>")), |
| 572 |
]; |
| 573 |
for cells in [&cells[..], &[]] { |
| 574 |
let mut streamed = String::new(); |
| 575 |
cells_html_into(&columns(), cells, &opts, &mut streamed); |
| 576 |
assert_eq!(streamed, cells_html(&columns(), cells, &opts)); |
| 577 |
} |
| 578 |
for column in &columns() { |
| 579 |
let mut streamed = String::new(); |
| 580 |
push_column_classes(&mut streamed, column, &opts); |
| 581 |
assert_eq!(streamed, column_classes(column, &opts)); |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
#[test] |
| 586 |
fn a_cell_naming_no_column_is_dropped() { |
| 587 |
let cells = [Cell::new("nonexistent", Markup("nowhere"))]; |
| 588 |
let html = cells_html(&columns(), &cells, &Emit::default()); |
| 589 |
assert!(!html.contains("nowhere"), "{html}"); |
| 590 |
} |
| 591 |
|
| 592 |
#[test] |
| 593 |
fn the_class_prefix_reaches_the_cells_and_the_narrowing() { |
| 594 |
let opts = Emit { |
| 595 |
class_prefix: "mk-", |
| 596 |
..Emit::default() |
| 597 |
}; |
| 598 |
let cells = [Cell::new("due", Markup("x"))]; |
| 599 |
assert!( |
| 600 |
cells_html(&columns(), &cells, &opts).contains("mk-cell mk-col-due"), |
| 601 |
"prefix missing" |
| 602 |
); |
| 603 |
assert!( |
| 604 |
narrowing_css(&columns(), ".t", &sizing(), Priority::Secondary, &opts) |
| 605 |
.contains(".mk-col-progress"), |
| 606 |
"prefix missing" |
| 607 |
); |
| 608 |
} |
| 609 |
} |
| 610 |
|