import { Injectable } from '@angular/core' ; import * as loki from 'lokijs' ; import * as moment from 'moment' ; import { Utils } from '../core/utils' ; import { Note } from './entities/note' ; import { Notebook } from './entities/notebook' ; @ Injectable ( ) export class DataStore { constructor ( ) { } private db : loki ; private notebooks : any ; private notes : any ; private isLoaded : boolean = false ; private databaseFile : string ; private databaseLoaded ( ) { let mustSaveDatabase = false ; this . notebooks = this . db . getCollection ( 'notebooks' ) ; if ( ! this . notebooks ) { this . notebooks = this . db . addCollection ( 'notebooks' ) ; mustSaveDatabase = true ; } this . notes = this . db . getCollection ( 'notes' ) ; if ( ! this . notes ) { this . notes = this . db . addCollection ( 'notes' ) ; mustSaveDatabase = true ; } if ( mustSaveDatabase ) { this . db . saveDatabase ( ) ; } this . isLoaded = true ; } private loadDatabase ( databaseFile ) { this . isLoaded = false ; this . databaseFile = databaseFile ; this . db = new loki ( databaseFile , { autoload : true , autoloadCallback : this . databaseLoaded . bind ( this ) , } ) ; } public async initializeAsync ( databaseFile ) { this . loadDatabase ( databaseFile ) ; while ( ! this . isLoaded ) { await Utils . sleep ( 100 ) ; } } public getNotebooks ( ) { const notebooks = this . notebooks . chain ( ) . sort ( Utils . caseInsensitiveNameSort ) . data ( ) ; return notebooks ; } public getNotebookById ( id ) { return this . notebooks . findOne ( { id : id } ) ; } public getNotebookByName ( name ) { return this . notebooks . findOne ( { name : name } ) ; } public addNotebook ( name ) { const newNotebook = new Notebook ( name ) ; this . notebooks . insert ( newNotebook ) ; this . db . saveDatabase ( ) ; return newNotebook . id ; } public deleteNotebook ( id ) { const notebookToDelete = this . getNotebookById ( id ) ; this . notebooks . remove ( notebookToDelete ) ; this . db . saveDatabase ( ) ; } public getNotes ( ) { const notes = this . notes . chain ( ) . where ( ( obj ) => { return ! obj . isTrashed ; } ) . simplesort ( 'modificationDate' , true ) . data ( ) ; return notes ; } public getTrashedNotes ( ) { const notes = this . notes . chain ( ) . where ( ( obj ) => { return obj . isTrashed ; } ) . simplesort ( 'trashedDate' , true ) . data ( ) ; return notes ; } public getUnfiledNotes ( ) { const notebookIds = this . notebooks . chain ( ) . data ( ) . map ( ( x ) => x . id ) ; const notes = this . notes . chain ( ) . where ( ( obj ) => { return ! obj . isTrashed && ( obj . notebookId === '' || ! notebookIds . includes ( obj . notebookId ) ) ; } ) . simplesort ( 'modificationDate' , true ) . data ( ) ; return notes ; } public getMarkedNotes ( ) { const notes = this . notes . chain ( ) . find ( { isMarked : true } ) . where ( ( obj ) => { return ! obj . isTrashed ; } ) . simplesort ( 'modificationDate' , true ) . data ( ) ; return notes ; } public getNotebookNotes ( notebookId ) { const notes = this . notes . chain ( ) . find ( { notebookId : notebookId } ) . where ( ( obj ) => { return ! obj . isTrashed ; } ) . simplesort ( 'modificationDate' , true ) . data ( ) ; return notes ; } public getNotesWithIdenticalBaseTitle ( baseTitle ) { const notesWithIdenticalBaseTitle = this . notes . chain ( ) . where ( ( obj ) => { return obj . title . startsWith ( baseTitle ) ; } ) . data ( ) ; return notesWithIdenticalBaseTitle ; } public addNote ( title , notebookId ) { const newNote = new Note ( title , notebookId ) ; this . notes . insert ( newNote ) ; this . db . saveDatabase ( ) ; return newNote . id ; } public getNoteById ( id ) { const note = this . notes . findOne ( { id : id } ) ; return note ; } public getNoteByTitle ( noteTitle ) { return this . notes . findOne ( { title : noteTitle } ) ; } public deleteNote ( id ) { const noteToDelete = this . getNoteById ( id ) ; this . notes . remove ( noteToDelete ) ; this . db . saveDatabase ( ) ; } public trashNote ( id ) { const noteToTrash = this . getNoteById ( id ) ; noteToTrash . isTrashed = true ; noteToTrash . isSelected = false ; noteToTrash . trashedDate = moment ( ) . valueOf ( ) ; this . db . saveDatabase ( ) ; } public restoreNote ( id ) { const noteToRestore = this . getNoteById ( id ) ; noteToRestore . isTrashed = false ; noteToRestore . isSelected = false ; noteToRestore . trashedDate = 0 ; this . db . saveDatabase ( ) ; } public updateNote ( note ) { this . notes . update ( note ) ; note . modificationDate = moment ( ) . valueOf ( ) ; this . db . saveDatabase ( ) ; } public updateNoteWithoutDate ( note ) { this . notes . update ( note ) ; this . db . saveDatabase ( ) ; } public updateNotebook ( notebook ) { this . notebooks . update ( notebook ) ; this . db . saveDatabase ( ) ; } }