Implemented functional for adding new item

pull/167/head
esadouski 2016-09-24 02:23:46 +03:00
parent 4e9ba8ab4e
commit b03539ab95
3 changed files with 71 additions and 9 deletions

View File

@ -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
}]

View File

@ -7,28 +7,57 @@
/** @ngInject */
function baSidebarServiceProvider() {
var staticMenuItems = [];
var menuItems = [];
this.addStaticItem = function() {
staticMenuItems.push.apply(staticMenuItems, arguments);
};
/** @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 () {
if (!menuItems.length) {
var menuItems = baSidebarModel.getMenuItems();
if (!menuItems) {
menuItems = createMenu();
baSidebarModel.setMenuItems(menuItems);
}
return menuItems;
};
this.addMenuItem = function(item) {
menuItems.push(item);
this.addMenuItem = function (item) {
var menuItems = baSidebarModel.getMenuItems();
var parent = null;
_findParent(menuItems, item);
if (parent) {
_addToSubMenu(item);
} else {
menuItems.push(item);
}
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;
@ -66,10 +95,10 @@
return item.level == parentLevel;
});
bindMenuItems(parents, ++parentLevel);
_bindMenuItems(parents, ++parentLevel);
return parents.concat(staticMenuItems);
function bindMenuItems(parents, childLevel) {
function _bindMenuItems(parents, childLevel) {
var child = states.filter(function (item) {
return item.level == childLevel;
});
@ -80,7 +109,7 @@
});
p.subMenu = children.length ? children : null;
});
bindMenuItems(child, ++childLevel)
_bindMenuItems(child, ++childLevel)
}
}
}

View File

@ -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
};
}
})();