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

Commit b2462ec

Browse files
committed
feat(core): Modify core module to implement style guidelines.
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
1 parent 59e6daa commit b2462ec

28 files changed

+610
-533
lines changed

modules/articles/client/articles.client.module.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33

44
app.registerModule('articles', ['core']);// The core module is required for special route handling; see /core/client/config/core.client.routes
55
app.registerModule('articles.services');
6-
app.registerModule('articles.routes', ['ui.router', 'articles.services']);
6+
app.registerModule('articles.routes', ['ui.router', 'core.routes', 'articles.services']);
77
}(ApplicationConfiguration));

modules/articles/client/config/articles.client.config.js renamed to modules/articles/client/config/articles.client.menus.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55
.module('articles')
66
.run(menuConfig);
77

8-
menuConfig.$inject = ['Menus'];
8+
menuConfig.$inject = ['menuService'];
99

10-
function menuConfig(Menus) {
11-
Menus.addMenuItem('topbar', {
10+
function menuConfig(menuService) {
11+
menuService.addMenuItem('topbar', {
1212
title: 'Articles',
1313
state: 'articles',
1414
type: 'dropdown',
1515
roles: ['*']
1616
});
1717

1818
// Add the dropdown list item
19-
Menus.addSubMenuItem('topbar', 'articles', {
19+
menuService.addSubMenuItem('topbar', 'articles', {
2020
title: 'List Articles',
2121
state: 'articles.list'
2222
});
2323

2424
// Add the dropdown create item
25-
Menus.addSubMenuItem('topbar', 'articles', {
25+
menuService.addSubMenuItem('topbar', 'articles', {
2626
title: 'Create Article',
2727
state: 'articles.create',
2828
roles: ['user']

modules/chat/client/chat.client.module.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
'use strict';
33

44
app.registerModule('chat', ['core']);
5-
app.registerModule('chat.routes', ['ui.router']);
5+
app.registerModule('chat.routes', ['ui.router', 'core.routes']);
66
}(ApplicationConfiguration));

modules/chat/client/config/chat.client.config.js renamed to modules/chat/client/config/chat.client.menus.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
.module('chat')
66
.run(menuConfig);
77

8-
menuConfig.$inject = ['Menus'];
8+
menuConfig.$inject = ['menuService'];
99

10-
function menuConfig(Menus) {
10+
function menuConfig(menuService) {
1111
// Set top bar menu items
12-
Menus.addMenuItem('topbar', {
12+
menuService.addMenuItem('topbar', {
1313
title: 'Chat',
1414
state: 'chat'
1515
});

modules/core/client/app/config.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
'use strict';
1+
(function (window) {
2+
'use strict';
23

3-
// Init the application configuration module for AngularJS application
4-
var ApplicationConfiguration = (function () {
5-
// Init module configuration options
64
var applicationModuleName = 'mean';
7-
var applicationModuleVendorDependencies = ['ngResource', 'ngAnimate', 'ngMessages', 'ui.router', 'ui.bootstrap', 'angularFileUpload'];
5+
6+
var service = {
7+
applicationModuleName: applicationModuleName,
8+
applicationModuleVendorDependencies: ['ngResource', 'ngAnimate', 'ngMessages', 'ui.router', 'ui.bootstrap', 'angularFileUpload'],
9+
registerModule: registerModule
10+
};
11+
12+
window.ApplicationConfiguration = service;
813

914
// Add a new vertical module
10-
var registerModule = function (moduleName, dependencies) {
15+
function registerModule(moduleName, dependencies) {
1116
// Create angular module
1217
angular.module(moduleName, dependencies || []);
1318

1419
// Add the module to the AngularJS configuration file
1520
angular.module(applicationModuleName).requires.push(moduleName);
16-
};
17-
18-
return {
19-
applicationModuleName: applicationModuleName,
20-
applicationModuleVendorDependencies: applicationModuleVendorDependencies,
21-
registerModule: registerModule
22-
};
23-
}());
21+
}
22+
}(window));

modules/core/client/app/init.js

Lines changed: 34 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,45 @@
1-
'use strict';
1+
(function (app) {
2+
'use strict';
23

3-
// Start by defining the main module and adding the module dependencies
4-
angular.module(ApplicationConfiguration.applicationModuleName, ApplicationConfiguration.applicationModuleVendorDependencies);
4+
// Start by defining the main module and adding the module dependencies
5+
angular
6+
.module(app.applicationModuleName, app.applicationModuleVendorDependencies);
57

6-
// Setting HTML5 Location Mode
7-
angular.module(ApplicationConfiguration.applicationModuleName).config(['$locationProvider', '$httpProvider',
8-
function ($locationProvider, $httpProvider) {
8+
// Setting HTML5 Location Mode
9+
angular
10+
.module(app.applicationModuleName)
11+
.config(bootstrapConfig);
12+
13+
function bootstrapConfig($locationProvider, $httpProvider) {
914
$locationProvider.html5Mode(true).hashPrefix('!');
1015

1116
$httpProvider.interceptors.push('authInterceptor');
1217
}
13-
]);
14-
15-
angular.module(ApplicationConfiguration.applicationModuleName).run(function ($rootScope, $state, Authentication) {
16-
17-
// Check authentication before changing state
18-
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
19-
if (toState.data && toState.data.roles && toState.data.roles.length > 0) {
20-
var allowed = false;
21-
toState.data.roles.forEach(function (role) {
22-
if ((role === 'guest') || (Authentication.user && Authentication.user.roles !== undefined && Authentication.user.roles.indexOf(role) !== -1)) {
23-
allowed = true;
24-
return true;
25-
}
26-
});
2718

28-
if (!allowed) {
29-
event.preventDefault();
30-
if (Authentication.user !== undefined && typeof Authentication.user === 'object') {
31-
$state.go('forbidden');
32-
} else {
33-
$state.go('authentication.signin').then(function () {
34-
storePreviousState(toState, toParams);
35-
});
36-
}
19+
bootstrapConfig.$inject = ['$locationProvider', '$httpProvider'];
20+
21+
// Then define the init function for starting up the application
22+
angular.element(document).ready(init);
23+
24+
function init() {
25+
// Fixing facebook bug with redirect
26+
if (window.location.hash && window.location.hash === '#_=_') {
27+
if (window.history && history.pushState) {
28+
window.history.pushState('', document.title, window.location.pathname);
29+
} else {
30+
// Prevent scrolling by storing the page's current scroll offset
31+
var scroll = {
32+
top: document.body.scrollTop,
33+
left: document.body.scrollLeft
34+
};
35+
window.location.hash = '';
36+
// Restore the scroll offset, should be flicker free
37+
document.body.scrollTop = scroll.top;
38+
document.body.scrollLeft = scroll.left;
3739
}
3840
}
39-
});
4041

41-
// Record previous state
42-
$rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
43-
storePreviousState(fromState, fromParams);
44-
});
45-
46-
// Store previous state
47-
function storePreviousState(state, params) {
48-
// only store this state if it shouldn't be ignored
49-
if (!state.data || !state.data.ignoreState) {
50-
$state.previous = {
51-
state: state,
52-
params: params,
53-
href: $state.href(state, params)
54-
};
55-
}
42+
// Then init the app
43+
angular.bootstrap(document, [app.applicationModuleName]);
5644
}
57-
});
58-
59-
// Then define the init function for starting up the application
60-
angular.element(document).ready(function () {
61-
// Fixing facebook bug with redirect
62-
if (window.location.hash && window.location.hash === '#_=_') {
63-
if (window.history && history.pushState) {
64-
window.history.pushState('', document.title, window.location.pathname);
65-
} else {
66-
// Prevent scrolling by storing the page's current scroll offset
67-
var scroll = {
68-
top: document.body.scrollTop,
69-
left: document.body.scrollLeft
70-
};
71-
window.location.hash = '';
72-
// Restore the scroll offset, should be flicker free
73-
document.body.scrollTop = scroll.top;
74-
document.body.scrollLeft = scroll.left;
75-
}
76-
}
77-
78-
// Then init the app
79-
angular.bootstrap(document, [ApplicationConfiguration.applicationModuleName]);
80-
});
45+
}(ApplicationConfiguration));
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
'use strict';
1+
(function () {
2+
'use strict';
23

3-
angular.module('core.admin').run(['Menus',
4-
function (Menus) {
5-
Menus.addMenuItem('topbar', {
4+
angular
5+
.module('core.admin')
6+
.run(menuConfig);
7+
8+
menuConfig.$inject = ['menuService'];
9+
10+
function menuConfig(menuService) {
11+
menuService.addMenuItem('topbar', {
612
title: 'Admin',
713
state: 'admin',
814
type: 'dropdown',
915
roles: ['admin']
1016
});
1117
}
12-
]);
18+
}());
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
'use strict';
1+
(function () {
2+
'use strict';
23

3-
// Setting up route
4-
angular.module('core.admin.routes').config(['$stateProvider',
5-
function ($stateProvider) {
4+
angular
5+
.module('core.admin.routes')
6+
.config(routeConfig);
7+
8+
routeConfig.$inject = ['$stateProvider'];
9+
10+
function routeConfig($stateProvider) {
611
$stateProvider
712
.state('admin', {
813
abstract: true,
@@ -13,4 +18,4 @@ angular.module('core.admin.routes').config(['$stateProvider',
1318
}
1419
});
1520
}
16-
]);
21+
}());

modules/core/client/config/core.client.menus.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,41 @@
22
'use strict';
33

44
angular
5-
.module('core')
6-
.run(MenuConfig);
5+
.module('core')
6+
.run(menuConfig);
77

8-
MenuConfig.$inject = ['Menus'];
8+
menuConfig.$inject = ['menuService'];
99

10-
function MenuConfig(Menus) {
11-
12-
Menus.addMenu('account', {
10+
function menuConfig(menuService) {
11+
menuService.addMenu('account', {
1312
roles: ['user']
1413
});
1514

16-
Menus.addMenuItem('account', {
15+
menuService.addMenuItem('account', {
1716
title: '',
1817
state: 'settings',
1918
type: 'dropdown',
2019
roles: ['user']
2120
});
2221

23-
Menus.addSubMenuItem('account', 'settings', {
22+
menuService.addSubMenuItem('account', 'settings', {
2423
title: 'Edit Profile',
2524
state: 'settings.profile'
2625
});
2726

28-
Menus.addSubMenuItem('account', 'settings', {
27+
menuService.addSubMenuItem('account', 'settings', {
2928
title: 'Edit Profile Picture',
3029
state: 'settings.picture'
3130
});
3231

33-
Menus.addSubMenuItem('account', 'settings', {
32+
menuService.addSubMenuItem('account', 'settings', {
3433
title: 'Change Password',
3534
state: 'settings.password'
3635
});
3736

38-
Menus.addSubMenuItem('account', 'settings', {
37+
menuService.addSubMenuItem('account', 'settings', {
3938
title: 'Manage Social Accounts',
4039
state: 'settings.accounts'
4140
});
42-
4341
}
44-
4542
}());
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
(function () {
2+
'use strict';
3+
4+
angular
5+
.module('core')
6+
.run(routeFilter);
7+
8+
routeFilter.$inject = ['$rootScope', '$state', 'Authentication'];
9+
10+
function routeFilter($rootScope, $state, Authentication) {
11+
$rootScope.$on('$stateChangeStart', stateChangeStart);
12+
$rootScope.$on('$stateChangeSuccess', stateChangeSuccess);
13+
14+
function stateChangeStart(event, toState, toParams, fromState, fromParams) {
15+
// Check authentication before changing state
16+
if (toState.data && toState.data.roles && toState.data.roles.length > 0) {
17+
var allowed = false;
18+
19+
for (var i = 0, roles = toState.data.roles; i < roles.length; i++) {
20+
if ((roles[i] === 'guest') || (Authentication.user && Authentication.user.roles !== undefined && Authentication.user.roles.indexOf(roles[i]) !== -1)) {
21+
allowed = true;
22+
break;
23+
}
24+
}
25+
26+
if (!allowed) {
27+
event.preventDefault();
28+
if (Authentication.user !== undefined && typeof Authentication.user === 'object') {
29+
$state.transitionTo('forbidden');
30+
} else {
31+
$state.go('authentication.signin').then(function () {
32+
// Record previous state
33+
storePreviousState(toState, toParams);
34+
});
35+
}
36+
}
37+
}
38+
}
39+
40+
function stateChangeSuccess(event, toState, toParams, fromState, fromParams) {
41+
// Record previous state
42+
storePreviousState(fromState, fromParams);
43+
}
44+
45+
// Store previous state
46+
function storePreviousState(state, params) {
47+
// only store this state if it shouldn't be ignored
48+
if (!state.data || !state.data.ignoreState) {
49+
$state.previous = {
50+
state: state,
51+
params: params,
52+
href: $state.href(state, params)
53+
};
54+
}
55+
}
56+
}
57+
}());

0 commit comments

Comments
 (0)