2016-06-14 08:31:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const events = require('../events.js');
|
|
|
|
|
|
|
|
const defaultSettings = {
|
|
|
|
listPosts: {
|
|
|
|
safe: true,
|
|
|
|
sketchy: true,
|
|
|
|
unsafe: false,
|
|
|
|
},
|
|
|
|
upscaleSmallPosts: false,
|
|
|
|
endlessScroll: false,
|
|
|
|
keyboardShortcuts: true,
|
|
|
|
transparencyGrid: true,
|
2016-06-29 16:54:49 +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,
|
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 {
|
|
|
|
Object.assign(ret, JSON.parse(localStorage.getItem('settings')));
|
|
|
|
} 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);
|
2016-06-14 08:31:48 +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) {
|
|
|
|
this.dispatchEvent(new CustomEvent('change', {
|
|
|
|
detail: {
|
2019-05-24 00:27:59 +00:00
|
|
|
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();
|