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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use std::{future::Future, sync::RwLock};
use web_sys::Element;
mod class;
pub mod div_handle;
pub mod error;
pub mod global;
mod pane;
mod state;
mod storage;
mod style;
mod utils;
pub use class::*;
pub use div_handle::*;
pub use error::*;
pub use global::*;
use state::*;
use storage::{ClassStorage, PaneHashMap, PaneStorage};
use style::*;
pub use utils::doc;
pub fn init() -> Result<(), DivError> {
init_ex(None, (0, 0), None)
}
pub fn init_to(id: &str) -> Result<(), DivError> {
init_ex(Some(id), (0, 0), None)
}
pub fn init_ex_with_element(
root: Element,
pos: (u32, u32),
size: Option<(u32, u32)>,
) -> Result<(), DivError> {
state::set_state(GlobalState {
root,
nodes: PaneHashMap::default(),
pos,
size,
zoom: (1.0, 1.0),
classes: JsClassStorage::default(),
})?;
add_div_styles_to_document()?;
init_div_rs();
Ok(())
}
pub fn init_ex(
id: Option<&str>,
pos: (u32, u32),
size: Option<(u32, u32)>,
) -> Result<(), DivError> {
let root = get_root(id)?;
init_ex_with_element(root, pos, size)
}
fn get_root(id: Option<&str>) -> Result<Element, DivError> {
let element = if id.is_some() {
doc()?
.get_element_by_id(id.unwrap())
.ok_or(DivError::MissingRoot(id.unwrap().to_owned()))?
} else {
doc()?.body().ok_or(DivError::MissingBody)?.into()
};
Ok(element)
}
pub fn new(x: u32, y: u32, w: u32, h: u32, html: &str) -> Result<DivHandle, DivError> {
let css = "";
let classes = "";
state::exec_mut(|state| state.new_pane(x, y, w, h, html, css, classes))
}
pub fn new_styled<'a, C, CSS, S1, S2, S3>(
x: u32,
y: u32,
w: u32,
h: u32,
html: &str,
classes: C,
css: CSS,
) -> Result<DivHandle, DivError>
where
C: IntoIterator<Item = &'a S1>,
CSS: IntoIterator<Item = &'a (S2, S3)>,
S1: AsRef<str> + 'a,
S2: AsRef<str> + 'a,
S3: AsRef<str> + 'a,
{
let css_str = css
.into_iter()
.map(|(attr, val)| attr.as_ref().to_owned() + ": " + val.as_ref() + ";")
.collect::<Vec<_>>()
.join(" ");
let classes_str = classes
.into_iter()
.map(AsRef::as_ref)
.collect::<Vec<_>>()
.join(" ");
state::exec_mut(|state| state.new_pane(x, y, w, h, html, &classes_str, &css_str))
}
pub fn load_js_class(
name: &str,
src: &str,
) -> Result<impl Future<Output = JsClassHandle>, DivError> {
let classes = load_js_classes(&[name], src)?;
Ok(async { classes.await[0] })
}
pub fn load_js_classes(
classes: &[&str],
src: &str,
) -> Result<impl Future<Output = Vec<JsClassHandle>>, DivError> {
let future = state::exec_mut(|state| state.classes.load(classes, src))?;
Ok(future)
}
pub fn from_js_class(
x: u32,
y: u32,
w: u32,
h: u32,
class_handle: JsClassHandle,
) -> Result<DivHandle, DivError> {
let ph = new(x, y, w, h, "")?;
let node = ph.parent_element()?;
let class = state::get_class(class_handle)?;
class.attach_new_instance(&node);
Ok(ph)
}
impl JsClass {
pub fn preregistered(name: &str) -> Option<JsClassHandle> {
state::exec_mut(|state| Ok(state.classes.preloaded(name))).unwrap()
}
}