Skip to content

Commit

Permalink
feat(core): Modify core module to implement style guidelines.
Browse files Browse the repository at this point in the history
Update the core module to implement the style guidelines.
Reduce size of init.js - moved filter logic out to it's own config.
Rename Menus to menuService
  • Loading branch information
rhutchison committed Mar 23, 2016
1 parent 59e6daa commit b2462ec
Show file tree
Hide file tree
Showing 28 changed files with 610 additions and 533 deletions.
2 changes: 1 addition & 1 deletion modules/articles/client/articles.client.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

app.registerModule('articles', ['core']);// The core module is required for special route handling; see /core/client/config/core.client.routes
app.registerModule('articles.services');
app.registerModule('articles.routes', ['ui.router', 'articles.services']);
app.registerModule('articles.routes', ['ui.router', 'core.routes', 'articles.services']);
}(ApplicationConfiguration));
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
.module('articles')
.run(menuConfig);

menuConfig.$inject = ['Menus'];
menuConfig.$inject = ['menuService'];

function menuConfig(Menus) {
Menus.addMenuItem('topbar', {
function menuConfig(menuService) {
menuService.addMenuItem('topbar', {
title: 'Articles',
state: 'articles',
type: 'dropdown',
roles: ['*']
});

// Add the dropdown list item
Menus.addSubMenuItem('topbar', 'articles', {
menuService.addSubMenuItem('topbar', 'articles', {
title: 'List Articles',
state: 'articles.list'
});

// Add the dropdown create item
Menus.addSubMenuItem('topbar', 'articles', {
menuService.addSubMenuItem('topbar', 'articles', {
title: 'Create Article',
state: 'articles.create',
roles: ['user']
Expand Down
2 changes: 1 addition & 1 deletion modules/chat/client/chat.client.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
'use strict';

app.registerModule('chat', ['core']);
app.registerModule('chat.routes', ['ui.router']);
app.registerModule('chat.routes', ['ui.router', 'core.routes']);
}(ApplicationConfiguration));
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
.module('chat')
.run(menuConfig);

menuConfig.$inject = ['Menus'];
menuConfig.$inject = ['menuService'];

function menuConfig(Menus) {
function menuConfig(menuService) {
// Set top bar menu items
Menus.addMenuItem('topbar', {
menuService.addMenuItem('topbar', {
title: 'Chat',
state: 'chat'
});
Expand Down
27 changes: 13 additions & 14 deletions modules/core/client/app/config.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
'use strict';
(function (window) {
'use strict';

// Init the application configuration module for AngularJS application
var ApplicationConfiguration = (function () {
// Init module configuration options
var applicationModuleName = 'mean';
var applicationModuleVendorDependencies = ['ngResource', 'ngAnimate', 'ngMessages', 'ui.router', 'ui.bootstrap', 'angularFileUpload'];

var service = {
applicationModuleName: applicationModuleName,
applicationModuleVendorDependencies: ['ngResource', 'ngAnimate', 'ngMessages', 'ui.router', 'ui.bootstrap', 'angularFileUpload'],
registerModule: registerModule
};

window.ApplicationConfiguration = service;

// Add a new vertical module
var registerModule = function (moduleName, dependencies) {
function registerModule(moduleName, dependencies) {
// Create angular module
angular.module(moduleName, dependencies || []);

// Add the module to the AngularJS configuration file
angular.module(applicationModuleName).requires.push(moduleName);
};

return {
applicationModuleName: applicationModuleName,
applicationModuleVendorDependencies: applicationModuleVendorDependencies,
registerModule: registerModule
};
}());
}
}(window));
103 changes: 34 additions & 69 deletions modules/core/client/app/init.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,45 @@
'use strict';
(function (app) {
'use strict';

// Start by defining the main module and adding the module dependencies
angular.module(ApplicationConfiguration.applicationModuleName, ApplicationConfiguration.applicationModuleVendorDependencies);
// Start by defining the main module and adding the module dependencies
angular
.module(app.applicationModuleName, app.applicationModuleVendorDependencies);

// Setting HTML5 Location Mode
angular.module(ApplicationConfiguration.applicationModuleName).config(['$locationProvider', '$httpProvider',
function ($locationProvider, $httpProvider) {
// Setting HTML5 Location Mode
angular
.module(app.applicationModuleName)
.config(bootstrapConfig);

function bootstrapConfig($locationProvider, $httpProvider) {
$locationProvider.html5Mode(true).hashPrefix('!');

$httpProvider.interceptors.push('authInterceptor');
}
]);

angular.module(ApplicationConfiguration.applicationModuleName).run(function ($rootScope, $state, Authentication) {

// Check authentication before changing state
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
if (toState.data && toState.data.roles && toState.data.roles.length > 0) {
var allowed = false;
toState.data.roles.forEach(function (role) {
if ((role === 'guest') || (Authentication.user && Authentication.user.roles !== undefined && Authentication.user.roles.indexOf(role) !== -1)) {
allowed = true;
return true;
}
});

if (!allowed) {
event.preventDefault();
if (Authentication.user !== undefined && typeof Authentication.user === 'object') {
$state.go('forbidden');
} else {
$state.go('authentication.signin').then(function () {
storePreviousState(toState, toParams);
});
}
bootstrapConfig.$inject = ['$locationProvider', '$httpProvider'];

// Then define the init function for starting up the application
angular.element(document).ready(init);

function init() {
// Fixing facebook bug with redirect
if (window.location.hash && window.location.hash === '#_=_') {
if (window.history && history.pushState) {
window.history.pushState('', document.title, window.location.pathname);
} else {
// Prevent scrolling by storing the page's current scroll offset
var scroll = {
top: document.body.scrollTop,
left: document.body.scrollLeft
};
window.location.hash = '';
// Restore the scroll offset, should be flicker free
document.body.scrollTop = scroll.top;
document.body.scrollLeft = scroll.left;
}
}
});

// Record previous state
$rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
storePreviousState(fromState, fromParams);
});

// Store previous state
function storePreviousState(state, params) {
// only store this state if it shouldn't be ignored
if (!state.data || !state.data.ignoreState) {
$state.previous = {
state: state,
params: params,
href: $state.href(state, params)
};
}
// Then init the app
angular.bootstrap(document, [app.applicationModuleName]);
}
});

// Then define the init function for starting up the application
angular.element(document).ready(function () {
// Fixing facebook bug with redirect
if (window.location.hash && window.location.hash === '#_=_') {
if (window.history && history.pushState) {
window.history.pushState('', document.title, window.location.pathname);
} else {
// Prevent scrolling by storing the page's current scroll offset
var scroll = {
top: document.body.scrollTop,
left: document.body.scrollLeft
};
window.location.hash = '';
// Restore the scroll offset, should be flicker free
document.body.scrollTop = scroll.top;
document.body.scrollLeft = scroll.left;
}
}

// Then init the app
angular.bootstrap(document, [ApplicationConfiguration.applicationModuleName]);
});
}(ApplicationConfiguration));
16 changes: 11 additions & 5 deletions modules/core/client/config/core-admin.client.menus.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
'use strict';
(function () {
'use strict';

angular.module('core.admin').run(['Menus',
function (Menus) {
Menus.addMenuItem('topbar', {
angular
.module('core.admin')
.run(menuConfig);

menuConfig.$inject = ['menuService'];

function menuConfig(menuService) {
menuService.addMenuItem('topbar', {
title: 'Admin',
state: 'admin',
type: 'dropdown',
roles: ['admin']
});
}
]);
}());
15 changes: 10 additions & 5 deletions modules/core/client/config/core-admin.client.routes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
'use strict';
(function () {
'use strict';

// Setting up route
angular.module('core.admin.routes').config(['$stateProvider',
function ($stateProvider) {
angular
.module('core.admin.routes')
.config(routeConfig);

routeConfig.$inject = ['$stateProvider'];

function routeConfig($stateProvider) {
$stateProvider
.state('admin', {
abstract: true,
Expand All @@ -13,4 +18,4 @@ angular.module('core.admin.routes').config(['$stateProvider',
}
});
}
]);
}());
23 changes: 10 additions & 13 deletions modules/core/client/config/core.client.menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,41 @@
'use strict';

angular
.module('core')
.run(MenuConfig);
.module('core')
.run(menuConfig);

MenuConfig.$inject = ['Menus'];
menuConfig.$inject = ['menuService'];

function MenuConfig(Menus) {

Menus.addMenu('account', {
function menuConfig(menuService) {
menuService.addMenu('account', {
roles: ['user']
});

Menus.addMenuItem('account', {
menuService.addMenuItem('account', {
title: '',
state: 'settings',
type: 'dropdown',
roles: ['user']
});

Menus.addSubMenuItem('account', 'settings', {
menuService.addSubMenuItem('account', 'settings', {
title: 'Edit Profile',
state: 'settings.profile'
});

Menus.addSubMenuItem('account', 'settings', {
menuService.addSubMenuItem('account', 'settings', {
title: 'Edit Profile Picture',
state: 'settings.picture'
});

Menus.addSubMenuItem('account', 'settings', {
menuService.addSubMenuItem('account', 'settings', {
title: 'Change Password',
state: 'settings.password'
});

Menus.addSubMenuItem('account', 'settings', {
menuService.addSubMenuItem('account', 'settings', {
title: 'Manage Social Accounts',
state: 'settings.accounts'
});

}

}());
57 changes: 57 additions & 0 deletions modules/core/client/config/core.client.route-filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
(function () {
'use strict';

angular
.module('core')
.run(routeFilter);

routeFilter.$inject = ['$rootScope', '$state', 'Authentication'];

function routeFilter($rootScope, $state, Authentication) {
$rootScope.$on('$stateChangeStart', stateChangeStart);
$rootScope.$on('$stateChangeSuccess', stateChangeSuccess);

function stateChangeStart(event, toState, toParams, fromState, fromParams) {
// Check authentication before changing state
if (toState.data && toState.data.roles && toState.data.roles.length > 0) {
var allowed = false;

for (var i = 0, roles = toState.data.roles; i < roles.length; i++) {
if ((roles[i] === 'guest') || (Authentication.user && Authentication.user.roles !== undefined && Authentication.user.roles.indexOf(roles[i]) !== -1)) {
allowed = true;
break;
}
}

if (!allowed) {
event.preventDefault();
if (Authentication.user !== undefined && typeof Authentication.user === 'object') {
$state.transitionTo('forbidden');
} else {
$state.go('authentication.signin').then(function () {
// Record previous state
storePreviousState(toState, toParams);
});
}
}
}
}

function stateChangeSuccess(event, toState, toParams, fromState, fromParams) {
// Record previous state
storePreviousState(fromState, fromParams);
}

// Store previous state
function storePreviousState(state, params) {
// only store this state if it shouldn't be ignored
if (!state.data || !state.data.ignoreState) {
$state.previous = {
state: state,
params: params,
href: $state.href(state, params)
};
}
}
}
}());
Loading

0 comments on commit b2462ec

Please sign in to comment.