2021-01-23 10:50:07 +05:00
|
|
|
import { EV, EVENTS } from "../common";
|
2020-12-05 11:26:02 +05:00
|
|
|
import id from "../utils/id";
|
2021-02-26 17:33:46 +05:00
|
|
|
import setManipulator from "../utils/set";
|
2020-12-05 11:26:02 +05:00
|
|
|
|
2020-11-25 15:04:39 +05:00
|
|
|
class Settings {
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {import("./index").default} db
|
|
|
|
|
*/
|
|
|
|
|
constructor(db) {
|
|
|
|
|
this._db = db;
|
2021-04-07 20:17:55 +05:00
|
|
|
this._initSettings();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_initSettings() {
|
2021-02-26 19:18:54 +05:00
|
|
|
this._settings = {
|
|
|
|
|
type: "settings",
|
|
|
|
|
id: id(),
|
|
|
|
|
pins: [],
|
2021-04-07 20:17:55 +05:00
|
|
|
dateEdited: 0,
|
|
|
|
|
dateCreated: 0,
|
2021-02-26 19:18:54 +05:00
|
|
|
};
|
2020-12-05 11:26:02 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get raw() {
|
|
|
|
|
return this._settings;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 14:23:33 +05:00
|
|
|
async merge(item) {
|
2021-02-26 17:33:46 +05:00
|
|
|
// TODO if (this.settings.dateEdited > (await this._db.lastSynced())) {
|
|
|
|
|
// this._settings.pins = setManipulator.union(
|
|
|
|
|
// this._settings.pins,
|
|
|
|
|
// item.pins
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
this._settings = item;
|
|
|
|
|
await this._db.context.write("settings", item);
|
2020-11-25 15:04:39 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async init() {
|
2020-12-05 11:26:02 +05:00
|
|
|
var settings = await this._db.context.read("settings");
|
2021-02-26 19:18:54 +05:00
|
|
|
if (settings) this._settings = settings;
|
2021-01-23 10:50:07 +05:00
|
|
|
EV.subscribe(EVENTS.userLoggedOut, () => {
|
2021-02-26 17:33:46 +05:00
|
|
|
this._settings = undefined;
|
2021-04-07 20:17:55 +05:00
|
|
|
this._initSettings();
|
2020-12-10 14:16:05 +05:00
|
|
|
});
|
2020-11-25 15:04:39 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async pin(type, data) {
|
2020-11-25 20:49:29 +05:00
|
|
|
if (type !== "notebook" && type !== "topic" && type !== "tag")
|
2020-11-25 15:04:39 +05:00
|
|
|
throw new Error("This item cannot be pinned.");
|
2020-11-25 20:52:54 +05:00
|
|
|
if (this.isPinned(data.id)) return;
|
2020-11-25 20:49:29 +05:00
|
|
|
this._settings.pins.push({ type, data });
|
2020-12-05 11:26:02 +05:00
|
|
|
this._settings.dateEdited = Date.now();
|
2020-11-25 15:04:39 +05:00
|
|
|
await this._db.context.write("settings", this._settings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async unpin(id) {
|
2020-11-25 20:49:29 +05:00
|
|
|
const index = this._settings.pins.findIndex((i) => i.data.id === id);
|
2020-11-25 15:04:39 +05:00
|
|
|
if (index <= -1) return;
|
|
|
|
|
this._settings.pins.splice(index, 1);
|
2020-12-05 11:26:02 +05:00
|
|
|
this._settings.dateEdited = Date.now();
|
2020-11-25 15:04:39 +05:00
|
|
|
await this._db.context.write("settings", this._settings);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-25 20:52:54 +05:00
|
|
|
isPinned(id) {
|
2020-11-25 20:59:27 +05:00
|
|
|
return this._settings.pins.findIndex((v) => v.data.id === id) > -1;
|
2020-11-25 20:52:54 +05:00
|
|
|
}
|
|
|
|
|
|
2020-11-25 15:04:39 +05:00
|
|
|
get pins() {
|
2021-01-10 10:54:47 +05:00
|
|
|
return this._settings.pins.reduce((prev, pin) => {
|
2021-06-03 09:17:50 +05:00
|
|
|
if (!pin || !pin.data) return;
|
|
|
|
|
|
2021-02-15 20:24:03 +05:00
|
|
|
let item;
|
2020-11-25 15:04:39 +05:00
|
|
|
if (pin.type === "notebook") {
|
2021-06-03 09:17:50 +05:00
|
|
|
const notebook = this._db.notebooks.notebook(pin.data.id);
|
|
|
|
|
item = notebook ? notebook.data : null;
|
2020-11-25 15:04:39 +05:00
|
|
|
} else if (pin.type === "topic") {
|
2021-06-03 09:17:50 +05:00
|
|
|
const notebook = this._db.notebooks.notebook(pin.data.notebookId);
|
|
|
|
|
if (!notebook) item = null;
|
|
|
|
|
const topic = notebook.topics.topic(pin.data.id);
|
|
|
|
|
if (!topic) item = null;
|
|
|
|
|
item = topic._topic;
|
2020-11-25 15:04:39 +05:00
|
|
|
} else if (pin.type === "tag") {
|
2021-02-20 11:31:44 +05:00
|
|
|
item = this._db.tags.tag(pin.data.id);
|
2020-11-25 15:04:39 +05:00
|
|
|
}
|
2021-02-15 20:24:03 +05:00
|
|
|
if (item) prev.push(item);
|
2021-02-20 11:31:44 +05:00
|
|
|
else this.unpin(pin.data.id); // TODO risky.
|
2021-01-10 10:54:47 +05:00
|
|
|
return prev;
|
|
|
|
|
}, []);
|
2020-11-25 15:04:39 +05:00
|
|
|
}
|
|
|
|
|
}
|
2020-11-25 15:18:57 +05:00
|
|
|
export default Settings;
|