(function () { 'use strict'; var soter = angular.module('soter', ['ngRoute', 'ngResource', 'chieffancypants.loadingBar']) .config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) { $routeProvider. when('/', {templateUrl: '/app/home/view.html'}). when('/login', {templateUrl: '/app/shared/login.html', controller: 'LoginController'}). when('/logout', {templateUrl: '/app/home/view.html', controller: 'LogoutController'}). //when('/album', {templateUrl: '/app/album/view.html', controller: 'AlbumController', resolve: AlbumCtrl.resolve}). //when('/album/:id', {templateUrl: '/app/album/view.html', controller: 'AlbumController', resolve: AlbumCtrl.resolve}). //when('/picture', {templateUrl: '/app/picture/view.html', controller: 'PictureController', resolve: PictureCtrl.resolve, reloadOnSearch:false}). //when('/picture/:id', {templateUrl: '/app/picture/view.html', controller: 'PictureController', resolve: PictureCtrl.resolve, reloadOnSearch:false}). when('/users', { templateUrl: '/app/user/list.html', controller: 'UserListController', resolve: UserListCtrlResolve }). when('/user', { templateUrl: '/app/user/view.html', controller: 'UserController', resolve: UserCtrlResolve }). when('/user/:id', { templateUrl: '/app/user/view.html', controller: 'UserController', resolve: UserCtrlResolve }). when('/roles', { templateUrl: '/app/role/list.html', controller: 'RoleListController', resolve: RoleListCtrlResolve }). when('/role', { templateUrl: '/app/role/view.html', controller: 'RoleController', resolve: RoleCtrlResolve }). when('/role/:id', { templateUrl: '/app/role/view.html', controller: 'RoleController', resolve: RoleCtrlResolve }). //when('/settings', {templateUrl: '/partial/settings.html', controller: 'SettingsController', resolve: SettingsCtrl.resolve}). otherwise({templateUrl: '/app/shared/404.html'}); $locationProvider.html5Mode(true).hashPrefix('!'); }]) .config(['$httpProvider', function ($httpProvider) { $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; }]) .config(['$httpProvider', function ($httpProvider) { $httpProvider.interceptors.push('responseInterceptor'); }]) .factory('responseInterceptor', ['$q', '$rootScope', function ($q, $rootScope) { return { response: function (response) { return response; }, responseError: function (response) { var headers = response.headers(), isResourceResponse = headers['content-type'] === "application/json; charset=utf-8", status = response.status; if (status === 401 && !isResourceResponse) { $rootScope.$broadcast('loginRequired'); } // otherwise return $q.reject(response); } }; }]); angular.module('soter').controller('BaseCtrl', ['$rootScope', '$scope', 'AuthService', '$location', '$routeParams', function ($rootScope, $scope, AuthService, $location, $routeParams) { $rootScope.$on('loginRequired', function () { if (Session.loggedIn === true) { $location.path('/'); } else { var came_from = $routeParams.came_from; if (angular.isUndefined(came_from) && $location.path() !== '/login') { came_from = $location.url(); } $location.path('/login').search({came_from: came_from}); } }); $scope.toasts = []; $scope.clearToast = function (item) { var idx = $scope.toasts.indexOf(item); if (idx !== -1) { $scope.toasts.splice(idx, 1); } }; $rootScope.$on("$routeChangeStart", function (event, next, current) { Auth.get(function (u, putResponseHeaders) { $rootScope.auth = u; $rootScope.perms = u.perms; }); }); }]); })();