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

127 lines
3.2 KiB
JavaScript
Raw Normal View History

2020-02-05 20:57:43 +05:00
import Topic from "../models/topic";
import { qclone } from "qclone";
import id from "../utils/id";
2020-02-04 18:27:32 +05:00
export default class Topics {
/**
*
2020-04-16 02:14:53 +05:00
* @param {import('../api').default} db
2020-02-04 18:27:32 +05:00
* @param {string} notebookId
*/
2020-04-16 02:14:53 +05:00
constructor(notebookId, db) {
this._db = db;
this._notebookId = notebookId;
2020-02-04 18:27:32 +05:00
}
has(topic) {
return (
this.all.findIndex(
(v) => v.id === topic || v.title === (topic.title || topic)
) > -1
);
2020-02-06 16:46:23 +05:00
}
2020-09-28 12:12:57 +05:00
/* _dedupe(source) {
let length = source.length,
seen = new Map();
for (let index = 0; index < length; index++) {
let value = source[index];
2020-03-11 12:04:37 +05:00
if (value.id) {
2020-09-28 12:12:57 +05:00
seen.set(value.id, {
...seen.get(value.id),
2020-03-11 12:04:37 +05:00
...value,
});
continue;
}
let title = value.title || value;
if (title.trim().length <= 0) continue;
seen.set(title, value);
2020-02-06 18:47:42 +05:00
}
return seen;
2020-09-28 12:12:57 +05:00
} */
async add(...topics) {
2020-04-16 02:14:53 +05:00
let notebook = qclone(this._db.notebooks.notebook(this._notebookId).data);
2020-03-11 12:04:37 +05:00
let allTopics = [...notebook.topics, ...topics];
notebook.topics = [];
notebook.totalNotes = 0;
for (let t of allTopics) {
let topic = makeTopic(t, this._notebookId);
2020-09-28 12:12:57 +05:00
if (notebook.topics.findIndex((_topic) => _topic.title === t) > -1)
continue;
2020-09-28 12:12:57 +05:00
if (topic.title.length <= 0) continue;
2020-09-28 12:12:57 +05:00
if (topics.findIndex((t) => topic.id === t.id) > -1)
topic.dateEdited = Date.now();
2020-09-28 12:12:57 +05:00
let index = notebook.topics.findIndex((t) => t.id === topic.id);
if (index > -1) {
notebook.totalNotes -= notebook.topics[index].totalNotes;
2020-09-28 12:12:57 +05:00
notebook.topics[index] = {
...notebook.topics[index],
...topic,
};
} else {
notebook.topics.push(topic);
}
notebook.totalNotes += topic.totalNotes;
}
2020-04-16 02:14:53 +05:00
return this._db.notebooks._collection.addItem(notebook);
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-04-16 02:14:53 +05:00
return this._db.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") {
topic = this.all.find((t) => t.id === topic || t.title === topic);
2020-02-04 18:27:32 +05:00
}
2020-02-05 00:17:12 +05:00
if (!topic) return;
2020-04-16 02:14:53 +05:00
return new Topic(topic, this._notebookId, this._db);
2020-02-04 18:27:32 +05:00
}
2020-05-16 14:02:59 +05:00
async delete(...topicIds) {
let allTopics = qclone(this.all); //FIXME: make a deep copy
2020-02-06 16:46:23 +05:00
for (let i = 0; i < allTopics.length; i++) {
let topic = allTopics[i];
2020-02-05 00:17:12 +05:00
if (!topic) continue;
2020-05-16 14:02:59 +05:00
let index = topicIds.findIndex((id) => topic.id === id);
let t = this.topic(topic);
await t.delete(...topic.notes);
await this._db.settings.unpin(topic.id);
2020-02-06 16:46:23 +05:00
if (index > -1) {
allTopics.splice(i, 1);
}
2020-02-04 18:27:32 +05:00
}
2020-04-16 02:14:53 +05:00
await this._db.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;
2020-02-06 18:47:42 +05:00
return {
type: "topic",
id: id(), //topic,
2020-02-06 18:47:42 +05:00
notebookId,
2020-09-28 12:12:57 +05:00
title: topic.trim(),
2020-02-06 18:47:42 +05:00
dateCreated: Date.now(),
dateEdited: Date.now(),
2020-02-06 18:47:42 +05:00
totalNotes: 0,
notes: [],
2020-02-06 18:47:42 +05:00
};
}