Advertisement
Guest User

Untitled

a guest
Apr 14th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /**
  2.      * myApp
  3.      */
  4.     var myApp = angular.module('myApp', ['ngMaterial', 'ngAnimate', 'ngRoute']);
  5.  
  6.     myApp.run(function($rootScope){
  7.  
  8.         $rootScope.snavbar = "";
  9.         $rootScope.iconBack = "";  
  10.         var user = JSON.parse(localStorage.getItem('userPrime'));
  11.         if(user)
  12.             $rootScope.is_log = "true";
  13.         else
  14.             $rootScope.is_log = "";
  15.  
  16.  
  17.         // Lista de filmes
  18.         // Pegar dinamicamente da DB
  19.         $rootScope.movies = [];    
  20.         /*,
  21.         {
  22.             'id'    : 2,
  23.             'name'  : 'Outro',
  24.             'rating': 2,
  25.             'poster': 'example.png'
  26.         },
  27.         {
  28.             'id'    : 3,
  29.             'name'  : 'Lorem ipsAção',
  30.             'rating': 1,
  31.             'poster': 'example.png'
  32.         },
  33.         {
  34.             'id'    : 4,
  35.             'name'  : 'MASODASDM',
  36.             'rating': 4,
  37.             'poster': 'example.png'
  38.         }];
  39.         */
  40.     });
  41.  
  42.  
  43.     /*
  44.     * Estrutura de requisição para usar de modelo
  45.     * nas próximas requisições que precisar    
  46.     myApp.service('myMovies', function($http, $q){
  47.  
  48.         return ({
  49.             get:myChoice,
  50.         });
  51.  
  52.  
  53.         function myChoice(){
  54.  
  55.             return $http.get("http://127.0.0.1/tests/webservice/movies.php").then(function(response){
  56.                 return response.data
  57.             });            
  58.         }
  59.     });
  60.     */
  61.  
  62.     // montar para para compartilhar infos do filme
  63.  
  64.     myApp.service('share',function($http){
  65.         return ({
  66.             getInformation:shareBox
  67.         });
  68.  
  69.         function shareBox(name,desc,image){
  70.             return $http.get("http://api.localhost:8080/api/share?name="+name+"&desc="+desc+"&image="+image).then(function(response){
  71.                 return response;
  72.             });
  73.         }
  74.     });
  75.  
  76.     //webservice para trazer histórico
  77.     // com nome do filme e do cinema
  78.  
  79.     myApp.service('history',function($http){
  80.         return ({
  81.             getHystory:getListHistory
  82.         });
  83.  
  84.         function getListHistory( userID){
  85.             return $http.get("http://primepassclub.com.br/api/history?user_id="+userID).then(function(response){
  86.                 return response.data;
  87.             });
  88.         }
  89.     });
  90.  
  91.     //webservice de funções do cartão
  92.     // carregar e ver saldo
  93.  
  94.     myApp.service('card',function($http){
  95.         return ({
  96.             getBallance:ballance,
  97.             getRecharge:recharge,
  98.         });
  99.  
  100.         function ballance( userID , sessionID  ){
  101.             return $http.get("http://primepassclub.com.br/api/cartao/saldo?id=" + userID + "&show_id=" + sessionID).then(function(response){
  102.                 return response.data;
  103.             });
  104.         }
  105.  
  106.         function recharge( userID , sessionID ){
  107.             return $http.get("http://primepassclub.com.br/api/cartao/recarga?id=" + userID + "&show_id=" + sessionID).then(function(response){
  108.                 return response.data;
  109.             });
  110.         }
  111.     });
  112.  
  113.     // webservice para trazer toda lista de cinemas
  114.     myApp.service('cinemas',function($http){
  115.  
  116.         return ({
  117.             getList:getCinemasList,
  118.             getMetters:getDistance,
  119.             getPosition: geoPositions
  120.         });
  121.        
  122.         function geoPositions(callback){
  123.             navigator.geolocation.getCurrentPosition(function(pos){
  124.                 callback(pos);
  125.             },errorPosition);
  126.         }
  127.  
  128.         // caso haja erros
  129.         function errorPosition(error){
  130.             console.log("Error: "+ error);
  131.         }
  132.  
  133.         /*
  134.         * Pega posição atual do usuario
  135.         * e faz o calculo da distancia entre ele e o cinema indexado no foreach
  136.         */
  137.          function getDistance( lat , lng, callback ){            
  138.                
  139.             geoPositions(function(pos){
  140.                 var userLocation      = new google.maps.LatLng(pos.coords.latitude , pos.coords.longitude);
  141.                 var targetLocation    = new google.maps.LatLng(lat, lng);
  142.                 var calculateDistance = parseInt(google.maps.geometry.spherical.computeDistanceBetween(userLocation, targetLocation)).toFixed(2);
  143.                
  144.                 callback(calculateDistance);
  145.             });                                
  146.                                
  147.         }        
  148.         /*
  149.         *   Caso haja erros
  150.         */
  151.         function errorPosition(err){
  152.              console.warn('ERROR(' + err.code + '): ' + err.message);
  153.         }
  154.  
  155.         /*
  156.         * Recebe lista dinâmica de todos cinemas
  157.         * retornar desta forma:
  158.         * {name,latitude,longitude,endereço}
  159.         */
  160.         function getCinemasList(){
  161.             return $http.get("http://primepassclub.com.br/api/cinemas").then(function(response){
  162.                 return response.data;
  163.             });
  164.         }
  165.     });
  166.  
  167.     // GoogleMaps latitude service
  168.     myApp.service('maps', function(){
  169.  
  170.         return ({
  171.             checkDistance: _checkDistance
  172.         });
  173.  
  174.  
  175.         /*
  176.          * Pega a posição atual do usuario
  177.          * Caso nao consiga, chama _error
  178.          * Caso consiga, faz um callback e manda as position
  179.          */
  180.         function getGeoLocation(callback) {
  181.             var options = { timeout: 31000, enableHighAccuracy: true, maximumAge: 90000 };        
  182.             navigator.geolocation.getCurrentPosition(function(positions){
  183.                 callback(positions);
  184.             }, _error, options);
  185.         }
  186.         /*
  187.          * Caso haja erros, exibve a mensagem abaixo no console
  188.          */
  189.         function _error(error){
  190.             console.log("Error: "+ error);
  191.         }
  192.  
  193.         /*
  194.          * Verifica se está na distancia de 100 metros do cinema
  195.          */
  196.         function _checkDistance(targetLat, targetLon, callback){
  197.  
  198.             // Acessamos a localizaçao do usuario
  199.             getGeoLocation(function(position){
  200.                 var userLocation      = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
  201.                 var targetLocation    = new google.maps.LatLng(targetLat, targetLon);
  202.                 var calculateDistance = parseInt(google.maps.geometry.spherical.computeDistanceBetween(userLocation, targetLocation)).toFixed(2);
  203.  
  204.                 callback(calculateDistance);
  205.  
  206.             });
  207.  
  208.         }
  209.  
  210.     });
  211.  
  212.     //service to check login to access app
  213.     //
  214.     myApp.service('user',function($http){
  215.  
  216.         return ({
  217.             login:getLogin
  218.         });
  219.  
  220.         // ----------
  221.         // -- Login
  222.         // ----------
  223.         function getLogin(login,pass){
  224.             return $http.get("http://primepassclub.com.br/api/user/auth?email="+login+"&pass="+pass).then(function(response){
  225.                 return response.data;
  226.             });
  227.         }
  228.  
  229.     });
  230.  
  231.     //service to get
  232.     // all current movie
  233.  
  234.     myApp.service('movies',function($http){
  235.  
  236.         return ({
  237.             all:getMovies
  238.         });
  239.  
  240.         function getMovies(){
  241.             return $http.get("http://primepassclub.com.br/api/movies").then(function(response){
  242.                 return response.data;
  243.             });
  244.         }
  245.     });
  246.  
  247.  
  248.     // get specific data
  249.     // of chosen moovies
  250.     myApp.service('shows', function($http){
  251.  
  252.         return ({
  253.             get: getShows,
  254.         });
  255.  
  256.         // -----------------
  257.         // -- Public
  258.         // -----------------
  259.         function getShows( movie_id ){
  260.  
  261.             return $http.get("http://primepassclub.com.br/api/movie/"+movie_id).then(function(response){
  262.                 return response.data;
  263.             });
  264.         }
  265.     });
  266.  
  267.     //service to use youtube api
  268.     myApp.service('youtubeAPI',function($window,$q){
  269.         return({
  270.             showMovie:loadPlayer,
  271.         });
  272.  
  273.         function getArtistId( id ) {
  274.             return id;
  275.         }
  276.  
  277.         function loadPlayer( video_id) {            
  278.             if (typeof(YT) == 'undefined' || typeof(YT.Player) == 'undefined') {
  279.                 //console.log('aqui');
  280.                 var tag = document.createElement('script');
  281.                 tag.src = "https://www.youtube.com/iframe_api";
  282.                 var firstScriptTag = document.getElementsByTagName('script')[0];
  283.                 firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  284.    
  285.                 window.onYouTubePlayerAPIReady = function() {                  
  286.                     onYouTubePlayer( video_id );
  287.                 };
  288.             }else{
  289.                 onYouTubePlayer( video_id );
  290.             }
  291.         }
  292.            
  293.  
  294.         function onYouTubePlayer( video_id) {
  295.           player = new YT.Player('player', {
  296.             height: '220',
  297.             width: '320',
  298.             videoId: getArtistId(video_id),
  299.             playerVars: { controls:0, showinfo: 0, rel: 0, showsearch: 0, iv_load_policy: 3, autohide: 1 },
  300.             events: {              
  301.               'onStateChange': onPlayerStateChange,              
  302.             }
  303.           });
  304.         }
  305.  
  306.         var player,iframe;
  307.        
  308.         var done = false;
  309.         function onPlayerStateChange(event) {
  310.             if (event.data == YT.PlayerState.PLAYING && !done) {
  311.                 stopVideo();
  312.                 done = true;
  313.             }
  314.             else if(event.data == YT.PlayerState.ENDED){
  315.                 location.reload();
  316.             }
  317.         }
  318.  
  319.         function stopVideo() {
  320.             player.stopVideo();        
  321.         }
  322.     });
  323.  
  324.     //obter sessões do filme clicado
  325.  
  326.     myApp.service('GetSessions',function($http){
  327.         return ({
  328.             getSession:getSessionList
  329.         });
  330.  
  331.         function getSessionList(id){
  332.             id = id || 266;
  333.             console.log(id);
  334.            
  335.             return $http.get("http://primepassclub.com.br/api/shows/"+id).then(function(response){
  336.                 return response.data;
  337.             });
  338.         }
  339.     });
  340.  
  341.     // ---
  342.     // PRIVATE METHODS.
  343.     // ---
  344.     // I transform the error response, unwrapping the application dta from
  345.     // the API response payload.
  346.     function handleError( response ) {
  347.         console.log('error')
  348.  
  349.     }
  350.     // I transform the successful response, unwrapping the application data
  351.     // from the API response payload.
  352.     function handleSuccess( response ) {
  353.         return (response.data);
  354.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement