--- /dev/null
+'use strict';
+
+angular.module('dashboardApp', [
+ 'ngCookies',
+ 'ngResource',
+ 'ngSanitize',
+ 'ui.router',
+ 'ui.bootstrap',
+ 'pascalprecht.translate'
+])
+ .config(function($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider, $translateProvider) {
+ $urlRouterProvider
+ .otherwise('/');
+
+ $locationProvider.html5Mode(true);
+ $httpProvider.interceptors.push('authInterceptor');
+
+ $translateProvider.useLoaderCache(true).useStaticFilesLoader({
+ prefix: 'assets/langs/locale-',
+ suffix: '.json'
+ })
+ .useCookieStorage()
+ .storagePrefix('twotoc_')
+ .useSanitizeValueStrategy('sanitize')
+ .preferredLanguage('it');
+ })
+
+ .factory('authInterceptor', function($rootScope, $q, $cookies, $injector) {
+ var state;
+ return {
+ // Add authorization token to headers
+ request: function(config) {
+ config.headers = config.headers || {};
+ if ($cookies.get('token')) {
+ config.headers.Authorization = 'Bearer ' + $cookies.get('token');
+ }
+ return config;
+ },
+
+ // Intercept 401s and redirect you to login
+ responseError: function(response) {
+ if (response.status === 401) {
+ (state || (state = $injector.get('$state'))).go('login');
+ // remove any stale tokens
+ $cookies.remove('token');
+ return $q.reject(response);
+ }
+ else {
+ return $q.reject(response);
+ }
+ }
+ };
+ })
+
+ .directive('resizewin', ['$window', function($window) {
+ return function (scope) {
+ var w = angular.element($window);
+ scope.getWindowDimensions = function () {
+ return {
+ 'h': w.height(),
+ 'w': w.width()
+ };
+ };
+ scope.$watch(scope.getWindowDimensions, function (newValue) {
+ scope.windowHeight = newValue.h;
+ scope.windowWidth = newValue.w;
+
+ scope.style = function (offset) {
+ return {
+ 'height': (newValue.h - offset) + 'px'
+ // 'width': (newValue.w - 100) + 'px'
+ };
+ };
+
+ }, true);
+
+ w.bind('resize', function () {
+ scope.$apply();
+ });
+ };
+ }])
+/*
+ .directive('mratio', ['$window', function($window) {
+ return {
+ restrict: 'A',
+ link: function(scope, element, attrs) {
+
+ var w = angular.element($window);
+ scope.getWindowDimensions = function () {
+ return {
+ 'w': element.width()
+ };
+ };
+
+ scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
+ mRatio(scope, element, attrs);
+ });
+
+ mRatio(scope, element, attrs);
+
+ function mRatio(scope, element, attrs) {
+ var image = new Image(); // or document.createElement('img')
+ var el_width, el_height, width, height, ratio;
+ el_width = element.width();
+ el_height = element.height();
+ image.onload = function() {
+ width = this.width;
+ height = this.height;
+ ratio = el_width / width;
+ var r_height = height * ratio;
+ if (el_height / height != ratio) {
+ attrs.$set('style', 'height: ' + r_height + 'px');
+ }
+ };
+ image.src = attrs.src;
+ }
+ }
+ }
+ }])
+*/
+ .run(function($rootScope, $state, Auth) {
+ // Redirect to login if route requires auth and the user is not logged in
+ $rootScope.$on('$stateChangeStart', function(event, next) {
+ if (next.authenticate) {
+ Auth.isLoggedIn(function(loggedIn) {
+ if (!loggedIn) {
+ event.preventDefault();
+ $state.go('login');
+ }
+ });
+ }
+ });
+ });