szurubooru/public_html/js/Presenters/UserBrowsingSettingsPresenter.js

73 lines
1.8 KiB
JavaScript
Raw Normal View History

var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.UserBrowsingSettingsPresenter = function(
2014-09-08 22:02:28 +02:00
_,
jQuery,
util,
promise,
2014-09-07 14:50:16 +02:00
auth,
browsingSettings,
messagePresenter) {
var target;
var template;
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'))
.then(function(html) {
template = _.template(html);
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-09-07 14:50:16 +02:00
$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);
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
browsingSettings.setSettings(newSettings)
.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
};
App.DI.register('userBrowsingSettingsPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'browsingSettings', 'messagePresenter'], App.Presenters.UserBrowsingSettingsPresenter);