Matteo Scandolo | a3844ec | 2016-06-02 15:45:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | (function () { |
| 18 | 'use strict'; |
| 19 | |
| 20 | angular.module('mCord') |
| 21 | .directive('header', function ($location, User) { |
| 22 | return { |
| 23 | restrict: 'E', |
| 24 | templateUrl: 'app/components/header/header.html', |
| 25 | controllerAs: 'vm', |
| 26 | controller: function() { |
| 27 | |
| 28 | this.user = User.isLoggedIn(); |
| 29 | |
| 30 | this.logout = () => { |
| 31 | User.logout() |
| 32 | .then(function(){ |
| 33 | $location.path('/login'); |
| 34 | }); |
| 35 | }; |
| 36 | } |
| 37 | }; |
| 38 | }) |
| 39 | .directive('closeOnRouteChange', function(){ |
| 40 | return { |
| 41 | restrict: 'A', |
| 42 | link: function(scope, elem){ |
| 43 | scope.$on('$routeChangeStart', function() { |
| 44 | elem.removeClass('in'); |
| 45 | }); |
| 46 | } |
| 47 | } |
| 48 | }) |
| 49 | .directive('activeClass', function($location) { |
| 50 | return { |
| 51 | restrict: 'A', |
| 52 | link: function($rootScope, $element, $attrs) { |
| 53 | const links = angular.element($element[0]).find('a'); |
| 54 | let urlMap = {}; |
| 55 | let activeClass = $attrs.activeClass || 'active'; |
| 56 | for (let i = links.length - 1; i >= 0; i--) { |
| 57 | let link = angular.element(links[i]); |
| 58 | let url = link.attr('href'); |
| 59 | |
| 60 | if (url.substring(0, 1) === '#') { |
| 61 | urlMap[url.substring(1)] = link; |
| 62 | } else { |
| 63 | urlMap[url] = link; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | const activateItem = () => { |
| 68 | var path = urlMap[$location.path()]; |
| 69 | links.parent('li').removeClass(activeClass); |
| 70 | if (path) { |
| 71 | path.parent('li').addClass(activeClass); |
| 72 | } |
| 73 | } |
| 74 | activateItem(); |
| 75 | $rootScope.$on('$stateChangeStart', activateItem); |
| 76 | } |
| 77 | } |
| 78 | }); |
| 79 | }()); |