2020-02-04 18:27:32 +05:00
|
|
|
import CachedCollection from "../database/cached-collection";
|
|
|
|
|
import fuzzysearch from "fuzzysearch";
|
2020-02-05 20:57:43 +05:00
|
|
|
import Notebook from "../models/notebook";
|
2020-02-04 18:27:32 +05:00
|
|
|
var tfun = require("transfun/transfun.js").tfun;
|
|
|
|
|
if (!tfun) {
|
|
|
|
|
tfun = global.tfun;
|
|
|
|
|
}
|
|
|
|
|
export default class Notebooks {
|
|
|
|
|
constructor(context) {
|
|
|
|
|
this.context = context;
|
|
|
|
|
this.collection = new CachedCollection(context, "notebooks");
|
2020-02-05 00:17:12 +05:00
|
|
|
this.notes = undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init(notes) {
|
|
|
|
|
this.notes = notes;
|
|
|
|
|
return this.collection.init();
|
2020-02-04 18:27:32 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async add(notebookArg) {
|
|
|
|
|
if (!notebookArg) throw new Error("Notebook cannot be undefined or null.");
|
|
|
|
|
//TODO reliably and efficiently check for duplicates.
|
|
|
|
|
const id = notebookArg.id || Date.now().toString() + "_notebook";
|
|
|
|
|
let oldNotebook = this.collection.getItem(id);
|
|
|
|
|
|
|
|
|
|
if (!oldNotebook && !notebookArg.title)
|
|
|
|
|
throw new Error("Notebook must contain at least a title.");
|
|
|
|
|
|
|
|
|
|
let notebook = {
|
|
|
|
|
...oldNotebook,
|
|
|
|
|
...notebookArg
|
|
|
|
|
};
|
2020-02-05 00:17:12 +05:00
|
|
|
/* if (notebookArg.topics) {
|
2020-02-04 18:27:32 +05:00
|
|
|
notebook.topics = [...notebook.topics, ...notebookArg.topics];
|
2020-02-05 00:17:12 +05:00
|
|
|
} */
|
|
|
|
|
if (oldNotebook && oldNotebook.topics) {
|
|
|
|
|
notebook.topics = [...notebook.topics, ...oldNotebook.topics];
|
2020-02-04 18:27:32 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let topics = notebook.topics || [];
|
|
|
|
|
if (!oldNotebook) {
|
|
|
|
|
topics[0] = makeTopic("General", id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < topics.length; i++) {
|
|
|
|
|
let topic = topics[i];
|
|
|
|
|
let isDuplicate =
|
|
|
|
|
topics.findIndex(t => t.title === (topic || topic.title)) > -1; //check for duplicate
|
|
|
|
|
|
|
|
|
|
let isEmpty =
|
|
|
|
|
!topic || (typeof topic === "string" && topic.trim().length <= 0);
|
|
|
|
|
|
|
|
|
|
if (isEmpty || isDuplicate) {
|
|
|
|
|
topics.splice(i, 1);
|
|
|
|
|
i--;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof topic === "string") {
|
|
|
|
|
topics[i] = makeTopic(topic, id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notebook = {
|
|
|
|
|
id,
|
|
|
|
|
type: "notebook",
|
|
|
|
|
title: notebook.title,
|
|
|
|
|
description: notebook.description,
|
|
|
|
|
dateCreated: notebook.dateCreated || Date.now(),
|
|
|
|
|
dateEdited: Date.now(),
|
|
|
|
|
pinned: !!notebook.pinned,
|
|
|
|
|
favorite: !!notebook.favorite,
|
|
|
|
|
topics,
|
|
|
|
|
totalNotes: 0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await this.collection.addItem(notebook);
|
|
|
|
|
return notebook.id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get all() {
|
|
|
|
|
return this.collection.getAllItems();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-05 20:57:43 +05:00
|
|
|
notebook(id) {
|
|
|
|
|
let notebook = this.collection.getItem(id);
|
|
|
|
|
if (!notebook) return;
|
|
|
|
|
return new Notebook(this, notebook);
|
2020-02-04 18:27:32 +05:00
|
|
|
}
|
|
|
|
|
|
2020-02-05 01:12:36 +05:00
|
|
|
async delete(...ids) {
|
|
|
|
|
for (let id of ids) {
|
2020-02-05 20:57:43 +05:00
|
|
|
let notebook = this.notebook(id);
|
2020-02-05 01:12:36 +05:00
|
|
|
if (!notebook) continue;
|
|
|
|
|
await this.collection.transaction(() =>
|
2020-02-05 20:57:43 +05:00
|
|
|
notebook.topics.delete(...notebook.topics.all)
|
2020-02-05 01:12:36 +05:00
|
|
|
);
|
|
|
|
|
await this.collection.removeItem(id);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-04 18:27:32 +05:00
|
|
|
|
|
|
|
|
filter(query) {
|
|
|
|
|
if (!query) return [];
|
|
|
|
|
return tfun.filter(v => fuzzysearch(query, v.title + " " + v.description))(
|
|
|
|
|
this.all
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function makeTopic(topic, notebookId) {
|
|
|
|
|
return {
|
|
|
|
|
type: "topic",
|
|
|
|
|
notebookId,
|
|
|
|
|
title: topic,
|
|
|
|
|
id: Date.now(),
|
|
|
|
|
dateCreated: Date.now(),
|
|
|
|
|
totalNotes: 0,
|
|
|
|
|
notes: []
|
|
|
|
|
};
|
|
|
|
|
}
|