refactor(sidebar): move sidebar toggling item controller to separate service

pull/46/head
Vladimir Lugovsky 2016-05-13 17:34:12 +03:00
parent 5f3efda01d
commit 0e3bd5b228
1 changed files with 55 additions and 51 deletions

View File

@ -9,6 +9,7 @@
.directive('baSidebarToggleMenu', baSidebarToggleMenu)
.directive('baSidebarCollapseMenu', baSidebarCollapseMenu)
.directive('baSidebarTogglingItem', baSidebarTogglingItem)
.controller('BaSidebarTogglingItemCtrl', BaSidebarTogglingItemCtrl)
.directive('baUiSrefTogglingSubmenu', baUiSrefTogglingSubmenu)
.directive('baUiSrefToggler', baUiSrefToggler);
@ -45,62 +46,65 @@
}
/** @ngInject */
function baSidebarTogglingItem($state, baSidebarService) {
function baSidebarTogglingItem() {
return {
restrict: 'A',
controller: function ($scope, $element, $attrs) {
var vm = this;
var menuItem = vm.$$menuItem = $scope.$eval($attrs.baSidebarTogglingItem);
if (menuItem && menuItem.subMenu && menuItem.subMenu.length) {
vm.$$expandSubmenu = function() { console.warn('$$expandMenu should be overwritten by baUiSrefTogglingSubmenu') };
vm.$$collapseSubmenu = function() { console.warn('$$collapseSubmenu should be overwritten by baUiSrefTogglingSubmenu') };
var subItemsStateRefs = baSidebarService.getAllStateRefsRecursive(menuItem);
vm.$expand = function() {
vm.$$expandSubmenu();
$element.addClass('ba-sidebar-item-expanded');
};
vm.$collapse = function() {
vm.$$collapseSubmenu();
$element.removeClass('ba-sidebar-item-expanded');
};
vm.$toggle = function() {
$element.hasClass('ba-sidebar-item-expanded') ?
vm.$collapse() :
vm.$expand();
};
if (_isState($state.current)) {
$element.addClass('ba-sidebar-item-expanded');
}
$scope.$on('$stateChangeStart', function (event, toState) {
if (!_isState(toState) && $element.hasClass('ba-sidebar-item-expanded')) {
vm.$collapse();
$element.removeClass('ba-sidebar-item-expanded');
}
});
$scope.$on('$stateChangeSuccess', function (event, toState) {
if (_isState(toState) && !$element.hasClass('ba-sidebar-item-expanded')) {
vm.$expand();
$element.addClass('ba-sidebar-item-expanded');
}
});
}
function _isState(state) {
return state && subItemsStateRefs.some(function(subItemState) {
return state.name.indexOf(subItemState) == 0;
});
}
}
controller: 'BaSidebarTogglingItemCtrl'
};
}
/** @ngInject */
function BaSidebarTogglingItemCtrl($scope, $element, $attrs, $state, baSidebarService) {
var vm = this;
var menuItem = vm.$$menuItem = $scope.$eval($attrs.baSidebarTogglingItem);
if (menuItem && menuItem.subMenu && menuItem.subMenu.length) {
vm.$$expandSubmenu = function() { console.warn('$$expandMenu should be overwritten by baUiSrefTogglingSubmenu') };
vm.$$collapseSubmenu = function() { console.warn('$$collapseSubmenu should be overwritten by baUiSrefTogglingSubmenu') };
var subItemsStateRefs = baSidebarService.getAllStateRefsRecursive(menuItem);
vm.$expand = function() {
vm.$$expandSubmenu();
$element.addClass('ba-sidebar-item-expanded');
};
vm.$collapse = function() {
vm.$$collapseSubmenu();
$element.removeClass('ba-sidebar-item-expanded');
};
vm.$toggle = function() {
$element.hasClass('ba-sidebar-item-expanded') ?
vm.$collapse() :
vm.$expand();
};
if (_isState($state.current)) {
$element.addClass('ba-sidebar-item-expanded');
}
$scope.$on('$stateChangeStart', function (event, toState) {
if (!_isState(toState) && $element.hasClass('ba-sidebar-item-expanded')) {
vm.$collapse();
$element.removeClass('ba-sidebar-item-expanded');
}
});
$scope.$on('$stateChangeSuccess', function (event, toState) {
if (_isState(toState) && !$element.hasClass('ba-sidebar-item-expanded')) {
vm.$expand();
$element.addClass('ba-sidebar-item-expanded');
}
});
}
function _isState(state) {
return state && subItemsStateRefs.some(function(subItemState) {
return state.name.indexOf(subItemState) == 0;
});
}
}
/** @ngInject */
function baUiSrefTogglingSubmenu($state) {
return {