TwoToc code
[YouAndWeb_TwoToc] / client / app / app.js
1 'use strict';
2
3 angular.module('dashboardApp', [
4   'ngCookies',
5   'ngResource',
6   'ngSanitize',
7   'ui.router',
8   'ui.bootstrap',
9   'pascalprecht.translate'
10 ])
11   .config(function($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider, $translateProvider) {
12     $urlRouterProvider
13       .otherwise('/');
14
15     $locationProvider.html5Mode(true);
16     $httpProvider.interceptors.push('authInterceptor');
17
18     $translateProvider.useLoaderCache(true).useStaticFilesLoader({
19       prefix: 'assets/langs/locale-',
20       suffix: '.json'
21     })
22     .useCookieStorage()
23     .storagePrefix('twotoc_')
24     .useSanitizeValueStrategy('sanitize')
25     .preferredLanguage('it');
26   })
27
28   .factory('authInterceptor', function($rootScope, $q, $cookies, $injector) {
29     var state;
30     return {
31       // Add authorization token to headers
32       request: function(config) {
33         config.headers = config.headers || {};
34         if ($cookies.get('token')) {
35           config.headers.Authorization = 'Bearer ' + $cookies.get('token');
36         }
37         return config;
38       },
39
40       // Intercept 401s and redirect you to login
41       responseError: function(response) {
42         if (response.status === 401) {
43           (state || (state = $injector.get('$state'))).go('login');
44           // remove any stale tokens
45           $cookies.remove('token');
46           return $q.reject(response);
47         }
48         else {
49           return $q.reject(response);
50         }
51       }
52     };
53   })
54
55   .directive('resizewin', ['$window', function($window) {
56     return function (scope) {
57       var w = angular.element($window);
58       scope.getWindowDimensions = function () {
59           return {
60               'h': w.height(),
61               'w': w.width()
62           };
63       };
64       scope.$watch(scope.getWindowDimensions, function (newValue) {
65           scope.windowHeight = newValue.h;
66           scope.windowWidth = newValue.w;
67
68           scope.style = function (offset) {
69               return {
70                   'height': (newValue.h - offset) + 'px'
71                       // 'width': (newValue.w - 100) + 'px'
72               };
73           };
74
75       }, true);
76
77       w.bind('resize', function () {
78           scope.$apply();
79       });
80     };
81   }])
82 /*
83   .directive('mratio', ['$window',  function($window) {
84       return {
85         restrict: 'A',
86         link: function(scope, element, attrs) {
87
88           var w = angular.element($window);
89           scope.getWindowDimensions = function () {
90               return {
91                   'w': element.width()
92               };
93           };
94
95           scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
96             mRatio(scope, element, attrs);
97           });
98
99           mRatio(scope, element, attrs);
100
101           function mRatio(scope, element, attrs) {
102             var image = new Image(); // or document.createElement('img')
103             var el_width, el_height, width, height, ratio;
104             el_width = element.width();
105             el_height = element.height();
106             image.onload = function() {
107               width = this.width;
108               height = this.height;
109               ratio = el_width / width;
110               var r_height = height * ratio;
111               if (el_height / height != ratio) {
112                 attrs.$set('style', 'height: ' + r_height + 'px');
113               }
114             };
115             image.src = attrs.src;
116           }
117         }
118       }
119     }])
120 */
121   .run(function($rootScope, $state, Auth) {
122     // Redirect to login if route requires auth and the user is not logged in
123     $rootScope.$on('$stateChangeStart', function(event, next) {
124       if (next.authenticate) {
125         Auth.isLoggedIn(function(loggedIn) {
126           if (!loggedIn) {
127             event.preventDefault();
128             $state.go('login');
129           }
130         });
131       }
132     });
133   });