From 0bfc4bcdd90876d13ba5a904f52ff955c62f043d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Wenzel?= Date: Mon, 16 Oct 2017 00:20:54 +0200 Subject: [PATCH] feat(router): viewport defaults Adds viewPortDefaults to RouterConfiguration so that optional viewports in route configurations can have a default module. Depending on aurelia/templating-router/optional-viewports. Closes aurelia/router#482. --- src/navigation-plan.js | 33 ++++++++++++++++++++++++--------- src/router-configuration.js | 5 +++++ src/router.js | 5 +++++ 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/navigation-plan.js b/src/navigation-plan.js index f29ca19b..c8f27c74 100644 --- a/src/navigation-plan.js +++ b/src/navigation-plan.js @@ -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. @@ -38,9 +38,13 @@ 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); @@ -48,15 +52,26 @@ export function _buildNavigationPlan(instruction: NavigationInstruction, forceLi } } - 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); @@ -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; diff --git a/src/router-configuration.js b/src/router-configuration.js index 79ba9a34..4e9d6157 100644 --- a/src/router-configuration.js +++ b/src/router-configuration.js @@ -11,6 +11,7 @@ export class RouterConfiguration { pipelineSteps: Array = []; title: string; unknownRouteConfig: any; + viewPortDefaults: any; /** * Adds a step to be run during the [[Router]]'s navigation pipeline. @@ -174,5 +175,9 @@ export class RouterConfiguration { pipelineProvider.addStep(name, step); } } + + if (this.viewPortDefaults) { + router.viewPortDefaults = this.viewPortDefaults; + } } } diff --git a/src/router.js b/src/router.js index c96ead0f..c7fa11c8 100644 --- a/src/router.js +++ b/src/router.js @@ -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