2020-06-06 00:03:37 +02:00
|
|
|
"use strict";
|
2016-06-14 10:31:48 +02:00
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
const events = require("../events.js");
|
2016-06-14 10:31:48 +02:00
|
|
|
|
|
|
|
const defaultSettings = {
|
|
|
|
listPosts: {
|
|
|
|
safe: true,
|
|
|
|
sketchy: true,
|
|
|
|
unsafe: false,
|
|
|
|
},
|
|
|
|
upscaleSmallPosts: false,
|
|
|
|
endlessScroll: false,
|
|
|
|
keyboardShortcuts: true,
|
|
|
|
transparencyGrid: true,
|
2020-06-06 00:03:37 +02:00
|
|
|
fitMode: "fit-both",
|
2016-07-31 23:07:01 +02:00
|
|
|
tagSuggestions: true,
|
2016-11-11 23:14:51 +01:00
|
|
|
autoplayVideos: false,
|
2016-08-27 16:16:50 +02:00
|
|
|
postsPerPage: 42,
|
2019-05-22 23:08:26 +02:00
|
|
|
tagUnderscoresAsSpaces: false,
|
2020-08-22 22:59:13 +02:00
|
|
|
darkTheme: false,
|
2016-06-14 10:31:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class Settings extends events.EventTarget {
|
2019-05-24 02:27:59 +02:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.cache = this._getFromLocalStorage();
|
|
|
|
}
|
|
|
|
|
|
|
|
_getFromLocalStorage() {
|
|
|
|
let ret = Object.assign({}, defaultSettings);
|
|
|
|
try {
|
2020-06-06 00:03:37 +02:00
|
|
|
Object.assign(ret, JSON.parse(localStorage.getItem("settings")));
|
2019-05-24 02:27:59 +02:00
|
|
|
} catch (e) {
|
2020-06-04 20:09:35 +02:00
|
|
|
// continue regardless of error
|
2019-05-24 02:27:59 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
save(newSettings, silent) {
|
2019-05-24 02:27:59 +02:00
|
|
|
newSettings = Object.assign(this.cache, newSettings);
|
2020-06-06 00:03:37 +02:00
|
|
|
localStorage.setItem("settings", JSON.stringify(newSettings));
|
2019-05-24 02:27:59 +02:00
|
|
|
this.cache = this._getFromLocalStorage();
|
2016-06-14 10:31:48 +02:00
|
|
|
if (silent !== true) {
|
2020-06-06 00:03:37 +02:00
|
|
|
this.dispatchEvent(
|
|
|
|
new CustomEvent("change", {
|
|
|
|
detail: {
|
|
|
|
settings: this.cache,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
2016-06-14 10:31:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get() {
|
2019-05-24 02:27:59 +02:00
|
|
|
return this.cache;
|
2016-06-14 10:31:48 +02:00
|
|
|
}
|
2020-06-04 20:09:35 +02:00
|
|
|
}
|
2016-06-14 10:31:48 +02:00
|
|
|
|
|
|
|
module.exports = new Settings();
|