forked from mgcrea/angular-strap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnavbar.js
More file actions
65 lines (44 loc) · 1.51 KB
/
navbar.js
File metadata and controls
65 lines (44 loc) · 1.51 KB
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
'use strict';
angular.module('mgcrea.ngStrap.navbar', [])
.provider('$navbar', function () {
var defaults = this.defaults = {
activeClass: 'active',
routeAttr: 'data-match-route',
strict: false
};
this.$get = function () {
return {defaults: defaults};
};
})
.directive('bsNavbar', function ($window, $location, $navbar) {
var defaults = $navbar.defaults;
return {
restrict: 'A',
link: function postLink (scope, element, attr, controller) {
// Directive options
var options = angular.copy(defaults);
angular.forEach(Object.keys(defaults), function (key) {
if (angular.isDefined(attr[key])) options[key] = attr[key];
});
// Watch for the $location
scope.$watch(function () {
return $location.path();
}, function (newValue, oldValue) {
var liElements = element[0].querySelectorAll('li[' + options.routeAttr + ']');
angular.forEach(liElements, function (li) {
var liElement = angular.element(li);
var pattern = liElement.attr(options.routeAttr).replace('/', '\\/');
if (options.strict) {
pattern = '^' + pattern + '$';
}
var regexp = new RegExp(pattern, 'i');
if (regexp.test(newValue)) {
liElement.addClass(options.activeClass);
} else {
liElement.removeClass(options.activeClass);
}
});
});
}
};
});