Skip to content

Commit

Permalink
Merge pull request maestrano#40 from alexnoox/feature/dock
Browse files Browse the repository at this point in the history
Refactored organisations service & Dock initialisation
  • Loading branch information
x4d3 authored Aug 17, 2016
2 parents a0eaabe + ded77f2 commit 20104c3
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ ul.dock > li.prev img.app-logo {
// Mobile
#app-dock-mobile-container {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
z-index: 1030;
Expand Down
24 changes: 12 additions & 12 deletions src/app/components/dashboard-menu/dashboard-menu.controller.coffee
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
DashboardMenuCtrl = ($scope, MnoeCurrentUser) ->
'ngInject'

MnoeCurrentUser.get().then(
(success) ->
self.current_user_role = MnoeCurrentUser.user.organizations[0].current_user_role

$scope.isAdminRole = ->
self.current_user_role == 'Super Admin' || self.current_user_role == 'Admin'
)

#============================================
# Dashboard Menu
#============================================
Expand All @@ -18,8 +7,19 @@ angular.module 'mnoEnterpriseAngular'
return {
restrict: 'EA'
templateUrl: 'app/components/dashboard-menu/dashboard-menu.html',
controller: DashboardMenuCtrl
controller: ($scope, $state, MnoeOrganizations) ->

$scope.isLoading = true

$scope.$watch(MnoeOrganizations.getSelected, (newValue, oldValue) ->
if newValue?
# Impac! is displayed only to admin and super admin
$scope.isAdmin = (MnoeOrganizations.role.isAdmin() || MnoeOrganizations.role.isSuperAdmin())
$scope.isLoading = false
$state.go('home.login') if oldValue? && newValue != oldValue
)

return

# We need to manually close the collapse menu as we actually stay on the same page
link: (scope, elem, attrs) ->
Expand Down
12 changes: 6 additions & 6 deletions src/app/components/dashboard-menu/dashboard-menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
</button>
<a class="navbar-brand" href="#">{{ 'mno_enterprise.templates.dashboard.menu.dashboard' | translate }}</a>
</div>
<div class="collapse navbar-collapse" uib-collapse="navCollapsed">
<div ng-show="!isLoading" class="collapse navbar-collapse" uib-collapse="navCollapsed">
<ul class="nav navbar-nav">
<!-- ------------------------- -->
<!-- My account section -->
<!-- ------------------------- -->
<li>
<a ui-sref="home.apps" ui-sref-active="active">{{ 'mno_enterprise.templates.dashboard.menu.dashboard' | translate }}</a>
<a ng-if="!isAdmin" ui-sref="home.apps" ui-sref-active="active">{{ 'mno_enterprise.templates.dashboard.menu.dashboard' | translate }}</a>
</li>
<li>
<a ng-if="isAdminRole()" ui-sref="home.impac" ui-sref-active="active">{{ 'mno_enterprise.templates.dashboard.menu.impac' | translate }}</a>
<a ng-if="isAdmin" ui-sref="home.impac" ui-sref-active="active">{{ 'mno_enterprise.templates.dashboard.menu.impac' | translate }}</a>
</li>
<li>
<a ui-sref="home.account" ui-sref-active="active">{{ 'mno_enterprise.templates.dashboard.menu.my_account' | translate }}</a>
Expand All @@ -43,18 +43,18 @@
<!-- Side Navbar -->
<!-------------------------->
<nav class="col-xs-1 hidden-xs scrollbar-hidden">
<div class="menu vertical expanded">
<div ng-show="!isLoading" class="menu vertical expanded">
<!--Home page link-->
<a href="/">
<div class='brand-logo'></div>
</a>

<a ng-if="!isAdminRole()" ui-sref="home.apps" class="dashboard-button" ui-sref-active="active">
<a ng-if="!isAdmin" ng-cloak ui-sref="home.apps" class="dashboard-button" ui-sref-active="active">
<div class='content'>{{ 'mno_enterprise.templates.dashboard.menu.dashboard' | translate }}</div>
<i class="dhb-icon-dashboard"></i>
</a>

<a ng-if="isAdminRole()" ui-sref="home.impac" class="dashboard-button top-buffer-1" ui-sref-active="active">
<a ng-if="isAdmin" ng-cloak ui-sref="home.impac" class="dashboard-button top-buffer-1" ui-sref-active="active">
<div class='content'>{{ 'mno_enterprise.templates.dashboard.menu.dashboard' | translate }}</div>
<i class="dhb-icon-impac"></i>
</a>
Expand Down
53 changes: 26 additions & 27 deletions src/app/components/mnoe-api/organizations.svc.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ angular.module 'mnoEnterpriseAngular'
organizationsApi = MnoeApiSvc.all('organizations')

# Store the selected entity id
# Mostly used to refresh the UI befor the selected entity is requested
# Mostly used to refresh the UI before the selected entity is requested
@selectedId = null

@getSelectedId = ->
Expand All @@ -25,13 +25,15 @@ angular.module 'mnoEnterpriseAngular'
response
)

@get = (id) ->
return if id == _self.selectedId
organizationPromise = null
@get = (id = null) ->
# return the cached promise if not a new call
return organizationPromise if ((!id? || id == _self.selectedId) && organizationPromise != null)

_self.selectedId = id
_self.selectedId = id if id?

# Get the selected organization
organizationPromise = MnoeApiSvc.one('/organizations', id).get().then(
organizationPromise = MnoeApiSvc.one('/organizations', _self.selectedId).get().then(
(response) ->
# Save the organization
_self.selected = response.plain()
Expand Down Expand Up @@ -126,31 +128,28 @@ angular.module 'mnoEnterpriseAngular'
)

# Load the current organization if defined (url, cookie or first)
@getCurrentId = (user = null, dhbRefId = null) ->
# Return the already selected id
if _self.selectedId
$log.debug "MnoeOrganizations.getCurrentId: selectedId", _self.selectedId
return _self.selectedId

@getCurrentId = (dhbRefId = null) ->
# Attempt to load organization from param
else if dhbRefId
_self.get(dhbRefId)
if dhbRefId
_self.selectedId = dhbRefId
$cookies.put("#{MnoeCurrentUser.user.id}_dhb_ref_id", _self.selectedId)
$log.debug "MnoeOrganizations.getCurrentId: dhbRefId", _self.selectedId
return dhbRefId

# Attempt to load last organization from cookie of the user
else if (val = $cookies.get("#{MnoeCurrentUser.user.id}_dhb_ref_id"))
_self.get(val)
$log.debug "MnoeOrganizations.getCurrentId: cookie", _self.selectedId
return val

# Load first organization from user
return $q.resolve(_self.selectedId)
else
# If the app is initializing, return the correct organization id
organization = user.organizations[0]
_self.get(organization.id)
$log.debug "MnoeOrganizations.getCurrentId: first", _self.selectedId
return organization.id
# Load user's first organization or from cookie
MnoeCurrentUser.get().then(
(response) ->
if (val = $cookies.get("#{MnoeCurrentUser.user.id}_dhb_ref_id"))
# Load organization id stored in cookie
_self.get(val)
$log.debug "MnoeOrganizations.getCurrentId: cookie", _self.selectedId
return _self.selectedId
else
# Load user's first organization id
_self.get(response.organizations[0].id)
$log.debug "MnoeOrganizations.getCurrentId: first", _self.selectedId
return _self.selectedId
)

#======================================
# User Role
Expand Down
7 changes: 4 additions & 3 deletions src/app/index.route.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ angular.module 'mnoEnterpriseAngular'
templateUrl: 'app/views/layout.html'
controller: 'LayoutController'
controllerAs: 'layout'
.state 'home.login',
url: '/login'
controller: 'LoginRouterCtrl'
.state 'home.apps',
data:
pageTitle:'Apss'
Expand All @@ -22,6 +25,7 @@ angular.module 'mnoEnterpriseAngular'
url: '/impac'
templateUrl: 'app/views/impac/impac.html'
controller: 'ImpacController'
controllerAs: 'vm'
.state 'home.account',
data:
pageTitle:'Account'
Expand All @@ -43,9 +47,6 @@ angular.module 'mnoEnterpriseAngular'
templateUrl: 'app/views/marketplace/marketplace.html'
controller: 'DashboardMarketplaceCtrl'
controllerAs: 'vm'
.state 'home.login',
url: '/login'
controller: 'LoginRouterCtrl'
.state 'home.marketplace.app',
data:
pageTitle:'Marketplace-App'
Expand Down
25 changes: 17 additions & 8 deletions src/app/views/impac/impac.controller.coffee
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
angular.module 'mnoEnterpriseAngular'
.controller 'ImpacController', ($scope, $log, MnoeOrganizations, ImpacDashboardsSvc, MnoeCurrentUser) ->
.controller 'ImpacController', ($scope, $state, MnoeOrganizations, ImpacDashboardsSvc) ->
'ngInject'

MnoeCurrentUser.get().then(
->
self.current_user_role = MnoeCurrentUser.user.organizations[0].current_user_role

$scope.isAdminRole = ->
self.current_user_role == 'Super Admin' || self.current_user_role == 'Admin'
)
vm = this

#====================================
# Post-Initialization
#====================================
$scope.$watch(MnoeOrganizations.getSelectedId, (newValue, oldValue) ->
vm.isLoaded = false

# Reload the dashboard
ImpacDashboardsSvc.reload(true) if newValue? && oldValue? && newValue != oldValue

# Fetch current organization to check if user is allowed
MnoeOrganizations.get().then(
->
# Impac is displayed only to admin and super admin
vm.isImpacShown = (MnoeOrganizations.role.isAdmin() || MnoeOrganizations.role.isSuperAdmin())

# The user is not allowed to se impac, he is redirected
if !vm.isImpacShown
$state.go('home.login')

vm.isLoaded = true
) if newValue?
)

return
14 changes: 4 additions & 10 deletions src/app/views/impac/impac.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
<div id="impac">
<div ng-show="isAdminRole()">
<dashboard-apps-dock></dashboard-apps-dock>
<dashboard-apps-dock></dashboard-apps-dock>

<div id="impac" ng-show="vm.isLoaded">
<div ng-if="vm.isImpacShown">
<impac-dashboard></impac-dashboard>
</div>
<div ng-show="!isAdminRole()">
<span class="alert-no-admin">
<strong>Warning!</strong>
<a ui-sref="home.apps" ui-sref-active="active">You are not allow to see this content. Click here to be redirected to the {{ 'mno_enterprise.templates.dashboard.menu.dashboard' | translate }}.</a>
</span>
</div>
</div>

18 changes: 9 additions & 9 deletions src/app/views/layout.controller.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
angular.module 'mnoEnterpriseAngular'
.controller 'LayoutController', ($stateParams, $location, MnoeCurrentUser, MnoeOrganizations, MnoeMarketplace) ->
.controller 'LayoutController', ($stateParams, $state, $q, MnoeCurrentUser, MnoeOrganizations, MnoeMarketplace) ->
'ngInject'

layout = this
Expand All @@ -8,21 +8,21 @@ angular.module 'mnoEnterpriseAngular'
layout.isLoggedIn = false

# App initialization
MnoeCurrentUser.get().then(
(response) ->
# Display the layout
layout.isLoggedIn = true
userPromise = MnoeCurrentUser.get()

layout.current_user = MnoeCurrentUser.user
# Load the current organization if defined (url param, cookie or first)
organizationPromise = MnoeOrganizations.getCurrentId($stateParams.dhbRefId)

# Load the current organization if defined (url param, cookie or first)
MnoeOrganizations.getCurrentId(response, $stateParams.dhbRefId)
$q.all([userPromise, organizationPromise]).then(
->
# Display the layout
layout.isLoggedIn = true

# Pre-load the market place
MnoeMarketplace.getApps()

# Remove param from url
$location.search('dhbRefId', null)
$state.go('.', {dhbRefId: undefined})
)

return
1 change: 0 additions & 1 deletion src/app/views/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<span class="layout-loading-text">Your dashboard is loading...</span>
</div>


<div class="container-fluid">
<mno-impersonation-bar></mno-impersonation-bar>
</div>
Expand Down
14 changes: 7 additions & 7 deletions src/app/views/login-router/login-router.coffee
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
angular.module 'mnoEnterpriseAngular'
.controller 'LoginRouterCtrl', ($scope, MnoeCurrentUser, $window, $state) ->
'ngInject'
.controller 'LoginRouterCtrl', ($scope, MnoeOrganizations, $window, $state) ->

$scope.init = MnoeCurrentUser.get().then(
(success) ->
self.current_user_role = MnoeCurrentUser.user.organizations[0].current_user_role
if self.current_user_role == 'Super Admin' || self.current_user_role == 'Admin'
# window.location.href = "/dashboard/#/impac"
$scope.$watch(MnoeOrganizations.getSelected, (newValue) ->
if newValue?
# Impac! is displayed only to admin and super admin
if (MnoeOrganizations.role.isAdmin() || MnoeOrganizations.role.isSuperAdmin())
$state.go('home.impac')
else
$state.go('home.apps')
)

return

0 comments on commit 20104c3

Please sign in to comment.