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

107 lines
2.7 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";
import { qclone } from "qclone";
2020-02-04 18:27:32 +05:00
export default class Topics {
/**
*
* @param {Notebooks} notebooks
* @param {string} notebookId
*/
constructor(notebooks, notebookId) {
this._notebooks = notebooks;
this._notebookId = notebookId;
2020-02-04 18:27:32 +05:00
}
has(topic) {
2020-02-06 16:46:23 +05:00
return this.all.findIndex(v => v.title === (topic.title || topic)) > -1;
}
_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) {
seen.set(value.id, {
...seen.get(value.id),
...value,
id: value.title
});
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;
}
async add(...topics) {
let notebook = qclone(this._notebooks.notebook(this._notebookId).data);
2020-03-11 12:04:37 +05:00
let allTopics = [...notebook.topics, ...topics];
const unique = this._dedupe(allTopics);
notebook.topics = [];
notebook.totalNotes = 0;
unique.forEach(t => {
let topic = makeTopic(t, this._notebookId);
notebook.topics.push(topic);
notebook.totalNotes += topic.totalNotes;
});
return this._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() {
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) {
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-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
}
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, dateEdited: Date.now() };
2020-02-06 18:47:42 +05:00
return {
type: "topic",
2020-03-11 12:04:37 +05:00
id: topic,
2020-02-06 18:47:42 +05:00
notebookId,
title: topic,
dateCreated: Date.now(),
dateEdited: Date.now(),
2020-02-06 18:47:42 +05:00
totalNotes: 0,
notes: []
};
}