Guest User

Untitled

a guest
Jun 10th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. 'use_strict';
  2.  
  3. angular.module('GEMASApp',
  4. ['ngRoute',
  5. 'ngCookies',
  6. 'pascalprecht.translate',
  7. 'ngResource',
  8. 'ui.tree',
  9. 'ui.bootstrap'
  10. ])
  11. .config(['$routeProvider', '$locationProvider', '$httpProvider', '$translateProvider', function($routeProvider, $locationProvider, $httpProvider, $translateProvider) {
  12.  
  13. $routeProvider
  14. .when('/create', {
  15. templateUrl: 'components/create/create.html',
  16. controller: 'CreateCtrl'
  17. })
  18. .when('/edit/:id', {
  19. templateUrl: 'components/edit/edit.html',
  20. controller: 'EditCtrl'
  21. })
  22. .when('/not-logged', {
  23. templateUrl: 'partials/not-logged.html',
  24. })
  25. .when('/index', {
  26. templateUrl: 'components/index/index.html',
  27. controller: 'IndexCtrl',
  28. label: 'INDEX'
  29. })
  30. // .when('/index', {
  31. // templateUrl: 'components/detail/detail.html',
  32. // controller: 'detailCtrl',
  33. // label: 'DETAIL'
  34. // })
  35.  
  36. .when('/search', {
  37. templateUrl: 'components/searchEvents/searchEvents.html',
  38. controller: 'SearchEventsCtrl',
  39. label: 'GestiĆ³n de eventos'
  40. })
  41. .when('/home', {
  42. templateUrl: 'components/home/home.html',
  43. controller: 'HomeCtrl',
  44. label: 'HOME'
  45. })
  46. .otherwise('/home');
  47.  
  48. $locationProvider.hashPrefix('!');
  49.  
  50. /* Register error provider that shows message on failed requests or redirects to login page on
  51. * unauthenticated requests */
  52. $httpProvider.interceptors.push(function ($q, $rootScope, $location) {
  53. return {
  54. 'responseError': function(rejection) {
  55. var status = rejection.status;
  56. var config = rejection.config;
  57. var method = config.method;
  58. var url = config.url;
  59.  
  60. if (status == 401) {
  61. $location.path( "/not-logged" );
  62. } else {
  63. $rootScope.error = method + " on " + url + " failed with status " + status;
  64. }
  65.  
  66. return $q.reject(rejection);
  67. }
  68. };
  69. });
  70.  
  71. /* Registers auth token interceptor, auth token is either passed by header or by query parameter
  72. * as soon as there is an authenticated user */
  73. $httpProvider.interceptors.push(function ($q, $rootScope, $location) {
  74. return {
  75. 'request': function(config) {
  76. var isRestCall = config.url.indexOf('rest') == 0;
  77. if (isRestCall) {
  78. config.url = GEMASAppConfig.restAddress + config.url;
  79.  
  80. if (angular.isDefined($rootScope.authToken)) {
  81. var authToken = $rootScope.authToken;
  82. if (GEMASAppConfig.useAuthTokenHeader) {
  83. config.headers['X-Auth-Token'] = authToken;
  84. } else {
  85. config.url = config.url + "?token=" + authToken;
  86. }
  87. }
  88. }
  89. return config || $q.when(config);
  90. }
  91. };
  92. });
  93.  
  94. $translateProvider.useStaticFilesLoader({
  95. prefix: 'languages/',
  96. suffix: '.json'
  97. });
  98.  
  99. $translateProvider.useLocalStorage();
  100. $translateProvider.preferredLanguage('es');
  101.  
  102. }])
  103. .run(function($rootScope, $location, $cookieStore, UserService, breadcrumbs, $route, CallRestService) {
  104.  
  105. /* Reset error when a new view is loaded */
  106. $rootScope.$on('$viewContentLoaded', function() {
  107. delete $rootScope.error;
  108. });
  109.  
  110. /* Change breadcrumb on route change */
  111. $rootScope.$on('$routeChangeSuccess', function(event, current){
  112. var pathElements = $location.path().split('/'), result = [], labels = [], i;
  113.  
  114. var path = "";
  115. angular.forEach(pathElements, function (item) {
  116. item = item.trim();
  117. if (item === "" || (path[path.length-1] != '/')) {
  118. path += '/';
  119. }
  120.  
  121. path += item;
  122.  
  123. if($route.routes.hasOwnProperty(path)) {
  124. var label = $route.routes[path].label || item;
  125. labels.push(label);
  126. }
  127.  
  128. });
  129.  
  130.  
  131. var breadcrumbPath = function (index) {
  132. return '#!/' + (pathElements.slice(0, index + 1)).join('/');
  133. };
  134.  
  135. pathElements.shift();
  136.  
  137. for (i = 0; i < pathElements.length; i++) {
  138. result.push({name: labels[i], path: breadcrumbPath(i)});
  139. }
  140.  
  141. breadcrumbs.setBreadcrumbs(result);
  142. });
  143.  
  144. $rootScope.hasRole = function(role) {
  145.  
  146. if ($rootScope.user === undefined) {
  147. return false;
  148. }
  149.  
  150. if ($rootScope.user.roles[role] === undefined) {
  151. return false;
  152. }
  153.  
  154. return $rootScope.user.roles[role];
  155. };
  156.  
  157. // Call service to retrieve events
  158. $rootScope.getProfileItems = function(){
  159. getUrlForCall.getUrl(function (url) {
  160. CallRestService.get(url, "")
  161. .then(function (response) {
  162. $rootScope.profileItems = response;
  163. });
  164. }, function (error) {
  165. console.log("Call Profile Service Error");
  166. }, jsonUrls.urlProfileItems);
  167. };
  168.  
  169. $rootScope.logout = function() {
  170. delete $rootScope.user;
  171. delete $rootScope.authToken;
  172. $cookieStore.remove('authToken');
  173. $location.path("/not-logged");
  174. $rootScope.showLogout = false;
  175. };
  176.  
  177. $rootScope.login = function() {
  178. UserService.authenticate($.param({username: $rootScope.username, password: $rootScope.password}), function(authenticationResult) {
  179. var authToken = authenticationResult.data.token;
  180. $rootScope.authToken = authToken;
  181. if ($rootScope.rememberMe) {
  182. $cookieStore.put('authToken', authToken);
  183. }
  184. UserService.current(function(user) {
  185. $rootScope.user = user;
  186. $rootScope.showLogout = true;
  187. $location.path("/");
  188. });
  189.  
  190. $rootScope.getProfileItems();
  191. });
  192.  
  193. };
  194.  
  195. /* Try getting valid user from cookie or go to login page */
  196. var originalPath = $location.path();
  197. $location.path("/not-logged");
  198. var authToken = $cookieStore.get('authToken');
  199. if (authToken !== undefined) {
  200. $rootScope.authToken = authToken;
  201. UserService.get(function(user) {
  202. $rootScope.user = user;
  203. $location.path(originalPath);
  204. });
  205. }
  206.  
  207. $rootScope.initialized = true;
  208. });
Add Comment
Please, Sign In to add comment