Pony.fm/public/scripts/app/controllers/application.coffee

73 lines
2.1 KiB
CoffeeScript
Raw Normal View History

2013-07-25 23:33:04 +02:00
angular.module('ponyfm').controller "application", [
2013-08-01 10:57:08 +02:00
'$scope', 'auth', '$location', 'upload', '$state', '$stateParams', '$injector', '$rootScope'
($scope, auth, $location, upload, $state, $stateParams, $injector, $rootScope) ->
2013-07-25 23:33:04 +02:00
$scope.auth = auth.data
$scope.$state = $state
$scope.$stateParams = $stateParams
2013-07-30 06:53:57 +02:00
$loadingElement = null
2013-08-16 01:49:20 +02:00
loadingStateName = null
2013-07-25 23:33:04 +02:00
2013-08-01 10:57:08 +02:00
$rootScope.safeApply = (fn) ->
phase = $rootScope.$$phase
if (phase == '$apply' || phase == 'digest')
fn()
return
$rootScope.$apply fn
2013-07-25 23:33:04 +02:00
$scope.logout = () ->
auth.logout().done -> location.reload()
$scope.isActive = (loc) -> $location.path() == loc
2013-07-29 22:54:53 +02:00
$scope.$on '$viewContentLoaded', () ->
window.setTimeout (-> window.handleResize()), 0
2013-07-25 23:33:04 +02:00
2013-07-30 06:53:57 +02:00
if $loadingElement
$loadingElement.removeClass 'loading'
$loadingElement = null
2013-07-29 22:54:53 +02:00
2013-08-16 01:49:20 +02:00
$scope.stateIncludes = (state) ->
if $loadingElement
newParts = state.split '.'
oldParts = loadingStateName.split '.'
for i in [0..newParts.length]
continue if !newParts[i]
return false if newParts[i] != oldParts[i]
return true
else
$state.includes(state)
2013-07-30 06:53:57 +02:00
statesPreloaded = {}
2013-08-16 01:49:20 +02:00
$scope.$on '$stateChangeStart', (e, newState, newParams, oldState, oldParams) ->
2013-07-30 06:53:57 +02:00
return if !oldState || !newState.controller
preloader = window.pfm.preloaders[newState.controller]
return if !preloader
if statesPreloaded[newState]
delete statesPreloaded[newState]
return
e.preventDefault()
2013-08-16 01:49:20 +02:00
loadingStateName = newState.name
2013-07-30 06:53:57 +02:00
selector = ''
newParts = newState.name.split '.'
oldParts = oldState.name.split '.'
zipped = _.zip(newParts, oldParts)
for i in [0..zipped.length]
break if !zipped[i] || zipped[i][0] != zipped[i][1]
selector += ' ui-view '
selector += ' ui-view ' if newState.name != oldState.name
$loadingElement = $ selector
$loadingElement.addClass 'loading'
stateToInject = angular.copy newState
stateToInject.params = newParams
$injector.invoke(preloader, null, {$state: stateToInject}).then ->
statesPreloaded[newState] = true
$state.transitionTo newState, newParams
2013-07-25 23:33:04 +02:00
]