Advertisement
xhosa

Untitled

Oct 26th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2. /**
  3.  * Clip-Two Main Controller
  4.  */
  5. app.controller('AppCtrl', ['$rootScope', '$scope', '$state', '$translate', '$localStorage', '$window', '$document', '$timeout', 'cfpLoadingBar', '$route', '$templateCache',
  6. function ($rootScope, $scope, $state, $translate, $localStorage, $window, $document, $timeout, cfpLoadingBar, $route, $templateCache) {
  7.  
  8.     // Loading bar transition
  9.     // -----------------------------------
  10.     var $win = $($window);
  11.  
  12.     $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
  13.         //start loading bar on stateChangeStart
  14.         cfpLoadingBar.start();
  15.  
  16.     });
  17.     $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
  18.  
  19.         //stop loading bar on stateChangeSuccess
  20.         event.targetScope.$watch("$viewContentLoaded", function () {
  21.  
  22.             cfpLoadingBar.complete();
  23.         });
  24.  
  25.         // scroll top the page on change state
  26.  
  27.         $document.scrollTo(0, 0);
  28.  
  29.         if (angular.element('.email-reader').length) {
  30.             angular.element('.email-reader').animate({
  31.                 scrollTop: 0
  32.             }, 0);
  33.         }
  34.  
  35.         // Save the route title
  36.         $rootScope.currTitle = $state.current.title;
  37.     });
  38.  
  39.     // State not found
  40.     $rootScope.$on('$stateNotFound', function (event, unfoundState, fromState, fromParams) {
  41.         //$rootScope.loading = false;
  42.         console.log(unfoundState.to);
  43.         // "lazy.state"
  44.         console.log(unfoundState.toParams);
  45.         // {a:1, b:2}
  46.         console.log(unfoundState.options);
  47.         // {inherit:false} + default options
  48.     });
  49.  
  50.     $rootScope.pageTitle = function () {
  51.         return $rootScope.app.name + ' - ' + ($rootScope.currTitle || $rootScope.app.description);
  52.     };
  53.  
  54.     // save settings to local storage
  55.     if (angular.isDefined($localStorage.layout)) {
  56.         $scope.app.layout = $localStorage.layout;
  57.  
  58.     } else {
  59.         $localStorage.layout = $scope.app.layout;
  60.     }
  61.     $scope.$watch('app.layout', function () {
  62.         // save to local storage
  63.         $localStorage.layout = $scope.app.layout;
  64.     }, true);
  65.  
  66.     //global function to scroll page up
  67.     $scope.toTheTop = function () {
  68.  
  69.         $document.scrollTopAnimated(0, 600);
  70.  
  71.     };
  72.  
  73.     // angular translate
  74.     // ----------------------
  75.  
  76.     $scope.language = {
  77.         // Handles language dropdown
  78.         listIsOpen: false,
  79.         // list of available languages
  80.         available: {
  81.             'en': 'English',
  82.             //'it_IT': 'Italiano',
  83.             'de_DE': 'Deutsch'
  84.         },
  85.         // display always the current ui language
  86.         init: function () {
  87.             var proposedLanguage = $translate.proposedLanguage() || $translate.use();
  88.             var preferredLanguage = $translate.preferredLanguage();
  89.             // we know we have set a preferred one in app.config
  90.             $scope.language.selected = $scope.language.available[(proposedLanguage || preferredLanguage)];
  91.         },
  92.         set: function (localeId, ev) {
  93.             $translate.use(localeId);
  94.             $scope.language.selected = $scope.language.available[localeId];
  95.             $scope.language.listIsOpen = !$scope.language.listIsOpen;
  96.         },
  97.         reloader: function(){    //reload the route here
  98.             var currentTemplate = $route.current.templateUrl;
  99.             $templateCache.remove(currentTemplate);
  100.             $route.reload();
  101.         },
  102.         uiReload: function(){
  103.             $state.reload();
  104.         }
  105.     };
  106.  
  107.     $scope.language.init();
  108.     // $scope.language.reloader();  //call the reload after initializing the language
  109.     $scope.language.uiReload();    //call this for ui-router
  110.  
  111.  
  112.  
  113.     // Function that find the exact height and width of the viewport in a cross-browser way
  114.     var viewport = function () {
  115.         var e = window, a = 'inner';
  116.         if (!('innerWidth' in window)) {
  117.             a = 'client';
  118.             e = document.documentElement || document.body;
  119.         }
  120.         return {
  121.             width: e[a + 'Width'],
  122.             height: e[a + 'Height']
  123.         };
  124.     };
  125.     // function that adds information in a scope of the height and width of the page
  126.     $scope.getWindowDimensions = function () {
  127.         return {
  128.             'h': viewport().height,
  129.             'w': viewport().width
  130.         };
  131.     };
  132.     // Detect when window is resized and set some variables
  133.     $scope.$watch($scope.getWindowDimensions, function (newValue, oldValue) {
  134.         $scope.windowHeight = newValue.h;
  135.         $scope.windowWidth = newValue.w;
  136.         if (newValue.w >= 992) {
  137.             $scope.isLargeDevice = true;
  138.         } else {
  139.             $scope.isLargeDevice = false;
  140.         }
  141.         if (newValue.w < 992) {
  142.             $scope.isSmallDevice = true;
  143.         } else {
  144.             $scope.isSmallDevice = false;
  145.         }
  146.         if (newValue.w <= 768) {
  147.             $scope.isMobileDevice = true;
  148.         } else {
  149.             $scope.isMobileDevice = false;
  150.         }
  151.     }, true);
  152.     // Apply on resize
  153.     $win.on('resize', function () {
  154.         $scope.$apply();
  155.     });
  156. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement