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'); $urlRouterProvider.otherwise('/dashboard');
baSidebarServiceProvider.addStaticItem({ baSidebarServiceProvider.addStaticItem({
name: 'pages',
title: 'Pages', title: 'Pages',
icon: 'ion-document', icon: 'ion-document',
subMenu: [{ subMenu: [{
name: 'pages.signIn',
title: 'Sign In', title: 'Sign In',
fixedHref: 'auth.html', fixedHref: 'auth.html',
blank: true blank: true
}, { }, {
name: 'pages.signUp',
title: 'Sign Up', title: 'Sign Up',
fixedHref: 'reg.html', fixedHref: 'reg.html',
blank: true blank: true
}, { }, {
name: 'pages.userProfile',
title: 'User Profile', title: 'User Profile',
stateRef: 'profile' stateRef: 'profile'
}, { }, {
name: 'pages.page404',
title: '404 Page', title: '404 Page',
fixedHref: '404.html', fixedHref: '404.html',
blank: true blank: true
}] }]
}); });
baSidebarServiceProvider.addStaticItem({ baSidebarServiceProvider.addStaticItem({
name: 'menuLevel1',
title: 'Menu Level 1', title: 'Menu Level 1',
icon: 'ion-ios-more', icon: 'ion-ios-more',
subMenu: [{ subMenu: [{
name: 'menuLevel1.menuLevel1.1',
title: 'Menu Level 1.1', title: 'Menu Level 1.1',
disabled: true disabled: true
}, { }, {
name: 'menuLevel1.menuLevel1.2',
title: 'Menu Level 1.2', title: 'Menu Level 1.2',
subMenu: [{ subMenu: [{
name: 'menuLevel1.menuLevel1.2.1',
title: 'Menu Level 1.2.1', title: 'Menu Level 1.2.1',
disabled: true disabled: true
}] }]

View File

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