Skip to content

Commit

Permalink
Merge pull request maestrano#84 from alexnoox/215-app-init-refactor
Browse files Browse the repository at this point in the history
[MNOE-215] App initialisation refactor
  • Loading branch information
alexnoox authored Oct 20, 2016
2 parents 67c9c88 + c2215b7 commit 5c54ec1
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ DashboardCompanySelectboxCtrl = ($scope, $location, $stateParams, $cookies, $sce
}

selectBox.changeTo = (organization) ->
MnoeAppInstances.emptyAppInstances()
MnoeAppInstances.clearCache()
MnoeOrganizations.get(organization.id)
selectBox.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ angular.module 'mnoEnterpriseAngular'
restrict: 'EA'
templateUrl: 'app/components/dashboard-menu/dashboard-menu.html',

controller: ($scope, $state, MnoeOrganizations, DOCK_CONFIG) ->
$scope.isLoading = true
controller: ($scope, MnoeCurrentUser, MnoeOrganizations, DOCK_CONFIG) ->
$scope.isDockEnabled = DOCK_CONFIG.enabled

$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.isDockEnabled = DOCK_CONFIG.enabled
$scope.isLoading = false
$state.go('home.login') if oldValue? && newValue != oldValue
$scope.$watch(MnoeOrganizations.getSelectedId, (newValue) ->
# Impac! is displayed only to admin and super admin
MnoeCurrentUser.get().then(
(response) ->
selectedOrg = _.find(response.organizations, {id: parseInt(newValue)})
$scope.isAdmin = MnoeOrganizations.role.atLeastAdmin(selectedOrg.current_user_role)
) if newValue?
)

return
Expand Down
6 changes: 3 additions & 3 deletions src/app/components/impac-config/impac-config.svc.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ angular.module 'mnoEnterpriseAngular'
MnoeCurrentUser.get()

@getOrganizations = ->
userOrgs = MnoeCurrentUser.get().then(
userOrgsPromise = MnoeCurrentUser.get().then(
->
userOrgs = MnoeCurrentUser.user.organizations

Expand All @@ -16,7 +16,7 @@ angular.module 'mnoEnterpriseAngular'
return userOrgs
)

currentOrgId = MnoeOrganizations.get().then(
currentOrgIdPromise = MnoeOrganizations.get(MnoeOrganizations.selectedId).then(
->
currentOrgId = parseInt(MnoeOrganizations.selectedId)

Expand All @@ -27,7 +27,7 @@ angular.module 'mnoEnterpriseAngular'
return currentOrgId
)

$q.all([userOrgs, currentOrgId]).then(
$q.all([userOrgsPromise, currentOrgIdPromise]).then(
(responses) ->
return {organizations: responses[0], currentOrgId: responses[1]}
)
Expand Down
3 changes: 3 additions & 0 deletions src/app/components/mnoe-api/app-instances.svc.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ angular.module 'mnoEnterpriseAngular'
MnoLocalStorage.setObject(MnoeCurrentUser.user.id + "_" + LOCALSTORAGE.appInstancesKey, _self.appInstances)
)

@emptyAppInstances = () ->
@appInstances.length = 0

@clearCache = () ->
MnoLocalStorage.removeItem(MnoeCurrentUser.user.id + "_" + LOCALSTORAGE.appInstancesKey)

Expand Down
2 changes: 1 addition & 1 deletion src/app/components/mnoe-api/organizations.svc.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ angular.module 'mnoEnterpriseAngular'
organizationPromise = null
@get = (id = null) ->
# return the cached promise if not a new call
return organizationPromise if ((!id? || id == _self.selectedId) && organizationPromise != null)
return organizationPromise if ((!id? || id == _self.selectedId) && organizationPromise?)

_self.selectedId = id if id?

Expand Down
5 changes: 1 addition & 4 deletions src/app/index.route.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ angular.module 'mnoEnterpriseAngular'
templateUrl: 'app/views/layout.html'
controller: 'LayoutController'
controllerAs: 'layout'
.state 'home.login',
url: '/login'
controller: 'LoginRouterCtrl'
.state 'home.apps',
data:
pageTitle:'Apps'
Expand Down Expand Up @@ -69,4 +66,4 @@ angular.module 'mnoEnterpriseAngular'
$window.location.href = "#{URI.login}"
)

$urlRouterProvider.otherwise '/login'
$urlRouterProvider.otherwise '/impac'
25 changes: 24 additions & 1 deletion src/app/index.run.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ angular.module 'mnoEnterpriseAngular'
# Display flash messages from the backend in toastr
# They're passed this way:
# ?flash={"msg":"An error message.","type":"error"}
.run (toastr, $location) ->
.run((toastr, $location) ->
if flash = $location.search().flash
message = JSON.parse(flash)
toastr[message.type](message.msg, _.capitalize(message.type), timeout: 10000)
$location.search('flash', null) # remove the flash from url
)

.run(($rootScope, $timeout, AnalyticsSvc) ->
$timeout ( -> AnalyticsSvc.init() )
Expand All @@ -61,3 +62,25 @@ angular.module 'mnoEnterpriseAngular'
AnalyticsSvc.update()
)
)

