/* globals document:true, window:true */ (function() { 'use strict'; angular .module('dialoga') .controller('InicioPageController', InicioPageController); /** @ngInject */ function InicioPageController(ArticleService, $rootScope, $sce, $log) { var vm = this; // aliases vm.ArticleService = ArticleService; vm.$sce = $sce; vm.$log = $log; vm.init(); vm.$log.debug('InicioPageController'); } InicioPageController.prototype.init = function() { var vm = this; vm.error = null; vm.loading = true; vm.loadHomeArticle(); }; InicioPageController.prototype.loadHomeArticle = function() { var vm = this; vm.content = vm.ArticleService.getHomeAbstract(); vm.isCached = !!vm.content; if (vm.isCached) { hideBackground(2000); } vm.ArticleService.getHome(function(data) { vm.loading = false; vm.article = data.article; }, function(error) { vm.$log.error('Error on getHome article.', error); vm.error = 'Erro ao carregar o conteúdo principal.'; }); }; InicioPageController.prototype.showVideo = function() { var vm = this; // we need handle home content if (vm.isCached) { hideBackground(0); // force to hide vm.$log.warn('The content already cached. Aborting.'); return; } vm.content = vm.handleHomeAbstract(vm.article.abstract); vm.ArticleService.setHomeAbstract(vm.content); // inject dependencies injectIframeApiJs(); window.onYouTubeIframeAPIReady = window.onYouTubeIframeAPIReady || onYouTubeIframeAPIReady; window.onYouTubePlayerReady = window.onYouTubePlayerReady || onYouTubePlayerReady; }; InicioPageController.prototype.handleHomeAbstract = function(abstract) { var vm = this; abstract = forceIframeParams(abstract); abstract = removeStylefromIframe(abstract); return vm.$sce.trustAsHtml(abstract); }; function injectIframeApiJs() { var tag = document.createElement('script'); tag.src = 'https://www.youtube.com/iframe_api'; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); } function onYouTubeIframeAPIReady() { var ytIframe = angular.element.find('.js-iframe iframe'); var YTPlayer = window.YT.Player; new YTPlayer(ytIframe[0], { events: { 'onReady': onYouTubePlayerReady } }); } function onYouTubePlayerReady (event) { event.target.playVideo(); hideBackground(1000); } function hideBackground (ms) { var $elBg = angular.element.find('.video-background'); angular.element($elBg).fadeOut(ms || 100); // angular.element($elBg).hide(); } function forceIframeParams(abstract) { var patternIframe = '