From 8f4ef28475648ce3e03ba45441617acd2e28df81 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sun, 2 Aug 2015 17:26:57 +0930 Subject: [PATCH] Make the back button a functional link --- js/admin/src/initializers/boot.js | 5 ++++- js/forum/src/utils/History.js | 11 +++++++++++ js/lib/components/Navigation.js | 13 ++++++++++--- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/js/admin/src/initializers/boot.js b/js/admin/src/initializers/boot.js index 1463ed9d5..ff2760bd8 100644 --- a/js/admin/src/initializers/boot.js +++ b/js/admin/src/initializers/boot.js @@ -31,7 +31,10 @@ export default function boot(app) { app.alerts = m.mount(document.getElementById('alerts'), AlertManager.component()); app.history = { canGoBack: () => true, - back: () => window.location = '/' + backUrl: () => app.forum.attribute('baseUrl'), + back: function() { + window.location = this.backUrl(); + } }; m.route.mode = 'hash'; diff --git a/js/forum/src/utils/History.js b/js/forum/src/utils/History.js index f687aafef..c52088422 100644 --- a/js/forum/src/utils/History.js +++ b/js/forum/src/utils/History.js @@ -85,6 +85,17 @@ export default class History { m.route(this.getTop().url); } + /** + * Get the URL of the previous page. + * + * @public + */ + backUrl() { + const secondTop = this.stack[this.stack.length - 2]; + + return secondTop.url; + } + /** * Go to the first route in the history stack. * diff --git a/js/lib/components/Navigation.js b/js/lib/components/Navigation.js index 131b29d65..2034a175d 100644 --- a/js/lib/components/Navigation.js +++ b/js/lib/components/Navigation.js @@ -1,5 +1,6 @@ import Component from 'flarum/Component'; import Button from 'flarum/components/Button'; +import LinkButton from 'flarum/components/LinkButton'; /** * The `Navigation` component displays a set of navigation buttons. Typically @@ -47,10 +48,16 @@ export default class Navigation extends Component { getBackButton() { const {history} = app; - return Button.component({ + return LinkButton.component({ className: 'Button Button--icon Navigation-back', - onclick: history.back.bind(history), - icon: 'chevron-left' + href: history.backUrl(), + icon: 'chevron-left', + config: () => {}, + onclick: e => { + if (e.shiftKey || e.ctrlKey || e.metaKey || e.which === 2) return; + e.preventDefault(); + history.back(); + } }); }