From 5ac335d2a8ec908f58c0e879ef61572e18f2b214 Mon Sep 17 00:00:00 2001 From: Bryan Smith Date: Sun, 3 May 2015 22:30:28 -0700 Subject: [PATCH] feat(nav-model): allow title to be set at any time by an active navModel fixes #69 --- doc/api.json | 2 +- src/nav-model.js | 38 ++++++++++++++++++++++++++++--------- src/navigation-context.js | 13 ++++++++----- src/router-configuration.js | 1 + src/router.js | 11 +++++++++++ 5 files changed, 50 insertions(+), 15 deletions(-) diff --git a/doc/api.json b/doc/api.json index c8e7b760..3fce0c54 100644 --- a/doc/api.json +++ b/doc/api.json @@ -1 +1 @@ -{"classes":[{"name":"NavModel","file":"/Users/bsmith/Code/router/src/nav-model.js","line":1,"description":"Class for storing and interacting with a route's navigation settings","is_constructor":1,"methods":[],"properties":[{"line":12,"description":"True if this nav item is currently active.","name":"isActive","type":"Boolean"},{"line":19,"description":"The title.","name":"title","type":"String"},{"line":26,"description":"This nav item's absolute href.","name":"href","type":"String"}],"events":[]},{"name":"Redirect","file":"/Users/bsmith/Code/router/src/navigation-commands.js","line":13,"description":"Used during the activation lifecycle to cause a redirect.","is_constructor":1,"params":[{"name":"url","description":"The url to redirect to.","type":"String"}],"methods":[{"line":27,"description":"Called by the activation system to set the child router.","name":"setRouter","params":[{"name":"router","description":"","type":"Router"}]},{"line":37,"description":"Called by the navigation pipeline to navigate.","name":"navigate","params":[{"name":"appRouter","description":"- a router which should redirect","type":"Router"}]}],"properties":[],"events":[]}],"methods":[],"properties":[],"events":[]} \ No newline at end of file +{"classes":[{"name":"NavModel","file":"/Users/bsmith/Code/router/src/nav-model.js","line":1,"description":"Class for storing and interacting with a route's navigation settings","is_constructor":1,"methods":[{"line":48,"description":"Sets the route's title and updates document.title.\n If the a navigation is in progress, the change will be applied\n to document.title when the navigation completes.","name":"setTitle","params":[{"name":"title","description":"The new title.","type":"String"}]}],"properties":[{"line":12,"description":"True if this nav item is currently active.","name":"isActive","type":"Boolean"},{"line":19,"description":"The title.","name":"title","type":"String"},{"line":26,"description":"This nav item's absolute href.","name":"href","type":"String"},{"line":33,"description":"Data attached to the route at configuration time.","name":"settings","type":"Any"},{"line":40,"description":"The route config.","name":"config","type":"Object"}],"events":[]},{"name":"Redirect","file":"/Users/bsmith/Code/router/src/navigation-commands.js","line":13,"description":"Used during the activation lifecycle to cause a redirect.","is_constructor":1,"params":[{"name":"url","description":"The url to redirect to.","type":"String"}],"methods":[{"line":27,"description":"Called by the activation system to set the child router.","name":"setRouter","params":[{"name":"router","description":"","type":"Router"}]},{"line":37,"description":"Called by the navigation pipeline to navigate.","name":"navigate","params":[{"name":"appRouter","description":"- a router which should redirect","type":"Router"}]}],"properties":[],"events":[]}],"methods":[],"properties":[],"events":[]} \ No newline at end of file diff --git a/src/nav-model.js b/src/nav-model.js index 6e0a0896..f3d580f7 100644 --- a/src/nav-model.js +++ b/src/nav-model.js @@ -29,15 +29,35 @@ export class NavModel { * @property {String} href */ this.href = null; + + /** + * Data attached to the route at configuration time. + * + * @property {any} settings + */ + this.settings = {}; + + /** + * The route config. + * + * @property {Object} config + */ + this.config = null; } - /** - * Sets the route's title and updates document.title. - * If the a navigation is in progress, the change will be applied - * to document.title when the navigation completes. - * @param {String} title The new title. - */ - setTitle(title) { - this.title = title; - } + /** + * Sets the route's title and updates document.title. + * If the a navigation is in progress, the change will be applied + * to document.title when the navigation completes. + * + * @method setTitle + * @param {String} title The new title. + */ + setTitle(title) { + this.title = title; + + if (this.isActive) { + this.router.updateTitle(); + } + } } \ No newline at end of file diff --git a/src/navigation-context.js b/src/navigation-context.js index a7152751..6ddccc5f 100644 --- a/src/navigation-context.js +++ b/src/navigation-context.js @@ -79,6 +79,13 @@ export class NavigationContext { }); } + updateTitle() { + let title = this.buildTitle(); + if (title) { + document.title = title; + } + } + buildTitle(separator=' | ') { var next = this.nextInstruction, title = next.config.navModel.title || '', @@ -111,11 +118,7 @@ export class NavigationContext { export class CommitChangesStep { run(navigationContext, next) { return navigationContext.commitChanges(true).then(() => { - var title = navigationContext.buildTitle(); - if (title) { - document.title = title; - } - + navigationContext.updateTitle(); return next(); }); } diff --git a/src/router-configuration.js b/src/router-configuration.js index de693364..9c1bc409 100644 --- a/src/router-configuration.js +++ b/src/router-configuration.js @@ -107,6 +107,7 @@ export class RouterConfiguration{ config.route = ensureConfigValue(config, 'route', this.deriveRoute); config.title = ensureConfigValue(config, 'title', this.deriveTitle); config.moduleId = ensureConfigValue(config, 'moduleId', this.deriveModuleId); + config.settings = config.settings || {}; } deriveName(config) { diff --git a/src/router.js b/src/router.js index ee347c1b..e3866dc5 100644 --- a/src/router.js +++ b/src/router.js @@ -3,6 +3,7 @@ import {RouteRecognizer} from 'aurelia-route-recognizer'; import {join} from 'aurelia-path'; import {NavigationContext} from './navigation-context'; import {NavigationInstruction} from './navigation-instruction'; +import {NavModel} from './nav-model'; import {RouterConfiguration} from './router-configuration'; import {processPotential} from './util'; @@ -175,6 +176,8 @@ export class Router { navModel.title = config.title; navModel.order = config.nav; navModel.href = config.href; + navModel.settings = config.settings; + navModel.config = config; return navModel; } @@ -263,6 +266,14 @@ export class Router { this.catchAllHandler = callback; } + updateTitle() { + if (this.parent) { + return this.parent.updateTitle(); + } + + this.currentInstruction.navigationContext.updateTitle(); + } + reset() { this.fallbackOrder = 100; this.recognizer = new RouteRecognizer();