use entity::note::{Model as Note};
use crate::backend::{BackendHandle, BroadcastMessage, NoteId};
+/// UI model for a notebook
#[derive(UiModel)]
pub struct Notebook {
pub doc_ref: UiDocRef<Notebook>,
pub selected_note: Option<UiDoc<crate::note::Note>>,
#[bind("notes")]
- pub notes: UiList<Note>
+ pub notes: UiList<NoteItem>
}
#[ui_actions]
pub fn set_notes(&mut self, notes: Vec<Note>) {
let notes_list = self.notes.data_mut();
notes_list.clear();
+
for note in notes {
- notes_list.push(note);
+ notes_list.push(NoteItem::new(note));
}
+
self.notes.update();
}
#[action]
pub fn note_selected(&mut self, event: &ActionEvent) {
if let EventType::ListSelection(s) = event.event_type {
- if let Some(note) = s.selected_element(self.notes.data()) {
- self.detach_current_note();
- self.selected_note = None;
-
- // Create the new note
- let note_data = crate::note::Note::new(self.collection_id, self.backend.clone());
-
- // Create the note document object
- let note_doc = UiDoc::new2(note_data, |n,d| {
- // TODO: remove this when toolkit is not that buggy
- // for some reason we have to attach the doc first, before we can
- // set the text
- if let Some(mut nb) = self.doc_ref.get_doc() {
- nb.ctx.attach(&d);
- }
+ self.detach_current_note();
+ self.selected_note = None;
+ if let Some(note) = s.selected_element_mut(self.notes.data_mut()) {
+ let doc = if let Some(doc) = ¬e.model {
+ doc.clone()
+ } else {
+ // Create the new note
+ let note_data = crate::note::Note::new(self.collection_id, self.backend.clone());
+
+ // Create the note document object
+ UiDoc::new2(note_data, |n,d| {
+ // TODO: remove this when toolkit is not that buggy
+ // for some reason we have to attach the doc first, before we can
+ // set the text
+ if let Some(mut nb) = self.doc_ref.get_doc() {
+ nb.ctx.attach(&d);
+ }
- n.init_from_model(¬e);
-
- let proxy = d.doc_proxy();
- self.backend.get_note_content(note.note_id, move|result|{
- proxy.call_mainthread(|doc, note|{
- match result {
- Ok(content) => {
- if let Some(content) = content {
- note.init_content(&content);
- } else {
- println!("note {}: no content", note.note_id);
+ n.init_from_model(¬e.data);
+
+ let proxy = d.doc_proxy();
+ self.backend.get_note_content(note.data.note_id, move|result|{
+ proxy.call_mainthread(|doc, note|{
+ match result {
+ Ok(content) => {
+ if let Some(content) = content {
+ note.init_content(&content);
+ } else {
+ println!("note {}: no content", note.note_id);
+ }
+ },
+ Err(e) => {
+ println!("Cannot get note content: {}", e);
}
- },
- Err(e) => {
- println!("Cannot get note content: {}", e);
}
- }
+ });
});
- });
- });
+ })
+ };
- // Attach the new note
if let Some(mut nb) = self.doc_ref.get_doc() {
- nb.ctx.attach(¬e_doc);
- self.selected_note = Some(note_doc);
+ nb.ctx.attach(&doc);
+ self.selected_note = Some(doc);
}
}
}
}
+ pub fn load_note(&self, note: &entity::note::Model) -> UiDoc<crate::note::Note> {
+ // Create the new note
+ let note_data = crate::note::Note::new(self.collection_id, self.backend.clone());
+
+ // Create the note document object
+ UiDoc::new2(note_data, |n,d| {
+ // TODO: remove this when toolkit is not that buggy
+ // for some reason we have to attach the doc first, before we can
+ // set the text
+ if let Some(mut nb) = self.doc_ref.get_doc() {
+ nb.ctx.attach(&d);
+ }
+
+ n.init_from_model(note);
+
+ let proxy = d.doc_proxy();
+ self.backend.get_note_content(note.note_id, move|result|{
+ proxy.call_mainthread(|doc, note|{
+ match result {
+ Ok(content) => {
+ if let Some(content) = content {
+ note.init_content(&content);
+ } else {
+ println!("note {}: no content", note.note_id);
+ }
+ },
+ Err(e) => {
+ println!("Cannot get note content: {}", e);
+ }
+ }
+ });
+ });
+ })
+ }
+
#[action]
pub fn message(&mut self, _event: &ActionEvent) {
while let Ok(msg) = self.broadcast_rx.try_recv() {
if let NoteId::Id(id) = u.note_id {
let note_data = self.notes.data_mut();
for (i, note) in note_data.iter_mut().enumerate() {
- if note.note_id == id {
+ if note.data.note_id == id {
// update note
if let Some(title) = &u.title {
- note.title = title.clone();
+ note.data.title = title.clone();
}
update_rows.push(i);
}
}
}
-pub fn notelist_getvalue<'a>(elm: &Note, col: i32, _row: i32) -> ListValue<'a> {
+pub fn notelist_getvalue<'a>(elm: &NoteItem, col: i32, _row: i32) -> ListValue<'a> {
match col {
- 0 => ListValue::String(elm.title.clone()),
- 1 => ListValue::String(elm.lastmodified.to_string()),
+ 0 => ListValue::String(elm.data.title.clone()),
+ 1 => ListValue::String(elm.data.lastmodified.to_string()),
_ => ListValue::None
}
}
fn drop(&mut self) {
}
+}
+
+/// ListView item
+pub struct NoteItem {
+ id: NoteId,
+ data: entity::note::Model,
+ model: Option<UiDoc<crate::note::Note>>,
+}
+
+impl NoteItem {
+ pub fn new(data: entity::note::Model) -> Self {
+ NoteItem {
+ id: NoteId::Id(data.note_id),
+ data: data,
+ model: None
+ }
+ }
}
\ No newline at end of file