Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- itaas.factories.AdultMode = function () {
- 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) {
- var destroyLocationChangeStart, destroyAdultModeEnter, destroyAdultModeLeave, destroyRouteChangeSuccess;
- var cookieKey = 'adultCookie';
- var isInAdultMode = false;
- var controllersToRaiseLeave = ['TvController', 'MyTvController', 'CatalogController'];
- var setCookie = function () {
- ipCookie(cookieKey, 'dumbvalue', { expires: moment().add(10, 'minutes').toDate(), path: '/' });
- };
- var isAdultCookiePresent = function () {
- return angular.isDefined(ipCookie(cookieKey));
- };
- var reset = function () {
- ipCookie.remove(cookieKey);
- };
- var redirect = function () {
- var url = urlHelper.build('AdultController');
- setCookie();
- if (url === $location.path())
- $route.reload();
- else
- $location.path(url);
- broadcastEnter();
- };
- var locationChangeStart = function (event, newUrl) {
- //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.
- if (!config.general.adultEnabled || isInAdultMode || newUrl.indexOf('adult') < 0)
- return;
- if (!isAdultCookiePresent()) {
- //display appropriated popup.
- downloadCustomData();
- event.preventDefault();
- return;
- }
- else {
- googleAnalytics.adult.reenteringWithoutPin();
- broadcastEnter();
- }
- };
- var routeChangeSuccess = function (event, route) {
- if (!isInAdultMode)
- return;
- var controller = route.controller;
- if (controllersToRaiseLeave.indexOf(controller) >= 0)
- broadcastLeave();
- };
- var init = function () {
- destroyLocationChangeStart = $rootScope.$on('$locationChangeStart', locationChangeStart);
- };
- var broadcastEnter = function () {
- isInAdultMode = true;
- destroyRouteChangeSuccess = $rootScope.$on('$routeChangeSuccess', routeChangeSuccess);
- $rootScope.$broadcast('adultMode:enter');
- };
- var onEnter = function (callback) {
- destroyAdultModeEnter = $rootScope.$on('adultMode:enter', callback);
- };
- var broadcastLeave = function () {
- isInAdultMode = false;
- destroyRouteChangeSuccess();
- $rootScope.$broadcast('adultMode:leave');
- };
- var onLeave = function (callback) {
- destroyAdultModeLeave = $rootScope.$on('adultMode:leave', callback);
- };
- var openCreatePIN = function (content) {
- $modal.open({
- templateUrl: 'adult/createPIN.html',
- windowClass: 'adult-modal adult-create-pin-modal',
- controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {
- $scope.cancel = function () {
- googleAnalytics.adult.createPinSkipped();
- $modalInstance.dismiss('cancel');
- redirect();
- };
- $scope.close = function () {
- $modalInstance.dismiss('close');
- };
- $scope.create = function (PIN) {
- $scope.loading = true;
- content.AdultPIN = PIN;
- UserContent.customData(content).then(function () {
- googleAnalytics.adult.onValidPin();
- $modalInstance.close();
- redirect();
- })['catch'](function (error) {
- googleAnalytics.adult.onInvalidPin();
- errorHandler.handle(error);
- })['finally'](function () {
- $scope.loading = false;
- });
- };
- }]
- });
- };
- var openVerifyAge = function (content) {
- $modal.open({
- templateUrl: 'adult/verifyAge.html',
- windowClass: 'adult-modal adult-age-verify-modal',
- controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {
- $scope.no = function () {
- googleAnalytics.adult.noClickedOnVerifyAge();
- $modalInstance.dismiss('no');
- };
- $scope.close = function () {
- $modalInstance.dismiss('close');
- };
- $scope.yes = function () {
- googleAnalytics.adult.yesClickedOnVerifyAge();
- if (!content.hasOwnProperty('AdultAgeVerified') || content.AdultAgeVerified === false) { //check if needs to update customData
- $scope.loading = true;
- content.AdultAgeVerified = true;
- UserContent.customData(content).then(function () {
- $modalInstance.close();
- if (!user.isAuthenticated())
- redirect();
- else
- openCreatePIN(content);
- })['catch'](function (error) {
- errorHandler.handle(error);
- })['finally'](function () {
- $scope.loading = false;
- });
- }
- else {
- checkCustomData(content);
- $modalInstance.close();
- }
- };
- }]
- });
- };
- var openInformPIN = function () {
- $modal.open({
- templateUrl: 'adult/informPIN.html',
- windowClass: 'adult-modal adult-inform-pin-modal',
- controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {
- $scope.incorrectPINError = false;
- $scope.reset = function () {
- googleAnalytics.adult.onResetPin();
- $modalInstance.close();
- openResetPIN();
- };
- $scope.close = function () {
- $modalInstance.dismiss('close');
- };
- $scope.inform = function (PIN) {
- $scope.loading = true;
- UserContent.customData().then(function (data) {
- var currentPIN = data.Content.hasOwnProperty('AdultPIN') ? data.Content.AdultPIN : undefined;
- if (currentPIN && currentPIN === PIN) {
- $modalInstance.close();
- redirect();
- }
- else {
- googleAnalytics.adult.onIncorrectPin();
- $scope.incorrectPINError = true;
- }
- })['catch'](function (error) {
- errorHandler.handle(error);
- })['finally'](function () {
- $scope.loading = false;
- });
- };
- }]
- });
- };
- var openResetPIN = function () {
- $modal.open({
- templateUrl: 'adult/resetPIN.html',
- windowClass: 'adult-modal adult-reset-pin-modal',
- controller: ['$scope', '$modalInstance', 'user', 'apiUrl', 'requestService', function ($scope, $modalInstance, user, apiUrl, requestService) {
- $scope.incorrectPasswordError = false;
- $scope.close = function () {
- $modalInstance.dismiss('close');
- };
- $scope.reset = function (password, PIN) {
- $scope.loading = true;
- var url = apiUrl.accessToken(user.get().Login, password);
- requestService.get(url).then(function (data) {
- user.set(data.Content);
- return UserContent.customData({ AdultPIN: PIN });
- }).then(function () {
- googleAnalytics.adult.onSuccessfullyResettingPin();
- redirect();
- $modalInstance.close();
- })['catch'](function (error) {
- if (errorHandler.isLoginError(error)) {
- googleAnalytics.adult.onWrongPasswordResettingPin();
- $scope.incorrectPasswordError = true;
- }
- else
- errorHandler.handle(error.StatusMessage);
- })['finally'](function () {
- $scope.loading = false;
- });
- };
- }]
- });
- };
- var checkCustomData = function (content) {
- if (_.isEmpty(content) || !content.hasOwnProperty('AdultAgeVerified') || content.AdultAgeVerified === false)
- openVerifyAge(content);
- else if (!content.hasOwnProperty('AdultPIN') || content.AdultPIN === null)
- openCreatePIN(content);
- else
- openInformPIN();
- };
- var downloadCustomData = function () {
- UserContent.customData().then(function (data) {
- checkCustomData(data.Content);
- })['catch'](function (error) {
- errorHandler.handle(error);
- });
- };
- //Clean up resources.
- $rootScope.$on('$destroy', function () {
- if (destroyLocationChangeStart)
- destroyLocationChangeStart();
- if (destroyAdultModeEnter)
- destroyAdultModeEnter();
- if (destroyAdultModeLeave)
- destroyAdultModeLeave();
- if (destroyRouteChangeSuccess)
- destroyRouteChangeSuccess();
- });
- return {
- init: init,
- onEnter: onEnter,
- onLeave: onLeave,
- reset: reset
- };
- }];
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement