From c1739d1d1df715bdb65d2e1feaeb60b3ff4808ab Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Thu, 11 Sep 2014 13:59:12 +0200 Subject: [PATCH] Added page title management (closed #33) --- public_html/index.html | 1 + public_html/js/Bootstrap.js | 6 ++-- public_html/js/PresenterManager.js | 36 +++++++++++++++++++ .../js/Presenters/CommentListPresenter.js | 1 + public_html/js/Presenters/HelpPresenter.js | 1 + public_html/js/Presenters/HomePresenter.js | 1 + public_html/js/Presenters/LoginPresenter.js | 1 + public_html/js/Presenters/LogoutPresenter.js | 1 + .../js/Presenters/PostListPresenter.js | 1 + .../js/Presenters/PostUploadPresenter.js | 1 + .../js/Presenters/RegistrationPresenter.js | 1 + public_html/js/Presenters/TagListPresenter.js | 1 + .../js/Presenters/TopNavigationPresenter.js | 10 ++++++ .../js/Presenters/UserActivationPresenter.js | 1 + .../js/Presenters/UserListPresenter.js | 1 + public_html/js/Presenters/UserPresenter.js | 1 + public_html/js/Router.js | 6 ++-- public_html/js/Util.js | 20 ----------- 18 files changed, 65 insertions(+), 26 deletions(-) create mode 100644 public_html/js/PresenterManager.js diff --git a/public_html/index.html b/public_html/index.html index 97c7795f..a09aee8b 100644 --- a/public_html/index.html +++ b/public_html/index.html @@ -58,6 +58,7 @@ + diff --git a/public_html/js/Bootstrap.js b/public_html/js/Bootstrap.js index dce924b8..aac55f5d 100644 --- a/public_html/js/Bootstrap.js +++ b/public_html/js/Bootstrap.js @@ -1,8 +1,8 @@ var App = App || {}; -App.Bootstrap = function(auth, router, util, promise) { +App.Bootstrap = function(auth, router, util, promise, presenterManager) { - util.initPresenter('topNavigationPresenter'); + presenterManager.init(); promise.wait(auth.tryLoginFromCookie()) .then(startRouting) @@ -25,7 +25,7 @@ App.Bootstrap = function(auth, router, util, promise) { }; -App.DI.registerSingleton('bootstrap', ['auth', 'router', 'util', 'promise'], App.Bootstrap); +App.DI.registerSingleton('bootstrap', ['auth', 'router', 'util', 'promise', 'presenterManager'], App.Bootstrap); App.DI.registerManual('jQuery', function() { return window.$; }); App.DI.registerManual('pathJs', function() { return window.Path; }); App.DI.registerManual('_', function() { return window._; }); diff --git a/public_html/js/PresenterManager.js b/public_html/js/PresenterManager.js new file mode 100644 index 00000000..10ec786e --- /dev/null +++ b/public_html/js/PresenterManager.js @@ -0,0 +1,36 @@ +var App = App || {}; + +App.PresenterManager = function(topNavigationPresenter) { + + var lastContentPresenterName; + var lastContentPresenter; + + function init() { + initPresenter('topNavigationPresenter'); + } + + function initPresenter(presenterName, args) { + var presenter = App.DI.get(presenterName); + presenter.init.call(presenter, args); + } + + function switchContentPresenter(presenterName, args) { + if (lastContentPresenterName !== presenterName) { + topNavigationPresenter.changeTitle(null); + var presenter = App.DI.get(presenterName); + presenter.init.call(presenter, args); + lastContentPresenterName = presenterName; + lastContentPresenter = presenter; + } else if (lastContentPresenter.reinit) { + lastContentPresenter.reinit.call(lastContentPresenter, args); + } + } + + return { + init: init, + switchContentPresenter: switchContentPresenter, + }; + +}; + +App.DI.registerSingleton('presenterManager', ['topNavigationPresenter'], App.PresenterManager); diff --git a/public_html/js/Presenters/CommentListPresenter.js b/public_html/js/Presenters/CommentListPresenter.js index dc1e4d11..ab65b62d 100644 --- a/public_html/js/Presenters/CommentListPresenter.js +++ b/public_html/js/Presenters/CommentListPresenter.js @@ -9,6 +9,7 @@ App.Presenters.CommentListPresenter = function( function init(args) { topNavigationPresenter.select('comments'); + topNavigationPresenter.changeTitle('Comments'); render(); } diff --git a/public_html/js/Presenters/HelpPresenter.js b/public_html/js/Presenters/HelpPresenter.js index 1978db0d..9f6fcff6 100644 --- a/public_html/js/Presenters/HelpPresenter.js +++ b/public_html/js/Presenters/HelpPresenter.js @@ -9,6 +9,7 @@ App.Presenters.HelpPresenter = function( function init(args) { topNavigationPresenter.select('help'); + topNavigationPresenter.changeTitle('Help'); render(); } diff --git a/public_html/js/Presenters/HomePresenter.js b/public_html/js/Presenters/HomePresenter.js index 85bf08d2..c550eb8a 100644 --- a/public_html/js/Presenters/HomePresenter.js +++ b/public_html/js/Presenters/HomePresenter.js @@ -9,6 +9,7 @@ App.Presenters.HomePresenter = function( function init(args) { topNavigationPresenter.select('home'); + topNavigationPresenter.changeTitle('Home'); render(); } diff --git a/public_html/js/Presenters/LoginPresenter.js b/public_html/js/Presenters/LoginPresenter.js index 5bf93e72..edb5068d 100644 --- a/public_html/js/Presenters/LoginPresenter.js +++ b/public_html/js/Presenters/LoginPresenter.js @@ -18,6 +18,7 @@ App.Presenters.LoginPresenter = function( function init(args) { topNavigationPresenter.select('login'); + topNavigationPresenter.changeTitle('Login'); previousRoute = args.previousRoute; promise.wait(util.promiseTemplate('login-form')).then(function(html) { template = _.template(html); diff --git a/public_html/js/Presenters/LogoutPresenter.js b/public_html/js/Presenters/LogoutPresenter.js index c3631951..ecd9eac0 100644 --- a/public_html/js/Presenters/LogoutPresenter.js +++ b/public_html/js/Presenters/LogoutPresenter.js @@ -13,6 +13,7 @@ App.Presenters.LogoutPresenter = function( function init() { topNavigationPresenter.select('logout'); + topNavigationPresenter.changeTitle('Logout'); promise.wait(auth.logout()).then(function() { $messages.empty(); var $messageDiv = messagePresenter.showInfo($messages, 'Logged out. Back to main page'); diff --git a/public_html/js/Presenters/PostListPresenter.js b/public_html/js/Presenters/PostListPresenter.js index e62ffae5..bf5ad9b0 100644 --- a/public_html/js/Presenters/PostListPresenter.js +++ b/public_html/js/Presenters/PostListPresenter.js @@ -9,6 +9,7 @@ App.Presenters.PostListPresenter = function( function init(args) { topNavigationPresenter.select('posts'); + topNavigationPresenter.changeTitle('Posts'); render(); } diff --git a/public_html/js/Presenters/PostUploadPresenter.js b/public_html/js/Presenters/PostUploadPresenter.js index 8949fbec..04e62426 100644 --- a/public_html/js/Presenters/PostUploadPresenter.js +++ b/public_html/js/Presenters/PostUploadPresenter.js @@ -9,6 +9,7 @@ App.Presenters.PostUploadPresenter = function( function init(args) { topNavigationPresenter.select('upload'); + topNavigationPresenter.changeTitle('Upload'); render(); } diff --git a/public_html/js/Presenters/RegistrationPresenter.js b/public_html/js/Presenters/RegistrationPresenter.js index 16c7b7bc..b96eb444 100644 --- a/public_html/js/Presenters/RegistrationPresenter.js +++ b/public_html/js/Presenters/RegistrationPresenter.js @@ -16,6 +16,7 @@ App.Presenters.RegistrationPresenter = function( function init() { topNavigationPresenter.select('register'); + topNavigationPresenter.changeTitle('Registration'); promise.wait(util.promiseTemplate('registration-form')).then(function(html) { template = _.template(html); render(); diff --git a/public_html/js/Presenters/TagListPresenter.js b/public_html/js/Presenters/TagListPresenter.js index 5bb9902e..3cb11590 100644 --- a/public_html/js/Presenters/TagListPresenter.js +++ b/public_html/js/Presenters/TagListPresenter.js @@ -9,6 +9,7 @@ App.Presenters.TagListPresenter = function( function init(args) { topNavigationPresenter.select('tags'); + topNavigationPresenter.changeTitle('Tags'); render(); } diff --git a/public_html/js/Presenters/TopNavigationPresenter.js b/public_html/js/Presenters/TopNavigationPresenter.js index 2dc099df..011e9de2 100644 --- a/public_html/js/Presenters/TopNavigationPresenter.js +++ b/public_html/js/Presenters/TopNavigationPresenter.js @@ -11,6 +11,7 @@ App.Presenters.TopNavigationPresenter = function( var selectedElement = null; var $el = jQuery('#top-navigation'); var template; + var baseTitle = document.title; function init() { promise.wait(util.promiseTemplate('top-navigation')).then(function(html) { @@ -44,10 +45,19 @@ App.Presenters.TopNavigationPresenter = function( $el.find('li.' + selectedElement).addClass('active'); } + function changeTitle(subTitle) { + var newTitle = baseTitle; + if (subTitle) { + newTitle += ' - ' + subTitle; + } + document.title = newTitle; + } + return { init: init, render: render, select: select, + changeTitle: changeTitle, }; }; diff --git a/public_html/js/Presenters/UserActivationPresenter.js b/public_html/js/Presenters/UserActivationPresenter.js index 55b1d1fb..c1bbcc82 100644 --- a/public_html/js/Presenters/UserActivationPresenter.js +++ b/public_html/js/Presenters/UserActivationPresenter.js @@ -20,6 +20,7 @@ App.Presenters.UserActivationPresenter = function( function init(args) { topNavigationPresenter.select('login'); + topNavigationPresenter.changeTitle('Account recovery'); reinit(args); } diff --git a/public_html/js/Presenters/UserListPresenter.js b/public_html/js/Presenters/UserListPresenter.js index 4fe33b74..0499f496 100644 --- a/public_html/js/Presenters/UserListPresenter.js +++ b/public_html/js/Presenters/UserListPresenter.js @@ -19,6 +19,7 @@ App.Presenters.UserListPresenter = function( function init(args) { topNavigationPresenter.select('users'); + topNavigationPresenter.changeTitle('Users'); promise.wait(util.promiseTemplate('user-list')).then(function(html) { template = _.template(html); diff --git a/public_html/js/Presenters/UserPresenter.js b/public_html/js/Presenters/UserPresenter.js index d224bc4a..0dc0afa3 100644 --- a/public_html/js/Presenters/UserPresenter.js +++ b/public_html/js/Presenters/UserPresenter.js @@ -24,6 +24,7 @@ App.Presenters.UserPresenter = function( function init(args) { userName = args.userName; topNavigationPresenter.select(auth.isLoggedIn(userName) ? 'my-account' : 'users'); + topNavigationPresenter.changeTitle(userName); promise.waitAll( util.promiseTemplate('user'), diff --git a/public_html/js/Router.js b/public_html/js/Router.js index 533f6554..e920167b 100644 --- a/public_html/js/Router.js +++ b/public_html/js/Router.js @@ -1,6 +1,6 @@ var App = App || {}; -App.Router = function(pathJs, _, jQuery, util, appState) { +App.Router = function(pathJs, _, jQuery, util, appState, presenterManager) { var root = '#/'; @@ -47,7 +47,7 @@ App.Router = function(pathJs, _, jQuery, util, appState) { additionalParams, {previousRoute: pathJs.routes.previous}); - util.initContentPresenter( presenterName, finalParams); + presenterManager.switchContentPresenter( presenterName, finalParams); }); } @@ -59,4 +59,4 @@ App.Router = function(pathJs, _, jQuery, util, appState) { }; -App.DI.registerSingleton('router', ['pathJs', '_', 'jQuery', 'util', 'appState'], App.Router); +App.DI.registerSingleton('router', ['pathJs', '_', 'jQuery', 'util', 'appState', 'presenterManager'], App.Router); diff --git a/public_html/js/Util.js b/public_html/js/Util.js index a01593f2..609b8448 100644 --- a/public_html/js/Util.js +++ b/public_html/js/Util.js @@ -3,8 +3,6 @@ var App = App || {}; App.Util = function(_, jQuery, promise) { var templateCache = {}; - var lastContentPresenterName; - var lastContentPresenter; function parseComplexRouteArgs(args) { var result = {}; @@ -32,22 +30,6 @@ App.Util = function(_, jQuery, promise) { return result; } - function initPresenter(presenterName, args) { - var presenter = App.DI.get(presenterName); - presenter.init.call(presenter, args); - } - - function initContentPresenter(presenterName, args) { - if (lastContentPresenterName !== presenterName) { - var presenter = App.DI.get(presenterName); - presenter.init.call(presenter, args); - lastContentPresenterName = presenterName; - lastContentPresenter = presenter; - } else if (lastContentPresenter.reinit) { - lastContentPresenter.reinit.call(lastContentPresenter, args); - } - } - function promiseTemplate(templateName) { return promiseTemplateFromCache(templateName) || promiseTemplateFromDOM(templateName) || @@ -151,8 +133,6 @@ App.Util = function(_, jQuery, promise) { return { promiseTemplate: promiseTemplate, - initPresenter : initPresenter, - initContentPresenter: initContentPresenter, parseComplexRouteArgs: parseComplexRouteArgs, compileComplexRouteArgs: compileComplexRouteArgs, formatRelativeTime: formatRelativeTime,