2020-02-04 18:27:32 +05:00
|
|
|
import Notebooks from "./notebooks";
|
|
|
|
|
import Notes from "./notes";
|
|
|
|
|
|
|
|
|
|
export default class Topics {
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {Notebooks} notebooks
|
2020-02-05 00:17:12 +05:00
|
|
|
* @param {Notes} notes
|
2020-02-04 18:27:32 +05:00
|
|
|
* @param {string} notebookId
|
|
|
|
|
*/
|
2020-02-05 00:17:12 +05:00
|
|
|
constructor(notebooks, notes, notebookId) {
|
2020-02-04 18:27:32 +05:00
|
|
|
this.notebooks = notebooks;
|
|
|
|
|
this.notebookId = notebookId;
|
2020-02-05 00:17:12 +05:00
|
|
|
this.notes = notes;
|
2020-02-04 18:27:32 +05:00
|
|
|
}
|
|
|
|
|
|
2020-02-05 00:17:12 +05:00
|
|
|
async add(topic) {
|
|
|
|
|
await this.notebooks.add({
|
2020-02-04 18:27:32 +05:00
|
|
|
id: this.notebookId,
|
|
|
|
|
topics: [topic]
|
|
|
|
|
});
|
2020-02-05 00:17:12 +05:00
|
|
|
return this.topic(topic);
|
2020-02-04 18:27:32 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get all() {
|
|
|
|
|
return this.notebooks.get(this.notebookId).topics;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-05 00:17:12 +05:00
|
|
|
topic(topic) {
|
2020-02-04 18:27:32 +05:00
|
|
|
if (typeof topic === "string") {
|
2020-02-05 00:17:12 +05:00
|
|
|
topic = this.all.find(t => t.title === topic);
|
2020-02-04 18:27:32 +05:00
|
|
|
}
|
2020-02-05 00:17:12 +05:00
|
|
|
if (!topic) return;
|
|
|
|
|
return new Topic(this, topic);
|
2020-02-04 18:27:32 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async delete(...topics) {
|
|
|
|
|
let notebook = this.notebooks.get(this.notebookId);
|
|
|
|
|
for (let topic of topics) {
|
2020-02-05 00:17:12 +05:00
|
|
|
if (!topic) continue;
|
|
|
|
|
let index = notebook.topics.findIndex(
|
|
|
|
|
t => t.title === topic.title || topic
|
|
|
|
|
);
|
2020-02-04 18:27:32 +05:00
|
|
|
if (index <= -1) continue;
|
2020-02-05 01:12:36 +05:00
|
|
|
topic = notebook.topics[index];
|
|
|
|
|
let t = this.topic(topic);
|
|
|
|
|
await t.transaction(() => t.delete(...topic.notes), false);
|
2020-02-04 18:27:32 +05:00
|
|
|
notebook.topics.splice(index, 1);
|
|
|
|
|
}
|
|
|
|
|
await this.notebooks.add({
|
|
|
|
|
id: notebook.id,
|
|
|
|
|
topics: notebook.topics
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-05 00:17:12 +05:00
|
|
|
|
|
|
|
|
class Topic {
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {Topics} topics
|
|
|
|
|
* @param {Object} topic
|
|
|
|
|
*/
|
|
|
|
|
constructor(topics, topic) {
|
|
|
|
|
this.topic = topic;
|
|
|
|
|
this.topics = topics;
|
|
|
|
|
this.transactionOpen = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-05 01:12:36 +05:00
|
|
|
transaction(ops, saveAfter = true) {
|
2020-02-05 00:17:12 +05:00
|
|
|
this.transactionOpen = true;
|
2020-02-05 01:12:36 +05:00
|
|
|
ops().then(() => {
|
2020-02-05 00:17:12 +05:00
|
|
|
this.transactionOpen = false;
|
|
|
|
|
});
|
2020-02-05 01:12:36 +05:00
|
|
|
if (!saveAfter) return this;
|
|
|
|
|
return this.save();
|
2020-02-05 00:17:12 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
has(noteId) {
|
|
|
|
|
return this.topic.notes.findIndex(n => n === noteId) > -1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-05 01:12:36 +05:00
|
|
|
async add(...noteIds) {
|
|
|
|
|
for (let noteId of noteIds) {
|
|
|
|
|
let note = this.topics.notes.get(noteId);
|
|
|
|
|
if (this.has(noteId) || !note) return this;
|
|
|
|
|
|
|
|
|
|
this.topic.notes.push(noteId);
|
|
|
|
|
|
|
|
|
|
if (note.notebook && note.notebook.id && note.notebook.topic) {
|
|
|
|
|
if (
|
|
|
|
|
note.notebook.id === this.topics.notebookId &&
|
|
|
|
|
note.notebook.topic === this.topic.title
|
|
|
|
|
)
|
|
|
|
|
return this;
|
|
|
|
|
await this.topics.notebooks
|
|
|
|
|
.topics(note.notebook.id)
|
|
|
|
|
.topic(note.notebook.topic)
|
|
|
|
|
.delete(note.id);
|
|
|
|
|
}
|
|
|
|
|
await this.topics.notes.add({
|
|
|
|
|
id: noteId,
|
|
|
|
|
notebook: { id: this.topics.notebookId, topic: this.topic.title }
|
|
|
|
|
});
|
2020-02-05 00:17:12 +05:00
|
|
|
}
|
|
|
|
|
return await this.save();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-05 01:12:36 +05:00
|
|
|
async delete(...noteIds) {
|
|
|
|
|
for (let noteId of noteIds) {
|
|
|
|
|
if (!this.has(noteId)) return this;
|
|
|
|
|
let index = this.topic.notes.findIndex(n => n === noteId);
|
|
|
|
|
this.topic.notes.splice(index, 1);
|
|
|
|
|
await this.topics.notes.add({
|
|
|
|
|
id: noteId,
|
|
|
|
|
notebook: {}
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-02-05 00:17:12 +05:00
|
|
|
return await this.save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
save() {
|
|
|
|
|
if (this.transactionOpen) return this;
|
|
|
|
|
return this.topics.add(this.topic);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get all() {
|
|
|
|
|
return this.topic.notes.map(note => this.topics.notes.get(note));
|
|
|
|
|
}
|
|
|
|
|
}
|