1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-05 10:39:25 +02:00

- New nav menu active links work.

This commit is contained in:
Gunther Brunner 2014-03-25 22:17:40 +09:00
parent 593e14a80a
commit 533ce2d967
4 changed files with 27 additions and 26 deletions

View file

@ -1,31 +1,42 @@
// Based from https://ryankaskel.com/blog/2013/05/27/a-different-approach-to-angularjs-navigation-menus
module.exports = function ($location) {
return function (scope, element, attrs) {
var links = element.find('a')
var onClass = attrs.navMenu || 'current'
var routePattern
var routeBasePattern = /\/#[^/]*/ // TODO: add regex to remove last part of the url
var link
var url
var currentLink
var urlMap = {}
var urlMap = []
var i
if (!$location.$$html5) {
routePattern = /^#[^/]*/
routePattern = /\/#[^/]*/
}
for (i = 0; i < links.length; i++) {
link = angular.element(links[i])
url = link.attr('href')
url = link.attr('ng-href')
if ($location.$$html5) {
urlMap[url] = link
urlMap.push({url: url, link: link})
} else {
urlMap[url.replace(routePattern, '')] = link
urlMap.push({url: url.replace(routePattern, ''), link: link})
}
}
scope.$on('$routeChangeStart', function () {
var pathLink = urlMap[$location.path()]
function activateLink() {
var location = $location.path()
var pathLink = ''
for (i = 0; i < urlMap.length; ++i) {
if (urlMap[i].url == location) {
pathLink = urlMap[i].link
}
}
if (pathLink) {
if (currentLink) {
@ -34,6 +45,9 @@ module.exports = function ($location) {
currentLink = pathLink;
currentLink.addClass(onClass);
}
})
}
activateLink()
scope.$on('$routeChangeStart', activateLink)
}
}

View file

@ -6,7 +6,7 @@ module.exports = angular.module('device-control', [
require('stf/screen').name
])
.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/devices/:serial', {
$routeProvider.when('/control/:serial', {
template: require('./device-control.jade'),
controller: 'DeviceControlCtrl'
})

View file

@ -1,16 +1,3 @@
module.exports = function MenuCtrl($scope) {
$scope.isActive = function (viewLocation) {
var pattern = '/' + viewLocation,
re = new RegExp(pattern);
return re.test($location.path());
};
$scope.isActive = function (path) {
console.log($location.path())
console.log($location.path().substr(0, path.length))
return $location.path().substr(0, path.length) == path;
}
}

View file

@ -1,15 +1,15 @@
.navbar.stf-menu
.container-fluid.stf-top-bar
a.stf-logo(href="/") STF 2
ul.nav.stf-nav(nav-menu)
ul.nav.stf-nav(nav-menu='current')
li(ng-cloak)
a(href='/#!/control')
a(ng-href='/#!/control')
span.fa.fa-mobile
| {{"Control"|translate}}
a(href='/#!/devices')
a(ng-href='/#!/devices')
span.fa.fa-sitemap
| {{"Devices"|translate}}
a(href='/#!/settings')
a(ng-href='/#!/settings')
span.fa.fa-gears
| {{"Settings"|translate}}
ul.nav.stf-nav.stf-feedback.pull-right(ng-cloak)