
This lets us infer a regulator's role from whether it has a set point and what text specifies the set point.
202 lines
No EOL
7.1 KiB
Rust
202 lines
No EOL
7.1 KiB
Rust
use itertools::Itertools;
|
|
use sycamore::prelude::*;
|
|
use web_sys::{
|
|
KeyboardEvent,
|
|
MouseEvent,
|
|
wasm_bindgen::JsCast
|
|
};
|
|
|
|
use crate::{
|
|
AppState,
|
|
assembly,
|
|
assembly::{
|
|
Regulator,
|
|
RegulatorKey,
|
|
ElementKey
|
|
}
|
|
};
|
|
|
|
// an editable view of a regulator
|
|
#[component(inline_props)]
|
|
fn RegulatorInput(regulator: Regulator) -> View {
|
|
let value = create_signal(regulator.set_point_spec.get_clone_untracked());
|
|
create_effect(move || value.set(regulator.set_point_spec.get_clone()));
|
|
view! {
|
|
input(
|
|
r#type="text",
|
|
placeholder=regulator.measurement.with(|result| result.to_string()),
|
|
bind:value=value,
|
|
on:change=move |_| {
|
|
let value_val = value.get_clone_untracked();
|
|
regulator.set_point.set(value_val.parse::<f64>().ok());
|
|
regulator.set_point_spec.set(value_val);
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
// a list item that shows a regulator in an outline view of an element
|
|
#[component(inline_props)]
|
|
fn RegulatorOutlineItem(regulator_key: RegulatorKey, element_key: ElementKey) -> View {
|
|
let state = use_context::<AppState>();
|
|
let assembly = &state.assembly;
|
|
let regulator = assembly.regulators.with(|regs| regs[regulator_key]);
|
|
let other_subject = if regulator.subjects.0 == element_key {
|
|
regulator.subjects.1
|
|
} else {
|
|
regulator.subjects.0
|
|
};
|
|
let other_subject_label = assembly.elements.with(|elts| elts[other_subject].label.clone());
|
|
let class = create_memo(move || {
|
|
match regulator.set_point.get() {
|
|
None if regulator.has_no_set_point_spec() => "regulator",
|
|
None => "regulator invalid-constraint",
|
|
Some(_) => "regulator valid-constraint"
|
|
}
|
|
});
|
|
view! {
|
|
li(class=class.get()) {
|
|
div(class="regulator-label") { (other_subject_label) }
|
|
div(class="regulator-type") { "Inversive distance" }
|
|
RegulatorInput(regulator=regulator)
|
|
div(class="status")
|
|
}
|
|
}
|
|
}
|
|
|
|
// a list item that shows an element in an outline view of an assembly
|
|
#[component(inline_props)]
|
|
fn ElementOutlineItem(key: ElementKey, element: assembly::Element) -> View {
|
|
let state = use_context::<AppState>();
|
|
let class = state.selection.map(
|
|
move |sel| if sel.contains(&key) { "selected" } else { "" }
|
|
);
|
|
let label = element.label.clone();
|
|
let rep_components = move || {
|
|
element.representation.with(
|
|
|rep| rep.iter().map(
|
|
|u| {
|
|
let u_str = format!("{:.3}", u).replace("-", "\u{2212}");
|
|
view! { div { (u_str) } }
|
|
}
|
|
).collect::<Vec<_>>()
|
|
)
|
|
};
|
|
let regulated = element.regulators.map(|regs| regs.len() > 0);
|
|
let regulator_list = element.regulators.map(
|
|
|regs| regs.clone().into_iter().collect()
|
|
);
|
|
let details_node = create_node_ref();
|
|
view! {
|
|
li {
|
|
details(ref=details_node) {
|
|
summary(
|
|
class=class.get(),
|
|
on:keydown={
|
|
move |event: KeyboardEvent| {
|
|
match event.key().as_str() {
|
|
"Enter" => {
|
|
state.select(key, event.shift_key());
|
|
event.prevent_default();
|
|
},
|
|
"ArrowRight" if regulated.get() => {
|
|
let _ = details_node
|
|
.get()
|
|
.unchecked_into::<web_sys::Element>()
|
|
.set_attribute("open", "");
|
|
},
|
|
"ArrowLeft" => {
|
|
let _ = details_node
|
|
.get()
|
|
.unchecked_into::<web_sys::Element>()
|
|
.remove_attribute("open");
|
|
},
|
|
_ => ()
|
|
}
|
|
}
|
|
}
|
|
) {
|
|
div(
|
|
class="element-switch",
|
|
on:click=|event: MouseEvent| event.stop_propagation()
|
|
)
|
|
div(
|
|
class="element",
|
|
on:click={
|
|
move |event: MouseEvent| {
|
|
if event.shift_key() {
|
|
state.selection.update(|sel| {
|
|
if !sel.remove(&key) {
|
|
sel.insert(key);
|
|
}
|
|
});
|
|
} else {
|
|
state.selection.update(|sel| {
|
|
sel.clear();
|
|
sel.insert(key);
|
|
});
|
|
}
|
|
event.stop_propagation();
|
|
event.prevent_default();
|
|
}
|
|
}
|
|
) {
|
|
div(class="element-label") { (label) }
|
|
div(class="element-representation") { (rep_components) }
|
|
div(class="status")
|
|
}
|
|
}
|
|
ul(class="regulators") {
|
|
Keyed(
|
|
list=regulator_list,
|
|
view=move |reg_key| view! {
|
|
RegulatorOutlineItem(
|
|
regulator_key=reg_key,
|
|
element_key=key
|
|
)
|
|
},
|
|
key=|reg_key| reg_key.clone()
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// a component that lists the elements of the current assembly, showing each
|
|
// element's regulators in a collapsible sub-list. its implementation is based
|
|
// on Kate Morley's HTML + CSS tree views:
|
|
//
|
|
// https://iamkate.com/code/tree-views/
|
|
//
|
|
#[component]
|
|
pub fn Outline() -> View {
|
|
let state = use_context::<AppState>();
|
|
|
|
// list the elements alphabetically by ID
|
|
let element_list = state.assembly.elements.map(
|
|
|elts| elts
|
|
.clone()
|
|
.into_iter()
|
|
.sorted_by_key(|(_, elt)| elt.id.clone())
|
|
.collect()
|
|
);
|
|
|
|
view! {
|
|
ul(
|
|
id="outline",
|
|
on:click={
|
|
let state = use_context::<AppState>();
|
|
move |_| state.selection.update(|sel| sel.clear())
|
|
}
|
|
) {
|
|
Keyed(
|
|
list=element_list,
|
|
view=|(key, elt)| view! {
|
|
ElementOutlineItem(key=key, element=elt)
|
|
},
|
|
key=|(_, elt)| elt.serial
|
|
)
|
|
}
|
|
}
|
|
} |