-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Tag.js
182 lines (148 loc) · 4.73 KB
/
Tag.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const BaseModel = require('lib/BaseModel.js');
const BaseItem = require('lib/models/BaseItem.js');
const NoteTag = require('lib/models/NoteTag.js');
const Note = require('lib/models/Note.js');
const { _ } = require('lib/locale');
class Tag extends BaseItem {
static tableName() {
return 'tags';
}
static modelType() {
return BaseModel.TYPE_TAG;
}
static async noteIds(tagId) {
let rows = await this.db().selectAll('SELECT note_id FROM note_tags WHERE tag_id = ?', [tagId]);
let output = [];
for (let i = 0; i < rows.length; i++) {
output.push(rows[i].note_id);
}
return output;
}
static async notes(tagId, options = null) {
if (options === null) options = {};
let noteIds = await this.noteIds(tagId);
if (!noteIds.length) return [];
return Note.previews(
null,
Object.assign({}, options, {
conditions: [`id IN ("${noteIds.join('","')}")`],
})
);
}
// Untag all the notes and delete tag
static async untagAll(tagId) {
const noteTags = await NoteTag.modelSelectAll('SELECT id FROM note_tags WHERE tag_id = ?', [tagId]);
for (let i = 0; i < noteTags.length; i++) {
await NoteTag.delete(noteTags[i].id);
}
await Tag.delete(tagId);
}
static async delete(id, options = null) {
if (!options) options = {};
await super.delete(id, options);
this.dispatch({
type: 'TAG_DELETE',
id: id,
});
}
static async addNote(tagId, noteId) {
let hasIt = await this.hasNote(tagId, noteId);
if (hasIt) return;
const output = await NoteTag.save({
tag_id: tagId,
note_id: noteId,
});
this.dispatch({
type: 'TAG_UPDATE_ONE',
item: await Tag.loadWithCount(tagId),
});
return output;
}
static async removeNote(tagId, noteId) {
let noteTags = await NoteTag.modelSelectAll('SELECT id FROM note_tags WHERE tag_id = ? and note_id = ?', [tagId, noteId]);
for (let i = 0; i < noteTags.length; i++) {
await NoteTag.delete(noteTags[i].id);
}
this.dispatch({
type: 'NOTE_TAG_REMOVE',
item: await Tag.load(tagId),
});
}
static loadWithCount(tagId) {
let sql = 'SELECT * FROM tags_with_note_count WHERE id = ?';
return this.modelSelectOne(sql, [tagId]);
}
static async hasNote(tagId, noteId) {
let r = await this.db().selectOne('SELECT note_id FROM note_tags WHERE tag_id = ? AND note_id = ? LIMIT 1', [tagId, noteId]);
return !!r;
}
static async allWithNotes() {
return await Tag.modelSelectAll('SELECT * FROM tags_with_note_count');
}
static async searchAllWithNotes(options) {
if (!options) options = {};
if (!options.conditions) options.conditions = [];
options.conditions.push('id IN (SELECT distinct id FROM tags_with_note_count)');
return this.search(options);
}
static async tagsByNoteId(noteId) {
const tagIds = await NoteTag.tagIdsByNoteId(noteId);
return this.modelSelectAll(`SELECT * FROM tags WHERE id IN ("${tagIds.join('","')}")`);
}
static async loadByTitle(title) {
return this.loadByField('title', title, { caseInsensitive: true });
}
static async addNoteTagByTitle(noteId, tagTitle) {
let tag = await this.loadByTitle(tagTitle);
if (!tag) tag = await Tag.save({ title: tagTitle }, { userSideValidation: true });
return await this.addNote(tag.id, noteId);
}
static async setNoteTagsByTitles(noteId, tagTitles) {
const previousTags = await this.tagsByNoteId(noteId);
const addedTitles = [];
for (let i = 0; i < tagTitles.length; i++) {
const title = tagTitles[i].trim().toLowerCase();
if (!title) continue;
let tag = await this.loadByTitle(title);
if (!tag) tag = await Tag.save({ title: title }, { userSideValidation: true });
await this.addNote(tag.id, noteId);
addedTitles.push(title);
}
for (let i = 0; i < previousTags.length; i++) {
if (addedTitles.indexOf(previousTags[i].title.toLowerCase()) < 0) {
await this.removeNote(previousTags[i].id, noteId);
}
}
}
static async setNoteTagsByIds(noteId, tagIds) {
const previousTags = await this.tagsByNoteId(noteId);
const addedIds = [];
for (let i = 0; i < tagIds.length; i++) {
const tagId = tagIds[i];
await this.addNote(tagId, noteId);
addedIds.push(tagId);
}
for (let i = 0; i < previousTags.length; i++) {
if (addedIds.indexOf(previousTags[i].id) < 0) {
await this.removeNote(previousTags[i].id, noteId);
}
}
}
static async save(o, options = null) {
if (options && options.userSideValidation) {
if ('title' in o) {
o.title = o.title.trim().toLowerCase();
const existingTag = await Tag.loadByTitle(o.title);
if (existingTag && existingTag.id !== o.id) throw new Error(_('The tag "%s" already exists. Please choose a different name.', o.title));
}
}
return super.save(o, options).then(tag => {
this.dispatch({
type: 'TAG_UPDATE_ONE',
item: tag,
});
return tag;
});
}
}
module.exports = Tag;