szurubooru/public_html/js/Presenters/UserBrowsingSettingsPresenter.js

72 lines
1.8 KiB
JavaScript
Raw Normal View History

var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.UserBrowsingSettingsPresenter = function(
jQuery,
util,
promise,
2014-09-07 14:50:16 +02:00
auth,
browsingSettings,
messagePresenter) {
var target;
2014-10-05 10:41:12 +02:00
var templates = {};
var user;
var privileges = {};
2014-09-17 22:37:49 +02:00
function init(args, loaded) {
user = args.user;
target = args.target;
2014-09-17 22:37:49 +02:00
privileges.canChangeBrowsingSettings = auth.isLoggedIn(user.name) && user.name === auth.getCurrentUser().name;
2014-09-17 22:37:49 +02:00
promise.wait(util.promiseTemplate('browsing-settings'))
2014-10-05 10:41:12 +02:00
.then(function(template) {
templates.browsingSettings = template;
render();
2014-09-17 22:37:49 +02:00
loaded();
});
}
function render() {
2014-09-07 09:57:01 +02:00
var $el = jQuery(target);
2014-10-05 10:41:12 +02:00
$el.html(templates.browsingSettings({user: user, settings: browsingSettings.getSettings()}));
2014-09-07 14:50:16 +02:00
$el.find('form').submit(browsingSettingsFormSubmitted);
}
function browsingSettingsFormSubmitted(e) {
e.preventDefault();
var $el = jQuery(target);
var $messages = $el.find('.messages');
messagePresenter.hideMessages($messages);
2014-09-17 22:37:49 +02:00
2014-09-07 14:50:16 +02:00
var newSettings = {
2014-09-18 11:30:48 +02:00
endlessScroll: $el.find('[name=endlessScroll]').is(':checked'),
hideDownvoted: $el.find('[name=hideDownvoted]').is(':checked'),
2014-09-07 14:50:16 +02:00
listPosts: {
2014-09-17 10:21:20 +02:00
safe: $el.find('[name=listSafePosts]').is(':checked'),
sketchy: $el.find('[name=listSketchyPosts]').is(':checked'),
unsafe: $el.find('[name=listUnsafePosts]').is(':checked'),
2014-09-07 14:50:16 +02:00
},
};
2014-09-17 22:37:49 +02:00
2014-10-02 00:30:25 +02:00
promise.wait(browsingSettings.setSettings(newSettings))
2014-09-17 22:37:49 +02:00
.then(function() {
messagePresenter.showInfo($messages, 'Browsing settings updated!');
});
}
function getPrivileges() {
return privileges;
}
return {
init: init,
render: render,
getPrivileges: getPrivileges,
};
2014-09-08 22:02:28 +02:00
};
2014-10-05 10:41:12 +02:00
App.DI.register('userBrowsingSettingsPresenter', ['jQuery', 'util', 'promise', 'auth', 'browsingSettings', 'messagePresenter'], App.Presenters.UserBrowsingSettingsPresenter);