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

104 lines
2.6 KiB
JavaScript
Raw Normal View History

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-06 16:46:23 +05:00
import Notes from "./notes";
import Trash from "./trash";
2020-03-02 15:40:20 +05:00
import sort from "fast-sort";
2020-03-18 14:06:20 +05:00
import getId from "../utils/id"
2020-03-02 15:40:20 +05:00
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._collection = new CachedCollection(context, "notebooks");
2020-02-05 00:17:12 +05:00
}
2020-02-06 16:46:23 +05:00
/**
*
* @param {Notes} notes
* @param {Trash} trash
*/
init(notes, trash) {
this._trash = trash;
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.
2020-03-18 14:06:20 +05:00
const id = notebookArg.id || getId();
let oldNotebook = this._collection.getItem(id);
2020-02-04 18:27:32 +05:00
if (!oldNotebook && !notebookArg.title)
throw new Error("Notebook must contain at least a title.");
let notebook = {
...oldNotebook,
...notebookArg
};
notebook = {
id,
type: "notebook",
title: notebook.title,
description: notebook.description,
dateCreated: notebook.dateCreated,
2020-02-04 18:27:32 +05:00
pinned: !!notebook.pinned,
favorite: !!notebook.favorite,
2020-02-06 18:47:42 +05:00
topics: notebook.topics || [],
2020-02-04 18:27:32 +05:00
totalNotes: 0
};
2020-02-06 18:47:42 +05:00
if (!oldNotebook) {
notebook.topics.splice(0, 0, "General");
}
2020-02-04 18:27:32 +05:00
await this._collection.addItem(notebook);
2020-02-06 18:47:42 +05:00
if (!oldNotebook) {
await this.notebook(notebook).topics.add(...notebook.topics);
}
return notebook;
2020-02-04 18:27:32 +05:00
}
get all() {
2020-03-02 15:40:20 +05:00
return sort(this._collection.getAllItems()).desc(t => t.pinned);
2020-02-04 18:27:32 +05:00
}
get pinned() {
return tfun.filter(".pinned === true")(this.all);
}
2020-02-06 22:46:57 +05:00
/**
*
* @param {string} id The id of the notebook
* @returns {Notebook} The notebook of the given id
*/
2020-02-05 20:57:43 +05:00
notebook(id) {
let notebook = id.type ? id : this._collection.getItem(id);
2020-02-05 20:57:43 +05:00
if (!notebook) return;
return new Notebook(this, notebook);
2020-02-04 18:27:32 +05:00
}
async delete(...ids) {
for (let id of ids) {
2020-02-05 20:57:43 +05:00
let notebook = this.notebook(id);
if (!notebook) continue;
await this._collection.transaction(() =>
notebook.topics.delete(...notebook.data.topics)
);
await this._collection.removeItem(id);
await this._trash.add(notebook.data);
}
}
2020-02-04 18:27:32 +05:00
filter(query) {
if (!query) return [];
2020-02-11 16:28:28 +05:00
let queryFn = v => fuzzysearch(query, v.title + " " + v.description);
if (query instanceof Function) queryFn = query;
return tfun.filter(queryFn)(this.all);
2020-02-04 18:27:32 +05:00
}
}