commit | author | age
|
1759c2
|
1 |
/** |
RM |
2 |
* angular-strap |
|
3 |
* @version v2.0.3 - 2014-05-30 |
|
4 |
* @link http://mgcrea.github.io/angular-strap |
|
5 |
* @author Olivier Louvignes (olivier@mg-crea.com) |
|
6 |
* @license MIT License, http://www.opensource.org/licenses/MIT |
|
7 |
*/ |
|
8 |
'use strict'; |
|
9 |
angular.module('mgcrea.ngStrap.navbar', []).provider('$navbar', function () { |
|
10 |
var defaults = this.defaults = { |
|
11 |
activeClass: 'active', |
|
12 |
routeAttr: 'data-match-route', |
|
13 |
strict: false |
|
14 |
}; |
|
15 |
this.$get = function () { |
|
16 |
return { defaults: defaults }; |
|
17 |
}; |
|
18 |
}).directive('bsNavbar', [ |
|
19 |
'$window', |
|
20 |
'$location', |
|
21 |
'$navbar', |
|
22 |
function ($window, $location, $navbar) { |
|
23 |
var defaults = $navbar.defaults; |
|
24 |
return { |
|
25 |
restrict: 'A', |
|
26 |
link: function postLink(scope, element, attr, controller) { |
|
27 |
// Directive options |
|
28 |
var options = angular.copy(defaults); |
|
29 |
angular.forEach(Object.keys(defaults), function (key) { |
|
30 |
if (angular.isDefined(attr[key])) |
|
31 |
options[key] = attr[key]; |
|
32 |
}); |
|
33 |
// Watch for the $location |
|
34 |
scope.$watch(function () { |
|
35 |
return $location.path(); |
|
36 |
}, function (newValue, oldValue) { |
|
37 |
var liElements = element[0].querySelectorAll('li[' + options.routeAttr + ']'); |
|
38 |
angular.forEach(liElements, function (li) { |
|
39 |
var liElement = angular.element(li); |
|
40 |
var pattern = liElement.attr(options.routeAttr).replace('/', '\\/'); |
|
41 |
if (options.strict) { |
|
42 |
pattern = '^' + pattern + '$'; |
|
43 |
} |
|
44 |
var regexp = new RegExp(pattern, ['i']); |
|
45 |
if (regexp.test(newValue)) { |
|
46 |
liElement.addClass(options.activeClass); |
|
47 |
} else { |
|
48 |
liElement.removeClass(options.activeClass); |
|
49 |
} |
|
50 |
}); |
|
51 |
}); |
|
52 |
} |
|
53 |
}; |
|
54 |
} |
|
55 |
]); |