refactor: deprecate app.logout

use logout module instead
move header related code out of app.js
isekai-main
Barış Soner Uşaklı 3 years ago
parent 415dfbb9f7
commit 8b4510cc70

@ -54,7 +54,9 @@
}
$('[component="logout"]').on('click', function () {
app.logout();
require(['logout'], function (logout) {
logout();
});
return false;
});

@ -72,24 +72,15 @@ app.flags = {};
app.handleEarlyClicks();
app.load = function () {
handleStatusChange();
$('body').on('click', '#new_topic', function (e) {
e.preventDefault();
app.newTopic();
});
$('#header-menu .container').on('click', '[component="user/logout"]', function () {
app.logout();
return false;
});
Visibility.change(function (event, state) {
app.isFocused = state === 'visible';
});
createHeaderTooltips();
registerServiceWorker();
require([
@ -100,12 +91,10 @@ app.flags = {};
'messages',
'search',
'forum/unread',
'forum/header/notifications',
'forum/header/chat',
'forum/header',
'timeago/jquery.timeago',
], function (taskbar, helpers, pagination, translator, messages, search, unread, notifications, chat) {
notifications.prepareDOM();
chat.prepareDOM();
], function (taskbar, helpers, pagination, translator, messages, search, unread, header) {
header.prepareDOM();
translator.prepareDOM();
taskbar.init();
helpers.register();
@ -144,29 +133,10 @@ app.flags = {};
};
app.logout = function (redirect) {
redirect = redirect === undefined ? true : redirect;
hooks.fire('action:app.logout');
$.ajax(config.relative_path + '/logout', {
type: 'POST',
headers: {
'x-csrf-token': config.csrf_token,
},
beforeSend: function () {
app.flags._logout = true;
},
success: function (data) {
hooks.fire('action:app.loggedOut', data);
if (redirect) {
if (data.next) {
window.location.href = data.next;
} else {
window.location.reload();
}
}
},
console.warn('[deprecated] app.logout is deprecated, please use logout module directly');
require(['logout'], function (logout) {
logout(redirect);
});
return false;
};
app.alert = function (params) {
@ -314,34 +284,6 @@ app.flags = {};
});
};
function createHeaderTooltips() {
const env = utils.findBootstrapEnvironment();
if (env === 'xs' || env === 'sm' || isTouchDevice) {
return;
}
$('#header-menu li a[title]').each(function () {
$(this).tooltip({
placement: 'bottom',
trigger: 'hover',
title: $(this).attr('title'),
});
});
$('#search-form').tooltip({
placement: 'bottom',
trigger: 'hover',
title: $('#search-button i').attr('title'),
});
$('#user_dropdown').tooltip({
placement: 'bottom',
trigger: 'hover',
title: $('#user_dropdown').attr('title'),
});
}
app.enableTopicSearch = function (options) {
console.warn('[deprecated] app.enableTopicSearch is deprecated, please use search.enableQuickSearch(options)');
require(['search'], function (search) {
@ -363,24 +305,6 @@ app.flags = {};
});
};
function handleStatusChange() {
$('[component="header/usercontrol"] [data-status]').off('click').on('click', function (e) {
const status = $(this).attr('data-status');
socket.emit('user.setStatus', status, function (err) {
if (err) {
return app.alertError(err.message);
}
$('[data-uid="' + app.user.uid + '"] [component="user/status"], [component="header/profilelink"] [component="user/status"]')
.removeClass('away online dnd offline')
.addClass(status);
$('[component="header/usercontrol"] [data-status]').each(function () {
$(this).find('span').toggleClass('bold', $(this).attr('data-status') === status);
});
app.user.status = status;
});
e.preventDefault();
});
}
app.updateUserStatus = function (el, status) {
if (!el.length) {

@ -89,7 +89,9 @@ define('forum/account/edit', [
}
confirmBtn.html('<i class="fa fa-check"></i>');
app.logout();
require(['logout'], function (logout) {
logout();
});
});
return false;

@ -0,0 +1,71 @@
'use strict';
define('forum/header', ['forum/header/notifications', 'forum/header/chat'], function (notifications, chat) {
const module = {};
module.prepareDOM = function () {
notifications.prepareDOM();
chat.prepareDOM();
handleStatusChange();
createHeaderTooltips();
handleLogout();
};
function handleStatusChange() {
$('[component="header/usercontrol"] [data-status]').off('click').on('click', function (e) {
const status = $(this).attr('data-status');
socket.emit('user.setStatus', status, function (err) {
if (err) {
return app.alertError(err.message);
}
$('[data-uid="' + app.user.uid + '"] [component="user/status"], [component="header/profilelink"] [component="user/status"]')
.removeClass('away online dnd offline')
.addClass(status);
$('[component="header/usercontrol"] [data-status]').each(function () {
$(this).find('span').toggleClass('bold', $(this).attr('data-status') === status);
});
app.user.status = status;
});
e.preventDefault();
});
}
function createHeaderTooltips() {
const env = utils.findBootstrapEnvironment();
if (env === 'xs' || env === 'sm' || utils.isTouchDevice()) {
return;
}
$('#header-menu li a[title]').each(function () {
$(this).tooltip({
placement: 'bottom',
trigger: 'hover',
title: $(this).attr('title'),
});
});
$('#search-form').tooltip({
placement: 'bottom',
trigger: 'hover',
title: $('#search-button i').attr('title'),
});
$('#user_dropdown').tooltip({
placement: 'bottom',
trigger: 'hover',
title: $('#user_dropdown').attr('title'),
});
}
function handleLogout() {
$('#header-menu .container').on('click', '[component="user/logout"]', function () {
require(['logout'], function (logout) {
logout();
});
return false;
});
}
return module;
});

@ -0,0 +1,28 @@
'use strict';
define('logout', ['hooks'], function (hooks) {
return function logout(redirect) {
redirect = redirect === undefined ? true : redirect;
hooks.fire('action:app.logout');
$.ajax(config.relative_path + '/logout', {
type: 'POST',
headers: {
'x-csrf-token': config.csrf_token,
},
beforeSend: function () {
app.flags._logout = true;
},
success: function (data) {
hooks.fire('action:app.loggedOut', data);
if (redirect) {
if (data.next) {
window.location.href = data.next;
} else {
window.location.reload();
}
}
},
});
};
});

@ -94,7 +94,9 @@ socket = window.socket;
socket.on('event:banned', onEventBanned);
socket.on('event:unbanned', onEventUnbanned);
socket.on('event:logout', function () {
app.logout();
require(['logout'], function (logout) {
logout();
});
});
socket.on('event:alert', function (params) {
app.alert(params);
@ -128,8 +130,8 @@ socket = window.socket;
function handleInvalidSession() {
socket.disconnect();
app.logout(false);
require(['messages'], function (messages) {
require(['messages', 'logout'], function (messages, logout) {
logout(false);
messages.showInvalidSession();
});
}

@ -41,6 +41,7 @@ JS.scripts = {
// files listed below are only available client-side, or are bundled in to reduce # of network requests on cold load
rjs: [
'public/src/client/header.js',
'public/src/client/header/chat.js',
'public/src/client/header/notifications.js',
'public/src/client/infinitescroll.js',

Loading…
Cancel
Save