Skip to content

Commit

Permalink
move class* methods to base-trait, added class_or
Browse files Browse the repository at this point in the history
  • Loading branch information
aclueless committed Jul 24, 2021
1 parent f8a03b6 commit 85662d6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 57 deletions.
46 changes: 46 additions & 0 deletions src/dom/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,52 @@ pub trait AttributeSetter {
.expect_throw("Unable to set string attribute");
}
}

fn add_class(&mut self, class_name: &str) {
self.ws_element()
.class_list()
.add_1(class_name)
.expect_throw("Unable to add new class");
}

fn remove_class(&mut self, class_name: &str) {
self.ws_element()
.class_list()
.remove_1(class_name)
.expect_throw("Unable to remove old class");
}

fn _class(&mut self, class_name: &str) {
let (changed, old_value) = self.check_str_attribute_and_return_old_value(class_name);
if let Some(old_value) = old_value {
self.remove_class(&old_value);
}
if changed {
self.add_class(class_name);
}
}

fn _class_if(&mut self, class_name: &str, class_on: bool) {
if self.check_bool_attribute(class_on) {
if class_on {
self.add_class(class_name);
} else {
self.remove_class(class_name);
}
}
}

fn _class_or(&mut self, first: bool, first_class: &str, second_class: &str) {
if self.check_bool_attribute(first) {
if first {
self.add_class(first_class);
self.remove_class(second_class);
} else {
self.remove_class(first_class);
self.add_class(second_class);
}
}
}
}

macro_rules! create_methods_for_events {
Expand Down
34 changes: 8 additions & 26 deletions src/dom/html/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,36 +192,18 @@ where
}

fn class(mut self, class_name: &str) -> Self {
let (changed, old_value) = self.check_str_attribute_and_return_old_value(class_name);
if let Some(old_value) = old_value {
self.ws_element()
.class_list()
.remove_1(&old_value)
.expect_throw("Unable to remove old class");
}
if changed {
self.ws_element()
.class_list()
.add_1(class_name)
.expect_throw("Unable to add new class");
}
self._class(class_name);
self
}

fn class_if(mut self, class_name: &str, class_on: bool) -> Self {
if self.check_bool_attribute(class_on) {
if class_on {
self.ws_element()
.class_list()
.add_1(class_name)
.expect_throw("Unable to add class");
} else {
self.ws_element()
.class_list()
.remove_1(class_name)
.expect_throw("Unable to remove class");
}
}
self._class_if(class_name, class_on);
self
}

/// Set the `first_class` if `first` is true, otherwise, set the `second_class`
fn class_or(mut self, first: bool, first_class: &str, second_class: &str) -> Self {
self._class_or(first, first_class, second_class);
self
}

Expand Down
40 changes: 9 additions & 31 deletions src/dom/svg/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use wasm_bindgen::UnwrapThrowExt;
//use wasm_bindgen::UnwrapThrowExt;

pub trait SvgAttributeSetter<C>: Sized + crate::dom::attributes::AttributeSetter {
fn bool_attr(mut self, name: &str, value: bool) -> Self {
Expand Down Expand Up @@ -283,41 +283,19 @@ pub trait SvgAttributeSetter<C>: Sized + crate::dom::attributes::AttributeSetter
str zoom_and_pan "zoomAndPan"
}

// Copied from crate::dom::html::attributes
// TODO: Simimilar code should be reuse
fn class(mut self, class_name: &str) -> Self {
let (changed, old_value) = self.check_str_attribute_and_return_old_value(class_name);
if let Some(old_value) = old_value {
self.ws_element()
.class_list()
.remove_1(&old_value)
.expect_throw("Unable to remove old class");
}
if changed {
self.ws_element()
.class_list()
.add_1(class_name)
.expect_throw("Unable to add new class");
}
self._class(class_name);
self
}

// Copied from crate::dom::html::attributes
// TODO: Simimilar code should be reuse
fn class_if(mut self, class_name: &str, class_on: bool) -> Self {
if self.check_bool_attribute(class_on) {
if class_on {
self.ws_element()
.class_list()
.add_1(class_name)
.expect_throw("Unable to add class");
} else {
self.ws_element()
.class_list()
.remove_1(class_name)
.expect_throw("Unable to remove class");
}
}
self._class_if(class_name, class_on);
self
}

/// Set the `first_class` if `first` is true, otherwise, set the `second_class`.
fn class_or(mut self, first: bool, first_class: &str, second_class: &str) -> Self {
self._class_or(first, first_class, second_class);
self
}
}
Expand Down

0 comments on commit 85662d6

Please sign in to comment.