|
@@ -4,6 +4,7 @@ import { DateAdapter } from '@angular/material/core';
|
|
import { JournalEntry } from 'src/interfaces/interfaces';
|
|
import { JournalEntry } from 'src/interfaces/interfaces';
|
|
import localeDe from '@angular/common/locales/de';
|
|
import localeDe from '@angular/common/locales/de';
|
|
import { registerLocaleData } from '@angular/common';
|
|
import { registerLocaleData } from '@angular/common';
|
|
|
|
+import { DataService } from 'src/services/data/data.service';
|
|
|
|
|
|
@Component({
|
|
@Component({
|
|
selector: 'app-journal-notes',
|
|
selector: 'app-journal-notes',
|
|
@@ -12,25 +13,6 @@ import { registerLocaleData } from '@angular/common';
|
|
})
|
|
})
|
|
export class JournalNotesComponent implements OnInit, OnDestroy {
|
|
export class JournalNotesComponent implements OnInit, OnDestroy {
|
|
editor: Editor = new Editor();
|
|
editor: Editor = new Editor();
|
|
- html = '';
|
|
|
|
- isInEditMode = true;
|
|
|
|
- currentEntryIndex: number = 0;
|
|
|
|
- isNewEntry = false;
|
|
|
|
-
|
|
|
|
- public entries: JournalEntry[] = [
|
|
|
|
- {
|
|
|
|
- title: 'testtitle',
|
|
|
|
- content: 'testcontent',
|
|
|
|
- created: new Date(2024, 8, 29),
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- title: 'zweiter titel',
|
|
|
|
- content: 'zweiter content',
|
|
|
|
- created: new Date(2023, 1, 2),
|
|
|
|
- },
|
|
|
|
- ];
|
|
|
|
- currentEntry: JournalEntry = this.entries[this.currentEntryIndex];
|
|
|
|
-
|
|
|
|
toolbar: any = [
|
|
toolbar: any = [
|
|
// default value
|
|
// default value
|
|
['bold', 'italic'],
|
|
['bold', 'italic'],
|
|
@@ -38,25 +20,59 @@ export class JournalNotesComponent implements OnInit, OnDestroy {
|
|
[{ heading: ['h3', 'h4', 'h5', 'h6'] }],
|
|
[{ heading: ['h3', 'h4', 'h5', 'h6'] }],
|
|
];
|
|
];
|
|
|
|
|
|
- constructor(private _adapter: DateAdapter<any>) {
|
|
|
|
|
|
+ /** Used to show the interactale form or the display version of an entry. */
|
|
|
|
+ public isInEditMode = false;
|
|
|
|
+
|
|
|
|
+ /** The index of the currently active entry */
|
|
|
|
+ public currentEntryIndex: number = 0;
|
|
|
|
+
|
|
|
|
+ private backupIndex: number = -1;
|
|
|
|
+ /** Indicates, if the currentEntry is a newly generated one that is still not saved. */
|
|
|
|
+ public isNewEntry = false;
|
|
|
|
+
|
|
|
|
+ /**The array of JournalEntries, synched to the */
|
|
|
|
+ public entries: JournalEntry[] = [];
|
|
|
|
+
|
|
|
|
+ /** Holds the data for the current entry */
|
|
|
|
+ public currentEntry: JournalEntry = {
|
|
|
|
+ title: '',
|
|
|
|
+ content: '',
|
|
|
|
+ created: new Date(),
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ constructor(
|
|
|
|
+ private _adapter: DateAdapter<any>,
|
|
|
|
+ private dataService: DataService,
|
|
|
|
+ ) {
|
|
registerLocaleData(localeDe);
|
|
registerLocaleData(localeDe);
|
|
|
|
+ this.entries = this.dataService.notesData;
|
|
}
|
|
}
|
|
|
|
|
|
ngOnInit(): void {
|
|
ngOnInit(): void {
|
|
this._adapter.setLocale('de');
|
|
this._adapter.setLocale('de');
|
|
- }
|
|
|
|
|
|
+ this.entries = this.dataService.notesData;
|
|
|
|
+ console.log('JournalNotesComponent: ', this.entries);
|
|
|
|
|
|
- // make sure to destory the editor
|
|
|
|
- ngOnDestroy(): void {
|
|
|
|
- this.editor.destroy();
|
|
|
|
|
|
+ // if the list is empty, set the currentEntryIndex to -1 to hide the entry-container
|
|
|
|
+ if (this.entries.length === 0) {
|
|
|
|
+ this.currentEntryIndex = -1;
|
|
|
|
+ } else {
|
|
|
|
+ this.currentEntry = this.entries[0];
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- public activateEntry(index: number): void {
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the currentEntry variable when being clicked on in the entries list on the left.
|
|
|
|
+ * @param index The index of the selected entry.
|
|
|
|
+ */
|
|
|
|
+ public selectEntry(index: number): void {
|
|
this.currentEntryIndex = index;
|
|
this.currentEntryIndex = index;
|
|
- this.currentEntry = this.entries[index];
|
|
|
|
|
|
+ this.currentEntry = this.getEntryAt(index);
|
|
|
|
+ this.isNewEntry = false;
|
|
this.isInEditMode = false;
|
|
this.isInEditMode = false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // DONE
|
|
addEntry(): void {
|
|
addEntry(): void {
|
|
this.currentEntry = {
|
|
this.currentEntry = {
|
|
title: '',
|
|
title: '',
|
|
@@ -65,26 +81,37 @@ export class JournalNotesComponent implements OnInit, OnDestroy {
|
|
};
|
|
};
|
|
this.isNewEntry = true;
|
|
this.isNewEntry = true;
|
|
this.isInEditMode = true;
|
|
this.isInEditMode = true;
|
|
- // Hightlight no entry
|
|
|
|
|
|
+ // Hightlight no entry because the placeholder is shown as active
|
|
|
|
+ this.backupIndex = this.currentEntryIndex;
|
|
this.currentEntryIndex = -1;
|
|
this.currentEntryIndex = -1;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public editEntry(): void {
|
|
|
|
+ this.isInEditMode = true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // DONE
|
|
public saveEntry(): void {
|
|
public saveEntry(): void {
|
|
if (this.isNewEntry) {
|
|
if (this.isNewEntry) {
|
|
// Prepend the new JournalEntry
|
|
// Prepend the new JournalEntry
|
|
- this.entries = [this.currentEntry, ...this.entries];
|
|
|
|
|
|
+ this.entries.unshift(this.currentEntry);
|
|
this.isNewEntry = false;
|
|
this.isNewEntry = false;
|
|
this.currentEntryIndex = 0;
|
|
this.currentEntryIndex = 0;
|
|
}
|
|
}
|
|
this.isInEditMode = false;
|
|
this.isInEditMode = false;
|
|
|
|
+ this.entries[this.currentEntryIndex] = this.currentEntry;
|
|
|
|
+ this.uploadNotes();
|
|
}
|
|
}
|
|
|
|
|
|
public discardEntry(): void {
|
|
public discardEntry(): void {
|
|
if (this.isNewEntry) {
|
|
if (this.isNewEntry) {
|
|
- // this.currentEntryIndex = this.entries.length - 1;
|
|
|
|
|
|
+ this.currentEntryIndex = this.backupIndex;
|
|
this.isNewEntry = false;
|
|
this.isNewEntry = false;
|
|
}
|
|
}
|
|
- this.currentEntry = this.entries[this.currentEntryIndex];
|
|
|
|
|
|
+ if (this.entries.length > 0) {
|
|
|
|
+ // Reset the currentEntry to the last saved version
|
|
|
|
+ this.currentEntry = this.getEntryAt(this.currentEntryIndex);
|
|
|
|
+ }
|
|
this.isInEditMode = false;
|
|
this.isInEditMode = false;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -94,7 +121,21 @@ export class JournalNotesComponent implements OnInit, OnDestroy {
|
|
this.currentEntryIndex = -1;
|
|
this.currentEntryIndex = -1;
|
|
} else {
|
|
} else {
|
|
this.currentEntryIndex = Math.max(this.currentEntryIndex - 1, 0);
|
|
this.currentEntryIndex = Math.max(this.currentEntryIndex - 1, 0);
|
|
- this.currentEntry = this.entries[this.currentEntryIndex];
|
|
|
|
|
|
+ this.currentEntry = this.getEntryAt(this.currentEntryIndex);
|
|
}
|
|
}
|
|
|
|
+ this.uploadNotes();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private getEntryAt(index: number): JournalEntry {
|
|
|
|
+ return JSON.parse(JSON.stringify(this.entries[index]));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private uploadNotes(): void {
|
|
|
|
+ console.log('Uploading notes to the server: ', this.entries);
|
|
|
|
+ this.dataService.notesData = this.entries;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ngOnDestroy(): void {
|
|
|
|
+ this.editor.destroy();
|
|
}
|
|
}
|
|
}
|
|
}
|