This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoplayview-media.js
193 lines (154 loc) · 3.39 KB
/
autoplayview-media.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
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
187
188
189
190
191
192
193
import ScrollMonitor from 'scrollmonitor'
const API_YOUTUBE = 'https://www.youtube.com/iframe_api'
const TYPE_YOUTUBE = 'youtube'
const TYPE_VIMEO = 'vimeo'
const ACTION_PLAY = 'play'
const ACTION_PAUSE = 'pause'
const ACTION_STOP = 'stop'
/**
* Auto play media when in viewport.
*
* @extends {module:base/features~Feature}
*/
class AutoPlayViewMedia extends gi.features.Feature {
init() {
var mediaType = this.node.getAttribute('data-type')
switch (mediaType) {
case TYPE_YOUTUBE:
this.controller = new YoutubeController(this.node)
break
case TYPE_VIMEO:
this.controller = new VimeoController(this.node)
break
default:
throw new Error(`Media type "${mediaType}" not defined`)
}
this.watcher = ScrollMonitor.create(this.node)
this.watcher.on('fullyEnterViewport', this._fullyEnterViewportListener())
this.watcher.on('exitViewport', this._exitViewportListener())
}
destroy() {
super.destroy()
this.controller.destroy()
}
_fullyEnterViewportListener() {
return () => {
if (!this.controller.ended) {
this.controller.play()
}
}
}
_exitViewportListener() {
return () => {
if (!this.controller.ended) {
this.controller.pause()
}
}
}
}
class Controller {
constructor($element) {
this.ended = false
this.$element = $element
this.loaded = false
this.actions = []
this.init()
}
init() {}
destroy() {}
playMedia() {}
pauseMedia() {}
stopMedia() {}
play() {
this.actions.push(ACTION_PLAY)
this.playMedia()
}
pause() {
this.actions.push(ACTION_PAUSE)
this.pauseMedia()
}
stop() {
this.actions.push(ACTION_STOP)
this.stopMedia()
}
}
class VimeoController extends Controller {
init() {
this._loadPlayer()
}
playMedia() {
if (this.loaded) {
this.player.play()
}
}
pauseMedia() {
if (this.loaded) {
this.player.pause()
}
}
stopMedia() {
if (this.loaded) {
this.player.stop()
}
}
_loadPlayer() {
this.player = new Vimeo.Player(this.$element)
this.loaded = true
}
}
class YoutubeController extends Controller {
init() {
if (gi.utils.media.youtubeApiLoaded) {
this._loadPlayer()
} else {
this.apiReadyListener = this._onApiReady()
// listen to global youtube api event hub load event
gi.eventHub.on('apiReady:youtube', this.apiReadyListener)
// load youtube api library
gi.utils.fetch.script(API_YOUTUBE)
}
}
destroy() {
gi.eventHub.off('apiReady:youtube', this.apiReadyListener)
}
playMedia() {
if (this.loaded) {
this.player.playVideo()
}
}
pauseMedia() {
if (this.loaded) {
this.player.pauseVideo()
}
}
stopMedia() {
if (this.loaded) {
this.player.stopVideo()
}
}
_loadPlayer() {
this.player = new YT.Player(this.$element, {
events: {
onStateChange: (e) => {
if (e.data == YT.PlayerState.ENDED) {
this.ended = true
} else {
this.ended = false
}
},
onReady: () => {
this.loaded = true
if (this.actions[this.actions.length - 1] == ACTION_PLAY) {
this.play()
}
}
}
})
}
_onApiReady() {
return () => {
this._loadPlayer()
}
}
}
export default AutoPlayViewMedia