diff --git a/public/src/admin/admin.js b/public/src/admin/admin.js index ebb697f42d..a91572c009 100644 --- a/public/src/admin/admin.js +++ b/public/src/admin/admin.js @@ -54,7 +54,9 @@ } $('[component="logout"]').on('click', function () { - app.logout(); + require(['logout'], function (logout) { + logout(); + }); return false; }); diff --git a/public/src/app.js b/public/src/app.js index 7d89a18002..f486c8f804 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -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) { diff --git a/public/src/client/account/edit.js b/public/src/client/account/edit.js index 6a001084fc..6c69eb6dba 100644 --- a/public/src/client/account/edit.js +++ b/public/src/client/account/edit.js @@ -89,7 +89,9 @@ define('forum/account/edit', [ } confirmBtn.html(''); - app.logout(); + require(['logout'], function (logout) { + logout(); + }); }); return false; diff --git a/public/src/client/header.js b/public/src/client/header.js new file mode 100644 index 0000000000..c6dfe9edb2 --- /dev/null +++ b/public/src/client/header.js @@ -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; +}); diff --git a/public/src/modules/logout.js b/public/src/modules/logout.js new file mode 100644 index 0000000000..400d5c25e1 --- /dev/null +++ b/public/src/modules/logout.js @@ -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(); + } + } + }, + }); + }; +}); diff --git a/public/src/sockets.js b/public/src/sockets.js index 94bc09dab0..5403717009 100644 --- a/public/src/sockets.js +++ b/public/src/sockets.js @@ -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(); }); } diff --git a/src/meta/js.js b/src/meta/js.js index 742d27d61d..6901932869 100644 --- a/src/meta/js.js +++ b/src/meta/js.js @@ -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',