#[action]
pub fn new_note(&mut self, _event: &ActionEvent) {
println!("new note");
-
+
let item = NoteItem::new(new_note_id());
self.notes.data_mut().insert(0, item);
self.notes.update();
}
}
+/// SourceList sublist item
+pub struct NotebookItem {
+ pub data: entity::collection::Model,
+ pub model: Option<UiDoc<Notebook>>
+}
+
+impl NotebookItem {
+ pub fn from_model(data: entity::collection::Model) -> Self {
+ NotebookItem {
+ data: data,
+ model: None
+ }
+ }
+
+ pub fn load_notebook(&mut self, backend: &BackendHandle) -> UiDoc<Notebook> {
+ // Create Notebook UI model
+ let notebook = Notebook::new(self.data.collection_id, backend);
+ let doc = UiDoc::new2(notebook, |notebook, doc| {
+ notebook.doc_ref = doc.doc_ref();
+ });
+
+ // Load notes
+ let proxy = doc.doc_proxy();
+ backend.get_notes(self.data.collection_id, |result|{
+ proxy.call_mainthread(|_, nb| {
+ match result {
+ Ok(notes) => {
+ nb.set_notes(notes);
+ },
+ Err(err) => {
+ eprintln!("get_notes failed: {}", err);
+ }
+ }
+ });
+ });
+
+ self.model = Some(doc.clone());
+
+ doc
+ }
+}
+
/// ListView item
pub struct NoteItem {
id: NoteId,
use entity::collection::{Model as Collection, Node};
use crate::backend::{BackendHandle, BroadcastMessage};
use crate::newnotebook::new_notebook_dialog;
-use crate::notebook::{notelist_getvalue, NoteItem, Notebook};
+use crate::notebook::{notelist_getvalue, NoteItem, Notebook, NotebookItem};
#[derive(UiModel)]
pub struct MainWindow {
groups: Vec<Collection>,
#[bind]
- notebooks: UiSourceList<Collection>
+ notebooks: UiSourceList<NotebookItem>
}
// TODO: maybe select a configured default notebook
if let Some(group) = self.notebooks.get(0) {
if let Some(notebook) = group.data().get(0) {
- self.select_notebook(notebook.collection_id);
+ self.select_notebook(notebook.data.collection_id);
// self.selected_notebook has a value after select_notebook()
if let Some(doc) = &self.selected_notebook {
// Now that a notebook is selected, we can open the new note UI there
let sublist_data = notebook.list().data_mut();
for child in elm.children.iter() {
- sublist_data.push(child.collection.clone());
+ sublist_data.push(NotebookItem::from_model(child.collection.clone()));
}
self.notebooks.push(notebook);
b.fill(true);
b.value(&data.notebooks);
b.getvalue(|elm,_row,item|{
- item.label = Some(elm.name.clone());
+ item.label = Some(elm.data.name.clone());
});
b.onactivate(|e|{
if let EventType::SubList(sl) = &e.event_type {
- if let Some(nb) = sl.get_from(&e.data.notebooks) {
- e.data.select_notebook(nb.collection_id);
+ if let Some(nb) = sl.get_mut_from(&mut e.data.notebooks) {
+ // detach current notebook
+ if let Some(current) = &e.data.selected_notebook {
+ e.obj.ctx.detach(current);
+ }
+
+ let notebook_doc = if let Some(doc) = &nb.model {
+ doc.clone()
+ } else {
+ let doc = nb.load_notebook(&e.data.backend);
+ doc
+ };
+
+ e.obj.ctx.attach(¬ebook_doc);
+ e.data.selected_notebook = Some(notebook_doc);
}
}
});