Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Clone, Debug)]
- pub struct Visit {
- pub country: String,
- pub start: NaiveDateTime,
- pub end: Option<NaiveDateTime>,
- pub expand: bool,
- }
- impl Visit {
- pub fn new(country: String, start: NaiveDateTime, end: Option<NaiveDateTime>) -> Visit {
- Visit {
- country,
- start,
- end,
- expand: false,
- }
- }
- pub fn start_to_string(&self) -> String {
- self.start.format("%d %b %Y").to_string()
- }
- pub fn end_to_string(&self) -> String {
- match self.end {
- Some(x) => x.format("%d %b %Y").to_string(),
- _ => "".to_string(),
- }
- }
- }
- impl PartialOrd for Visit {
- fn partial_cmp(&self, other: &Visit) -> Option<Ordering> {
- Some(self.cmp(other))
- }
- }
- impl Ord for Visit {
- fn cmp(&self, other: &Visit) -> Ordering {
- if self.country == other.country {
- self.start.date().cmp(&other.start.date())
- } else {
- self.country.cmp(&other.country)
- }
- }
- }
- impl PartialEq for Visit {
- fn eq(&self, other: &Visit) -> bool {
- self.country == other.country &&
- self.start.date() == other.start.date() &&
- match (self.end, other.end) {
- (Some(x), Some(y)) => x.date() == y.date(),
- (None, None) => true,
- _ => false,
- }
- }
- }
- impl Eq for Visit {}
- pub trait VisitsMethods {
- fn dom(&self)
- -> Dom<CountryApplication>;
- }
- pub type Visits = Vec<Visit>;
- impl VisitsMethods for Visits {
- fn dom(&self)
- -> Dom<CountryApplication> {
- let sorted = self.iter().fold(BTreeMap::new(), |mut m, c| {
- m.entry(c.country.clone())
- .or_insert_with(Vec::new)
- .push(c);
- m
- });
- let mut dom = Dom::new(NodeType::Div)
- .with_class("__country-table");
- for (key, visits) in sorted {
- let mut tmp = Dom::new(NodeType::Div)
- .with_child(Label::new(key.clone()).dom()
- .with_id(key)
- .with_hit_test(On::MouseUp))
- .with_callback(On::MouseUp, Callback(toggle_expand));
- for visit in visits {
- if visit.expand {
- let mut tmp2 = Dom::new(NodeType::Div)
- .with_class("__country-row");
- tmp2.add_child(Label::new(visit.start_to_string()).dom());
- tmp2.add_child(Label::new(visit.end_to_string()).dom());
- tmp.add_child(tmp2);
- }
- }
- dom.add_child(tmp);
- }
- dom
- }
- }
- fn toggle_expand(app_state: &mut AppState<CountryApplication>, event: WindowEvent<CountryApplication>) -> UpdateScreen {
- let (x, clicked_row_id) = match event.get_first_hit_child(event.hit_dom_node, On::MouseUp) {
- Some(s) => s,
- None => return UpdateScreen::DontRedraw,
- };
- println!("{:?} {:?}", x, clicked_row_id);
- app_state.data.modify(|state| if let Some(ref mut visits) = state.visits {
- for visit in visits {
- if visit.country == clicked_row_id.to_string() {
- visit.expand = true;
- }
- }
- });
- UpdateScreen::Redraw
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement