2018-10-22 20:16:54 -04:00
|
|
|
use std::pin::Pin;
|
2018-09-17 17:17:55 -04:00
|
|
|
use std::convert::AsRef;
|
|
|
|
use std::marker::PhantomData;
|
2019-04-28 20:30:55 -04:00
|
|
|
use std::future::Future;
|
|
|
|
use std::task::{Context, Poll};
|
2019-06-11 09:29:46 -04:00
|
|
|
|
|
|
|
use lazy_static::lazy_static;
|
2018-10-25 19:44:17 -04:00
|
|
|
use futures_signals::signal::{Signal, not};
|
2018-10-22 20:16:54 -04:00
|
|
|
use futures_signals::signal_vec::SignalVec;
|
|
|
|
use futures_util::FutureExt;
|
2018-07-04 07:34:13 -04:00
|
|
|
use futures_channel::oneshot;
|
2018-03-16 02:50:21 -04:00
|
|
|
use discard::{Discard, DiscardOnDrop};
|
2019-06-11 09:29:46 -04:00
|
|
|
use wasm_bindgen::{JsValue, UnwrapThrowExt, JsCast};
|
|
|
|
use js_sys::Reflect;
|
|
|
|
use web_sys::{window, HtmlElement, Node, EventTarget, Element, CssStyleSheet, HtmlStyleElement, CssStyleRule, CssStyleDeclaration};
|
|
|
|
use gloo::events::{EventListener, EventListenerOptions};
|
|
|
|
|
|
|
|
use crate::callbacks::Callbacks;
|
|
|
|
use crate::traits::*;
|
|
|
|
use crate::operations;
|
|
|
|
use crate::operations::{for_each, spawn_future};
|
|
|
|
use crate::dom_operations;
|
|
|
|
use crate::utils::{document, on, on_with_options, ValueDiscard, FnDiscard, EventDiscard};
|
2018-02-25 06:58:20 -05:00
|
|
|
|
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
pub struct RefFn<A, B, C> where B: ?Sized {
|
2018-06-22 13:04:08 -04:00
|
|
|
value: A,
|
2018-09-17 17:17:55 -04:00
|
|
|
callback: C,
|
|
|
|
return_value: PhantomData<B>,
|
2018-06-22 13:04:08 -04:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
impl<A, B, C> RefFn<A, B, C> where B: ?Sized, C: Fn(&A) -> &B {
|
2018-06-22 13:04:08 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn new(value: A, callback: C) -> Self {
|
2018-09-17 17:17:55 -04:00
|
|
|
Self {
|
|
|
|
value,
|
|
|
|
callback,
|
|
|
|
return_value: PhantomData,
|
|
|
|
}
|
2018-06-22 13:04:08 -04:00
|
|
|
}
|
2018-07-13 18:26:45 -04:00
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn call_ref(&self) -> &B {
|
|
|
|
(self.callback)(&self.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*pub fn map<D, E>(self, callback: E) -> RefFn<A, impl Fn(&A) -> &D>
|
2018-07-13 18:26:45 -04:00
|
|
|
where D: ?Sized,
|
|
|
|
E: Fn(&B) -> &D {
|
|
|
|
|
|
|
|
let old_callback = self.callback;
|
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
RefFn {
|
2018-07-13 18:26:45 -04:00
|
|
|
value: self.value,
|
|
|
|
callback: move |value| callback(old_callback(value)),
|
|
|
|
}
|
|
|
|
}*/
|
2018-06-22 13:04:08 -04:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
/*impl<A, B, C> Deref for RefFn<A, C> where B: ?Sized, C: Fn(&A) -> &B {
|
2018-06-22 13:04:08 -04:00
|
|
|
type Target = B;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2018-09-17 17:17:55 -04:00
|
|
|
self.call_ref()
|
2018-06-22 13:04:08 -04:00
|
|
|
}
|
2018-09-17 17:17:55 -04:00
|
|
|
}*/
|
|
|
|
|
|
|
|
/*impl<A, B, C> AsRef<B> for RefFn<A, C> where B: ?Sized, C: Fn(&A) -> &B {
|
|
|
|
#[inline]
|
|
|
|
fn as_ref(&self) -> &B {
|
|
|
|
self.call_ref()
|
|
|
|
}
|
|
|
|
}*/
|
2018-06-22 13:04:08 -04:00
|
|
|
|
|
|
|
|
2018-02-21 01:37:09 -05:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS#Valid%20Namespace%20URIs
|
|
|
|
pub const HTML_NAMESPACE: &str = "http://www.w3.org/1999/xhtml";
|
|
|
|
pub const SVG_NAMESPACE: &str = "http://www.w3.org/2000/svg";
|
|
|
|
|
2018-06-12 03:06:51 -04:00
|
|
|
// 32-bit signed int
|
|
|
|
pub const HIGHEST_ZINDEX: &str = "2147483647";
|
|
|
|
|
2018-02-21 01:37:09 -05:00
|
|
|
|
2018-11-24 15:59:46 -05:00
|
|
|
lazy_static! {
|
|
|
|
static ref HIDDEN_CLASS: String = class! {
|
|
|
|
.style_important("display", "none")
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
// TODO should return HtmlBodyElement ?
|
2018-03-18 14:49:40 -04:00
|
|
|
pub fn body() -> HtmlElement {
|
2019-06-11 09:29:46 -04:00
|
|
|
document().body().unwrap_throw()
|
2018-03-18 14:49:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-15 06:54:18 -04:00
|
|
|
pub struct DomHandle {
|
|
|
|
parent: Node,
|
|
|
|
dom: Dom,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Discard for DomHandle {
|
|
|
|
#[inline]
|
|
|
|
fn discard(self) {
|
2019-06-11 09:29:46 -04:00
|
|
|
self.parent.remove_child(&self.dom.element).unwrap_throw();
|
2018-03-15 06:54:18 -04:00
|
|
|
self.dom.callbacks.discard();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-25 06:58:20 -05:00
|
|
|
#[inline]
|
2019-06-11 12:20:15 -04:00
|
|
|
pub fn append_dom(parent: &Node, mut dom: Dom) -> DomHandle {
|
2019-06-11 09:29:46 -04:00
|
|
|
parent.append_child(&dom.element).unwrap_throw();
|
2018-02-21 01:37:09 -05:00
|
|
|
|
2018-02-25 06:58:20 -05:00
|
|
|
dom.callbacks.trigger_after_insert();
|
2018-02-21 01:37:09 -05:00
|
|
|
|
2018-03-15 06:54:18 -04:00
|
|
|
// This prevents it from triggering after_remove
|
|
|
|
dom.callbacks.leak();
|
|
|
|
|
|
|
|
DomHandle {
|
2019-06-11 12:20:15 -04:00
|
|
|
parent: parent.clone(),
|
2019-06-11 09:29:46 -04:00
|
|
|
dom,
|
2018-07-04 07:34:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-15 05:23:38 -04:00
|
|
|
// TODO use must_use ?
|
2018-07-04 07:51:39 -04:00
|
|
|
enum IsWindowLoaded {
|
2018-07-04 07:34:13 -04:00
|
|
|
Initial {},
|
|
|
|
Pending {
|
2019-06-11 09:29:46 -04:00
|
|
|
receiver: oneshot::Receiver<Option<bool>>,
|
|
|
|
_event: EventListener,
|
2018-07-04 07:34:13 -04:00
|
|
|
},
|
|
|
|
Done {},
|
|
|
|
}
|
|
|
|
|
2018-07-04 07:51:39 -04:00
|
|
|
impl Signal for IsWindowLoaded {
|
2018-07-04 07:34:13 -04:00
|
|
|
type Item = bool;
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
fn poll_change(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
|
|
|
let result = match *self {
|
2018-07-04 07:51:39 -04:00
|
|
|
IsWindowLoaded::Initial {} => {
|
2019-06-11 09:29:46 -04:00
|
|
|
let is_ready = document().ready_state() == "complete";
|
2018-07-04 07:34:13 -04:00
|
|
|
|
|
|
|
if is_ready {
|
2018-10-22 20:16:54 -04:00
|
|
|
Poll::Ready(Some(true))
|
2018-07-04 07:34:13 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
let (sender, receiver) = oneshot::channel();
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
*self = IsWindowLoaded::Pending {
|
2018-07-04 07:34:13 -04:00
|
|
|
receiver,
|
2019-06-11 09:29:46 -04:00
|
|
|
_event: EventListener::once(&window().unwrap_throw(), "load", move |_| {
|
2018-07-04 07:34:13 -04:00
|
|
|
// TODO test this
|
2019-06-11 09:29:46 -04:00
|
|
|
sender.send(Some(true)).unwrap_throw();
|
2018-07-04 07:34:13 -04:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2018-10-22 20:16:54 -04:00
|
|
|
Poll::Ready(Some(false))
|
2018-07-04 07:34:13 -04:00
|
|
|
}
|
|
|
|
},
|
2019-06-11 09:29:46 -04:00
|
|
|
IsWindowLoaded::Pending { ref mut receiver, .. } => {
|
|
|
|
receiver.poll_unpin(cx).map(|x| x.unwrap_throw())
|
2018-07-04 07:34:13 -04:00
|
|
|
},
|
2018-07-04 07:51:39 -04:00
|
|
|
IsWindowLoaded::Done {} => {
|
2018-10-22 20:16:54 -04:00
|
|
|
Poll::Ready(None)
|
2018-07-04 07:34:13 -04:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-10-22 20:16:54 -04:00
|
|
|
if let Poll::Ready(Some(true)) = result {
|
2019-06-11 09:29:46 -04:00
|
|
|
*self = IsWindowLoaded::Done {};
|
2018-07-04 07:34:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
// TODO this should be moved into gloo
|
2018-07-04 07:34:13 -04:00
|
|
|
#[inline]
|
2018-07-04 07:51:39 -04:00
|
|
|
pub fn is_window_loaded() -> impl Signal<Item = bool> {
|
|
|
|
IsWindowLoaded::Initial {}
|
2018-07-04 07:34:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-25 06:58:20 -05:00
|
|
|
#[inline]
|
2018-04-25 19:01:52 -04:00
|
|
|
pub fn text(value: &str) -> Dom {
|
2019-06-11 09:29:46 -04:00
|
|
|
Dom::new(document().create_text_node(value).into())
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO should this inline ?
|
|
|
|
pub fn text_signal<A, B>(value: B) -> Dom
|
2018-09-17 17:17:55 -04:00
|
|
|
where A: AsStr,
|
2018-10-22 20:16:54 -04:00
|
|
|
B: Signal<Item = A> + 'static {
|
2018-04-25 19:01:52 -04:00
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
let element = document().create_text_node("");
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
let mut callbacks = Callbacks::new();
|
|
|
|
|
|
|
|
{
|
|
|
|
let element = element.clone();
|
|
|
|
|
2018-10-22 20:16:54 -04:00
|
|
|
callbacks.after_remove(for_each(value, move |value| {
|
2018-09-17 17:17:55 -04:00
|
|
|
let value = value.as_str();
|
2019-06-11 09:29:46 -04:00
|
|
|
|
|
|
|
// http://jsperf.com/textnode-performance
|
|
|
|
element.set_data(value);
|
2018-04-25 19:01:52 -04:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
Dom {
|
|
|
|
element: element.into(),
|
|
|
|
callbacks: callbacks,
|
|
|
|
}
|
2018-02-21 01:37:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-15 05:23:38 -04:00
|
|
|
// TODO better warning message for must_use
|
|
|
|
#[must_use]
|
2018-02-21 03:08:36 -05:00
|
|
|
#[derive(Debug)]
|
2018-02-21 01:37:09 -05:00
|
|
|
pub struct Dom {
|
2018-02-25 06:58:20 -05:00
|
|
|
pub(crate) element: Node,
|
|
|
|
pub(crate) callbacks: Callbacks,
|
2018-02-21 01:37:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Dom {
|
|
|
|
#[inline]
|
2018-05-20 05:53:42 -04:00
|
|
|
pub fn new(element: Node) -> Self {
|
2018-02-21 01:37:09 -05:00
|
|
|
Self {
|
|
|
|
element,
|
|
|
|
callbacks: Callbacks::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn empty() -> Self {
|
|
|
|
// TODO is there a better way of doing this ?
|
2019-06-11 09:29:46 -04:00
|
|
|
Self::new(document().create_comment("").into())
|
2018-02-21 01:37:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn with_state<A, F>(mut state: A, initializer: F) -> Dom
|
|
|
|
where A: 'static,
|
|
|
|
F: FnOnce(&mut A) -> Dom {
|
|
|
|
|
|
|
|
let mut dom = initializer(&mut state);
|
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
dom.callbacks.after_remove(ValueDiscard::new(state));
|
2018-02-21 01:37:09 -05:00
|
|
|
|
|
|
|
dom
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
// TODO create HTML / SVG specific versions of this ?
|
|
|
|
#[inline]
|
2019-06-11 09:29:46 -04:00
|
|
|
pub fn create_element_ns<A>(name: &str, namespace: &str) -> A where A: JsCast {
|
|
|
|
document().create_element_ns(Some(namespace), name).unwrap_throw().dyn_into().unwrap_throw()
|
2018-02-21 01:37:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
fn set_option<A, B, C, D, F>(element: A, callbacks: &mut Callbacks, value: D, mut f: F)
|
|
|
|
where A: 'static,
|
2018-09-17 17:17:55 -04:00
|
|
|
C: OptionStr<Output = B>,
|
2018-10-22 20:16:54 -04:00
|
|
|
D: Signal<Item = C> + 'static,
|
2018-09-17 17:17:55 -04:00
|
|
|
F: FnMut(&A, Option<B>) + 'static {
|
2018-06-09 14:45:41 -04:00
|
|
|
|
|
|
|
let mut is_set = false;
|
|
|
|
|
2018-10-22 20:16:54 -04:00
|
|
|
callbacks.after_remove(for_each(value, move |value| {
|
2018-09-17 17:17:55 -04:00
|
|
|
let value = value.into_option();
|
2018-06-09 14:45:41 -04:00
|
|
|
|
|
|
|
if value.is_some() {
|
|
|
|
is_set = true;
|
|
|
|
|
|
|
|
} else if is_set {
|
|
|
|
is_set = false;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
f(&element, value);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
fn set_style<A, B>(style: &CssStyleDeclaration, name: &A, value: B, important: bool)
|
|
|
|
where A: MultiStr,
|
|
|
|
B: MultiStr {
|
2018-09-17 17:17:55 -04:00
|
|
|
|
|
|
|
let mut names = vec![];
|
|
|
|
let mut values = vec![];
|
|
|
|
|
|
|
|
let okay = name.any(|name| {
|
|
|
|
value.any(|value| {
|
2019-06-11 09:29:46 -04:00
|
|
|
assert!(value != "");
|
|
|
|
|
|
|
|
// TODO handle browser prefixes ?
|
|
|
|
style.remove_property(name).unwrap_throw();
|
|
|
|
|
|
|
|
style.set_property_with_priority(name, value, if important { "important" } else { "" }).unwrap_throw();
|
|
|
|
|
|
|
|
// TODO maybe use cfg(debug_assertions) ?
|
|
|
|
let is_changed = style.get_property_value(name).unwrap_throw() != "";
|
|
|
|
|
|
|
|
if is_changed {
|
2018-09-17 17:17:55 -04:00
|
|
|
true
|
|
|
|
|
|
|
|
} else {
|
|
|
|
names.push(name.to_string());
|
|
|
|
values.push(value.to_string());
|
|
|
|
false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
if !okay {
|
|
|
|
// TODO maybe make this configurable
|
|
|
|
panic!("style is incorrect:\n names: {}\n values: {}", names.join(", "), values.join(", "));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
fn set_style_signal<A, B, C, D>(style: CssStyleDeclaration, callbacks: &mut Callbacks, name: A, value: D, important: bool)
|
|
|
|
where A: MultiStr + 'static,
|
|
|
|
B: MultiStr,
|
|
|
|
C: OptionStr<Output = B>,
|
|
|
|
D: Signal<Item = C> + 'static {
|
2018-09-17 17:17:55 -04:00
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
set_option(style, callbacks, value, move |style, value| {
|
2018-09-17 17:17:55 -04:00
|
|
|
match value {
|
|
|
|
Some(value) => {
|
2019-06-11 09:29:46 -04:00
|
|
|
set_style(style, &name, value, important);
|
2018-09-17 17:17:55 -04:00
|
|
|
},
|
|
|
|
None => {
|
|
|
|
name.each(|name| {
|
2019-06-11 09:29:46 -04:00
|
|
|
// TODO handle browser prefixes ?
|
|
|
|
style.remove_property(name).unwrap_throw();
|
2018-09-17 17:17:55 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
// TODO check that the property *actually* was changed ?
|
|
|
|
fn set_property<A, B, C>(element: &A, name: &B, value: C) where A: AsRef<JsValue>, B: MultiStr, C: Into<JsValue> {
|
|
|
|
let element = element.as_ref();
|
|
|
|
let value = value.into();
|
|
|
|
|
|
|
|
name.each(|name| {
|
|
|
|
// TODO can this be made more efficient ?
|
|
|
|
assert!(Reflect::set(element, &JsValue::from(name), &value).unwrap_throw());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-09 14:45:41 -04:00
|
|
|
|
2019-06-15 05:23:38 -04:00
|
|
|
// TODO better warning message for must_use
|
|
|
|
#[must_use]
|
2018-04-25 19:01:52 -04:00
|
|
|
pub struct DomBuilder<A> {
|
|
|
|
element: A,
|
|
|
|
callbacks: Callbacks,
|
|
|
|
// TODO verify this with static types instead ?
|
|
|
|
has_children: bool,
|
2018-03-15 06:54:18 -04:00
|
|
|
}
|
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
impl<A> DomBuilder<A> {
|
2018-03-15 06:54:18 -04:00
|
|
|
#[inline]
|
2018-04-25 19:01:52 -04:00
|
|
|
pub fn new(value: A) -> Self {
|
|
|
|
Self {
|
|
|
|
element: value,
|
|
|
|
callbacks: Callbacks::new(),
|
|
|
|
has_children: false,
|
2018-03-15 06:54:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
#[inline]
|
|
|
|
fn _event<T, F>(&mut self, element: &EventTarget, listener: F)
|
|
|
|
where T: StaticEvent,
|
2018-02-21 01:37:09 -05:00
|
|
|
F: FnMut(T) + 'static {
|
2019-06-11 09:29:46 -04:00
|
|
|
self.callbacks.after_remove(EventDiscard::new(on(element, listener)));
|
|
|
|
}
|
2018-02-21 01:37:09 -05:00
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
#[inline]
|
|
|
|
fn _event_with_options<T, F>(&mut self, element: &EventTarget, options: EventListenerOptions, listener: F)
|
|
|
|
where T: StaticEvent,
|
|
|
|
F: FnMut(T) + 'static {
|
|
|
|
self.callbacks.after_remove(EventDiscard::new(on_with_options(element, options, listener)));
|
2018-02-21 01:37:09 -05:00
|
|
|
}
|
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
// TODO add this to the StylesheetBuilder and ClassBuilder too
|
|
|
|
#[inline]
|
|
|
|
pub fn global_event<T, F>(mut self, listener: F) -> Self
|
2019-06-11 09:29:46 -04:00
|
|
|
where T: StaticEvent,
|
2018-04-25 19:01:52 -04:00
|
|
|
F: FnMut(T) + 'static {
|
2019-06-11 09:29:46 -04:00
|
|
|
self._event(&window().unwrap_throw(), listener);
|
2018-04-25 19:01:52 -04:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-10-22 20:16:54 -04:00
|
|
|
pub fn future<F>(mut self, future: F) -> Self where F: Future<Output = ()> + 'static {
|
2018-04-25 19:01:52 -04:00
|
|
|
self.callbacks.after_remove(DiscardOnDrop::leak(spawn_future(future)));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-10-30 05:06:22 -04:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn apply<F>(self, f: F) -> Self where F: FnOnce(Self) -> Self {
|
|
|
|
self.apply_if(true, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn apply_if<F>(self, test: bool, f: F) -> Self where F: FnOnce(Self) -> Self {
|
|
|
|
if test {
|
|
|
|
f(self)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
impl<A> DomBuilder<A> where A: Clone {
|
2018-06-12 03:33:10 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn with_element<B, F>(self, f: F) -> B where F: FnOnce(Self, A) -> B {
|
|
|
|
let element = self.element.clone();
|
|
|
|
f(self, element)
|
|
|
|
}
|
|
|
|
|
2018-05-20 05:53:42 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn before_inserted<F>(self, f: F) -> Self where F: FnOnce(A) {
|
|
|
|
let element = self.element.clone();
|
|
|
|
f(element);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
impl<A> DomBuilder<A> where A: Clone + 'static {
|
2018-04-25 19:01:52 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn after_inserted<F>(mut self, f: F) -> Self where F: FnOnce(A) + 'static {
|
|
|
|
let element = self.element.clone();
|
|
|
|
self.callbacks.after_insert(move |_| f(element));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn after_removed<F>(mut self, f: F) -> Self where F: FnOnce(A) + 'static {
|
|
|
|
let element = self.element.clone();
|
|
|
|
self.callbacks.after_remove(FnDiscard::new(move || f(element)));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
impl<A> DomBuilder<A> where A: Into<Node> {
|
2018-04-25 19:01:52 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn into_dom(self) -> Dom {
|
|
|
|
Dom {
|
|
|
|
element: self.element.into(),
|
|
|
|
callbacks: self.callbacks,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
impl<A> DomBuilder<A> where A: AsRef<JsValue> {
|
2018-04-25 19:01:52 -04:00
|
|
|
#[inline]
|
2019-06-11 09:29:46 -04:00
|
|
|
pub fn property<B, C>(self, name: B, value: C) -> Self where B: MultiStr, C: Into<JsValue> {
|
|
|
|
set_property(&self.element, &name, value);
|
2018-04-25 19:01:52 -04:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
impl<A> DomBuilder<A> where A: AsRef<JsValue> {
|
2018-09-17 17:17:55 -04:00
|
|
|
fn set_property_signal<B, C, D>(&mut self, name: B, value: D)
|
|
|
|
where B: MultiStr + 'static,
|
2019-06-11 09:29:46 -04:00
|
|
|
C: Into<JsValue>,
|
2018-10-22 20:16:54 -04:00
|
|
|
D: Signal<Item = C> + 'static {
|
2018-04-25 19:01:52 -04:00
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
let element = self.element.as_ref().clone();
|
2018-04-25 19:01:52 -04:00
|
|
|
|
2018-10-22 20:16:54 -04:00
|
|
|
self.callbacks.after_remove(for_each(value, move |value| {
|
2019-06-11 09:29:46 -04:00
|
|
|
set_property(&element, &name, value);
|
2018-04-25 19:01:52 -04:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn property_signal<B, C, D>(mut self, name: B, value: D) -> Self
|
|
|
|
where B: MultiStr + 'static,
|
2019-06-11 09:29:46 -04:00
|
|
|
C: Into<JsValue>,
|
2018-10-22 20:16:54 -04:00
|
|
|
D: Signal<Item = C> + 'static {
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
self.set_property_signal(name, value);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
impl<A> DomBuilder<A> where A: AsRef<EventTarget> {
|
2018-02-21 01:37:09 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn event<T, F>(mut self, listener: F) -> Self
|
2019-06-11 09:29:46 -04:00
|
|
|
where T: StaticEvent,
|
2018-02-21 01:37:09 -05:00
|
|
|
F: FnMut(T) + 'static {
|
2019-06-11 09:29:46 -04:00
|
|
|
// TODO can this clone be avoided ?
|
|
|
|
self._event(&self.element.as_ref().clone(), listener);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn event_preventable<T, F>(mut self, listener: F) -> Self
|
|
|
|
where T: StaticEvent,
|
|
|
|
F: FnMut(T) + 'static {
|
|
|
|
// TODO can this clone be avoided ?
|
|
|
|
self._event_with_options(&self.element.as_ref().clone(), EventListenerOptions::enable_prevent_default(), listener);
|
2018-02-21 01:37:09 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
impl<A> DomBuilder<A> where A: AsRef<Node> {
|
2018-04-25 19:01:52 -04:00
|
|
|
// TODO figure out how to make this owned rather than &mut
|
2018-02-21 01:37:09 -05:00
|
|
|
#[inline]
|
2018-04-25 19:01:52 -04:00
|
|
|
pub fn children<'a, B: IntoIterator<Item = &'a mut Dom>>(mut self, children: B) -> Self {
|
2018-02-21 01:37:09 -05:00
|
|
|
assert_eq!(self.has_children, false);
|
|
|
|
self.has_children = true;
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
operations::insert_children_iter(self.element.as_ref(), &mut self.callbacks, children);
|
2018-02-21 01:37:09 -05:00
|
|
|
self
|
|
|
|
}
|
2018-10-25 05:39:55 -04:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn text(self, value: &str) -> Self {
|
|
|
|
// TODO make this more efficient ?
|
|
|
|
self.children(&mut [
|
|
|
|
text(value),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn text_signal<B, C>(self, value: C) -> Self
|
|
|
|
where B: AsStr,
|
|
|
|
C: Signal<Item = B> + 'static {
|
|
|
|
// TODO make this more efficient ?
|
|
|
|
self.children(&mut [
|
|
|
|
text_signal(value),
|
|
|
|
])
|
|
|
|
}
|
2018-02-21 01:37:09 -05:00
|
|
|
}
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
impl<A> DomBuilder<A> where A: AsRef<Node> {
|
2018-02-21 01:37:09 -05:00
|
|
|
#[inline]
|
2018-04-25 19:01:52 -04:00
|
|
|
pub fn children_signal_vec<B>(mut self, children: B) -> Self
|
2018-10-22 20:16:54 -04:00
|
|
|
where B: SignalVec<Item = Dom> + 'static {
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
assert_eq!(self.has_children, false);
|
|
|
|
self.has_children = true;
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
operations::insert_children_signal_vec(self.element.as_ref().clone(), &mut self.callbacks, children);
|
2018-02-21 01:37:09 -05:00
|
|
|
self
|
|
|
|
}
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
2018-02-21 01:37:09 -05:00
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
impl<A> DomBuilder<A> where A: AsRef<Element> {
|
2018-02-21 01:37:09 -05:00
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn attribute<B>(self, name: B, value: &str) -> Self where B: MultiStr {
|
|
|
|
name.each(|name| {
|
2019-06-11 09:29:46 -04:00
|
|
|
dom_operations::set_attribute(self.element.as_ref(), name, value);
|
2018-09-17 17:17:55 -04:00
|
|
|
});
|
2018-02-21 01:37:09 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn attribute_namespace<B>(self, namespace: &str, name: B, value: &str) -> Self where B: MultiStr {
|
|
|
|
name.each(|name| {
|
2019-06-11 09:29:46 -04:00
|
|
|
dom_operations::set_attribute_ns(self.element.as_ref(), namespace, name, value);
|
2018-09-17 17:17:55 -04:00
|
|
|
});
|
2018-04-25 19:01:52 -04:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn class<B>(self, name: B) -> Self where B: MultiStr {
|
|
|
|
name.each(|name| {
|
2019-06-11 09:29:46 -04:00
|
|
|
dom_operations::add_class(self.element.as_ref(), name);
|
2018-09-17 17:17:55 -04:00
|
|
|
});
|
2018-02-21 01:37:09 -05:00
|
|
|
self
|
|
|
|
}
|
2019-06-11 09:29:46 -04:00
|
|
|
|
|
|
|
// TODO make this more efficient ?
|
|
|
|
#[inline]
|
|
|
|
pub fn visible(self, value: bool) -> Self {
|
|
|
|
if value {
|
|
|
|
// TODO remove the class somehow ?
|
|
|
|
self
|
|
|
|
|
|
|
|
} else {
|
|
|
|
self.class(&*HIDDEN_CLASS)
|
|
|
|
}
|
|
|
|
}
|
2018-02-21 01:37:09 -05:00
|
|
|
}
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
impl<A> DomBuilder<A> where A: AsRef<Element> {
|
2018-09-17 17:17:55 -04:00
|
|
|
fn set_attribute_signal<B, C, D, E>(&mut self, name: B, value: E)
|
|
|
|
where B: MultiStr + 'static,
|
|
|
|
C: AsStr,
|
|
|
|
D: OptionStr<Output = C>,
|
2018-10-22 20:16:54 -04:00
|
|
|
E: Signal<Item = D> + 'static {
|
2018-09-17 17:17:55 -04:00
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
set_option(self.element.as_ref().clone(), &mut self.callbacks, value, move |element, value| {
|
2018-06-09 14:45:41 -04:00
|
|
|
match value {
|
2018-09-17 17:17:55 -04:00
|
|
|
Some(value) => {
|
|
|
|
let value = value.as_str();
|
|
|
|
|
|
|
|
name.each(|name| {
|
|
|
|
dom_operations::set_attribute(element, &name, value);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
name.each(|name| {
|
|
|
|
dom_operations::remove_attribute(element, &name)
|
|
|
|
});
|
|
|
|
},
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
2018-06-09 14:45:41 -04:00
|
|
|
});
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn attribute_signal<B, C, D, E>(mut self, name: B, value: E) -> Self
|
|
|
|
where B: MultiStr + 'static,
|
|
|
|
C: AsStr,
|
|
|
|
D: OptionStr<Output = C>,
|
2018-10-22 20:16:54 -04:00
|
|
|
E: Signal<Item = D> + 'static {
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
self.set_attribute_signal(name, value);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
fn set_attribute_namespace_signal<B, C, D, E>(&mut self, namespace: &str, name: B, value: E)
|
|
|
|
where B: MultiStr + 'static,
|
|
|
|
C: AsStr,
|
|
|
|
D: OptionStr<Output = C>,
|
2018-10-22 20:16:54 -04:00
|
|
|
E: Signal<Item = D> + 'static {
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
let namespace = namespace.to_owned();
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
set_option(self.element.as_ref().clone(), &mut self.callbacks, value, move |element, value| {
|
2018-06-09 14:45:41 -04:00
|
|
|
match value {
|
2018-09-17 17:17:55 -04:00
|
|
|
Some(value) => {
|
|
|
|
let value = value.as_str();
|
|
|
|
|
|
|
|
name.each(|name| {
|
|
|
|
dom_operations::set_attribute_ns(element, &namespace, &name, value);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
name.each(|name| {
|
|
|
|
dom_operations::remove_attribute_ns(element, &namespace, &name);
|
|
|
|
});
|
|
|
|
},
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
2018-06-09 14:45:41 -04:00
|
|
|
});
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn attribute_namespace_signal<B, C, D, E>(mut self, namespace: &str, name: B, value: E) -> Self
|
|
|
|
where B: MultiStr + 'static,
|
|
|
|
C: AsStr,
|
|
|
|
D: OptionStr<Output = C>,
|
2018-10-22 20:16:54 -04:00
|
|
|
E: Signal<Item = D> + 'static {
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
self.set_attribute_namespace_signal(namespace, name, value);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
fn set_class_signal<B, C>(&mut self, name: B, value: C)
|
|
|
|
where B: MultiStr + 'static,
|
2018-10-22 20:16:54 -04:00
|
|
|
C: Signal<Item = bool> + 'static {
|
2018-04-25 19:01:52 -04:00
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
let element = self.element.as_ref().clone();
|
2018-04-25 19:01:52 -04:00
|
|
|
|
2018-06-09 14:45:41 -04:00
|
|
|
let mut is_set = false;
|
|
|
|
|
2018-10-22 20:16:54 -04:00
|
|
|
self.callbacks.after_remove(for_each(value, move |value| {
|
2018-04-25 19:01:52 -04:00
|
|
|
if value {
|
2018-06-09 14:45:41 -04:00
|
|
|
if !is_set {
|
|
|
|
is_set = true;
|
2018-09-17 17:17:55 -04:00
|
|
|
|
|
|
|
name.each(|name| {
|
|
|
|
dom_operations::add_class(&element, name);
|
|
|
|
});
|
2018-06-09 14:45:41 -04:00
|
|
|
}
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
} else {
|
2018-06-09 14:45:41 -04:00
|
|
|
if is_set {
|
|
|
|
is_set = false;
|
2018-09-17 17:17:55 -04:00
|
|
|
|
|
|
|
name.each(|name| {
|
|
|
|
dom_operations::remove_class(&element, name);
|
|
|
|
});
|
2018-06-09 14:45:41 -04:00
|
|
|
}
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2018-02-21 01:37:09 -05:00
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn class_signal<B, C>(mut self, name: B, value: C) -> Self
|
|
|
|
where B: MultiStr + 'static,
|
2018-10-22 20:16:54 -04:00
|
|
|
C: Signal<Item = bool> + 'static {
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
self.set_class_signal(name, value);
|
2018-02-21 01:37:09 -05:00
|
|
|
self
|
|
|
|
}
|
2018-07-03 23:44:50 -04:00
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
// TODO make this more efficient ?
|
|
|
|
#[inline]
|
|
|
|
pub fn visible_signal<B>(self, value: B) -> Self where B: Signal<Item = bool> + 'static {
|
|
|
|
self.class_signal(&*HIDDEN_CLASS, not(value))
|
|
|
|
}
|
|
|
|
|
2018-07-04 05:56:07 -04:00
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
// TODO use OptionStr ?
|
2018-07-03 23:58:43 -04:00
|
|
|
fn set_scroll_signal<B, F>(&mut self, signal: B, mut f: F)
|
2019-06-11 09:29:46 -04:00
|
|
|
where B: Signal<Item = Option<i32>> + 'static,
|
|
|
|
F: FnMut(&Element, i32) + 'static {
|
2018-07-03 23:58:43 -04:00
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
let element: Element = self.element.as_ref().clone();
|
2018-07-03 23:44:50 -04:00
|
|
|
|
2018-07-03 23:58:43 -04:00
|
|
|
// This needs to use `after_insert` because scrolling an element before it is in the DOM has no effect
|
|
|
|
self.callbacks.after_insert(move |callbacks| {
|
|
|
|
callbacks.after_remove(for_each(signal, move |value| {
|
|
|
|
if let Some(value) = value {
|
|
|
|
f(&element, value);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
2018-07-03 23:44:50 -04:00
|
|
|
|
2018-07-03 23:58:43 -04:00
|
|
|
#[inline]
|
2019-06-11 09:29:46 -04:00
|
|
|
pub fn scroll_left_signal<B>(mut self, signal: B) -> Self where B: Signal<Item = Option<i32>> + 'static {
|
|
|
|
self.set_scroll_signal(signal, Element::set_scroll_left);
|
2018-07-03 23:44:50 -04:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-11 09:29:46 -04:00
|
|
|
pub fn scroll_top_signal<B>(mut self, signal: B) -> Self where B: Signal<Item = Option<i32>> + 'static {
|
|
|
|
self.set_scroll_signal(signal, Element::set_scroll_top);
|
2018-07-03 23:44:50 -04:00
|
|
|
self
|
|
|
|
}
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
2018-02-21 01:37:09 -05:00
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
impl<A> DomBuilder<A> where A: AsRef<HtmlElement> {
|
2018-02-21 01:37:09 -05:00
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn style<B, C>(self, name: B, value: C) -> Self
|
|
|
|
where B: MultiStr,
|
|
|
|
C: MultiStr {
|
2019-06-11 09:29:46 -04:00
|
|
|
set_style(&self.element.as_ref().style(), &name, value, false);
|
2018-02-21 01:37:09 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn style_important<B, C>(self, name: B, value: C) -> Self
|
|
|
|
where B: MultiStr,
|
|
|
|
C: MultiStr {
|
2019-06-11 09:29:46 -04:00
|
|
|
set_style(&self.element.as_ref().style(), &name, value, true);
|
2018-02-21 01:37:09 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
impl<A> DomBuilder<A> where A: AsRef<HtmlElement> {
|
2018-02-21 01:37:09 -05:00
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn style_signal<B, C, D, E>(mut self, name: B, value: E) -> Self
|
|
|
|
where B: MultiStr + 'static,
|
|
|
|
C: MultiStr,
|
|
|
|
D: OptionStr<Output = C>,
|
2018-10-22 20:16:54 -04:00
|
|
|
E: Signal<Item = D> + 'static {
|
2018-09-17 17:17:55 -04:00
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
set_style_signal(self.element.as_ref().style(), &mut self.callbacks, name, value, false);
|
2018-04-25 19:01:52 -04:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn style_important_signal<B, C, D, E>(mut self, name: B, value: E) -> Self
|
|
|
|
where B: MultiStr + 'static,
|
|
|
|
C: MultiStr,
|
|
|
|
D: OptionStr<Output = C>,
|
2018-10-22 20:16:54 -04:00
|
|
|
E: Signal<Item = D> + 'static {
|
2018-09-17 17:17:55 -04:00
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
set_style_signal(self.element.as_ref().style(), &mut self.callbacks, name, value, true);
|
2018-04-25 19:01:52 -04:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO remove the `value` argument ?
|
|
|
|
#[inline]
|
|
|
|
pub fn focused(mut self, value: bool) -> Self {
|
2019-06-11 09:29:46 -04:00
|
|
|
let element = self.element.as_ref().clone();
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
// This needs to use `after_insert` because calling `.focus()` on an element before it is in the DOM has no effect
|
|
|
|
self.callbacks.after_insert(move |_| {
|
2018-06-09 14:45:41 -04:00
|
|
|
// TODO avoid updating if the focused state hasn't changed ?
|
2018-04-25 19:01:52 -04:00
|
|
|
dom_operations::set_focused(&element, value);
|
|
|
|
});
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn set_focused_signal<B>(&mut self, value: B)
|
2018-10-22 20:16:54 -04:00
|
|
|
where B: Signal<Item = bool> + 'static {
|
2018-04-25 19:01:52 -04:00
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
let element = self.element.as_ref().clone();
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
// This needs to use `after_insert` because calling `.focus()` on an element before it is in the DOM has no effect
|
|
|
|
self.callbacks.after_insert(move |callbacks| {
|
|
|
|
// TODO verify that this is correct under all circumstances
|
2018-07-03 23:58:43 -04:00
|
|
|
callbacks.after_remove(for_each(value, move |value| {
|
2018-06-09 14:45:41 -04:00
|
|
|
// TODO avoid updating if the focused state hasn't changed ?
|
2018-04-25 19:01:52 -04:00
|
|
|
dom_operations::set_focused(&element, value);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn focused_signal<B>(mut self, value: B) -> Self
|
2018-10-22 20:16:54 -04:00
|
|
|
where B: Signal<Item = bool> + 'static {
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
self.set_focused_signal(value);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-25 06:58:20 -05:00
|
|
|
// TODO better warning message for must_use
|
|
|
|
#[must_use]
|
2018-02-21 01:37:09 -05:00
|
|
|
pub struct StylesheetBuilder {
|
2019-06-11 09:29:46 -04:00
|
|
|
element: CssStyleDeclaration,
|
2018-02-21 01:37:09 -05:00
|
|
|
callbacks: Callbacks,
|
|
|
|
}
|
|
|
|
|
2018-02-25 06:58:20 -05:00
|
|
|
// TODO remove the CssStyleRule when this is discarded
|
2018-02-21 01:37:09 -05:00
|
|
|
impl StylesheetBuilder {
|
|
|
|
#[inline]
|
|
|
|
pub fn new(selector: &str) -> Self {
|
2019-06-11 09:29:46 -04:00
|
|
|
// TODO can this be made faster ?
|
|
|
|
// TODO somehow share this safely between threads ?
|
|
|
|
thread_local! {
|
|
|
|
static STYLESHEET: CssStyleSheet = {
|
2018-02-21 01:37:09 -05:00
|
|
|
// TODO use createElementNS ?
|
2019-06-11 09:29:46 -04:00
|
|
|
let e = document().create_element("style").unwrap_throw();
|
|
|
|
// TODO maybe don't use unchecked ?
|
|
|
|
let e: &HtmlStyleElement = e.unchecked_ref();
|
|
|
|
|
|
|
|
e.set_type("text/css");
|
|
|
|
|
|
|
|
document().head().unwrap_throw().append_child(e).unwrap_throw();
|
|
|
|
|
|
|
|
// TODO maybe don't use unchecked ?
|
|
|
|
e.sheet().unwrap_throw().unchecked_into()
|
|
|
|
};
|
2018-02-21 01:37:09 -05:00
|
|
|
}
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
let element = STYLESHEET.with(|stylesheet| {
|
|
|
|
let rules = stylesheet.css_rules().unwrap_throw();
|
|
|
|
|
|
|
|
let length = rules.length();
|
|
|
|
|
|
|
|
stylesheet.insert_rule_with_index(&format!("{}{{}}", selector), length).unwrap_throw();
|
|
|
|
|
|
|
|
// TODO maybe don't use unchecked ?
|
|
|
|
rules.get(length).unwrap_throw().unchecked_ref::<CssStyleRule>().style()
|
|
|
|
});
|
|
|
|
|
2018-02-21 01:37:09 -05:00
|
|
|
Self {
|
2019-06-11 09:29:46 -04:00
|
|
|
element,
|
2018-02-21 01:37:09 -05:00
|
|
|
callbacks: Callbacks::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn style<B, C>(self, name: B, value: C) -> Self
|
|
|
|
where B: MultiStr,
|
|
|
|
C: MultiStr {
|
|
|
|
set_style(&self.element, &name, value, false);
|
2018-04-25 19:01:52 -04:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn style_important<B, C>(self, name: B, value: C) -> Self
|
|
|
|
where B: MultiStr,
|
|
|
|
C: MultiStr {
|
|
|
|
set_style(&self.element, &name, value, true);
|
2018-04-25 19:01:52 -04:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn style_signal<B, C, D, E>(mut self, name: B, value: E) -> Self
|
|
|
|
where B: MultiStr + 'static,
|
|
|
|
C: MultiStr,
|
|
|
|
D: OptionStr<Output = C>,
|
2018-10-22 20:16:54 -04:00
|
|
|
E: Signal<Item = D> + 'static {
|
2018-09-17 17:17:55 -04:00
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
set_style_signal(self.element.clone(), &mut self.callbacks, name, value, false);
|
2018-02-21 01:37:09 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn style_important_signal<B, C, D, E>(mut self, name: B, value: E) -> Self
|
|
|
|
where B: MultiStr + 'static,
|
|
|
|
C: MultiStr,
|
|
|
|
D: OptionStr<Output = C>,
|
2018-10-22 20:16:54 -04:00
|
|
|
E: Signal<Item = D> + 'static {
|
2018-09-17 17:17:55 -04:00
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
set_style_signal(self.element.clone(), &mut self.callbacks, name, value, true);
|
2018-02-21 01:37:09 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO return a Handle
|
|
|
|
#[inline]
|
|
|
|
pub fn done(mut self) {
|
|
|
|
self.callbacks.trigger_after_insert();
|
|
|
|
|
2018-03-15 06:54:18 -04:00
|
|
|
// This prevents it from triggering after_remove
|
|
|
|
self.callbacks.leak();
|
2018-02-21 01:37:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-25 06:58:20 -05:00
|
|
|
// TODO better warning message for must_use
|
|
|
|
#[must_use]
|
2018-02-21 01:37:09 -05:00
|
|
|
pub struct ClassBuilder {
|
|
|
|
stylesheet: StylesheetBuilder,
|
|
|
|
class_name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ClassBuilder {
|
|
|
|
#[inline]
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let class_name = {
|
2019-06-11 09:29:46 -04:00
|
|
|
use std::sync::atomic::{AtomicU32, Ordering};
|
2018-03-16 02:50:21 -04:00
|
|
|
|
2018-03-18 12:11:43 -04:00
|
|
|
// TODO replace this with a global counter in JavaScript ?
|
2018-02-21 01:37:09 -05:00
|
|
|
lazy_static! {
|
|
|
|
// TODO can this be made more efficient ?
|
2019-06-11 09:29:46 -04:00
|
|
|
static ref CLASS_ID: AtomicU32 = AtomicU32::new(0);
|
2018-02-21 01:37:09 -05:00
|
|
|
}
|
|
|
|
|
2018-03-16 02:50:21 -04:00
|
|
|
// TODO check for overflow ?
|
|
|
|
let id = CLASS_ID.fetch_add(1, Ordering::Relaxed);
|
2018-02-21 01:37:09 -05:00
|
|
|
|
|
|
|
// TODO make this more efficient ?
|
|
|
|
format!("__class_{}__", id)
|
|
|
|
};
|
|
|
|
|
|
|
|
Self {
|
|
|
|
// TODO make this more efficient ?
|
|
|
|
stylesheet: StylesheetBuilder::new(&format!(".{}", class_name)),
|
|
|
|
class_name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn style<B, C>(mut self, name: B, value: C) -> Self
|
|
|
|
where B: MultiStr,
|
|
|
|
C: MultiStr {
|
2018-02-21 01:37:09 -05:00
|
|
|
self.stylesheet = self.stylesheet.style(name, value);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn style_important<B, C>(mut self, name: B, value: C) -> Self
|
|
|
|
where B: MultiStr,
|
|
|
|
C: MultiStr {
|
2018-02-21 01:37:09 -05:00
|
|
|
self.stylesheet = self.stylesheet.style_important(name, value);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn style_signal<B, C, D, E>(mut self, name: B, value: E) -> Self
|
|
|
|
where B: MultiStr + 'static,
|
|
|
|
C: MultiStr,
|
|
|
|
D: OptionStr<Output = C>,
|
2018-10-22 20:16:54 -04:00
|
|
|
E: Signal<Item = D> + 'static {
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
self.stylesheet = self.stylesheet.style_signal(name, value);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
pub fn style_important_signal<B, C, D, E>(mut self, name: B, value: E) -> Self
|
|
|
|
where B: MultiStr + 'static,
|
|
|
|
C: MultiStr,
|
|
|
|
D: OptionStr<Output = C>,
|
2018-10-22 20:16:54 -04:00
|
|
|
E: Signal<Item = D> + 'static {
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
self.stylesheet = self.stylesheet.style_important_signal(name, value);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-02-21 01:37:09 -05:00
|
|
|
// TODO return a Handle ?
|
|
|
|
#[inline]
|
|
|
|
pub fn done(self) -> String {
|
|
|
|
self.stylesheet.done();
|
|
|
|
self.class_name
|
|
|
|
}
|
|
|
|
}
|
2018-06-13 19:48:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-09-17 17:17:55 -04:00
|
|
|
use super::{create_element_ns, DomBuilder, HTML_NAMESPACE, text_signal, RefFn};
|
2018-06-22 13:04:08 -04:00
|
|
|
use futures_signals::signal::{always, SignalExt};
|
2019-06-11 09:29:46 -04:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use web_sys::HtmlElement;
|
2018-06-13 19:48:41 -04:00
|
|
|
|
|
|
|
#[test]
|
2019-03-15 06:07:14 -04:00
|
|
|
fn apply() {
|
2018-06-13 19:48:41 -04:00
|
|
|
let a: DomBuilder<HtmlElement> = DomBuilder::new(create_element_ns("div", HTML_NAMESPACE));
|
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
fn my_mixin<A: AsRef<HtmlElement>>(builder: DomBuilder<A>) -> DomBuilder<A> {
|
2018-06-13 19:48:41 -04:00
|
|
|
builder.style("foo", "bar")
|
|
|
|
}
|
|
|
|
|
2019-03-15 06:07:14 -04:00
|
|
|
a.apply(my_mixin);
|
2018-06-13 19:48:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn text_signal_types() {
|
|
|
|
text_signal(always("foo"));
|
|
|
|
text_signal(always("foo".to_owned()));
|
2018-09-17 17:17:55 -04:00
|
|
|
text_signal(always("foo".to_owned()).map(|x| RefFn::new(x, |x| x.as_str())));
|
2018-06-22 13:04:08 -04:00
|
|
|
//text_signal(always(Arc::new("foo")));
|
|
|
|
//text_signal(always(Arc::new("foo".to_owned())));
|
|
|
|
//text_signal(always(Rc::new("foo")));
|
|
|
|
//text_signal(always(Rc::new("foo".to_owned())));
|
|
|
|
//text_signal(always(Box::new("foo")));
|
|
|
|
//text_signal(always(Box::new("foo".to_owned())));
|
|
|
|
//text_signal(always(Cow::Borrowed(&"foo")));
|
|
|
|
//text_signal(always(Cow::Owned::<String>("foo".to_owned())));
|
2018-06-13 19:48:41 -04:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
#[test]
|
|
|
|
fn property_signal_types() {
|
|
|
|
let _a: DomBuilder<HtmlElement> = DomBuilder::new(create_element_ns("div", HTML_NAMESPACE))
|
|
|
|
.property("foo", "hi")
|
|
|
|
.property("foo", 5)
|
|
|
|
.property(["foo", "-webkit-foo", "-ms-foo"], "hi")
|
|
|
|
|
|
|
|
.property_signal("foo", always("hi"))
|
|
|
|
.property_signal("foo", always(5))
|
|
|
|
.property_signal("foo", always(Some("hi")))
|
|
|
|
|
|
|
|
.property_signal(["foo", "-webkit-foo", "-ms-foo"], always("hi"))
|
|
|
|
.property_signal(["foo", "-webkit-foo", "-ms-foo"], always(5))
|
|
|
|
.property_signal(["foo", "-webkit-foo", "-ms-foo"], always(Some("hi")))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn attribute_signal_types() {
|
|
|
|
let _a: DomBuilder<HtmlElement> = DomBuilder::new(create_element_ns("div", HTML_NAMESPACE))
|
|
|
|
.attribute("foo", "hi")
|
|
|
|
.attribute(["foo", "-webkit-foo", "-ms-foo"], "hi")
|
|
|
|
|
|
|
|
.attribute_signal("foo", always("hi"))
|
|
|
|
.attribute_signal("foo", always(Some("hi")))
|
|
|
|
|
|
|
|
.attribute_signal(["foo", "-webkit-foo", "-ms-foo"], always("hi"))
|
|
|
|
.attribute_signal(["foo", "-webkit-foo", "-ms-foo"], always(Some("hi")))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn class_signal_types() {
|
|
|
|
let _a: DomBuilder<HtmlElement> = DomBuilder::new(create_element_ns("div", HTML_NAMESPACE))
|
|
|
|
.class("foo")
|
|
|
|
.class(["foo", "-webkit-foo", "-ms-foo"])
|
|
|
|
|
|
|
|
.class_signal("foo", always(true))
|
|
|
|
.class_signal(["foo", "-webkit-foo", "-ms-foo"], always(true))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2018-06-13 19:48:41 -04:00
|
|
|
#[test]
|
|
|
|
fn style_signal_types() {
|
2018-09-17 17:58:05 -04:00
|
|
|
lazy_static! {
|
|
|
|
static ref FOO: String = "foo".to_owned();
|
|
|
|
}
|
|
|
|
|
2018-06-13 19:48:41 -04:00
|
|
|
let _a: DomBuilder<HtmlElement> = DomBuilder::new(create_element_ns("div", HTML_NAMESPACE))
|
|
|
|
.style_signal("foo", always("bar"))
|
|
|
|
.style_signal("foo", always("bar".to_owned()))
|
2018-09-17 17:17:55 -04:00
|
|
|
.style_signal("foo", always("bar".to_owned()).map(|x| RefFn::new(x, |x| x.as_str())))
|
|
|
|
|
2018-09-17 17:58:05 -04:00
|
|
|
.style("foo".to_owned(), "bar".to_owned())
|
|
|
|
.style_signal("foo".to_owned(), always("bar".to_owned()))
|
|
|
|
|
|
|
|
.style(&"foo".to_owned(), &"bar".to_owned())
|
2018-09-18 17:04:03 -04:00
|
|
|
//.style(Box::new("foo".to_owned()), Box::new("bar".to_owned()))
|
|
|
|
//.style_signal(Box::new("foo".to_owned()), always(Box::new("bar".to_owned())))
|
2018-09-17 17:58:05 -04:00
|
|
|
|
|
|
|
.style_signal(&*FOO, always(&*FOO))
|
|
|
|
|
2018-09-18 17:04:03 -04:00
|
|
|
//.style(vec!["-moz-foo", "-webkit-foo", "foo"].as_slice(), vec!["bar"].as_slice())
|
2018-09-18 06:50:07 -04:00
|
|
|
.style_signal(RefFn::new(vec!["-moz-foo", "-webkit-foo", "foo"], |x| x.as_slice()), always(RefFn::new(vec!["bar"], |x| x.as_slice())))
|
2018-06-28 13:02:35 -04:00
|
|
|
|
|
|
|
.style_signal(["-moz-foo", "-webkit-foo", "foo"], always("bar"))
|
|
|
|
.style_signal(["-moz-foo", "-webkit-foo", "foo"], always("bar".to_owned()))
|
2018-09-17 17:17:55 -04:00
|
|
|
.style_signal(["-moz-foo", "-webkit-foo", "foo"], always("bar".to_owned()).map(|x| RefFn::new(x, |x| x.as_str())))
|
|
|
|
|
|
|
|
.style_signal(["-moz-foo", "-webkit-foo", "foo"], always(["bar", "qux"]))
|
|
|
|
.style_signal(["-moz-foo", "-webkit-foo", "foo"], always(["bar".to_owned(), "qux".to_owned()]))
|
2018-09-18 06:50:07 -04:00
|
|
|
|
|
|
|
//.style_signal(["-moz-foo", "-webkit-foo", "foo"], always(AsSlice::new(["foo", "bar"])))
|
|
|
|
//.style_signal(["-moz-foo", "-webkit-foo", "foo"], always(("bar".to_owned(), "qux".to_owned())).map(|x| RefFn::new(x, |x| AsSlice::new([x.0.as_str(), x.1.as_str()]))))
|
2018-06-13 19:48:41 -04:00
|
|
|
|
|
|
|
.style_signal("foo", always(Some("bar")))
|
|
|
|
.style_signal("foo", always(Some("bar".to_owned())))
|
2018-09-17 17:17:55 -04:00
|
|
|
.style_signal("foo", always("bar".to_owned()).map(|x| Some(RefFn::new(x, |x| x.as_str()))))
|
2018-06-28 13:02:35 -04:00
|
|
|
|
|
|
|
.style_signal(["-moz-foo", "-webkit-foo", "foo"], always(Some("bar")))
|
|
|
|
.style_signal(["-moz-foo", "-webkit-foo", "foo"], always(Some("bar".to_owned())))
|
2018-09-17 17:17:55 -04:00
|
|
|
.style_signal(["-moz-foo", "-webkit-foo", "foo"], always("bar".to_owned()).map(|x| Some(RefFn::new(x, |x| x.as_str()))))
|
2018-06-13 19:48:41 -04:00
|
|
|
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|