Advertisement
Guest User

Untitled

a guest
Sep 30th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. itaas.factories.AdultMode = function () {
  2.     return ['$rootScope', '$route', '$location', '$modal', 'user', 'UserContent', 'config', 'errorHandler', 'ipCookie', 'moment', 'urlHelper', '_', 'googleAnalytics', function ($rootScope, $route, $location, $modal, user, UserContent, config, errorHandler, ipCookie, moment, urlHelper, _, googleAnalytics) {
  3.         var destroyLocationChangeStart, destroyAdultModeEnter, destroyAdultModeLeave, destroyRouteChangeSuccess;
  4.         var cookieKey = 'adultCookie';
  5.         var isInAdultMode = false;
  6.         var controllersToRaiseLeave = ['TvController', 'MyTvController', 'CatalogController'];
  7.  
  8.         var setCookie = function () {
  9.             ipCookie(cookieKey, 'dumbvalue', { expires: moment().add(10, 'minutes').toDate(), path: '/' });
  10.         };
  11.  
  12.         var isAdultCookiePresent = function () {
  13.             return angular.isDefined(ipCookie(cookieKey));
  14.         };
  15.  
  16.         var reset = function () {
  17.             ipCookie.remove(cookieKey);
  18.         };
  19.  
  20.         var redirect = function () {
  21.             var url = urlHelper.build('AdultController');
  22.             setCookie();
  23.             if (url === $location.path())
  24.                 $route.reload();
  25.             else
  26.                 $location.path(url);
  27.  
  28.             broadcastEnter();
  29.         };
  30.  
  31.         var locationChangeStart = function (event, newUrl) {
  32.             //TODO: change this, because if there is an 'adult' text in url, then it will prompt for PIN, even if the url is not adult.
  33.             if (!config.general.adultEnabled || isInAdultMode || newUrl.indexOf('adult') < 0)
  34.                 return;
  35.  
  36.             if (!isAdultCookiePresent()) {
  37.                 //display appropriated popup.
  38.                 downloadCustomData();
  39.                 event.preventDefault();
  40.                 return;
  41.             }
  42.             else {
  43.                 googleAnalytics.adult.reenteringWithoutPin();
  44.                 broadcastEnter();
  45.             }
  46.         };
  47.  
  48.         var routeChangeSuccess = function (event, route) {
  49.             if (!isInAdultMode)
  50.                 return;
  51.             var controller = route.controller;
  52.             if (controllersToRaiseLeave.indexOf(controller) >= 0)
  53.                 broadcastLeave();
  54.         };
  55.  
  56.         var init = function () {
  57.             destroyLocationChangeStart = $rootScope.$on('$locationChangeStart', locationChangeStart);
  58.         };
  59.  
  60.         var broadcastEnter = function () {
  61.             isInAdultMode = true;
  62.             destroyRouteChangeSuccess = $rootScope.$on('$routeChangeSuccess', routeChangeSuccess);
  63.             $rootScope.$broadcast('adultMode:enter');
  64.         };
  65.  
  66.         var onEnter = function (callback) {
  67.             destroyAdultModeEnter = $rootScope.$on('adultMode:enter', callback);
  68.         };
  69.  
  70.         var broadcastLeave = function () {
  71.             isInAdultMode = false;
  72.             destroyRouteChangeSuccess();
  73.             $rootScope.$broadcast('adultMode:leave');
  74.         };
  75.  
  76.         var onLeave = function (callback) {
  77.             destroyAdultModeLeave = $rootScope.$on('adultMode:leave', callback);
  78.         };
  79.  
  80.         var openCreatePIN = function (content) {
  81.             $modal.open({
  82.                 templateUrl: 'adult/createPIN.html',
  83.                 windowClass: 'adult-modal adult-create-pin-modal',
  84.                 controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {
  85.                     $scope.cancel = function () {
  86.                         googleAnalytics.adult.createPinSkipped();
  87.                         $modalInstance.dismiss('cancel');
  88.                         redirect();
  89.                     };
  90.                     $scope.close = function () {
  91.                         $modalInstance.dismiss('close');
  92.                     };
  93.                     $scope.create = function (PIN) {
  94.                         $scope.loading = true;
  95.                         content.AdultPIN = PIN;
  96.                         UserContent.customData(content).then(function () {
  97.                             googleAnalytics.adult.onValidPin();
  98.                             $modalInstance.close();
  99.                             redirect();
  100.                         })['catch'](function (error) {
  101.                             googleAnalytics.adult.onInvalidPin();
  102.                             errorHandler.handle(error);
  103.                         })['finally'](function () {
  104.                             $scope.loading = false;
  105.                         });
  106.                     };
  107.                 }]
  108.             });
  109.         };
  110.  
  111.         var openVerifyAge = function (content) {
  112.             $modal.open({
  113.                 templateUrl: 'adult/verifyAge.html',
  114.                 windowClass: 'adult-modal adult-age-verify-modal',
  115.                 controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {
  116.                     $scope.no = function () {
  117.                         googleAnalytics.adult.noClickedOnVerifyAge();
  118.                         $modalInstance.dismiss('no');
  119.                     };
  120.                     $scope.close = function () {
  121.                         $modalInstance.dismiss('close');
  122.                     };
  123.                     $scope.yes = function () {
  124.                         googleAnalytics.adult.yesClickedOnVerifyAge();
  125.                         if (!content.hasOwnProperty('AdultAgeVerified') || content.AdultAgeVerified === false) { //check if needs to update customData
  126.                             $scope.loading = true;
  127.                             content.AdultAgeVerified = true;
  128.                             UserContent.customData(content).then(function () {
  129.                                 $modalInstance.close();
  130.                                 if (!user.isAuthenticated())
  131.                                     redirect();
  132.                                 else
  133.                                     openCreatePIN(content);
  134.                             })['catch'](function (error) {
  135.                                 errorHandler.handle(error);
  136.                             })['finally'](function () {
  137.                                 $scope.loading = false;
  138.                             });
  139.                         }
  140.                         else {
  141.                             checkCustomData(content);
  142.                             $modalInstance.close();
  143.                         }
  144.                     };
  145.                 }]
  146.             });
  147.         };
  148.  
  149.         var openInformPIN = function () {
  150.             $modal.open({
  151.                 templateUrl: 'adult/informPIN.html',
  152.                 windowClass: 'adult-modal adult-inform-pin-modal',
  153.                 controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {
  154.                     $scope.incorrectPINError = false;
  155.                     $scope.reset = function () {
  156.                         googleAnalytics.adult.onResetPin();
  157.                         $modalInstance.close();
  158.                         openResetPIN();
  159.                     };
  160.                     $scope.close = function () {
  161.                         $modalInstance.dismiss('close');
  162.                     };
  163.                     $scope.inform = function (PIN) {
  164.                         $scope.loading = true;
  165.                         UserContent.customData().then(function (data) {
  166.                             var currentPIN = data.Content.hasOwnProperty('AdultPIN') ? data.Content.AdultPIN : undefined;
  167.                             if (currentPIN && currentPIN === PIN) {
  168.                                 $modalInstance.close();
  169.                                 redirect();
  170.                             }
  171.                             else {
  172.                                 googleAnalytics.adult.onIncorrectPin();
  173.                                 $scope.incorrectPINError = true;
  174.                             }
  175.                         })['catch'](function (error) {
  176.                             errorHandler.handle(error);
  177.                         })['finally'](function () {
  178.                             $scope.loading = false;
  179.                         });
  180.                     };
  181.                 }]
  182.             });
  183.         };
  184.  
  185.         var openResetPIN = function () {
  186.             $modal.open({
  187.                 templateUrl: 'adult/resetPIN.html',
  188.                 windowClass: 'adult-modal adult-reset-pin-modal',
  189.                 controller: ['$scope', '$modalInstance', 'user', 'apiUrl', 'requestService', function ($scope, $modalInstance, user, apiUrl, requestService) {
  190.                     $scope.incorrectPasswordError = false;
  191.                     $scope.close = function () {
  192.                         $modalInstance.dismiss('close');
  193.                     };
  194.                     $scope.reset = function (password, PIN) {
  195.                         $scope.loading = true;
  196.                         var url = apiUrl.accessToken(user.get().Login, password);
  197.                         requestService.get(url).then(function (data) {
  198.                             user.set(data.Content);
  199.                             return UserContent.customData({ AdultPIN: PIN });
  200.                         }).then(function () {
  201.                             googleAnalytics.adult.onSuccessfullyResettingPin();
  202.                             redirect();
  203.                             $modalInstance.close();
  204.                         })['catch'](function (error) {
  205.                             if (errorHandler.isLoginError(error)) {
  206.                                 googleAnalytics.adult.onWrongPasswordResettingPin();
  207.                                 $scope.incorrectPasswordError = true;
  208.                             }
  209.                             else
  210.                                 errorHandler.handle(error.StatusMessage);
  211.                         })['finally'](function () {
  212.                             $scope.loading = false;
  213.                         });
  214.                     };
  215.                 }]
  216.             });
  217.         };
  218.  
  219.         var checkCustomData = function (content) {
  220.             if (_.isEmpty(content) || !content.hasOwnProperty('AdultAgeVerified') || content.AdultAgeVerified === false)
  221.                 openVerifyAge(content);
  222.             else if (!content.hasOwnProperty('AdultPIN') || content.AdultPIN === null)
  223.                 openCreatePIN(content);
  224.             else
  225.                 openInformPIN();
  226.         };
  227.  
  228.         var downloadCustomData = function () {
  229.             UserContent.customData().then(function (data) {
  230.                 checkCustomData(data.Content);
  231.             })['catch'](function (error) {
  232.                 errorHandler.handle(error);
  233.             });
  234.         };
  235.  
  236.         //Clean up resources.
  237.         $rootScope.$on('$destroy', function () {
  238.             if (destroyLocationChangeStart)
  239.                 destroyLocationChangeStart();
  240.             if (destroyAdultModeEnter)
  241.                 destroyAdultModeEnter();
  242.             if (destroyAdultModeLeave)
  243.                 destroyAdultModeLeave();
  244.             if (destroyRouteChangeSuccess)
  245.                 destroyRouteChangeSuccess();
  246.         });
  247.  
  248.         return {
  249.             init: init,
  250.             onEnter: onEnter,
  251.             onLeave: onLeave,
  252.             reset: reset
  253.         };
  254.     }];
  255. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement