Last active
August 29, 2015 14:24
-
-
Save ilguzin/0b1afa71a46a501be2e7 to your computer and use it in GitHub Desktop.
example code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class dashboardCtrl | |
NOTIFICATIONS_STATUS_UPDATE_PERIOD = 30 # Seconds | |
@$inject = ['$scope', '$q', '$log', '$cookies', '$state', '$rootScope', '$timeout', 'MonitoringService', | |
'loginService', 'SettingsService'] | |
_s = {} | |
scheduleAgentDataUpdate = () -> | |
_s.MonitoringService.getUpdatePeriod().then (updatePeriod) => | |
_s.$log.debug "schedule update agents data: #{updatePeriod} seconds" | |
_s.$timeout (=> | |
@updateAgentsData().finally => scheduleAgentDataUpdate.call @ | |
), (+updatePeriod) * 1000 | |
scheduleNotificationsStatusUpdate = () -> | |
_s.SettingsService.getNotificationsStatus().then (notificationsState) => | |
@user.notificationsState = +notificationsState | |
_s.$timeout (=> scheduleNotificationsStatusUpdate.call @), NOTIFICATIONS_STATUS_UPDATE_PERIOD * 1000 | |
constructor: (args...) -> | |
_s = _.object dashboardCtrl.$inject, args | |
@agentsMenuShow = true | |
@agentsMenuOpen = false | |
@hasParentState = _s.$state.includes | |
@user = _s.loginService.getUser() | |
_s.$scope.$watch (-> _s.MonitoringService.getAgentsData()), (newVal, oldVal) => | |
@monitoringAgents = _s.MonitoringService.getAgentsData() | |
_s.$scope.notificationsOnOffCallback = => | |
_s.SettingsService.notificationsOnOff(@user.notificationsState) | |
_s.$scope.$on 'showAgentsMenu', @showAgentsMenu | |
_s.$scope.$on 'dashboard.updateAgents', () => @updateAgentsData() | |
_s.$scope.$on '$stateChangeSuccess', => | |
@agentsMenuShow = _s.$state.includes 'dashboard.monitoring' | |
if _s.$state.current.name isnt 'dashboard.monitoring.phones' | |
@hideAgentsMenu() | |
else | |
@showAgentsMenu() | |
scheduleNotificationsStatusUpdate.call @ | |
scheduleAgentDataUpdate.call @ | |
@updateAgentsData() | |
_s.$log.debug "dashboardCtrl", _s.$scope | |
isAgentsOverallStatusBad: () -> | |
(_.select @monitoringAgents, (agent) -> +agent.status in [3, 5])?.length > 0 | |
logout: -> | |
_s.$cookies.authCode = ""; | |
_s.$state.go 'login' | |
updateAgentsData: () -> | |
promise = _s.MonitoringService.getAgents() | |
_s.$scope.$broadcast 'dashboard.monitoring.phonesUpdate' | |
_s.$scope.$broadcast 'dashboard.monitoring.diagramUpdate' | |
promise | |
hasRole: (roles) -> | |
_s.loginService.hasRole(roles) | |
toggleAgentsMenu: -> | |
@agentsMenuOpen = not @agentsMenuOpen | |
showAgentsMenu: -> | |
@agentsMenuOpen = true | |
hideAgentsMenu: -> | |
@agentsMenuOpen = false | |
isAgentActive: (agent) -> | |
activeAgent = _s.MonitoringService.getActiveAgent() | |
activeAgent and activeAgent.agentId is agent.agentId | |
goAgent: (agent) -> | |
_s.$state.go 'dashboard.monitoring.phones', {agentId: agent.agentId} | |
goMonitoring: () -> | |
unless @hasParentState 'dashboard.monitoring' | |
_s.$state.go 'dashboard.monitoring' | |
if (@hasParentState 'dashboard.monitoring.phones') or (@hasParentState 'dashboard.monitoring.diagram') | |
@toggleAgentsMenu() | |
@updateAgentsData() | |
espp.controller 'dashboardCtrl', dashboardCtrl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment