1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
/**
* Set a $uiRoute boolean to see if the current route matches
*/
angular.module('ui.directives').directive('uiRoute', ['$location', '$parse', function ($location, $parse) {
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 ' + tElement[0]);
}
return function ($scope, elm, attrs) {
var modelSetter = $parse(attrs.ngModel || attrs.routeModel || '$uiRoute').assign;
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() {
modelSetter($scope, ($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']);
modelSetter($scope, 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();
});
}
}
};
}]);
|