Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #780 from codydaig/feature/issue583
Browse files Browse the repository at this point in the history
Make TopBar Public By Default
  • Loading branch information
lirantal committed Aug 15, 2015
2 parents a5250c3 + 1f7bfdd commit 114706e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 64 deletions.
6 changes: 4 additions & 2 deletions modules/articles/client/config/articles.client.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ angular.module('articles').run(['Menus',
Menus.addMenuItem('topbar', {
title: 'Articles',
state: 'articles',
type: 'dropdown'
type: 'dropdown',
roles: ['*']
});

// Add the dropdown list item
Expand All @@ -19,7 +20,8 @@ angular.module('articles').run(['Menus',
// Add the dropdown create item
Menus.addSubMenuItem('topbar', 'articles', {
title: 'Create Articles',
state: 'articles.create'
state: 'articles.create',
roles: ['user']
});
}
]);
30 changes: 13 additions & 17 deletions modules/core/client/services/menus.client.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@
angular.module('core').service('Menus', [
function () {
// Define a set of default roles
this.defaultRoles = ['*'];
this.defaultRoles = ['user', 'admin'];

// Define the menus object
this.menus = {};

// A private function for rendering decision
var shouldRender = function (user) {
if (user) {
if (!!~this.roles.indexOf('*')) {
return true;
} else {
for (var userRoleIndex in user.roles) {
for (var roleIndex in this.roles) {
if (this.roles[roleIndex] === user.roles[userRoleIndex]) {
return true;
}
if (!!~this.roles.indexOf('*')) {
return true;
} else {
if(!user) {
return false;
}
for (var userRoleIndex in user.roles) {
for (var roleIndex in this.roles) {
if (this.roles[roleIndex] === user.roles[userRoleIndex]) {
return true;
}
}
}
} else {
return this.isPublic;
}

return false;
Expand Down Expand Up @@ -60,7 +59,6 @@ angular.module('core').service('Menus', [

// Create the new menu
this.menus[menuId] = {
isPublic: ((options.isPublic === null || typeof options.isPublic === 'undefined') ? true : options.isPublic),
roles: options.roles || this.defaultRoles,
items: options.items || [],
shouldRender: shouldRender
Expand Down Expand Up @@ -92,8 +90,7 @@ angular.module('core').service('Menus', [
state: options.state || '',
type: options.type || 'item',
class: options.class,
isPublic: ((options.isPublic === null || typeof options.isPublic === 'undefined') ? this.menus[menuId].isPublic : options.isPublic),
roles: ((options.roles === null || typeof options.roles === 'undefined') ? this.menus[menuId].roles : options.roles),
roles: ((options.roles === null || typeof options.roles === 'undefined') ? this.defaultRoles : options.roles),
position: options.position || 0,
items: [],
shouldRender: shouldRender
Expand Down Expand Up @@ -124,7 +121,6 @@ angular.module('core').service('Menus', [
this.menus[menuId].items[itemIndex].items.push({
title: options.title || '',
state: options.state || '',
isPublic: ((options.isPublic === null || typeof options.isPublic === 'undefined') ? this.menus[menuId].items[itemIndex].isPublic : options.isPublic),
roles: ((options.roles === null || typeof options.roles === 'undefined') ? this.menus[menuId].items[itemIndex].roles : options.roles),
position: options.position || 0,
shouldRender: shouldRender
Expand Down Expand Up @@ -172,7 +168,7 @@ angular.module('core').service('Menus', [

//Adding the topbar menu
this.addMenu('topbar', {
isPublic: false
roles: ['*']
});
}
]);
52 changes: 7 additions & 45 deletions modules/core/tests/client/menus.client.service.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@
expect(Menus.menus.topbar).toBeDefined();
});

it('should have private topbar', function() {
expect(Menus.menus.topbar.isPublic).toBeFalsy();
});

it('should have default roles to *', function() {
expect(Menus.defaultRoles).toEqual(['*']);
it('should have default roles to user and admin', function() {
expect(Menus.defaultRoles).toEqual(['user', 'admin']);
});

describe('addMenu', function() {
Expand All @@ -45,12 +41,8 @@
expect(menu.items).toEqual([]);
});

it('should be public by default', function() {
expect(menu.isPublic).toBeTruthy();
});

it('should set shouldRender to shouldRender function handle', function() {
expect(menu.shouldRender()).toBeTruthy();
expect(menu.shouldRender()).toBeFalsy();
});
});

Expand All @@ -64,17 +56,6 @@
menu = Menus.addMenu('menu1', options);
});

it('should set isPublic to true if options.isPublic equal to null', function() {
var menu = Menus.addMenu('menu1', {
isPublic: null
});
expect(menu.isPublic).toBeTruthy();
});

it('should set isPublic to true if options.isPublic equal to undefined', function() {
expect(menu.isPublic).toBeTruthy();
});

it('should set items to options.items list', function() {
expect(menu.items).toBe(options.items);
});
Expand All @@ -98,13 +79,6 @@
it('should render if menu is public', function() {
expect(menu.shouldRender()).toBeTruthy();
});

it('should not render if menu is private', function() {
menu = Menus.addMenu('menu1', {
isPublic: false
});
expect(menu.shouldRender()).toBeFalsy();
});
});

describe('when logged in', function() {
Expand Down Expand Up @@ -250,10 +224,6 @@
expect(menuItem.class).toBe(menuItemOptions.class);
});

it('should set menu item isPublic to options isPublic', function() {
expect(menuItem.isPublic).toBe(menuItemOptions.isPublic);
});

it('should set menu item position to options position', function() {
expect(menuItem.position).toBe(menuItemOptions.position);
});
Expand All @@ -278,12 +248,12 @@
expect(menuItem.title).toBe('');
});

it('should set menu item isPublic to menu.isPublic', function() {
expect(menuItem.isPublic).toBe(menu.isPublic);
it('should set menu item isPublic to false', function() {
expect(menuItem.isPublic).toBeFalsy();
});

it('should set menu item roles to menu roles', function() {
expect(menuItem.roles).toEqual(menu.roles);
it('should set menu item roles to default roles', function() {
expect(menuItem.roles).toEqual(Menus.defaultRoles);
});

it('should set menu item position to 0', function() {
Expand Down Expand Up @@ -393,10 +363,6 @@
expect(menuItem1.items.length).toBe(1);
});

it('should set isPublic to options isPublic', function() {
expect(subMenuItem.isPublic).toBe(subItemOptions.isPublic);
});

it('should set title to options title', function() {
expect(subMenuItem.title).toBe(subItemOptions.title);
});
Expand Down Expand Up @@ -424,10 +390,6 @@
expect(menuItem2.items.length).toBe(1);
});

it('should set isPublic to parent isPublic', function() {
expect(subMenuItem.isPublic).toBe(menuItem2.isPublic);
});

it('should set title to blank', function() {
expect(subMenuItem.title).toBe('');
});
Expand Down

0 comments on commit 114706e

Please sign in to comment.