-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
note_group_controller.ts
84 lines (72 loc) · 2.07 KB
/
note_group_controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { removeFromArray, UuidString } from '@standardnotes/snjs';
import { NoteViewController } from '@/views/note_view/note_view_controller';
import { WebApplication } from '@/ui_models/application';
type NoteControllerGroupChangeCallback = () => void;
export class NoteGroupController {
public noteControllers: NoteViewController[] = [];
private application: WebApplication;
changeObservers: NoteControllerGroupChangeCallback[] = [];
constructor(application: WebApplication) {
this.application = application;
}
public deinit() {
(this.application as unknown) = undefined;
for (const controller of this.noteControllers) {
this.deleteNoteView(controller);
}
}
async createNoteView(
noteUuid?: string,
noteTitle?: string,
noteTag?: UuidString
) {
const controller = new NoteViewController(
this.application,
noteUuid,
noteTitle,
noteTag
);
await controller.initialize();
this.noteControllers.push(controller);
this.notifyObservers();
}
deleteNoteView(controller: NoteViewController) {
controller.deinit();
removeFromArray(this.noteControllers, controller);
}
closeNoteView(controller: NoteViewController) {
this.deleteNoteView(controller);
this.notifyObservers();
}
closeActiveNoteView() {
const activeController = this.activeNoteViewController;
if (activeController) {
this.deleteNoteView(activeController);
}
}
closeAllNoteViews() {
for (const controller of this.noteControllers) {
this.deleteNoteView(controller);
}
}
get activeNoteViewController() {
return this.noteControllers[0];
}
/**
* Notifies observer when the active controller has changed.
*/
public addChangeObserver(callback: NoteControllerGroupChangeCallback) {
this.changeObservers.push(callback);
if (this.activeNoteViewController) {
callback();
}
return () => {
removeFromArray(this.changeObservers, callback);
};
}
private notifyObservers() {
for (const observer of this.changeObservers) {
observer();
}
}
}