UNPKG

acha-framework

Version:

is a modular framework on both client (angular.js) and server (node.js) side, it provides security, orm, ioc, obfuscation and ...

131 lines 4.74 kB
(function ($, angular, underscore, window, document, undefined) { 'use strict'; angular.module('frontend.directives').directive('sideNavigation', [ '$rootScope', '$route', 'sideMenuService', 'cultureService', function ($rootScope, $route, sideMenuService, cultureService) { return { restrict: 'E', replace: true, scope: {}, templateUrl: '/templates/framework/panel/directives/side-navigation/template.html', link: function (scope, element, attr) { scope.vm = { dataSource: sideMenuService.menus, selectedMenu: null, selectedSub: null }; scope.vm.init = function () { scope.vm.bind(); scope.vm.preSelect(); }; scope.vm.getMenuClass = function (menu) { var cls = scope.vm.selectedMenu === menu ? 'active' : ''; if (!cls) return cls; if (menu.type || menu.categories && menu.categories.length) { cls += ' open'; } return cls; }; scope.vm.getSubMenuClass = function (sub) { return sub === scope.vm.selectedSub ? 'active' : ''; }; scope.vm.subMenuAction = function (sub) { scope.vm.selectedSub = sub; if (sub.action) { sub.action(sub); } }; scope.vm.menuAction = function (menu) { scope.vm.selectedMenu = menu; if (menu.action) { menu.action(menu); } }; scope.vm.preSelect = function () { var menuFound = false; var hash = window.location.hash; scope.vm.dataSource.forEach(function (item) { if (item.url === hash) { menuFound = true; scope.vm.selectedMenu = item; scope.vm.findSub(item); } }); if (!menuFound) scope.vm.findSub(); }; scope.vm.findSub = function (menu) { var k, i, j; var hash = window.location.hash; var subFound = false, menuFound = false; if (menu && menu.categories) { for (i = 0; i < menu.categories.length; i++) { for (j = 0; j < menu.categories[i].menus.length; j++) { if (menu.categories[i].menus[j].href === hash) { subFound = true; scope.vm.selectedSub = menu.categories[i].menus[j]; break; } } if (subFound) break; } return; } for (k = 0; k < scope.vm.dataSource.length; k++) { if (scope.vm.dataSource[k].href === hash) { scope.vm.selectedMenu = scope.vm.dataSource[k]; menuFound = true; } if (!scope.vm.dataSource[k].categories) continue; for (i = 0; i < scope.vm.dataSource[k].categories.length; i++) { for (j = 0; j < scope.vm.dataSource[k].categories[i].menus.length; j++) { if (scope.vm.dataSource[k].categories[i].menus[j].href === hash) { subFound = true; scope.vm.selectedMenu = scope.vm.dataSource[k]; scope.vm.selectedSub = scope.vm.dataSource[k].categories[i].menus[j]; break; } } if (subFound) break; } if (subFound || menuFound) break; } }; scope.vm.onWindowResize = function () { element.slimscroll({ destroy: true }); scope.vm.create(); }; scope.vm.create = function () { element.slimscroll({ touchScrollStep: 80, height: $(window).height() - 175, position: cultureService.rtl ? 'left' : 'right', size: '3px', color: '#29c7ca' }); }; scope.vm.bind = function () { $(window).on('resize', scope.vm.onWindowResize); scope.$on('$destroy', function () { element.slimscroll({ destroy: true }); $(window).off('resize', scope.vm.onWindowResize); }); $rootScope.$on('$routeChangeSuccess', function () { scope.vm.preSelect(); }); setTimeout(scope.vm.create, 500); }; scope.vm.init(); } }; } ]); }(jQuery, angular, _, window, document));