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