diff --git a/src/grammar.ats b/src/grammar.ats index 8ef066a..fe29fee 100644 --- a/src/grammar.ats +++ b/src/grammar.ats @@ -71,6 +71,43 @@ export class Grammar { return instruction; } + recognizeComponent(name:string, parentComponentName = '/') { + if (typeof name === 'undefined') { + return; + } + + var componentRecognizer = this.rules[parentComponentName]; + if (!componentRecognizer) { + return; + } + + var handlers = componentRecognizer.handlersFor(name); + if (!handlers) { + return; + } + + var handler = handlers[0].handler; + + var instruction = { + viewports: {}, + params: {}, + canonicalUrl: handler.path + }; + + + forEach(handler.components, (componentName, viewportName) => { + instruction.viewports[viewportName] = { + viewports: {} + }; + }); + + forEach(instruction.viewports, (instruction, componentName) => { + instruction.component = handler.components[componentName]; + }); + + return instruction; + } + generate(name, params) { var path = ''; var solution; @@ -183,6 +220,10 @@ class CanonicalRecognizer { return context; } + handlersFor(componentName) { + return this.recognizer.handlersFor(componentName); + } + generate(name, params) { return this.recognizer.generate(name, params); } diff --git a/src/router.ats b/src/router.ats index 89d3856..1db1a8f 100644 --- a/src/router.ats +++ b/src/router.ats @@ -63,7 +63,7 @@ class Router { /** - * @description Navigate to a URL. + * @description Navigate to a URL or Component. * Returns the cannonical URL for the route navigated to. */ navigate(url) { @@ -73,7 +73,7 @@ class Router { this.lastNavigationAttempt = url; - var instruction = this.recognize(url); + var instruction = this.recognize(url) || this.recognizeComponent(url); if (!instruction) { return Promise.reject(); @@ -161,6 +161,10 @@ class Router { return this.registry.recognize(url); } + recognizeComponent(name) { + return this.registry.recognizeComponent(name); + } + /** diff --git a/test/router-viewport.es5.spec.js b/test/router-viewport.es5.spec.js index 53f12c4..9c41f5c 100644 --- a/test/router-viewport.es5.spec.js +++ b/test/router-viewport.es5.spec.js @@ -701,6 +701,18 @@ describe('ngOutlet', function () { expect($router.navigating).toBe(false); }); + it('Should allow navigating by component name', function () { + compile(''); + + $router.config([ + { path: '/', component: 'one' } + ]); + + $router.navigate('one'); + $rootScope.$digest(); + + expect(elt.text()).toBe('one'); + }); function registerComponent(name, template, componentConstructor, routeConfig) { if (!template) {