# App initialization: Retrieve current user and current organization, then preload marketplace
.run(($rootScope, $q, $stateParams, MnoeCurrentUser, MnoeOrganizations, MnoeMarketplace) ->

# Hide the layout with a loader
$rootScope.isLoggedIn = false

# Load the current user
userPromise = MnoeCurrentUser.get()

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

$q.all([userPromise, organizationPromise]).then(
->
# Display the layout
$rootScope.isLoggedIn = true

# Pre-load the market place
MnoeMarketplace.getApps()
)
)
25 changes: 13 additions & 12 deletions src/app/views/impac/impac.controller.coffee
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
angular.module 'mnoEnterpriseAngular'
.controller 'ImpacController', ($scope, $state, MnoeOrganizations, DOCK_CONFIG) ->
.controller 'ImpacController', ($scope, $state, ImpacDashboardsSvc, MnoeCurrentUser, MnoeOrganizations, DOCK_CONFIG) ->
'ngInject'

vm = this
vm.isImpacShown = false
vm.isDockEnabled = DOCK_CONFIG.enabled

#====================================
# Post-Initialization
#====================================
$scope.$watch(MnoeOrganizations.getSelectedId, (newValue) ->
vm.isImpacShown = true

# 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())

if !vm.isImpacShown
$state.go('home.login')
$scope.$watch(MnoeOrganizations.getSelectedId, (newValue, oldValue) ->
MnoeCurrentUser.get().then(
(response) ->
selectedOrg = _.find(response.organizations, {id: parseInt(newValue)})
# Needs to be at least admin to display impac! or user is redirected to apps dashboard
if MnoeOrganizations.role.atLeastAdmin(selectedOrg.current_user_role)
# Display impac! and force it to reload if necessary
vm.isImpacShown = true
ImpacDashboardsSvc.reload(true) if newValue? && oldValue? && newValue != oldValue
else
$state.go('home.apps')
) if newValue?
)

Expand Down
33 changes: 11 additions & 22 deletions src/app/views/layout.controller.coffee
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
angular.module 'mnoEnterpriseAngular'
.controller 'LayoutController', ($stateParams, $state, $q, MnoeCurrentUser, MnoeOrganizations, MnoeMarketplace) ->
.controller 'LayoutController', ($scope, $stateParams, $state, $q, MnoeCurrentUser, MnoeOrganizations) ->
'ngInject'

layout = this

# Hide the layout
layout.isLoggedIn = false

# App initialization
userPromise = MnoeCurrentUser.get()

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

$q.all([userPromise, organizationPromise]).then(
->
# Display the layout
layout.isLoggedIn = true

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

# Remove param from url
$state.go('.', {dhbRefId: undefined})
# Impac! is displayed only to admin and super admin
$scope.$watch(MnoeOrganizations.getSelectedId, (newValue) ->
MnoeCurrentUser.get().then(
(response) ->
selectedOrg = _.find(response.organizations, {id: parseInt(newValue)})
if MnoeOrganizations.role.atLeastAdmin(selectedOrg.current_user_role)
$state.go('home.impac')
else
$state.go('home.apps')
) if newValue?
)

return
2 changes: 1 addition & 1 deletion src/app/views/layout.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div id="layout-loading" ng-show="!layout.isLoggedIn" class="text-center">
<div id="layout-loading" ng-show="!isLoggedIn" class="text-center">
<mno-loader></mno-loader>
<span class="layout-loading-text">Your dashboard is loading...</span>
</div>
Expand Down
13 changes: 0 additions & 13 deletions src/app/views/login-router/login-router.coffee

This file was deleted.

0 comments on commit 5c54ec1

Please sign in to comment.