2020-02-04 18:27:32 +05:00
|
|
|
import Notebooks from "./notebooks";
|
|
|
|
|
import Notes from "./notes";
|
2020-02-05 20:57:43 +05:00
|
|
|
import Topic from "../models/topic";
|
2020-02-04 18:27:32 +05:00
|
|
|
|
|
|
|
|
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-06 16:46:23 +05:00
|
|
|
exists(topic) {
|
|
|
|
|
return this.all.findIndex(v => v.title === (topic.title || topic)) > -1;
|
|
|
|
|
}
|
|
|
|
|
|
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() {
|
2020-02-05 20:57:43 +05:00
|
|
|
return this.notebooks.notebook(this.notebookId).data.topics;
|
2020-02-04 18:27:32 +05:00
|
|
|
}
|
|
|
|
|
|
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) {
|
2020-02-06 16:46:23 +05:00
|
|
|
let allTopics = JSON.parse(JSON.stringify(this.all)); //FIXME: make a deep copy
|
|
|
|
|
for (let i = 0; i < allTopics.length; i++) {
|
|
|
|
|
let topic = allTopics[i];
|
2020-02-05 00:17:12 +05:00
|
|
|
if (!topic) continue;
|
2020-02-06 16:46:23 +05:00
|
|
|
let index = topics.findIndex(t => (t.title || t) === topic.title);
|
2020-02-05 01:12:36 +05:00
|
|
|
let t = this.topic(topic);
|
|
|
|
|
await t.transaction(() => t.delete(...topic.notes), false);
|
2020-02-06 16:46:23 +05:00
|
|
|
if (index > -1) {
|
|
|
|
|
allTopics.splice(i, 1);
|
|
|
|
|
}
|
2020-02-04 18:27:32 +05:00
|
|
|
}
|
|
|
|
|
await this.notebooks.add({
|
2020-02-06 16:46:23 +05:00
|
|
|
id: this.notebookId,
|
|
|
|
|
topics: allTopics
|
2020-02-04 18:27:32 +05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|