diff --git a/public_html/index.html b/public_html/index.html index 0eff7b27..840a7858 100644 --- a/public_html/index.html +++ b/public_html/index.html @@ -34,6 +34,7 @@ + diff --git a/public_html/js/BrowsingSettings.js b/public_html/js/BrowsingSettings.js new file mode 100644 index 00000000..2c4a8fea --- /dev/null +++ b/public_html/js/BrowsingSettings.js @@ -0,0 +1,86 @@ +var App = App || {}; + +App.BrowsingSettings = function( + promise, + auth, + api) { + + var settings = getDefaultSettings(); + + auth.startObservingLoginChanges('top-navigation', loginStateChanged); + + readFromLocalStorage(); + if (auth.isLoggedIn()) + loginStateChanged(); + + function setSettings(newSettings) { + settings = newSettings; + return save(); + } + + function getSettings() { + return settings; + } + + function getDefaultSettings() { + return { + hideDownvoted: true, + endlessScroll: true, + listPosts: { + safe: true, + sketchy: true, + unsafe: true, + }, + }; + } + + function loginStateChanged() { + readFromUser(auth.getCurrentUser()); + } + + function readFromLocalStorage() { + readFromString(localStorage.getItem('browsingSettings')); + } + + function readFromUser(user) { + readFromString(user.browsingSettings); + } + + function readFromString(string) { + if (!string) + return; + try { + settings = JSON.parse(string); + } catch (e) { + } + } + + function saveToLocalStorage() { + localStorage.setItem('browsingSettings', JSON.stringify(settings)); + } + + function saveToUser(user) { + var formData = { + browsingSettings: JSON.stringify(settings), + }; + return api.put('/users/' + user.name, formData); + } + + function save() { + return promise.make(function(resolve, reject) { + saveToLocalStorage(); + if (auth.isLoggedIn()) { + saveToUser(auth.getCurrentUser()).then(resolve).fail(reject); + } else { + resolve(); + } + }); + } + + return { + getSettings: getSettings, + setSettings: setSettings, + }; +} + +App.DI.registerSingleton('browsingSettings', App.BrowsingSettings); diff --git a/public_html/js/Presenters/UserBrowsingSettingsPresenter.js b/public_html/js/Presenters/UserBrowsingSettingsPresenter.js index d4907d15..e366af28 100644 --- a/public_html/js/Presenters/UserBrowsingSettingsPresenter.js +++ b/public_html/js/Presenters/UserBrowsingSettingsPresenter.js @@ -5,7 +5,9 @@ App.Presenters.UserBrowsingSettingsPresenter = function( jQuery, util, promise, - auth) { + auth, + browsingSettings, + messagePresenter) { var target; var template; @@ -29,7 +31,27 @@ App.Presenters.UserBrowsingSettingsPresenter = function( function render() { var $el = jQuery(target); - $el.html(template({user: user})); + $el.html(template({user: user, settings: browsingSettings.getSettings()})); + $el.find('form').submit(browsingSettingsFormSubmitted); + } + + function browsingSettingsFormSubmitted(e) { + e.preventDefault(); + var $el = jQuery(target); + var $messages = $el.find('.messages'); + messagePresenter.hideMessages($messages); + var newSettings = { + endlessScroll: $el.find('[name=endless-scroll]:visible').prop('checked'), + hideDownvoted: $el.find('[name=hide-downvoted]:visible').prop('checked'), + listPosts: { + safe: $el.find('[name=listSafePosts]:visible').prop('checked'), + sketchy: $el.find('[name=listSketchyPosts]:visible').prop('checked'), + unsafe: $el.find('[name=listUnsafePosts]:visible').prop('checked'), + }, + }; + browsingSettings.setSettings(newSettings).then(function() { + messagePresenter.showInfo($messages, 'Browsing settings updated!'); + }); } function getPrivileges() { diff --git a/public_html/templates/account-removal.tpl b/public_html/templates/account-removal.tpl index 3daef798..076355b1 100644 --- a/public_html/templates/account-removal.tpl +++ b/public_html/templates/account-removal.tpl @@ -4,9 +4,8 @@