-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
92 lines (90 loc) · 1.85 KB
/
javascript.js
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
var app = new Vue({
el: '#app',
data: function () {
return {
player: null,
searchKeyword: '',
searchResults: [],
showSearchResults: false,
loading:false,
videoDialog: false,
currentVideo: {snippet:{}},
logoSize: 'logo-big',
}
},
methods: {
enablePlayer () {
this.player = new YT.Player('player', {
height: '390',
width: '640',
videoId: this.currentVideo.id.videoId,
events: {
'onReady': this.onPlayerReady,
'onStateChange': this.onPlayerStateChange
}
})
},
onPlayerReady(event) {
event.target.playVideo()
},
onPlayerStateChange(event) {
if(event.data === YT.PlayerState.ENDED) {
this.stopVideo()
}
},
stopVideo() {
this.player.stopVideo()
},
search() {
let self = this
this.loading = true
let params = {
'maxResults': '25',
'part': 'snippet',
'q': this.searchKeyword,
'type': '',
'key': 'YOUR_API_KEY'
}
axios({
method: 'get',
url: 'https://www.googleapis.com/youtube/v3/search',
params: params
})
.then(function (response) {
self.showSearchResults = true
self.logoSize = 'logo-small'
self.searchResults = response.data.items
self.loading = false
})
.catch(function (error) {
console.log(error);
self.loading = false
});
},
showVideo(video) {
this.videoDialog = true
this.currentVideo = video
this.player=null
this.enablePlayer()
},
hideVideo() {
this.videoDialog = false
this.currentVideo = {snippet:{}}
let self = this
setTimeout(function() {self.player.destroy()},500);
},
reset() {
this.showSearchResults = false
this.logoSize="logo-big"
this.searchResults = []
}
},
watch: {
searchKeyword(val) {
this.search()
},
videoDialog(val) {
if(val===false) this.hideVideo()
}
}
})