This repository was archived by the owner on Feb 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 354
feat(route) prototyping a new route checking directive #374
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bdc9cdc
feat(route) prototyping a new route checking directive
1e89e69
fix(route) working on fleshing out tests and refactoring structure
834557e
fix(route) adjusted priority and added comment
f0ea1a4
Corrected uiRoute regex
ad322e5
fix(route) added support for # and changed flag to $uiRoute
f932ee9
fix(route) Hack to work around attrs.uiRoute being undefined (YOU LYI…
8438919
Checking properties at compile time instead of link time
54f89c1
fix(route) slowly working towards success!
6771078
fix(route) FINALLY completed ui-route directive
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /** | ||
| * Set a $uiRoute boolean to see if the current route matches | ||
| */ | ||
| angular.module('ui.directives').directive('uiRoute', ['$location', function ($location) { | ||
| return { | ||
| restrict: 'AC', | ||
| compile: function(tElement, tAttrs) { | ||
| var useProperty; | ||
| if (tAttrs.uiRoute) { | ||
| useProperty = 'uiRoute'; | ||
| } else if (tAttrs.ngHref) { | ||
| useProperty = 'ngHref'; | ||
| } else if (tAttrs.href) { | ||
| useProperty = 'href'; | ||
| } else { | ||
| throw new Error('uiRoute missing a route or href property on ' + elm[0]); | ||
| } | ||
| return function ($scope, elm, attrs) { | ||
| var watcher = angular.noop; | ||
|
|
||
| // Used by href and ngHref | ||
| function staticWatcher(newVal) { | ||
| if ((hash = newVal.indexOf('#')) > -1) | ||
| newVal = newVal.substr(hash + 1); | ||
| watcher = function watchHref() { | ||
| $scope.$uiRoute = ($location.path().indexOf(newVal) > -1); | ||
| }; | ||
| watcher(); | ||
| } | ||
| // Used by uiRoute | ||
| function regexWatcher(newVal) { | ||
| if ((hash = newVal.indexOf('#')) > -1) | ||
| newVal = newVal.substr(hash + 1); | ||
| watcher = function watchRegex() { | ||
| var regexp = new RegExp('^' + newVal + '$', ['i']); | ||
| $scope.$uiRoute = regexp.test($location.path()); | ||
| }; | ||
| watcher(); | ||
| } | ||
|
|
||
| switch (useProperty) { | ||
| case 'uiRoute': | ||
| // if uiRoute={{}} this will be undefined, otherwise it will have a value and $observe() never gets triggered | ||
| if (attrs.uiRoute) | ||
| regexWatcher(attrs.uiRoute); | ||
| else | ||
| attrs.$observe('uiRoute', regexWatcher); | ||
| break; | ||
| case 'ngHref': | ||
| // Setup watcher() every time ngHref changes | ||
| if (attrs.ngHref) | ||
| staticWatcher(attrs.ngHref); | ||
| else | ||
| attrs.$observe('ngHref', staticWatcher); | ||
| break; | ||
| case 'href': | ||
| // Setup watcher() | ||
| staticWatcher(attrs.href); | ||
| } | ||
|
|
||
| $scope.$on('$routeChangeSuccess', function(){ | ||
| watcher(); | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
| }]); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /*global describe, beforeEach, module, inject, it, spyOn, expect, $ */ | ||
| describe('uiRoute', function () { | ||
| 'use strict'; | ||
|
|
||
| var scope, $compile, $location; | ||
| beforeEach(module('ui.directives')); | ||
| beforeEach(inject(function (_$rootScope_, _$compile_, _$window_, _$location_) { | ||
| scope = _$rootScope_.$new(); | ||
| $compile = _$compile_; | ||
| $location = _$location_; | ||
| })); | ||
|
|
||
| function setPath(path) { | ||
| $location.path(path); | ||
| scope.$broadcast('$routeChangeSuccess'); | ||
| scope.$apply(); | ||
| } | ||
|
|
||
| describe('with uiRoute defined', function(){ | ||
| it('should use the uiRoute property', function(){ | ||
| $compile('<div ui-route="/foo" />')(scope); | ||
| }); | ||
| it('should update $uiRoute on $observe', function(){ | ||
| setPath('/bar'); | ||
| scope.$apply('foobar = "foo"'); | ||
| $compile('<div ui-route="/{{foobar}}" />')(scope); | ||
| expect(scope.$uiRoute).toBeFalsy(); | ||
| scope.$apply('foobar = "bar"'); | ||
| expect(scope.$uiRoute).toBe(true); | ||
| scope.$apply('foobar = "foo"'); | ||
| expect(scope.$uiRoute).toBe(false); | ||
| }); | ||
| it('should support regular expression', function(){ | ||
| setPath('/foo/123'); | ||
| $compile('<div ui-route="/foo/[0-9]*" />')(scope); | ||
| expect(scope.$uiRoute).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('with ngHref defined', function(){ | ||
|
|
||
| iit('should use the ngHref property', function(){ | ||
| setPath('/foo'); | ||
| $compile('<a ng-href="/foo" ui-route />')(scope); | ||
| expect(scope.$uiRoute).toBe(true); | ||
| }); | ||
| it('should update $uiRoute on $observe', function(){ | ||
| setPath('/bar'); | ||
| scope.$apply('foobar = "foo"'); | ||
| $compile('<a ng-href="/{{foobar}}" ui-route />')(scope); | ||
| expect(scope.$uiRoute).toBeFalsy(); | ||
| scope.$apply('foobar = "bar"'); | ||
| expect(scope.$uiRoute).toBe(true); | ||
| scope.$apply('foobar = "foo"'); | ||
| expect(scope.$uiRoute).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('with href defined', function(){ | ||
|
|
||
| it('should use the href property', function(){ | ||
| setPath('/foo'); | ||
| $compile('<a href="/foo" ui-route />')(scope); | ||
| expect(scope.$uiRoute).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| it('should throw an error if no route property available', function(){ | ||
| expect(function(){ | ||
| $compile('<div ui-route/>')(scope); | ||
| }).toThrow(); | ||
| }); | ||
|
|
||
| it('should update $uiRoute on route change', function(){ | ||
| setPath('/bar'); | ||
| $compile('<div ui-route="/foo" />')(scope); | ||
| expect(scope.$uiRoute).toBeFalsy(); | ||
| setPath('/foo'); | ||
| expect(scope.$uiRoute).toBe(true); | ||
| setPath('/bar'); | ||
| expect(scope.$uiRoute).toBe(false); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably should restrict A