2020-03-02 12:36:10 +05:00
|
|
|
import { qclone } from "qclone";
|
2020-02-05 20:57:43 +05:00
|
|
|
export default class Topic {
|
|
|
|
|
/**
|
|
|
|
|
* @param {Object} topic
|
2020-04-16 02:14:53 +05:00
|
|
|
* @param {string} notebookId
|
|
|
|
|
* @param {import('../api').default} db
|
2020-02-05 20:57:43 +05:00
|
|
|
*/
|
2020-04-16 02:14:53 +05:00
|
|
|
constructor(topic, notebookId, db) {
|
2020-02-22 21:53:56 +05:00
|
|
|
this._topic = topic;
|
2020-04-16 02:14:53 +05:00
|
|
|
this._db = db;
|
|
|
|
|
this._notebookId = notebookId;
|
2020-02-22 21:53:56 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get totalNotes() {
|
|
|
|
|
return this._topic.totalNotes;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
has(noteId) {
|
2020-04-12 11:04:30 +05:00
|
|
|
return this._topic.notes.findIndex((n) => n === noteId) > -1;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async add(...noteIds) {
|
2020-03-02 12:36:10 +05:00
|
|
|
const topic = qclone(this._topic);
|
2020-02-05 20:57:43 +05:00
|
|
|
for (let noteId of noteIds) {
|
2020-04-16 02:14:53 +05:00
|
|
|
let note = this._db.notes.note(noteId);
|
2020-03-23 13:22:28 +05:00
|
|
|
if (this.has(noteId) || !note || note.data.deleted) continue;
|
2020-02-22 21:53:56 +05:00
|
|
|
topic.notes.push(noteId);
|
2020-12-05 12:38:15 +05:00
|
|
|
|
2020-12-08 13:09:29 +05:00
|
|
|
let array = note.notebooks || [];
|
|
|
|
|
array = array.slice();
|
2020-12-07 10:15:53 +05:00
|
|
|
const notebookIndex = array.findIndex((nb) => nb.id === this._notebookId);
|
|
|
|
|
if (notebookIndex === -1) {
|
|
|
|
|
let notebook = {};
|
|
|
|
|
notebook.id = this._notebookId;
|
|
|
|
|
notebook.topics = [topic.id];
|
|
|
|
|
array.push(notebook);
|
|
|
|
|
} else {
|
|
|
|
|
const topicIndex = array[notebookIndex].topics.indexOf(topic.id);
|
|
|
|
|
if (topicIndex > -1) return;
|
|
|
|
|
array[notebookIndex].topics.push(topic.id);
|
|
|
|
|
}
|
2020-12-05 12:38:15 +05:00
|
|
|
|
2020-04-16 02:14:53 +05:00
|
|
|
await this._db.notes.add({
|
2020-02-05 20:57:43 +05:00
|
|
|
id: noteId,
|
2020-12-05 12:38:15 +05:00
|
|
|
notebooks: array,
|
2020-02-05 20:57:43 +05:00
|
|
|
});
|
2020-02-22 21:53:56 +05:00
|
|
|
topic.totalNotes++;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
2020-02-22 21:53:56 +05:00
|
|
|
return await this._save(topic);
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async delete(...noteIds) {
|
2020-03-03 22:10:13 +05:00
|
|
|
const topic = qclone(this._topic);
|
2020-02-05 20:57:43 +05:00
|
|
|
for (let noteId of noteIds) {
|
2020-12-05 12:38:15 +05:00
|
|
|
let note = this._db.notes.note(noteId);
|
2020-12-08 13:09:29 +05:00
|
|
|
if (!this.has(noteId) || !note || note.data.deleted || !note.notebooks)
|
|
|
|
|
return this;
|
2020-12-07 10:15:53 +05:00
|
|
|
let index = topic.notes.indexOf(noteId);
|
2020-02-22 21:53:56 +05:00
|
|
|
topic.notes.splice(index, 1);
|
2020-12-05 12:38:15 +05:00
|
|
|
|
2020-12-08 13:09:29 +05:00
|
|
|
let array = note.notebooks.slice();
|
2020-12-07 10:15:53 +05:00
|
|
|
const notebookIndex = array.findIndex((nb) => nb.id === this._notebookId);
|
|
|
|
|
if (notebookIndex === -1) return;
|
|
|
|
|
|
|
|
|
|
const topicIndex = array[notebookIndex].topics.indexOf(topic.id);
|
|
|
|
|
if (topicIndex === -1) return;
|
|
|
|
|
|
|
|
|
|
array[notebookIndex].topics.splice(topicIndex, 1);
|
|
|
|
|
if (array[notebookIndex].topics.length <= 0)
|
|
|
|
|
array.splice(notebookIndex, 1);
|
2020-12-05 12:38:15 +05:00
|
|
|
|
2020-04-16 02:14:53 +05:00
|
|
|
await this._db.notes.add({
|
2020-02-05 20:57:43 +05:00
|
|
|
id: noteId,
|
2020-12-05 12:38:15 +05:00
|
|
|
notebooks: array,
|
2020-02-05 20:57:43 +05:00
|
|
|
});
|
2020-02-22 21:53:56 +05:00
|
|
|
topic.totalNotes--;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
2020-02-22 21:53:56 +05:00
|
|
|
return await this._save(topic);
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
2020-02-22 21:53:56 +05:00
|
|
|
async _save(topic) {
|
2020-04-16 02:14:53 +05:00
|
|
|
await this._db.notebooks.notebook(this._notebookId).topics.add(topic);
|
2020-02-06 18:47:42 +05:00
|
|
|
return this;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get all() {
|
2020-02-22 21:53:56 +05:00
|
|
|
return this._topic.notes
|
2020-04-12 11:04:30 +05:00
|
|
|
.map((note) => {
|
2020-04-16 02:14:53 +05:00
|
|
|
let fullNote = this._db.notes.note(note);
|
2020-02-22 21:53:56 +05:00
|
|
|
if (fullNote) return fullNote.data;
|
|
|
|
|
})
|
2020-04-12 11:04:30 +05:00
|
|
|
.filter((v) => v);
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
}
|