client/settings: decouple settings from controller
This commit is contained in:
parent
00bf279396
commit
c2aa497d91
3 changed files with 41 additions and 33 deletions
|
@ -1,7 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
const events = require('../events.js');
|
||||
const settingsController = require('./settings_controller.js');
|
||||
const settings = require('../settings.js');
|
||||
const EndlessPageView = require('../views/endless_page_view.js');
|
||||
const ManualPageView = require('../views/manual_page_view.js');
|
||||
|
||||
|
@ -14,7 +14,7 @@ class PageController {
|
|||
}
|
||||
|
||||
update() {
|
||||
if (settingsController.getSettings().endlessScroll) {
|
||||
if (settings.getSettings().endlessScroll) {
|
||||
this.pageView = new EndlessPageView();
|
||||
} else {
|
||||
this.pageView = new ManualPageView();
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
const page = require('page');
|
||||
const events = require('../events.js');
|
||||
const settings = require('../settings.js');
|
||||
const topNavController = require('../controllers/top_nav_controller.js');
|
||||
const SettingsView = require('../views/settings_view.js');
|
||||
|
||||
|
@ -17,38 +17,10 @@ class SettingsController {
|
|||
settingsRoute() {
|
||||
topNavController.activate('settings');
|
||||
this.settingsView.render({
|
||||
getSettings: () => this.getSettings(),
|
||||
saveSettings: newSettings => this.saveSettings(newSettings),
|
||||
getSettings: () => settings.getSettings(),
|
||||
saveSettings: newSettings => settings.saveSettings(newSettings),
|
||||
});
|
||||
}
|
||||
|
||||
saveSettings(browsingSettings) {
|
||||
localStorage.setItem('settings', JSON.stringify(browsingSettings));
|
||||
events.notify(events.Success, 'Settings saved');
|
||||
events.notify(events.SettingsChange);
|
||||
}
|
||||
|
||||
getSettings(settings) {
|
||||
const defaultSettings = {
|
||||
endlessScroll: false,
|
||||
};
|
||||
let ret = {};
|
||||
let userSettings = localStorage.getItem('settings');
|
||||
if (userSettings) {
|
||||
userSettings = JSON.parse(userSettings);
|
||||
}
|
||||
if (!userSettings) {
|
||||
userSettings = {};
|
||||
}
|
||||
for (let key of Object.keys(defaultSettings)) {
|
||||
if (key in userSettings) {
|
||||
ret[key] = userSettings[key];
|
||||
} else {
|
||||
ret[key] = defaultSettings[key];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = new SettingsController();
|
||||
|
|
36
client/js/settings.js
Normal file
36
client/js/settings.js
Normal file
|
@ -0,0 +1,36 @@
|
|||
'use strict';
|
||||
|
||||
const events = require('./events.js');
|
||||
|
||||
function saveSettings(browsingSettings) {
|
||||
localStorage.setItem('settings', JSON.stringify(browsingSettings));
|
||||
events.notify(events.Success, 'Settings saved');
|
||||
events.notify(events.SettingsChange);
|
||||
}
|
||||
|
||||
function getSettings(settings) {
|
||||
const defaultSettings = {
|
||||
endlessScroll: false,
|
||||
};
|
||||
let ret = {};
|
||||
let userSettings = localStorage.getItem('settings');
|
||||
if (userSettings) {
|
||||
userSettings = JSON.parse(userSettings);
|
||||
}
|
||||
if (!userSettings) {
|
||||
userSettings = {};
|
||||
}
|
||||
for (let key of Object.keys(defaultSettings)) {
|
||||
if (key in userSettings) {
|
||||
ret[key] = userSettings[key];
|
||||
} else {
|
||||
ret[key] = defaultSettings[key];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getSettings: getSettings,
|
||||
saveSettings: saveSettings,
|
||||
};
|
Loading…
Reference in a new issue