-
Notifications
You must be signed in to change notification settings - Fork 46
/
main.component.ts
186 lines (161 loc) · 5.63 KB
/
main.component.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { Component, AfterViewInit } from '@angular/core';
import { YoutubeApiService } from '../shared/services/youtube-api.service';
import { YoutubePlayerService } from '../shared/services/youtube-player.service';
import { PlaylistStoreService } from '../shared/services/playlist-store.service';
import { NotificationService } from '../shared/services/notification.service';
@Component({
selector: 'main-block',
templateUrl: 'main.component.html',
styleUrls: ['main.component.css']
})
export class MainComponent implements AfterViewInit {
public videoList = [];
public videoPlaylist = [];
public loadingInProgress = false;
public playlistToggle = false;
public playlistNames = false;
public repeat = false;
public shuffle = false;
public playlistElement: any;
private pageLoadingFinished = false;
constructor(
private youtubeService: YoutubeApiService,
private youtubePlayer: YoutubePlayerService,
private playlistService: PlaylistStoreService,
private notificationService: NotificationService
) {
this.videoPlaylist = this.playlistService.retrieveStorage().playlists;
}
ngAfterViewInit() {
this.playlistElement = document.getElementById('playlist');
}
playFirstInPlaylist(): void {
if (this.videoPlaylist[0]) {
this.playlistElement.scrollTop = 0;
this.youtubePlayer.playVideo(this.videoPlaylist[0].id, this.videoPlaylist[0].snippet.title);
}
}
handleSearchVideo(videos: Array<any>): void {
this.videoList = videos;
}
checkAddToPlaylist(video: any): void {
if (!this.videoPlaylist.some((e) => e.id === video.id)) {
this.videoPlaylist.push(video);
this.playlistService.addToPlaylist(video);
let inPlaylist = this.videoPlaylist.length - 1;
setTimeout(() => {
let topPos = document.getElementById(this.videoPlaylist[inPlaylist].id).offsetTop;
this.playlistElement.scrollTop = topPos - 100;
});
}
}
repeatActive(val: boolean): void {
this.repeat = val;
this.shuffle = false;
}
shuffleActive(val: boolean): void {
this.shuffle = val;
this.repeat = false;
}
togglePlaylist(): void {
this.playlistToggle = !this.playlistToggle;
setTimeout(() => {
this.playlistNames = !this.playlistNames;
}, 200);
}
searchMore(): void {
if (this.loadingInProgress || this.pageLoadingFinished || this.videoList.length < 1) {
return;
}
this.loadingInProgress = true;
this.youtubeService.searchNext()
.then(data => {
this.loadingInProgress = false;
if (data.length < 1 || data.status === 400) {
setTimeout(() => {
this.pageLoadingFinished = true;
setTimeout(() => {
this.pageLoadingFinished = false;
}, 10000);
})
return;
}
data.forEach((val) => {
this.videoList.push(val);
});
}).catch(error => {
this.loadingInProgress = false;
})
}
nextVideo(): void {
this.playPrevNext(true);
}
prevVideo(): void {
this.playPrevNext(false);
}
playPrevNext(value): void {
let current = this.youtubePlayer.getCurrentVideo();
let inPlaylist;
this.videoPlaylist.forEach((video, index) => {
if (video.id === current) {
inPlaylist = index;
}
});
// if-else hell
if (inPlaylist !== undefined) {
let topPos = document.getElementById(this.videoPlaylist[inPlaylist].id).offsetTop;
if (this.shuffle) {
let shuffled = this.videoPlaylist[this.youtubePlayer.getShuffled(inPlaylist, this.videoPlaylist.length)];
this.youtubePlayer.playVideo(shuffled.id, shuffled.snippet.title);
this.playlistElement.scrollTop = document.getElementById(shuffled.id).offsetTop - 100;
} else {
if (value) {
if (this.videoPlaylist.length - 1 === inPlaylist) {
this.youtubePlayer.playVideo(this.videoPlaylist[0].id, this.videoPlaylist[0].snippet.title);
this.playlistElement.scrollTop = 0;
} else {
this.youtubePlayer.playVideo(this.videoPlaylist[inPlaylist + 1].id, this.videoPlaylist[inPlaylist + 1].snippet.title)
this.playlistElement.scrollTop = topPos - 100;
}
} else {
if (inPlaylist === 0) {
this.youtubePlayer.playVideo(this.videoPlaylist[this.videoPlaylist.length - 1].id,
this.videoPlaylist[this.videoPlaylist.length - 1].snippet.title);
this.playlistElement.scrollTop = this.playlistElement.offsetHeight;
} else {
this.youtubePlayer.playVideo(this.videoPlaylist[inPlaylist - 1].id, this.videoPlaylist[inPlaylist - 1].snippet.title)
this.playlistElement.scrollTop = topPos - 230;
}
}
}
} else {
this.playFirstInPlaylist();
}
}
closePlaylist(): void {
this.playlistToggle = false;
this.playlistNames = false;
}
clearPlaylist(): void {
this.videoPlaylist = [];
this.playlistService.clearPlaylist();
this.notificationService.showNotification('Playlist cleared.');
}
exportPlaylist(): void {
if (this.videoPlaylist.length < 1) {
this.notificationService.showNotification('Nothing to export.');
return;
}
let data = JSON.stringify(this.videoPlaylist);
let a = document.createElement('a');
let file = new Blob([data], { type: 'text/json' });
a.href = URL.createObjectURL(file);
a.download = 'playlist.json';
a.click();
this.notificationService.showNotification('Playlist exported.');
}
importPlaylist(playlist: any): void {
this.videoPlaylist = playlist;
this.playlistService.importPlaylist(this.videoPlaylist);
}
}