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-09-14 16:09:01 +05:00
|
|
|
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;
|
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-09-14 16:09:01 +05:00
|
|
|
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) {
|
2020-02-22 21:53:56 +05:00
|
|
|
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;
|
|
|
|
|
}
|
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;
|
2020-09-28 12:12:57 +05:00
|
|
|
} */
|
2020-02-22 21:53:56 +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
|
|
|
|
2020-02-22 21:53:56 +05:00
|
|
|
let allTopics = [...notebook.topics, ...topics];
|
|
|
|
|
|
|
|
|
|
notebook.topics = [];
|
|
|
|
|
notebook.totalNotes = 0;
|
2020-12-07 11:19:50 +05:00
|
|
|
for (let t of allTopics) {
|
2020-02-22 21:53:56 +05:00
|
|
|
let topic = makeTopic(t, this._notebookId);
|
2020-09-28 12:12:57 +05:00
|
|
|
|
|
|
|
|
if (notebook.topics.findIndex((_topic) => _topic.title === t) > -1)
|
2020-12-07 11:19:50 +05:00
|
|
|
continue;
|
2020-09-28 12:12:57 +05:00
|
|
|
|
2020-12-07 11:19:50 +05:00
|
|
|
if (topic.title.length <= 0) continue;
|
2020-09-28 12:12:57 +05:00
|
|
|
|
2020-12-29 13:02:42 +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) {
|
2020-12-07 11:19:50 +05:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-22 21:53:56 +05:00
|
|
|
notebook.totalNotes += topic.totalNotes;
|
2020-12-07 11:19:50 +05:00
|
|
|
}
|
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-09-14 16:09:01 +05:00
|
|
|
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) {
|
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-05-16 14:02:59 +05:00
|
|
|
let index = topicIds.findIndex((id) => topic.id === id);
|
2020-02-05 01:12:36 +05:00
|
|
|
let t = this.topic(topic);
|
2020-11-04 10:28:52 +05:00
|
|
|
await t.delete(...topic.notes);
|
2021-01-03 10:52:05 +05:00
|
|
|
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) {
|
2020-12-29 13:02:42 +05:00
|
|
|
if (typeof topic !== "string") return topic;
|
2020-02-06 18:47:42 +05:00
|
|
|
return {
|
|
|
|
|
type: "topic",
|
2020-09-14 16:09:01 +05:00
|
|
|
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(),
|
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
|
|
|
};
|
|
|
|
|
}
|