| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use crate::form::{Filling, Markup, Value, field_html}; |
| 25 |
use crate::list::{Cell, cells_html}; |
| 26 |
use crate::{Emit, facet::facet_html, figure::figures_html}; |
| 27 |
use crate::{meter::meter_html, placeholder::placeholder_html}; |
| 28 |
use makeover_layout::{ |
| 29 |
Accepted, CellPart, Choice, Column, Facet, FacetValue, Field, FieldKind, Figure, Meter, |
| 30 |
Priority, Readiness, Selecting, Sort, Standing, Tone, Width, |
| 31 |
}; |
| 32 |
use std::collections::BTreeSet; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
pub(crate) fn emitted() -> BTreeSet<String> { |
| 38 |
emitted_with(&Emit::default()) |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
fn emitted_with(opts: &Emit) -> BTreeSet<String> { |
| 43 |
let mut found = BTreeSet::new(); |
| 44 |
for html in documents(opts) { |
| 45 |
let mut rest = html.as_str(); |
| 46 |
while let Some(at) = rest.find("class=\"") { |
| 47 |
rest = &rest[at + "class=\"".len()..]; |
| 48 |
let end = rest.find('"').expect("an attribute closes"); |
| 49 |
for name in rest[..end].split_whitespace() { |
| 50 |
found.insert(name.to_owned()); |
| 51 |
} |
| 52 |
rest = &rest[end..]; |
| 53 |
} |
| 54 |
} |
| 55 |
found |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
fn documents(opts: &Emit) -> Vec<String> { |
| 66 |
let mut out = vec![placeholders(opts), fields(opts), rows(opts)]; |
| 67 |
out.push(figures_html( |
| 68 |
&[ |
| 69 |
Figure::new("42", "Tasks"), |
| 70 |
Figure::new("12.5%", "Growth") |
| 71 |
.change("+3") |
| 72 |
.tone(Tone::Success), |
| 73 |
], |
| 74 |
opts, |
| 75 |
)); |
| 76 |
for mode in [ |
| 77 |
Selecting::OneOf, |
| 78 |
Selecting::AnyOf, |
| 79 |
Selecting::Range, |
| 80 |
Selecting::Text, |
| 81 |
Selecting::Subtree, |
| 82 |
] { |
| 83 |
let values = [ |
| 84 |
FacetValue::new("music", "Music") |
| 85 |
.standing(Standing::Taken) |
| 86 |
.counted(128) |
| 87 |
.at(0, true), |
| 88 |
FacetValue::new("music/synths", "Synths") |
| 89 |
.standing(Standing::Inherited) |
| 90 |
.at(1, false), |
| 91 |
FacetValue::new("music/drums", "Drums") |
| 92 |
.standing(Standing::Pruned) |
| 93 |
.at(1, false), |
| 94 |
FacetValue::new("talk", "Talk").at(0, false), |
| 95 |
]; |
| 96 |
out.push(facet_html(&Facet::new("Tag", mode, &values), opts)); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
for meter in [ |
| 101 |
Meter::new(3, 7), |
| 102 |
Meter::new(3, 7).tone(Tone::Success).label("subtasks"), |
| 103 |
Meter::new(9, 7).tone(Tone::Danger), |
| 104 |
] { |
| 105 |
out.push(meter_html(&meter, opts)); |
| 106 |
} |
| 107 |
out |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
fn placeholders(opts: &Emit) -> String { |
| 112 |
let mut html = String::new(); |
| 113 |
for state in [ |
| 114 |
Readiness::Ready, |
| 115 |
Readiness::Pending, |
| 116 |
Readiness::Empty, |
| 117 |
Readiness::Failed, |
| 118 |
] { |
| 119 |
html.push_str(&placeholder_html(state, "Nothing here", None, opts)); |
| 120 |
html.push_str(&placeholder_html( |
| 121 |
state, |
| 122 |
"Nothing here", |
| 123 |
Some(Markup("<button>Add one</button>")), |
| 124 |
opts, |
| 125 |
)); |
| 126 |
} |
| 127 |
html |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
fn fields(opts: &Emit) -> String { |
| 137 |
const OPTIONS: &[Choice<'_>] = &[ |
| 138 |
Choice::new("a", "The first"), |
| 139 |
Choice::new("b", "The second").unless("Not while the first is running"), |
| 140 |
]; |
| 141 |
const ACCEPT: &[Accepted<'_>] = &[Accepted::Type("image/png"), Accepted::Suffix(".zip")]; |
| 142 |
|
| 143 |
let mut html = String::new(); |
| 144 |
for kind in [ |
| 145 |
FieldKind::Text, |
| 146 |
FieldKind::Secret, |
| 147 |
FieldKind::Number, |
| 148 |
FieldKind::Range, |
| 149 |
FieldKind::Interval, |
| 150 |
FieldKind::Email, |
| 151 |
FieldKind::Url, |
| 152 |
FieldKind::Tel, |
| 153 |
FieldKind::Date, |
| 154 |
FieldKind::DateTime, |
| 155 |
FieldKind::Textarea, |
| 156 |
FieldKind::Rich, |
| 157 |
FieldKind::Select, |
| 158 |
FieldKind::Radio, |
| 159 |
FieldKind::Checkbox, |
| 160 |
FieldKind::File, |
| 161 |
FieldKind::Hidden, |
| 162 |
] { |
| 163 |
let plain = Field { |
| 164 |
options: OPTIONS, |
| 165 |
accept: ACCEPT, |
| 166 |
upper_name: Some("upper"), |
| 167 |
min: Some("0"), |
| 168 |
max: Some("10"), |
| 169 |
..Field::new(kind, "name", "Label") |
| 170 |
}; |
| 171 |
let dressed = Field { |
| 172 |
hint: Some("What it is for"), |
| 173 |
error: Some("That will not do"), |
| 174 |
unit: Some("minutes"), |
| 175 |
required: true, |
| 176 |
..plain |
| 177 |
}; |
| 178 |
for field in [plain, dressed] { |
| 179 |
for value in [ |
| 180 |
Value::Absent, |
| 181 |
Value::Text("a"), |
| 182 |
Value::On(true), |
| 183 |
Value::Between { |
| 184 |
lower: "1", |
| 185 |
upper: "9", |
| 186 |
}, |
| 187 |
] { |
| 188 |
html.push_str(&field_html(&field, &Filling::of(value), opts)); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
html |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
fn rows(opts: &Emit) -> String { |
| 201 |
let mut columns = Vec::new(); |
| 202 |
for (index, width) in [Width::Content, Width::Fixed, Width::Fill] |
| 203 |
.into_iter() |
| 204 |
.enumerate() |
| 205 |
{ |
| 206 |
for (rank, priority) in [Priority::Optional, Priority::Secondary, Priority::Essential] |
| 207 |
.into_iter() |
| 208 |
.enumerate() |
| 209 |
{ |
| 210 |
columns.push(Column { |
| 211 |
width, |
| 212 |
priority, |
| 213 |
sortable: true, |
| 214 |
sorted: Some(if rank % 2 == 0 { |
| 215 |
Sort::Ascending |
| 216 |
} else { |
| 217 |
Sort::Descending |
| 218 |
}), |
| 219 |
..Column::new(NAMES[index * 3 + rank]) |
| 220 |
}); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
let parts = [ |
| 225 |
None, |
| 226 |
Some(CellPart::Value), |
| 227 |
Some(CellPart::Tokens), |
| 228 |
Some(CellPart::Actions), |
| 229 |
Some(CellPart::Link), |
| 230 |
]; |
| 231 |
let mut html = String::new(); |
| 232 |
for part in parts { |
| 233 |
let cells: Vec<Cell<'_>> = columns |
| 234 |
.iter() |
| 235 |
.map(|column| Cell { |
| 236 |
column: column.name, |
| 237 |
part, |
| 238 |
content: Markup("<span>x</span>"), |
| 239 |
}) |
| 240 |
.collect(); |
| 241 |
html.push_str(&cells_html(&columns, &cells, opts)); |
| 242 |
} |
| 243 |
html |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
const NAMES: [&str; 9] = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]; |
| 248 |
|
| 249 |
#[cfg(test)] |
| 250 |
mod tests { |
| 251 |
use super::*; |
| 252 |
|
| 253 |
#[test] |
| 254 |
fn every_class_this_crate_emits_is_one_its_vocabulary_names() { |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
let opts = Emit::default(); |
| 261 |
let names = crate::vocabulary::names(&opts); |
| 262 |
let emitted = emitted(); |
| 263 |
assert!( |
| 264 |
emitted.len() > 30, |
| 265 |
"the corpus rendered {} classes, which reads as the corpus having \ |
| 266 |
stopped calling the emitters rather than the crate having shrunk", |
| 267 |
emitted.len() |
| 268 |
); |
| 269 |
|
| 270 |
let stranger: Vec<&String> = emitted |
| 271 |
.iter() |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
.filter(|class| !class.starts_with("col-")) |
| 276 |
.filter(|class| !names.contains(*class)) |
| 277 |
.collect(); |
| 278 |
assert!( |
| 279 |
stranger.is_empty(), |
| 280 |
"{} class(es) emitted that `vocabulary::names` does not hold. Give \ |
| 281 |
them a rule, or add them to the unruled list `names` reads:\n{}", |
| 282 |
stranger.len(), |
| 283 |
stranger |
| 284 |
.iter() |
| 285 |
.map(|c| format!(" .{c}")) |
| 286 |
.collect::<Vec<_>>() |
| 287 |
.join("\n") |
| 288 |
); |
| 289 |
} |
| 290 |
|
| 291 |
#[test] |
| 292 |
fn a_class_prefix_reaches_every_class_but_the_states() { |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
let opts = Emit { |
| 299 |
class_prefix: "mk-", |
| 300 |
..Emit::default() |
| 301 |
}; |
| 302 |
let names = crate::vocabulary::names(&opts); |
| 303 |
let stranger: Vec<String> = emitted_with(&opts) |
| 304 |
.into_iter() |
| 305 |
.filter(|class| !class.starts_with("mk-col-")) |
| 306 |
.filter(|class| !names.contains(class)) |
| 307 |
.collect(); |
| 308 |
assert!( |
| 309 |
stranger.is_empty(), |
| 310 |
"{} class(es) a prefixed render emits that `vocabulary::names` does \ |
| 311 |
not hold:\n{}", |
| 312 |
stranger.len(), |
| 313 |
stranger |
| 314 |
.iter() |
| 315 |
.map(|c| format!(" .{c}")) |
| 316 |
.collect::<Vec<_>>() |
| 317 |
.join("\n") |
| 318 |
); |
| 319 |
} |
| 320 |
} |
| 321 |
|