2020-02-05 20:57:43 +05:00
|
|
|
import Topic from "../models/topic";
|
2020-02-07 04:39:28 +05:00
|
|
|
import { qclone } from "qclone";
|
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;
|
2020-02-22 21:53:56 +05:00
|
|
|
this._notebookId = notebookId;
|
2020-02-04 18:27:32 +05:00
|
|
|
}
|
|
|
|
|
|
2020-02-22 21:53:56 +05:00
|
|
|
has(topic) {
|
2020-04-12 11:04:30 +05:00
|
|
|
return this.all.findIndex((v) => v.title === (topic.title || topic)) > -1;
|
2020-02-06 16:46:23 +05:00
|
|
|
}
|
|
|
|
|
|
2020-02-22 21:53:56 +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) {
|
|
|
|
|
seen.set(value.id, {
|
|
|
|
|
...seen.get(value.id),
|
|
|
|
|
...value,
|
2020-04-12 11:04:30 +05:00
|
|
|
id: value.title,
|
2020-03-11 12:04:37 +05:00
|
|
|
});
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-02-22 21:53:56 +05:00
|
|
|
let title = value.title || value;
|
|
|
|
|
if (title.trim().length <= 0) continue;
|
|
|
|
|
seen.set(title, value);
|
2020-02-06 18:47:42 +05:00
|
|
|
}
|
2020-02-22 21:53:56 +05:00
|
|
|
return seen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-02-22 21:53:56 +05:00
|
|
|
let allTopics = [...notebook.topics, ...topics];
|
|
|
|
|
const unique = this._dedupe(allTopics);
|
|
|
|
|
|
|
|
|
|
notebook.topics = [];
|
|
|
|
|
notebook.totalNotes = 0;
|
2020-04-12 11:04:30 +05:00
|
|
|
unique.forEach((t) => {
|
2020-02-22 21:53:56 +05:00
|
|
|
let topic = makeTopic(t, this._notebookId);
|
|
|
|
|
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") {
|
2020-04-12 11:04:30 +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;
|
2020-04-16 02:14:53 +05:00
|
|
|
return new Topic(topic, this._notebookId, this._db);
|
2020-02-04 18:27:32 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async delete(...topics) {
|
2020-02-07 04:39:28 +05:00
|
|
|
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-04-12 11:04:30 +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
|
|
|
}
|
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) {
|
2020-02-11 11:10:08 +05:00
|
|
|
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(),
|
2020-02-11 11:10:08 +05:00
|
|
|
dateEdited: Date.now(),
|
2020-02-06 18:47:42 +05:00
|
|
|
totalNotes: 0,
|
2020-04-12 11:04:30 +05:00
|
|
|
notes: [],
|
2020-02-06 18:47:42 +05:00
|
|
|
};
|
|
|
|
|
}
|