mirror of
https://github.com/yewprint/yewprint
synced 2024-11-22 03:23:03 +00:00
Move Tree's doc to tree module
This commit is contained in:
parent
35b77edda9
commit
792c31ac86
2 changed files with 132 additions and 97 deletions
99
src/app.rs
99
src/app.rs
|
@ -3,8 +3,7 @@ use crate::collapse::Collapse;
|
|||
use crate::forms::controls::Switch;
|
||||
use crate::icon::*;
|
||||
use crate::menu::*;
|
||||
use crate::tree::*;
|
||||
use crate::Intent;
|
||||
use crate::tree::doc::*;
|
||||
use yew::prelude::*;
|
||||
|
||||
const DARK_BG_COLOR: &str = "#30404d";
|
||||
|
@ -17,9 +16,6 @@ pub struct App {
|
|||
counter: i64,
|
||||
dark_theme: bool,
|
||||
collapsed: bool,
|
||||
tree: TreeData<i32>,
|
||||
callback_expand_node: Callback<(NodeId, MouseEvent)>,
|
||||
callback_select_node: Callback<(NodeId, MouseEvent)>,
|
||||
doc_menu: DocMenu,
|
||||
}
|
||||
|
||||
|
@ -27,8 +23,6 @@ pub enum Msg {
|
|||
AddOne,
|
||||
ToggleLight,
|
||||
ToggleCollapse,
|
||||
ExpandNode(NodeId),
|
||||
SelectNode(NodeId),
|
||||
GoToMenu(DocMenu),
|
||||
}
|
||||
|
||||
|
@ -37,74 +31,10 @@ impl Component for App {
|
|||
type Properties = ();
|
||||
|
||||
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
|
||||
let mut tree = TreeBuilder::new().build();
|
||||
let root_id = tree
|
||||
.insert(
|
||||
Node::new(NodeData {
|
||||
data: 0,
|
||||
..Default::default()
|
||||
}),
|
||||
InsertBehavior::AsRoot,
|
||||
)
|
||||
.unwrap();
|
||||
let dir1 = tree
|
||||
.insert(
|
||||
Node::new(NodeData {
|
||||
icon: Some(IconName::FolderClose),
|
||||
label: "Big directory".into(),
|
||||
has_caret: true,
|
||||
data: 1,
|
||||
..Default::default()
|
||||
}),
|
||||
InsertBehavior::UnderNode(&root_id),
|
||||
)
|
||||
.unwrap();
|
||||
for i in 0..10 {
|
||||
let dir2 = tree
|
||||
.insert(
|
||||
Node::new(NodeData {
|
||||
icon: Some(IconName::FolderClose),
|
||||
label: format!("Directory {}", i + 1).into(),
|
||||
has_caret: true,
|
||||
data: 1,
|
||||
..Default::default()
|
||||
}),
|
||||
InsertBehavior::UnderNode(&dir1),
|
||||
)
|
||||
.unwrap();
|
||||
for i in 0..10 {
|
||||
tree.insert(
|
||||
Node::new(NodeData {
|
||||
icon: Some(IconName::Document),
|
||||
label: format!("File {}", i + 1).into(),
|
||||
data: i,
|
||||
..Default::default()
|
||||
}),
|
||||
InsertBehavior::UnderNode(&dir2),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
tree.insert(
|
||||
Node::new(NodeData {
|
||||
icon: Some(IconName::Tag),
|
||||
icon_intent: Some(Intent::Primary),
|
||||
label: "Outer file".into(),
|
||||
secondary_label: Some(html!(<Icon icon=IconName::EyeOpen />)),
|
||||
data: 3,
|
||||
..Default::default()
|
||||
}),
|
||||
InsertBehavior::UnderNode(&root_id),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
App {
|
||||
counter: 0,
|
||||
dark_theme: true,
|
||||
collapsed: true,
|
||||
tree: tree.into(),
|
||||
callback_expand_node: link.callback(|(node_id, _)| Msg::ExpandNode(node_id)),
|
||||
callback_select_node: link.callback(|(node_id, _)| Msg::SelectNode(node_id)),
|
||||
doc_menu: DocMenu::Tree,
|
||||
link,
|
||||
}
|
||||
|
@ -115,22 +45,6 @@ impl Component for App {
|
|||
Msg::AddOne => self.counter += 1,
|
||||
Msg::ToggleLight => self.dark_theme ^= true,
|
||||
Msg::ToggleCollapse => self.collapsed ^= true,
|
||||
Msg::ExpandNode(node_id) => {
|
||||
let mut tree = self.tree.borrow_mut();
|
||||
let node = tree.get_mut(&node_id).unwrap();
|
||||
let data = node.data_mut();
|
||||
data.is_expanded ^= true;
|
||||
data.icon = Some(if data.is_expanded {
|
||||
IconName::FolderOpen
|
||||
} else {
|
||||
IconName::FolderClose
|
||||
})
|
||||
}
|
||||
Msg::SelectNode(node_id) => {
|
||||
let mut tree = self.tree.borrow_mut();
|
||||
let node = tree.get_mut(&node_id).unwrap();
|
||||
node.data_mut().is_selected ^= true;
|
||||
}
|
||||
Msg::GoToMenu(doc_menu) => {
|
||||
self.doc_menu = doc_menu;
|
||||
}
|
||||
|
@ -214,16 +128,7 @@ impl Component for App {
|
|||
</Collapse>
|
||||
</div>
|
||||
},
|
||||
DocMenu::Tree => html! {
|
||||
<div>
|
||||
<Tree<i32>
|
||||
tree=self.tree.clone()
|
||||
on_collapse=Some(self.callback_expand_node.clone())
|
||||
on_expand=Some(self.callback_expand_node.clone())
|
||||
onclick=Some(self.callback_select_node.clone())
|
||||
/>
|
||||
</div>
|
||||
},
|
||||
DocMenu::Tree => html!(<TreeDoc />),
|
||||
DocMenu::Icon => html! {
|
||||
<div>
|
||||
<Icon icon=IconName::Print />
|
||||
|
|
130
src/tree.rs
130
src/tree.rs
|
@ -362,3 +362,133 @@ impl Component for TreeNode {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "dev")]
|
||||
pub mod doc {
|
||||
use super::*;
|
||||
|
||||
pub struct TreeDoc {
|
||||
tree: TreeData<i32>,
|
||||
callback_expand_node: Callback<(NodeId, MouseEvent)>,
|
||||
callback_select_node: Callback<(NodeId, MouseEvent)>,
|
||||
}
|
||||
|
||||
pub enum Msg {
|
||||
ExpandNode(NodeId),
|
||||
SelectNode(NodeId),
|
||||
}
|
||||
|
||||
impl Component for TreeDoc {
|
||||
type Message = Msg;
|
||||
type Properties = ();
|
||||
|
||||
fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self {
|
||||
let mut tree = TreeBuilder::new().build();
|
||||
let root_id = tree
|
||||
.insert(
|
||||
Node::new(NodeData {
|
||||
data: 0,
|
||||
..Default::default()
|
||||
}),
|
||||
InsertBehavior::AsRoot,
|
||||
)
|
||||
.unwrap();
|
||||
let dir1 = tree
|
||||
.insert(
|
||||
Node::new(NodeData {
|
||||
icon: Some(IconName::FolderClose),
|
||||
label: "Big directory".into(),
|
||||
has_caret: true,
|
||||
data: 1,
|
||||
..Default::default()
|
||||
}),
|
||||
InsertBehavior::UnderNode(&root_id),
|
||||
)
|
||||
.unwrap();
|
||||
for i in 0..10 {
|
||||
let dir2 = tree
|
||||
.insert(
|
||||
Node::new(NodeData {
|
||||
icon: Some(IconName::FolderClose),
|
||||
label: format!("Directory {}", i + 1).into(),
|
||||
has_caret: true,
|
||||
data: 1,
|
||||
..Default::default()
|
||||
}),
|
||||
InsertBehavior::UnderNode(&dir1),
|
||||
)
|
||||
.unwrap();
|
||||
for i in 0..10 {
|
||||
tree.insert(
|
||||
Node::new(NodeData {
|
||||
icon: Some(IconName::Document),
|
||||
label: format!("File {}", i + 1).into(),
|
||||
data: i,
|
||||
..Default::default()
|
||||
}),
|
||||
InsertBehavior::UnderNode(&dir2),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
tree.insert(
|
||||
Node::new(NodeData {
|
||||
icon: Some(IconName::Tag),
|
||||
icon_intent: Some(Intent::Primary),
|
||||
label: "Outer file".into(),
|
||||
secondary_label: Some(html!(<Icon icon=IconName::EyeOpen />)),
|
||||
data: 3,
|
||||
..Default::default()
|
||||
}),
|
||||
InsertBehavior::UnderNode(&root_id),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
Self {
|
||||
tree: tree.into(),
|
||||
callback_expand_node: link.callback(|(node_id, _)| Msg::ExpandNode(node_id)),
|
||||
callback_select_node: link.callback(|(node_id, _)| Msg::SelectNode(node_id)),
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
||||
match msg {
|
||||
Msg::ExpandNode(node_id) => {
|
||||
let mut tree = self.tree.borrow_mut();
|
||||
let node = tree.get_mut(&node_id).unwrap();
|
||||
let data = node.data_mut();
|
||||
data.is_expanded ^= true;
|
||||
data.icon = Some(if data.is_expanded {
|
||||
IconName::FolderOpen
|
||||
} else {
|
||||
IconName::FolderClose
|
||||
})
|
||||
}
|
||||
Msg::SelectNode(node_id) => {
|
||||
let mut tree = self.tree.borrow_mut();
|
||||
let node = tree.get_mut(&node_id).unwrap();
|
||||
node.data_mut().is_selected ^= true;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
fn change(&mut self, _props: Self::Properties) -> ShouldRender {
|
||||
true
|
||||
}
|
||||
|
||||
fn view(&self) -> Html {
|
||||
html! {
|
||||
<div>
|
||||
<Tree<i32>
|
||||
tree=self.tree.clone()
|
||||
on_collapse=Some(self.callback_expand_node.clone())
|
||||
on_expand=Some(self.callback_expand_node.clone())
|
||||
onclick=Some(self.callback_select_node.clone())
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue