inicio.controller.js
3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* globals document:true, window:true */
(function() {
'use strict';
angular
.module('dialoga')
.controller('InicioPageController', InicioPageController);
/** @ngInject */
function InicioPageController(DialogaService, $scope, $sce, $log) {
var vm = this;
// aliases
vm.DialogaService = DialogaService;
vm.$scope = $scope;
vm.$sce = $sce;
vm.$log = $log;
vm.init();
vm.$log.debug('InicioPageController');
}
InicioPageController.prototype.init = function() {
var vm = this;
vm.article = null;
vm.themes = null;
vm.selectedTheme = null;
vm.programs = null;
vm.filtredPrograms = null;
vm.query = null;
vm.error = null;
vm.loadData();
vm.attachListeners();
};
InicioPageController.prototype.loadData = function() {
var vm = this;
vm.loading = true;
vm.loadingEvents = true;
vm.loadingThemes = true;
vm.loadingPrograms = true;
// Load main content
vm.DialogaService.getHome(function(data) {
vm.article = data.article;
if (vm.article.videoIsLoaded) {
hideBackground(2000);
}
loadAfterHome();
vm.loading = false;
}, function(error) {
vm.$log.error('Error on getHome.', error);
});
// Load event list
vm.DialogaService.getEvents({}, function(data) {
vm.events = data;
vm.loadingEvents = false;
}, function(error) {
vm.$log.error('Error on getEvents.', error);
vm.loadingEvents = false;
vm.eventsError = true;
});
function loadAfterHome () {
// Load theme list
vm.DialogaService.getThemes(function(data) {
vm.themes = data;
vm.loadingThemes = false;
}, function(error) {
vm.$log.error('Error on getThemes.', error);
});
// Load program list
vm.DialogaService.getProgramsRandom(function(data) {
vm.programs = vm.article.children;
vm.filtredPrograms = data;
vm.loadingPrograms = false;
}, function(error) {
vm.$log.error('Error on getPrograms.', error);
});
}
};
InicioPageController.prototype.showVideo = function() {
var vm = this;
// we need handle home content
if (vm.article.videoIsLoaded) {
hideBackground(0); // force to hide
vm.$log.debug('The content already cached. Show-it!');
return;
}
// inject dependencies
injectIframeApiJs();
window.onYouTubeIframeAPIReady = window.onYouTubeIframeAPIReady || onYouTubeIframeAPIReady;
window.onYouTubePlayerReady = window.onYouTubePlayerReady || onYouTubePlayerReady;
vm.article.videoIsLoaded = true;
};
InicioPageController.prototype.attachListeners = function() {
var vm = this;
vm.$scope.$on('change-selectedCategory', function (selectedCategory) {
vm.selectedTheme = selectedCategory;
});
};
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();
}
})();