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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ Copyright (c) 2017, the Perspective Authors. ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃ This file is part of the Perspective library, distributed under the terms ┃
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
use web_sys::{HtmlInputElement, InputEvent};
use yew::prelude::*;
use super::modal::{ModalLink, SetModalLink};
use crate::components::form::select_enum_field::SelectEnumField;
use crate::config::*;
use crate::utils::WeakScope;
#[derive(Properties)]
pub struct NumberSeriesStyleProps {
pub config: Option<NumberSeriesStyleConfig>,
pub default_config: NumberSeriesStyleDefaultConfig,
#[prop_or_default]
pub on_change: Callback<ColumnConfigFieldUpdate>,
#[prop_or_default]
pub keys: Vec<String>,
#[prop_or_default]
weak_link: WeakScope<NumberSeriesStyle>,
}
impl ModalLink<NumberSeriesStyle> for NumberSeriesStyleProps {
fn weak_link(&self) -> &'_ WeakScope<NumberSeriesStyle> {
&self.weak_link
}
}
impl PartialEq for NumberSeriesStyleProps {
fn eq(&self, other: &Self) -> bool {
self.config == other.config && self.default_config == other.default_config
}
}
pub enum NumberSeriesStyleMsg {
ChartTypeChanged(Option<ChartType>),
StackChanged(Option<bool>),
}
/// Form control for the per-column `chart_type` + `stack` picker. Rendered
/// inside the column-settings sidebar when the active plugin returns a
/// `ControlSpec::NumberSeriesStyle` from its `column_config_schema` hook.
pub struct NumberSeriesStyle {
config: NumberSeriesStyleConfig,
}
impl Component for NumberSeriesStyle {
type Message = NumberSeriesStyleMsg;
type Properties = NumberSeriesStyleProps;
fn create(ctx: &Context<Self>) -> Self {
ctx.set_modal_link();
Self {
config: ctx.props().config.clone().unwrap_or_default(),
}
}
fn changed(&mut self, ctx: &Context<Self>, _old: &Self::Properties) -> bool {
let new_config = ctx.props().config.clone().unwrap_or_default();
if self.config != new_config {
self.config = new_config;
true
} else {
false
}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
NumberSeriesStyleMsg::ChartTypeChanged(val) => {
self.config.chart_type = val.unwrap_or_default();
// Hiding the stack checkbox on Line/Scatter also clears any
// lingering override so the JSON stays empty by default.
if !self.config.chart_type.supports_stack() {
self.config.stack = None;
}
self.dispatch_config(ctx);
true
},
NumberSeriesStyleMsg::StackChanged(val) => {
self.config.stack = val;
self.dispatch_config(ctx);
true
},
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
let chart_type_changed = ctx.link().callback(NumberSeriesStyleMsg::ChartTypeChanged);
let stack_controls = if self.config.chart_type.supports_stack() {
// Default: bar/area stack. `None` == inherit the default.
let checked = self.config.stack.unwrap_or(true);
let oninput = ctx.link().callback(move |e: InputEvent| {
let input: HtmlInputElement = e.target_unchecked_into();
let next = input.checked();
// Persist explicit `false` overrides; the "stacked" default
// round-trips as `None` to keep JSON empty.
NumberSeriesStyleMsg::StackChanged(if next { None } else { Some(false) })
});
html! {
<div class="row">
<label id="stack-label" />
<input type="checkbox" id="stack-checkbox" {checked} {oninput} />
</div>
}
} else {
html! {}
};
html! {
<>
<div id="column-style-container" class="number-series-style-container">
<SelectEnumField<ChartType>
label="chart-type"
on_change={chart_type_changed}
current_value={self.config.chart_type}
/>
{ stack_controls }
</div>
</>
}
}
}
impl NumberSeriesStyle {
/// Dispatch the current config as an update. The default (Bar + no
/// stack override) round-trips as an empty JSON object via
/// `skip_serializing_if`, which means a field-level reset for this
/// schema field.
fn dispatch_config(&self, ctx: &Context<Self>) {
let value = if self.config == NumberSeriesStyleConfig::default() {
serde_json::Map::new()
} else {
match serde_json::to_value(&self.config) {
Ok(serde_json::Value::Object(m)) => m,
_ => serde_json::Map::new(),
}
};
ctx.props().on_change.emit(ColumnConfigFieldUpdate {
keys: ctx.props().keys.clone(),
value,
});
}
}