Adding in attr and prop aliases

This commit is contained in:
Pauan 2021-05-27 23:54:52 +02:00
parent 5cd4ff8553
commit 089a073913
2 changed files with 115 additions and 34 deletions

View File

@ -200,10 +200,10 @@ fn make_text_signal<A, B>(callbacks: &mut Callbacks, value: B) -> Text
let element = element.clone();
callbacks.after_remove(for_each(value, move |value| {
let value = value.as_str();
// TODO maybe this should intern ?
bindings::set_text(&element, value);
value.with_str(|value| {
// TODO maybe this should intern ?
bindings::set_text(&element, value);
});
}));
}
@ -520,6 +520,12 @@ impl<A> DomBuilder<A> where A: Into<Node> {
}
impl<A> DomBuilder<A> where A: AsRef<JsValue> {
#[inline]
pub fn prop<B, C>(self, name: B, value: C) -> Self where B: MultiStr, C: Into<JsValue> {
self.property(name, value)
}
/// The same as the [`prop`](#method.prop) method.
#[inline]
pub fn property<B, C>(self, name: B, value: C) -> Self where B: MultiStr, C: Into<JsValue> {
set_property(&self.element, &name, value);
@ -541,6 +547,15 @@ impl<A> DomBuilder<A> where A: AsRef<JsValue> {
}));
}
#[inline]
pub fn prop_signal<B, C, D>(self, name: B, value: D) -> Self
where B: MultiStr + 'static,
C: Into<JsValue>,
D: Signal<Item = C> + 'static {
self.property_signal(name, value)
}
/// The same as the [`prop_signal`](#method.prop_signal) method.
#[inline]
pub fn property_signal<B, C, D>(mut self, name: B, value: D) -> Self
where B: MultiStr + 'static,
@ -573,19 +588,6 @@ impl<A> DomBuilder<A> where A: AsRef<EventTarget> {
}
impl<A> DomBuilder<A> where A: AsRef<Node> {
#[inline]
pub fn child<B: BorrowMut<Dom>>(mut self, mut child: B) -> Self {
operations::insert_children_one(self.element.as_ref(), &mut self.callbacks, child.borrow_mut());
self
}
// TODO figure out how to make this owned rather than &mut
#[inline]
pub fn children<B: BorrowMut<Dom>, C: IntoIterator<Item = B>>(mut self, children: C) -> Self {
operations::insert_children_iter(self.element.as_ref(), &mut self.callbacks, children);
self
}
#[inline]
pub fn text(self, value: &str) -> Self {
// TODO should this intern ?
@ -604,10 +606,8 @@ impl<A> DomBuilder<A> where A: AsRef<Node> {
}
#[inline]
pub fn children_signal_vec<B>(mut self, children: B) -> Self
where B: SignalVec<Item = Dom> + 'static {
operations::insert_children_signal_vec(self.element.as_ref().clone(), &mut self.callbacks, children);
pub fn child<B: BorrowMut<Dom>>(mut self, mut child: B) -> Self {
operations::insert_children_one(self.element.as_ref(), &mut self.callbacks, child.borrow_mut());
self
}
@ -618,6 +618,21 @@ impl<A> DomBuilder<A> where A: AsRef<Node> {
operations::insert_child_signal(self.element.as_ref().clone(), &mut self.callbacks, child);
self
}
// TODO figure out how to make this owned rather than &mut
#[inline]
pub fn children<B: BorrowMut<Dom>, C: IntoIterator<Item = B>>(mut self, children: C) -> Self {
operations::insert_children_iter(self.element.as_ref(), &mut self.callbacks, children);
self
}
#[inline]
pub fn children_signal_vec<B>(mut self, children: B) -> Self
where B: SignalVec<Item = Dom> + 'static {
operations::insert_children_signal_vec(self.element.as_ref().clone(), &mut self.callbacks, children);
self
}
}
impl<A> DomBuilder<A> where A: AsRef<Element> {
@ -628,6 +643,12 @@ impl<A> DomBuilder<A> where A: AsRef<Element> {
DomBuilder::new(shadow)
}
#[inline]
pub fn attr<B>(self, name: B, value: &str) -> Self where B: MultiStr {
self.attribute(name, value)
}
/// The same as the [`attr`](#method.attr) method.
#[inline]
pub fn attribute<B>(self, name: B, value: &str) -> Self where B: MultiStr {
let element = self.element.as_ref();
@ -641,6 +662,12 @@ impl<A> DomBuilder<A> where A: AsRef<Element> {
self
}
#[inline]
pub fn attr_ns<B>(self, namespace: &str, name: B, value: &str) -> Self where B: MultiStr {
self.attribute_namespace(namespace, name, value)
}
/// The same as the [`attr_ns`](#method.attr_ns) method.
#[inline]
pub fn attribute_namespace<B>(self, namespace: &str, name: B, value: &str) -> Self where B: MultiStr {
let element = self.element.as_ref();
@ -690,11 +717,11 @@ impl<A> DomBuilder<A> where A: AsRef<Element> {
set_option(self.element.as_ref().clone(), &mut self.callbacks, value, move |element, value| {
match value {
Some(value) => {
let value = value.as_str();
name.each(|name| {
// TODO should this intern the value ?
bindings::set_attribute(element, intern(name), &value);
value.with_str(|value| {
name.each(|name| {
// TODO should this intern the value ?
bindings::set_attribute(element, intern(name), &value);
});
});
},
None => {
@ -706,6 +733,16 @@ impl<A> DomBuilder<A> where A: AsRef<Element> {
});
}
#[inline]
pub fn attr_signal<B, C, D, E>(self, name: B, value: E) -> Self
where B: MultiStr + 'static,
C: AsStr,
D: OptionStr<Output = C>,
E: Signal<Item = D> + 'static {
self.attribute_signal(name, value)
}
/// The same as the [`attr_signal`](#method.attr_signal) method.
#[inline]
pub fn attribute_signal<B, C, D, E>(mut self, name: B, value: E) -> Self
where B: MultiStr + 'static,
@ -731,11 +768,11 @@ impl<A> DomBuilder<A> where A: AsRef<Element> {
set_option(self.element.as_ref().clone(), &mut self.callbacks, value, move |element, value| {
match value {
Some(value) => {
let value = value.as_str();
name.each(|name| {
// TODO should this intern the value ?
bindings::set_attribute_ns(element, &namespace, intern(name), &value);
value.with_str(|value| {
name.each(|name| {
// TODO should this intern the value ?
bindings::set_attribute_ns(element, &namespace, intern(name), &value);
});
});
},
None => {
@ -747,6 +784,16 @@ impl<A> DomBuilder<A> where A: AsRef<Element> {
});
}
#[inline]
pub fn attr_ns_signal<B, C, D, E>(self, namespace: &str, name: B, value: E) -> Self
where B: MultiStr + 'static,
C: AsStr,
D: OptionStr<Output = C>,
E: Signal<Item = D> + 'static {
self.attribute_namespace_signal(namespace, name, value)
}
/// The same as the [`attr_ns_signal`](#method.attr_ns_signal) method.
#[inline]
pub fn attribute_namespace_signal<B, C, D, E>(mut self, namespace: &str, name: B, value: E) -> Self
where B: MultiStr + 'static,
@ -824,6 +871,7 @@ impl<A> DomBuilder<A> where A: AsRef<Element> {
});
}
// TODO rename to scroll_x_signal ?
#[inline]
pub fn scroll_left_signal<B>(mut self, signal: B) -> Self where B: Signal<Item = Option<i32>> + 'static {
// TODO bindings function for this ?
@ -831,6 +879,7 @@ impl<A> DomBuilder<A> where A: AsRef<Element> {
self
}
// TODO rename to scroll_y_signal ?
#[inline]
pub fn scroll_top_signal<B>(mut self, signal: B) -> Self where B: Signal<Item = Option<i32>> + 'static {
// TODO bindings function for this ?

View File

@ -27,14 +27,26 @@ impl<A, F> Mixin<A> for F where F: FnOnce(A) -> A {
// TODO figure out a way to implement this for all of AsRef / Borrow / etc.
// TODO implementations for &String and &mut String
pub trait AsStr {
#[deprecated(since = "0.5.18", note = "Use with_str instead")]
fn as_str(&self) -> &str;
fn with_str<A, F>(&self, f: F) -> A where F: FnOnce(&str) -> A {
#[allow(deprecated)]
f(self.as_str())
}
}
impl<'a, A> AsStr for &'a A where A: AsStr {
#[inline]
fn as_str(&self) -> &str {
#[allow(deprecated)]
AsStr::as_str(*self)
}
#[inline]
fn with_str<B, F>(&self, f: F) -> B where F: FnOnce(&str) -> B {
AsStr::with_str(*self, f)
}
}
impl AsStr for String {
@ -42,6 +54,11 @@ impl AsStr for String {
fn as_str(&self) -> &str {
self
}
#[inline]
fn with_str<A, F>(&self, f: F) -> A where F: FnOnce(&str) -> A {
f(&self)
}
}
impl AsStr for str {
@ -49,6 +66,11 @@ impl AsStr for str {
fn as_str(&self) -> &str {
self
}
#[inline]
fn with_str<A, F>(&self, f: F) -> A where F: FnOnce(&str) -> A {
f(self)
}
}
impl<'a> AsStr for &'a str {
@ -56,6 +78,11 @@ impl<'a> AsStr for &'a str {
fn as_str(&self) -> &str {
self
}
#[inline]
fn with_str<A, F>(&self, f: F) -> A where F: FnOnce(&str) -> A {
f(self)
}
}
impl<A, C> AsStr for RefFn<A, str, C> where C: Fn(&A) -> &str {
@ -63,6 +90,11 @@ impl<A, C> AsStr for RefFn<A, str, C> where C: Fn(&A) -> &str {
fn as_str(&self) -> &str {
self.call_ref()
}
#[inline]
fn with_str<B, F>(&self, f: F) -> B where F: FnOnce(&str) -> B {
f(self.call_ref())
}
}
@ -80,8 +112,8 @@ pub trait MultiStr {
impl<A> MultiStr for A where A: AsStr {
#[inline]
fn find_map<B, F>(&self, mut f: F) -> Option<B> where F: FnMut(&str) -> Option<B> {
f(self.as_str())
fn find_map<B, F>(&self, f: F) -> Option<B> where F: FnMut(&str) -> Option<B> {
self.with_str(f)
}
}
@ -106,7 +138,7 @@ macro_rules! array_multi_str {
impl<A> MultiStr for [A; $size] where A: AsStr {
#[inline]
fn find_map<B, F>(&self, mut f: F) -> Option<B> where F: FnMut(&str) -> Option<B> {
self.iter().find_map(|x| f(x.as_str()))
self.iter().find_map(|x| x.with_str(|x| f(x)))
}
}
};