Advertisement
Guest User

Untitled

a guest
Nov 11th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.32 KB | None | 0 0
  1. #[derive(Clone, Debug)]
  2. pub struct Visit {
  3.     pub country: String,
  4.     pub start: NaiveDateTime,
  5.     pub end: Option<NaiveDateTime>,
  6.     pub expand: bool,
  7. }
  8.  
  9. impl Visit {
  10.     pub fn new(country: String, start: NaiveDateTime, end: Option<NaiveDateTime>) -> Visit {
  11.         Visit {
  12.             country,
  13.             start,
  14.             end,
  15.             expand: false,
  16.         }
  17.     }
  18.  
  19.     pub fn start_to_string(&self) -> String {
  20.         self.start.format("%d %b %Y").to_string()
  21.     }
  22.  
  23.     pub fn end_to_string(&self) -> String {
  24.         match self.end {
  25.             Some(x) => x.format("%d %b %Y").to_string(),
  26.             _ => "".to_string(),
  27.         }
  28.     }
  29. }
  30.  
  31. impl PartialOrd for Visit {
  32.     fn partial_cmp(&self, other: &Visit) -> Option<Ordering> {
  33.         Some(self.cmp(other))
  34.     }
  35. }
  36.  
  37. impl Ord for Visit {
  38.     fn cmp(&self, other: &Visit) -> Ordering {
  39.         if self.country == other.country {
  40.             self.start.date().cmp(&other.start.date())
  41.         } else {
  42.             self.country.cmp(&other.country)
  43.         }
  44.     }
  45. }
  46.  
  47. impl PartialEq for Visit {
  48.     fn eq(&self, other: &Visit) -> bool {
  49.         self.country == other.country &&
  50.         self.start.date() == other.start.date() &&
  51.         match (self.end, other.end) {
  52.             (Some(x), Some(y)) => x.date() == y.date(),
  53.             (None, None) => true,
  54.             _ => false,
  55.         }
  56.     }
  57. }
  58.  
  59. impl Eq for Visit {}
  60.  
  61. pub trait VisitsMethods {
  62.     fn dom(&self)
  63.     -> Dom<CountryApplication>;
  64. }
  65.  
  66. pub type Visits = Vec<Visit>;
  67.  
  68. impl VisitsMethods for Visits {
  69.     fn dom(&self)
  70.     -> Dom<CountryApplication> {
  71.         let sorted = self.iter().fold(BTreeMap::new(), |mut m, c| {
  72.             m.entry(c.country.clone())
  73.                 .or_insert_with(Vec::new)
  74.                 .push(c);
  75.             m
  76.         });
  77.  
  78.         let mut dom = Dom::new(NodeType::Div)
  79.                         .with_class("__country-table");
  80.         for (key, visits) in sorted {
  81.             let mut tmp = Dom::new(NodeType::Div)
  82.                 .with_child(Label::new(key.clone()).dom()
  83.                             .with_id(key)
  84.                             .with_hit_test(On::MouseUp))
  85.                 .with_callback(On::MouseUp, Callback(toggle_expand));
  86.             for visit in visits {
  87.                 if visit.expand {
  88.                     let mut tmp2 = Dom::new(NodeType::Div)
  89.                     .with_class("__country-row");
  90.                     tmp2.add_child(Label::new(visit.start_to_string()).dom());
  91.                     tmp2.add_child(Label::new(visit.end_to_string()).dom());
  92.                     tmp.add_child(tmp2);
  93.                 }
  94.             }
  95.             dom.add_child(tmp);
  96.         }
  97.         dom
  98.     }
  99. }
  100.  
  101. fn toggle_expand(app_state: &mut AppState<CountryApplication>, event: WindowEvent<CountryApplication>) -> UpdateScreen {
  102.     let (x, clicked_row_id) = match event.get_first_hit_child(event.hit_dom_node, On::MouseUp) {
  103.         Some(s) => s,
  104.         None => return UpdateScreen::DontRedraw,
  105.     };
  106.  
  107.     println!("{:?} {:?}", x, clicked_row_id);
  108.     app_state.data.modify(|state| if let Some(ref mut visits) = state.visits {
  109.         for visit in visits {
  110.             if visit.country == clicked_row_id.to_string() {
  111.                 visit.expand = true;
  112.             }
  113.         }
  114.     });
  115.     UpdateScreen::Redraw
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement