Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use_strict';
- angular.module('GEMASApp',
- ['ngRoute',
- 'ngCookies',
- 'pascalprecht.translate',
- 'ngResource',
- 'ui.tree',
- 'ui.bootstrap'
- ])
- .config(['$routeProvider', '$locationProvider', '$httpProvider', '$translateProvider', function($routeProvider, $locationProvider, $httpProvider, $translateProvider) {
- $routeProvider
- .when('/create', {
- templateUrl: 'components/create/create.html',
- controller: 'CreateCtrl'
- })
- .when('/edit/:id', {
- templateUrl: 'components/edit/edit.html',
- controller: 'EditCtrl'
- })
- .when('/not-logged', {
- templateUrl: 'partials/not-logged.html',
- })
- .when('/index', {
- templateUrl: 'components/index/index.html',
- controller: 'IndexCtrl',
- label: 'INDEX'
- })
- // .when('/index', {
- // templateUrl: 'components/detail/detail.html',
- // controller: 'detailCtrl',
- // label: 'DETAIL'
- // })
- .when('/search', {
- templateUrl: 'components/searchEvents/searchEvents.html',
- controller: 'SearchEventsCtrl',
- label: 'GestiĆ³n de eventos'
- })
- .when('/home', {
- templateUrl: 'components/home/home.html',
- controller: 'HomeCtrl',
- label: 'HOME'
- })
- .otherwise('/home');
- $locationProvider.hashPrefix('!');
- /* Register error provider that shows message on failed requests or redirects to login page on
- * unauthenticated requests */
- $httpProvider.interceptors.push(function ($q, $rootScope, $location) {
- return {
- 'responseError': function(rejection) {
- var status = rejection.status;
- var config = rejection.config;
- var method = config.method;
- var url = config.url;
- if (status == 401) {
- $location.path( "/not-logged" );
- } else {
- $rootScope.error = method + " on " + url + " failed with status " + status;
- }
- return $q.reject(rejection);
- }
- };
- });
- /* Registers auth token interceptor, auth token is either passed by header or by query parameter
- * as soon as there is an authenticated user */
- $httpProvider.interceptors.push(function ($q, $rootScope, $location) {
- return {
- 'request': function(config) {
- var isRestCall = config.url.indexOf('rest') == 0;
- if (isRestCall) {
- config.url = GEMASAppConfig.restAddress + config.url;
- if (angular.isDefined($rootScope.authToken)) {
- var authToken = $rootScope.authToken;
- if (GEMASAppConfig.useAuthTokenHeader) {
- config.headers['X-Auth-Token'] = authToken;
- } else {
- config.url = config.url + "?token=" + authToken;
- }
- }
- }
- return config || $q.when(config);
- }
- };
- });
- $translateProvider.useStaticFilesLoader({
- prefix: 'languages/',
- suffix: '.json'
- });
- $translateProvider.useLocalStorage();
- $translateProvider.preferredLanguage('es');
- }])
- .run(function($rootScope, $location, $cookieStore, UserService, breadcrumbs, $route, CallRestService) {
- /* Reset error when a new view is loaded */
- $rootScope.$on('$viewContentLoaded', function() {
- delete $rootScope.error;
- });
- /* Change breadcrumb on route change */
- $rootScope.$on('$routeChangeSuccess', function(event, current){
- var pathElements = $location.path().split('/'), result = [], labels = [], i;
- var path = "";
- angular.forEach(pathElements, function (item) {
- item = item.trim();
- if (item === "" || (path[path.length-1] != '/')) {
- path += '/';
- }
- path += item;
- if($route.routes.hasOwnProperty(path)) {
- var label = $route.routes[path].label || item;
- labels.push(label);
- }
- });
- var breadcrumbPath = function (index) {
- return '#!/' + (pathElements.slice(0, index + 1)).join('/');
- };
- pathElements.shift();
- for (i = 0; i < pathElements.length; i++) {
- result.push({name: labels[i], path: breadcrumbPath(i)});
- }
- breadcrumbs.setBreadcrumbs(result);
- });
- $rootScope.hasRole = function(role) {
- if ($rootScope.user === undefined) {
- return false;
- }
- if ($rootScope.user.roles[role] === undefined) {
- return false;
- }
- return $rootScope.user.roles[role];
- };
- // Call service to retrieve events
- $rootScope.getProfileItems = function(){
- getUrlForCall.getUrl(function (url) {
- CallRestService.get(url, "")
- .then(function (response) {
- $rootScope.profileItems = response;
- });
- }, function (error) {
- console.log("Call Profile Service Error");
- }, jsonUrls.urlProfileItems);
- };
- $rootScope.logout = function() {
- delete $rootScope.user;
- delete $rootScope.authToken;
- $cookieStore.remove('authToken');
- $location.path("/not-logged");
- $rootScope.showLogout = false;
- };
- $rootScope.login = function() {
- UserService.authenticate($.param({username: $rootScope.username, password: $rootScope.password}), function(authenticationResult) {
- var authToken = authenticationResult.data.token;
- $rootScope.authToken = authToken;
- if ($rootScope.rememberMe) {
- $cookieStore.put('authToken', authToken);
- }
- UserService.current(function(user) {
- $rootScope.user = user;
- $rootScope.showLogout = true;
- $location.path("/");
- });
- $rootScope.getProfileItems();
- });
- };
- /* Try getting valid user from cookie or go to login page */
- var originalPath = $location.path();
- $location.path("/not-logged");
- var authToken = $cookieStore.get('authToken');
- if (authToken !== undefined) {
- $rootScope.authToken = authToken;
- UserService.get(function(user) {
- $rootScope.user = user;
- $location.path(originalPath);
- });
- }
- $rootScope.initialized = true;
- });
Add Comment
Please, Sign In to add comment