Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sidebar is built once #147 #167

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/app/pages/pages.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,43 @@
$urlRouterProvider.otherwise('/dashboard');

baSidebarServiceProvider.addStaticItem({
name: 'pages',
title: 'Pages',
icon: 'ion-document',
subMenu: [{
name: 'pages.signIn',
title: 'Sign In',
fixedHref: 'auth.html',
blank: true
}, {
name: 'pages.signUp',
title: 'Sign Up',
fixedHref: 'reg.html',
blank: true
}, {
name: 'pages.userProfile',
title: 'User Profile',
stateRef: 'profile'
}, {
name: 'pages.page404',
title: '404 Page',
fixedHref: '404.html',
blank: true
}]
});
baSidebarServiceProvider.addStaticItem({
name: 'menuLevel1',
title: 'Menu Level 1',
icon: 'ion-ios-more',
subMenu: [{
name: 'menuLevel1.menuLevel1.1',
title: 'Menu Level 1.1',
disabled: true
}, {
name: 'menuLevel1.menuLevel1.2',
title: 'Menu Level 1.2',
subMenu: [{
name: 'menuLevel1.menuLevel1.2.1',
title: 'Menu Level 1.2.1',
disabled: true
}]
Expand Down
79 changes: 65 additions & 14 deletions src/app/theme/components/baSidebar/baSidebar.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,51 @@
};

/** @ngInject */
this.$get = function($state, layoutSizes) {
return new _factory();
this.$get = function ($state, layoutSizes, baSidebarModel) {
return new _factory();

function _factory() {
var isMenuCollapsed = shouldMenuBeCollapsed();

this.getMenuItems = function() {
var states = defineMenuItemStates();
var menuItems = states.filter(function(item) {
return item.level == 0;
});
this.getMenuItems = function () {
var menuItems = baSidebarModel.getMenuItems();
if (!menuItems) {
menuItems = createMenu();
baSidebarModel.setMenuItems(menuItems);
}
return menuItems;
};

menuItems.forEach(function(item) {
var children = states.filter(function(child) {
return child.level == 1 && child.name.indexOf(item.name) === 0;
});
item.subMenu = children.length ? children : null;
});
this.addMenuItem = function (item) {
var menuItems = baSidebarModel.getMenuItems();
var parent = null;
_findParent(menuItems, item);
if (parent) {
_addToSubMenu(item);
} else {
menuItems.push(item);
}

return menuItems.concat(staticMenuItems);
baSidebarModel.setMenuItems(menuItems);

function _findParent(parents, item) {
parent = parents
.filter(function (p) {
return item.name.indexOf(p.name) === 0;
})
.pop();
if (parent && parent.subMenu) {
_findParent(parent.subMenu, item);
}
}

function _addToSubMenu(item) {
if (parent.subMenu) {
parent.subMenu.push(item);
} else {
parent.subMenu = [item];
}
}
};

this.shouldMenuBeCollapsed = shouldMenuBeCollapsed;
Expand Down Expand Up @@ -63,6 +88,32 @@
}
};

function createMenu() {
var parentLevel = 0;
var states = defineMenuItemStates();
var parents = states.filter(function(item) {
return item.level == parentLevel;
});

_bindMenuItems(parents, ++parentLevel);
return parents.concat(staticMenuItems);

function _bindMenuItems(parents, childLevel) {
var child = states.filter(function (item) {
return item.level == childLevel;
});
if (child.length) {
parents.forEach(function (p) {
var children = child.filter(function (c) {
return c.name.indexOf(p.name) === 0;
});
p.subMenu = children.length ? children : null;
});
_bindMenuItems(child, ++childLevel)
}
}
}

function defineMenuItemStates() {
return $state.get()
.filter(function(s) {
Expand Down
24 changes: 24 additions & 0 deletions src/app/theme/components/baSidebar/baSidebarModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(function () {
'use strict';

angular.module('BlurAdmin.theme.components')
.factory('baSidebarModel', baSidebarModel);

/** @ngInject */
function baSidebarModel() {
var menuItems = null;

function getMenuItems() {
return menuItems;
}

function setMenuItems(items) {
menuItems = items;
}

return {
getMenuItems: getMenuItems,
setMenuItems: setMenuItems
};
}
})();