diff --git a/TODO b/TODO index 58124ade..b60c6852 100644 --- a/TODO +++ b/TODO @@ -20,7 +20,6 @@ first major release. - misc: add README - apache2 has good README: http://svn.apache.org/repos/asf/httpd/httpd/trunk/README - misc: add spinner to forms such as registration, login, settings... -- misc: add 404 pages - misc: improve throbber appearance - misc: try not to fade in images that are already in browser cache diff --git a/public_html/.htaccess b/public_html/.htaccess index 3e0484cc..8ed23b2d 100644 --- a/public_html/.htaccess +++ b/public_html/.htaccess @@ -1,8 +1,12 @@ DirectoryIndex app.min.html DirectoryIndex index.html +ErrorDocument 404 /404.html + RewriteEngine On +RewriteRule ^/?404.html$ /#/404 [NE,R,L] + #RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^/?data/posts/([^/]+)/? /api/posts/$1/content [L] diff --git a/public_html/css/http-error.css b/public_html/css/http-error.css new file mode 100644 index 00000000..39da9d92 --- /dev/null +++ b/public_html/css/http-error.css @@ -0,0 +1,6 @@ +.http-error { + text-align: center; +} +.http-error img { + margin: 0 auto; +} diff --git a/public_html/img/404.png b/public_html/img/404.png new file mode 100644 index 00000000..668956d0 Binary files /dev/null and b/public_html/img/404.png differ diff --git a/public_html/index.html b/public_html/index.html index e6e384d7..eba9e20f 100644 --- a/public_html/index.html +++ b/public_html/index.html @@ -46,6 +46,7 @@ + @@ -105,6 +106,7 @@ + diff --git a/public_html/js/Presenters/HttpErrorPresenter.js b/public_html/js/Presenters/HttpErrorPresenter.js new file mode 100644 index 00000000..7e1a9b38 --- /dev/null +++ b/public_html/js/Presenters/HttpErrorPresenter.js @@ -0,0 +1,48 @@ +var App = App || {}; +App.Presenters = App.Presenters || {}; + +App.Presenters.HttpErrorPresenter = function( + jQuery, + promise, + util, + topNavigationPresenter) { + + var $el = jQuery('#content'); + var templates = {}; + + function init(params, loaded) { + topNavigationPresenter.changeTitle('Error ' + params.error); + + if (params.error === 404) { + promise.wait(util.promiseTemplate('404')) + .then(function(template) { + templates.errorPage = template; + reinit(params, loaded); + }).fail(function() { + console.log(arguments); + loaded(); + }); + } else { + console.log('Not supported.'); + loaded(); + } + } + + function reinit(params, loaded) { + render(); + loaded(); + } + + function render() { + $el.html(templates.errorPage()); + } + + return { + init: init, + reinit: reinit, + render: render, + }; + +}; + +App.DI.register('httpErrorPresenter', ['jQuery', 'promise', 'util', 'topNavigationPresenter'], App.Presenters.HttpErrorPresenter); diff --git a/public_html/js/Router.js b/public_html/js/Router.js index 005a963f..bfc02b25 100644 --- a/public_html/js/Router.js +++ b/public_html/js/Router.js @@ -11,6 +11,7 @@ App.Router = function(_, jQuery, promise, util, appState, presenterManager) { function injectRoutes() { inject('', 'homePresenter'); inject('#/', 'homePresenter'); + inject('#/404', 'httpErrorPresenter', {error: 404}); inject('#/home', 'homePresenter'); inject('#/login', 'loginPresenter'); inject('#/logout', 'logoutPresenter'); @@ -28,20 +29,29 @@ App.Router = function(_, jQuery, promise, util, appState, presenterManager) { inject('#/help(/:tab)', 'helpPresenter'); } - function navigate(url) { - window.location.href = url; + function navigate(url, useBrowserDispatcher) { + if (('pushState' in history) && !useBrowserDispatcher) { + history.pushState('', '', url); + dispatch(); + } else { + window.location.href = url; + } } function navigateToMainPage() { navigate(root); } - function navigateInplace(url) { + function navigateInplace(url, useBrowserDispatcher) { if ('replaceState' in history) { history.replaceState('', '', url); - dispatch(); + if (!useBrowserDispatcher) { + dispatch(); + } else { + location.reload(); + } } else { - navigate(url); + navigate(url, useBrowserDispatcher); } } @@ -90,8 +100,7 @@ App.Router = function(_, jQuery, promise, util, appState, presenterManager) { return true; } } - //todo: 404 - console.log(new Error('Unhandled route: ' + url)); + navigateInplace('#/404', true); return false; } diff --git a/public_html/templates/404.tpl b/public_html/templates/404.tpl new file mode 100644 index 00000000..42fa41a2 --- /dev/null +++ b/public_html/templates/404.tpl @@ -0,0 +1,5 @@ +