client/settings: decouple settings from controller
This commit is contained in:
parent
00bf279396
commit
c2aa497d91
|
@ -1,7 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const events = require('../events.js');
|
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 EndlessPageView = require('../views/endless_page_view.js');
|
||||||
const ManualPageView = require('../views/manual_page_view.js');
|
const ManualPageView = require('../views/manual_page_view.js');
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ class PageController {
|
||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
update() {
|
||||||
if (settingsController.getSettings().endlessScroll) {
|
if (settings.getSettings().endlessScroll) {
|
||||||
this.pageView = new EndlessPageView();
|
this.pageView = new EndlessPageView();
|
||||||
} else {
|
} else {
|
||||||
this.pageView = new ManualPageView();
|
this.pageView = new ManualPageView();
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const page = require('page');
|
const page = require('page');
|
||||||
const events = require('../events.js');
|
const settings = require('../settings.js');
|
||||||
const topNavController = require('../controllers/top_nav_controller.js');
|
const topNavController = require('../controllers/top_nav_controller.js');
|
||||||
const SettingsView = require('../views/settings_view.js');
|
const SettingsView = require('../views/settings_view.js');
|
||||||
|
|
||||||
|
@ -17,38 +17,10 @@ class SettingsController {
|
||||||
settingsRoute() {
|
settingsRoute() {
|
||||||
topNavController.activate('settings');
|
topNavController.activate('settings');
|
||||||
this.settingsView.render({
|
this.settingsView.render({
|
||||||
getSettings: () => this.getSettings(),
|
getSettings: () => settings.getSettings(),
|
||||||
saveSettings: newSettings => this.saveSettings(newSettings),
|
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();
|
module.exports = new SettingsController();
|
||||||
|
|
|
@ -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 New Issue