Files
notesnook/packages/core/collections/topics.js

88 lines
2.3 KiB
JavaScript
Raw Normal View History

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-06 18:47:42 +05:00
async add(...topics) {
let notebook = this.notebooks.notebook(this.notebookId);
let uniqueTopics = [...notebook.data.topics, ...topics];
uniqueTopics = uniqueTopics.filter(
(v, i) =>
v &&
(v.title || v).trim().length > 0 &&
uniqueTopics.findIndex(t => (v.title || v) === (t.title || t)) === i
);
notebook.data.topics = [];
2020-02-06 18:54:35 +05:00
notebook.data.totalNotes = 0;
2020-02-06 18:47:42 +05:00
for (let topic of uniqueTopics) {
2020-02-06 18:54:35 +05:00
let t = makeTopic(topic, this.notebookId);
notebook.data.topics.push(t);
notebook.data.totalNotes += t.totalNotes;
2020-02-06 18:47:42 +05:00
}
await this.notebooks.collection.addItem(notebook.data);
2020-02-04 18:27:32 +05:00
}
2020-02-06 22:46:57 +05:00
/**
* @returns {Array} an array containing all the topics
*/
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-06 22:46:57 +05:00
/**
*
* @param {string | Object} topic can be an object or string containing the topic title.
* @returns {Topic} The topic by the given title
*/
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);
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
}
2020-02-06 18:47:42 +05:00
await this.notebooks.add({ id: this.notebookId, topics: allTopics });
2020-02-04 18:27:32 +05:00
}
}
2020-02-06 18:47:42 +05:00
function makeTopic(topic, notebookId) {
if (typeof topic !== "string") return topic;
return {
type: "topic",
notebookId,
title: topic,
dateCreated: Date.now(),
totalNotes: 0,
notes: []
};
}