Skip to content

Commit

Permalink
feat(router): viewport defaults
Browse files Browse the repository at this point in the history
Adds viewPortDefaults to RouterConfiguration so that optional viewports in route configurations can have a default module.

Depending on aurelia/templating-router/optional-viewports.
Closes #482.
  • Loading branch information
jwx committed Oct 15, 2017
1 parent 8878b45 commit 0bfc4bc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/navigation-plan.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Redirect} from './navigation-commands';
import {_resolveUrl} from './util';
import { Redirect } from './navigation-commands';
import { _resolveUrl } from './util';

/**
* The strategy to use when activating modules during navigation.
Expand Down Expand Up @@ -38,25 +38,40 @@ export function _buildNavigationPlan(instruction: NavigationInstruction, forceLi

let newParams = prev ? hasDifferentParameterValues(prev, instruction) : true;

let viewPortDefaults = Object.assign({}, instruction.router.viewPortDefaults);

if (prev && !config.explicitViewPorts) {
for (let viewPortName in prev.viewPortInstructions) {
let viewPortPlan = buildViewPortPlan(instruction, forceLifecycleMinimum, newParams, viewPortName, true);
delete viewPortDefaults[viewPortName.split('.')[0]];

let viewPortPlan = buildViewPortPlan(instruction, instruction.config.viewPorts, forceLifecycleMinimum, newParams, viewPortName, true);
plan[viewPortPlan.name] = viewPortPlan.plan;
if (viewPortPlan.task) {
pending.push(viewPortPlan.task);
}
}
}

let viewPortNames = {};
let viewPorts = {};

if (viewPortDefaults) {
for (let viewPortName in viewPortDefaults) {
if (config.viewPorts[viewPortName] === undefined) {
viewPorts[viewPortName] = viewPortDefaults[viewPortName];
}
}
}

if (config.viewPorts) {
for (let viewPortName in config.viewPorts) {
viewPortNames[viewPortName] = config.viewPorts[viewPortName];
if (config.viewPorts[viewPortName] !== undefined || !viewPorts[viewPortName]) {
viewPorts[viewPortName] = config.viewPorts[viewPortName];
}
}
}

for (let viewPortName in viewPortNames) {
let viewPortPlan = buildViewPortPlan(instruction, forceLifecycleMinimum, newParams, viewPortName, false);
for (let viewPortName in viewPorts) {
let viewPortPlan = buildViewPortPlan(instruction, viewPorts, forceLifecycleMinimum, newParams, viewPortName, false);
plan[viewPortPlan.name] = viewPortPlan.plan;
if (viewPortPlan.task) {
pending.push(viewPortPlan.task);
Expand All @@ -66,13 +81,13 @@ export function _buildNavigationPlan(instruction: NavigationInstruction, forceLi
return Promise.all(pending).then(() => plan);
}

function buildViewPortPlan(instruction: NavigationInstruction, forceLifecycleMinimum, newParams: boolean, viewPortName: string, previous: boolean) {
function buildViewPortPlan(instruction: NavigationInstruction, viewPorts: any, forceLifecycleMinimum, newParams: boolean, viewPortName: string, previous: boolean) {
let plan = {};
let prev = instruction.previousInstruction;
let config = instruction.config;
let configViewPortName = viewPortName;
let prevViewPortInstruction = prev ? prev.viewPortInstructions[viewPortName] : undefined;
let nextViewPortConfig = !previous ? config.viewPorts[configViewPortName] : undefined;
let nextViewPortConfig = !previous ? viewPorts[configViewPortName] : undefined;

if (config.explicitViewPorts && nextViewPortConfig === undefined) {
nextViewPortConfig = null;
Expand Down
5 changes: 5 additions & 0 deletions src/router-configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class RouterConfiguration {
pipelineSteps: Array<Function|PipelineStep> = [];
title: string;
unknownRouteConfig: any;
viewPortDefaults: any;

/**
* Adds a step to be run during the [[Router]]'s navigation pipeline.
Expand Down Expand Up @@ -174,5 +175,9 @@ export class RouterConfiguration {
pipelineProvider.addStep(name, step);
}
}

if (this.viewPortDefaults) {
router.viewPortDefaults = this.viewPortDefaults;
}
}
}
5 changes: 5 additions & 0 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ export class Router {

options: any = {};

/**
* The defaults used when a viewport lacks specified content
*/
viewPortDefaults: any;

/**
* Extension point to transform the document title before it is built and displayed.
* By default, child routers delegate to the parent router, and the app router
Expand Down

0 comments on commit 0bfc4bc

Please sign in to comment.