This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
/
modelManager.js
160 lines (134 loc) · 4.17 KB
/
modelManager.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import Storage from "./storageManager"
import "../../models/extend/item.js";
SFModelManager.ContentTypeClassMapping = {
"Note" : SNNote,
"Tag" : SNTag,
"SN|Theme" : SNTheme,
"SN|Component" : SNComponent
};
export default class ModelManager extends SFModelManager {
static instance = null;
static get() {
if (this.instance == null) {
this.instance = new ModelManager();
}
return this.instance;
}
constructor() {
super();
this.notes = [];
this.tags = [];
this.themes = [];
this.acceptableContentTypes = ["Note", "Tag", "SN|Theme", "SN|Component"];
}
handleSignout() {
super.handleSignout();
this.notes.length = 0;
this.tags.length = 0;
this.themes.length = 0;
}
addItems(items, globalOnly = false) {
super.addItems(items, globalOnly);
items.forEach((item) => {
// In some cases, you just want to add the item to this.items, and not to the individual arrays
// This applies when you want to keep an item syncable, but not display it via the individual arrays
if(!globalOnly) {
if(item.content_type == "Tag") {
if(!_.find(this.tags, {uuid: item.uuid})) {
this.tags.splice(_.sortedIndexBy(this.tags, item, function(item){
if (item.title) return item.title.toLowerCase();
else return ''
}), 0, item);
}
} else if(item.content_type == "Note") {
if(!_.find(this.notes, {uuid: item.uuid})) {
this.notes.unshift(item);
}
} else if(item.content_type == "SN|Theme") {
if(!_.find(this.themes, {uuid: item.uuid})) {
this.themes.unshift(item);
}
}
}
});
}
async removeItemLocally(item) {
await super.removeItemLocally(item);
if(item.content_type == "Tag") {
_.remove(this.tags, {uuid: item.uuid});
} else if(item.content_type == "Note") {
_.remove(this.notes, {uuid: item.uuid});
} else if(item.content_type == "SN|Theme") {
_.remove(this.themes, {uuid: item.uuid});
}
return Storage.get().deleteModel(item);
}
getNotes(options = {}) {
var notes;
var tags = [];
// if(options.selectedTagIds && options.selectedTagIds.length > 0 && options.selectedTagIds[0].key !== "all") {
if(options.selectedTagIds && options.selectedTagIds.length > 0) {
tags = ModelManager.get().findItems(options.selectedTagIds);
if(tags.length > 0) {
var taggedNotes = new Set();
for(var tag of tags) {
taggedNotes = new Set([...taggedNotes, ...new Set(tag.notes)])
}
notes = Array.from(taggedNotes);
}
}
if(!notes) {
notes = this.notes;
}
var searchTerm = options.searchTerm;
if(searchTerm) {
searchTerm = searchTerm.toLowerCase();
notes = notes.filter(function(note){
return note.safeTitle().toLowerCase().includes(searchTerm) || note.safeText().toLowerCase().includes(searchTerm);
})
}
var sortBy = options.sortBy;
notes = notes.filter(function(note){
if(note.deleted) {
return false;
}
if(options.archivedOnly) {
return note.archived;
} else {
return !note.archived;
}
})
let sortValueFn = (a, b, pinCheck = false) => {
if(!pinCheck) {
if(a.pinned && b.pinned) {
return sortValueFn(a, b, true);
}
if(a.pinned) { return -1; }
if(b.pinned) { return 1; }
}
var aValue = a[sortBy] || "";
var bValue = b[sortBy] || "";
let vector = 1;
if(sortBy == "title") {
aValue = aValue.toLowerCase();
bValue = bValue.toLowerCase();
if(aValue.length == 0 && bValue.length == 0) {
return 0;
} else if(aValue.length == 0 && bValue.length != 0) {
return 1;
} else if(aValue.length != 0 && bValue.length == 0) {
return -1;
} else {
vector = -1;
}
}
if(aValue > bValue) { return -1 * vector;}
else if(aValue < bValue) { return 1 * vector;}
return 0;
}
notes = notes.sort(function(a, b){
return sortValueFn(a, b);
})
return {notes: notes, tags: tags};
}
}