2013-08-28 00:07:28 +00:00
"use strict" ;
2013-08-29 02:08:59 +00:00
window . habitrpg = angular . module ( 'habitrpg' ,
2013-10-30 18:13:21 +00:00
[ 'ngResource' , 'ngSanitize' , 'userServices' , 'groupServices' , 'memberServices' , 'challengeServices' ,
2014-01-12 23:00:47 +00:00
'authServices' , 'notificationServices' , 'guideServices' , 'authCtrl' ,
2014-01-23 07:20:59 +00:00
'ui.bootstrap' , 'ui.keypress' , 'ui.router' , 'chieffancypants.loadingBar' , 'At' , 'pasvaz.bindonce' , 'infinite-scroll' , 'ui.select2' ] )
2013-08-29 02:08:59 +00:00
2013-11-02 23:07:12 +00:00
// @see https://github.com/angular-ui/ui-router/issues/110 and https://github.com/HabitRPG/habitrpg/issues/1705
// temporary hack until they have a better solution
. value ( '$anchorScroll' , angular . noop )
2013-08-28 01:13:05 +00:00
. constant ( "API_URL" , "" )
2013-08-29 02:08:59 +00:00
. constant ( "STORAGE_USER_ID" , 'habitrpg-user' )
. constant ( "STORAGE_SETTINGS_ID" , 'habit-mobile-settings' )
2013-12-16 04:28:59 +00:00
. constant ( "MOBILE_APP" , false )
2013-08-29 02:08:59 +00:00
//.constant("STORAGE_GROUPS_ID", "") // if we decide to take groups offline
2013-10-28 05:27:07 +00:00
. config ( [ '$stateProvider' , '$urlRouterProvider' , '$httpProvider' , 'STORAGE_SETTINGS_ID' ,
function ( $stateProvider , $urlRouterProvider , $httpProvider , STORAGE _SETTINGS _ID ) {
2013-08-29 02:08:59 +00:00
2013-10-28 05:27:07 +00:00
$urlRouterProvider
// Setup default selected tabs
. when ( '/options' , '/options/profile/avatar' )
. when ( '/options/profile' , '/options/profile/avatar' )
. when ( '/options/groups' , '/options/groups/tavern' )
2013-10-29 22:25:50 +00:00
. when ( '/options/groups/guilds' , '/options/groups/guilds/public' )
2014-01-06 01:03:47 +00:00
. when ( '/options/groups/hall' , '/options/groups/hall/heroes' )
2013-12-17 23:51:27 +00:00
. when ( '/options/inventory' , '/options/inventory/drops' )
2013-11-16 00:25:09 +00:00
. when ( '/options/settings' , '/options/settings/settings' )
2013-10-28 05:27:07 +00:00
// redirect states that don't match
. otherwise ( "/tasks" ) ;
$stateProvider
// Tasks
. state ( 'tasks' , {
url : "/tasks" ,
templateUrl : "partials/main.html"
} )
// Options
. state ( 'options' , {
url : "/options" ,
templateUrl : "partials/options.html" ,
controller : function ( ) { }
} )
// Options > Profile
. state ( 'options.profile' , {
url : "/profile" ,
templateUrl : "partials/options.profile.html" ,
controller : 'UserCtrl'
} )
. state ( 'options.profile.avatar' , {
url : "/avatar" ,
templateUrl : "partials/options.profile.avatar.html"
} )
. state ( 'options.profile.stats' , {
url : "/stats" ,
templateUrl : "partials/options.profile.stats.html"
} )
. state ( 'options.profile.profile' , {
2013-11-04 08:41:16 +00:00
url : "/profile" ,
2013-10-28 05:27:07 +00:00
templateUrl : "partials/options.profile.profile.html"
} )
// Options > Groups
2013-10-31 21:37:04 +00:00
. state ( 'options.social' , {
2013-10-28 05:27:07 +00:00
url : "/groups" ,
2013-10-31 21:37:04 +00:00
templateUrl : "partials/options.social.html"
2013-10-28 05:27:07 +00:00
} )
2014-01-06 01:03:47 +00:00
2013-10-31 21:37:04 +00:00
. state ( 'options.social.tavern' , {
2013-10-28 05:27:07 +00:00
url : "/tavern" ,
2013-10-31 21:37:04 +00:00
templateUrl : "partials/options.social.tavern.html" ,
2013-10-28 05:27:07 +00:00
controller : 'TavernCtrl'
} )
2014-01-06 01:03:47 +00:00
2013-10-31 21:37:04 +00:00
. state ( 'options.social.party' , {
2013-10-28 05:27:07 +00:00
url : '/party' ,
2013-10-31 21:37:04 +00:00
templateUrl : "partials/options.social.party.html" ,
2013-10-28 05:27:07 +00:00
controller : 'PartyCtrl'
} )
2014-01-06 01:03:47 +00:00
. state ( 'options.social.hall' , {
url : '/hall' ,
templateUrl : "partials/options.social.hall.html"
} )
. state ( 'options.social.hall.heroes' , {
url : '/heroes' ,
templateUrl : "partials/options.social.hall.heroes.html" ,
controller : 'HallHeroesCtrl'
} )
. state ( 'options.social.hall.patrons' , {
url : '/patrons' ,
templateUrl : "partials/options.social.hall.patrons.html" ,
controller : 'HallPatronsCtrl'
} )
2013-10-31 21:37:04 +00:00
. state ( 'options.social.guilds' , {
2013-10-29 19:26:13 +00:00
url : '/guilds' ,
2013-10-31 21:37:04 +00:00
templateUrl : "partials/options.social.guilds.html" ,
2013-10-28 05:27:07 +00:00
controller : 'GuildsCtrl'
} )
2013-10-31 21:37:04 +00:00
. state ( 'options.social.guilds.public' , {
2013-10-29 22:25:50 +00:00
url : '/public' ,
2013-10-31 21:37:04 +00:00
templateUrl : "partials/options.social.guilds.public.html"
2013-10-29 22:25:50 +00:00
} )
2013-10-31 21:37:04 +00:00
. state ( 'options.social.guilds.create' , {
2013-10-29 22:25:50 +00:00
url : '/create' ,
2013-10-31 21:37:04 +00:00
templateUrl : "partials/options.social.guilds.create.html"
2013-10-29 22:25:50 +00:00
} )
2013-10-31 21:37:04 +00:00
. state ( 'options.social.guilds.detail' , {
2013-10-29 22:25:50 +00:00
url : '/:gid' ,
2013-10-31 21:37:04 +00:00
templateUrl : 'partials/options.social.guilds.detail.html' ,
2013-10-29 22:25:50 +00:00
controller : [ '$scope' , 'Groups' , '$stateParams' , function ( $scope , Groups , $stateParams ) {
$scope . group = Groups . Group . get ( { gid : $stateParams . gid } ) ;
} ]
} )
2013-10-28 05:27:07 +00:00
2013-11-02 06:11:28 +00:00
// Options > Social > Challenges
2013-10-31 21:37:04 +00:00
. state ( 'options.social.challenges' , {
2013-10-28 05:27:07 +00:00
url : "/challenges" ,
controller : 'ChallengesCtrl' ,
2013-10-31 21:37:04 +00:00
templateUrl : "partials/options.social.challenges.html"
2013-10-30 00:49:49 +00:00
} )
2013-10-31 21:37:04 +00:00
. state ( 'options.social.challenges.detail' , {
2013-10-30 00:49:49 +00:00
url : '/:cid' ,
2013-10-31 21:37:04 +00:00
templateUrl : 'partials/options.social.challenges.detail.html' ,
2013-10-30 02:07:39 +00:00
controller : [ '$scope' , 'Challenges' , '$stateParams' ,
function ( $scope , Challenges , $stateParams ) {
$scope . obj = $scope . challenge = Challenges . Challenge . get ( { cid : $stateParams . cid } , function ( ) {
$scope . challenge . _locked = true ;
} ) ;
} ]
2013-10-28 05:27:07 +00:00
} )
2013-11-02 06:11:28 +00:00
. state ( 'options.social.challenges.detail.member' , {
url : '/:uid' ,
templateUrl : 'partials/options.social.challenges.detail.member.html' ,
controller : [ '$scope' , 'Challenges' , '$stateParams' ,
function ( $scope , Challenges , $stateParams ) {
$scope . obj = Challenges . Challenge . getMember ( { cid : $stateParams . cid , uid : $stateParams . uid } , function ( ) {
$scope . obj . _locked = true ;
} ) ;
} ]
} )
// Options > Inventory
. state ( 'options.inventory' , {
url : '/inventory' ,
2013-11-10 04:15:55 +00:00
templateUrl : "partials/options.inventory.html" ,
controller : 'InventoryCtrl'
2013-11-02 06:11:28 +00:00
} )
2013-12-17 23:51:27 +00:00
. state ( 'options.inventory.drops' , {
url : '/drops' ,
templateUrl : "partials/options.inventory.drops.html"
2013-11-02 06:11:28 +00:00
} )
2013-12-17 23:51:27 +00:00
. state ( 'options.inventory.pets' , {
2013-11-10 19:47:33 +00:00
url : '/pets' ,
2013-12-17 23:51:27 +00:00
templateUrl : "partials/options.inventory.pets.html"
2013-11-10 19:47:33 +00:00
} )
2013-12-17 23:51:27 +00:00
. state ( 'options.inventory.mounts' , {
2013-11-10 19:47:33 +00:00
url : '/mounts' ,
2013-12-17 23:51:27 +00:00
templateUrl : "partials/options.inventory.mounts.html"
} )
. state ( 'options.inventory.equipment' , {
url : '/equipment' ,
templateUrl : "partials/options.inventory.equipment.html"
2013-11-10 19:47:33 +00:00
} )
2013-10-28 05:27:07 +00:00
// Options > Settings
. state ( 'options.settings' , {
url : "/settings" ,
controller : 'SettingsCtrl' ,
templateUrl : "partials/options.settings.html"
} )
2013-11-16 00:25:09 +00:00
. state ( 'options.settings.settings' , {
url : "/settings" ,
templateUrl : "partials/options.settings.settings.html"
} )
. state ( 'options.settings.api' , {
url : "/api" ,
templateUrl : "partials/options.settings.api.html"
} )
. state ( 'options.settings.export' , {
url : "/export" ,
templateUrl : "partials/options.settings.export.html"
} )
2014-01-28 00:34:22 +00:00
. state ( 'options.settings.subscription' , {
url : "/subscription" ,
templateUrl : "partials/options.settings.subscription.html"
} )
2013-08-29 02:08:59 +00:00
var settings = JSON . parse ( localStorage . getItem ( STORAGE _SETTINGS _ID ) ) ;
2013-08-29 05:45:19 +00:00
if ( settings && settings . auth ) {
$httpProvider . defaults . headers . common [ 'Content-Type' ] = 'application/json;charset=utf-8' ;
$httpProvider . defaults . headers . common [ 'x-api-user' ] = settings . auth . apiId ;
2013-09-04 03:40:27 +00:00
$httpProvider . defaults . headers . common [ 'x-api-key' ] = settings . auth . apiToken ;
2013-08-29 05:45:19 +00:00
}
2013-09-01 15:34:00 +00:00
// Handle errors
2014-02-06 19:12:44 +00:00
$httpProvider . responseInterceptors . push ( [ '$rootScope' , '$q' , function ( $rootScope , $q ) {
2013-09-01 15:34:00 +00:00
function success ( response ) {
return response ;
}
function error ( response ) {
2014-02-06 19:12:44 +00:00
debugger ;
// Offline
if ( response . status == 0 ||
// don't know why we're getting 404 here, should be 0
( response . status == 404 && _ . isEmpty ( response . data ) ) ) {
$rootScope . $broadcast ( 'responseText' , window . env . t ( 'serverUnreach' ) ) ;
// Needs refresh
} else if ( response . needRefresh ) {
$rootScope . $broadcast ( 'responseError' , "The site has been updated and the page needs to refresh. The last action has not been recorded, please refresh and try again." ) ;
// 400 range?
} else if ( response < 500 ) {
$rootScope . $broadcast ( 'responseText' , response . data . err || response . data ) ;
// Error
} else {
var error = '<strong>Please reload</strong>, ' +
'"' + window . env . t ( 'error' ) + ' ' + ( response . data . err || response . data || 'something went wrong' ) + '"' +
window . env . t ( 'seeConsole' ) ;
$rootScope . $broadcast ( 'responseError' , error ) ;
console . error ( response ) ;
}
//return $q.reject(response); // this completely halts the chain, meaning we can't queue offline actions
return response ;
2013-09-01 15:34:00 +00:00
}
return function ( promise ) {
return promise . then ( success , error ) ;
}
2014-02-06 19:12:44 +00:00
} ] ) ;
2014-01-12 23:00:47 +00:00
} ] )