Skip to content

Commit

Permalink
feat(nav-model): allow title to be set at any time by an active navModel
Browse files Browse the repository at this point in the history
fixes #69
  • Loading branch information
bryanrsmith committed May 9, 2015
1 parent 6f702a9 commit 5ac335d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
2 changes: 1 addition & 1 deletion doc/api.json
Original file line number Diff line number Diff line change
@@ -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":[]}
{"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":[]}
38 changes: 29 additions & 9 deletions src/nav-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
13 changes: 8 additions & 5 deletions src/navigation-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || '',
Expand Down Expand Up @@ -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();
});
}
Expand Down
1 change: 1 addition & 0 deletions src/router-configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 5ac335d

Please sign in to comment.