-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcustom-header-vimeo.js
159 lines (127 loc) · 3.62 KB
/
custom-header-vimeo.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
(function( window, wp ) {
var VimeoHandler = wp.customHeader.BaseVideoHandler.extend({
test: function( settings ) {
return 'video/x-vimeo' === settings.mimeType;
},
ready: function() {
var handler = this;
if ( 'Vimeo' in window ) {
handler.loadVideo();
} else {
var tag = document.createElement( 'script' );
tag.src = 'https://player.vimeo.com/api/player.js';
tag.onload = function () { handler.loadVideo(); };
document.getElementsByTagName( 'head' )[0].appendChild( tag );
}
},
loadVideo: function() {
var player,
handler = this;
// Track the paused state since the getPaused() method is asynchronous.
this._paused = true;
this.player = player = new Vimeo.Player( this.container, {
autopause: false,
autoplay: true,
// Background isn't currently supported in Vimeo's player library:
// https://github.com/vimeo/player.js/issues/39
background: true,
byline: false,
height: this.settings.height,
loop: true,
portrait: false,
title: false,
url: this.settings.videoUrl,
width: this.settings.width
});
player.on( 'play', function() {
handler._paused = false;
handler.trigger( 'play' );
});
player.on( 'pause', function() {
handler._paused = true;
handler.trigger( 'pause' );
});
player.ready().then(function() {
handler.showControls();
// Autoplay doesn't trigger a play event, so check the video
// state when ready is triggered.
player.getPaused().then(function( paused ) {
handler._paused = paused;
if ( ! paused ) {
handler.trigger( 'play' );
}
});
});
player.setVolume( 0 );
},
isPaused: function() {
return this._paused;
},
pause: function() {
this.player.pause();
},
play: function() {
this.player.play();
},
});
var VimeoLegacyHandler = wp.customHeader.BaseVideoHandler.extend({
origin: 'https://player.vimeo.com',
test: function( settings ) {
return 'video/x-vimeo' === settings.mimeType;
},
ready: function() {
var handler = this,
videoId = this.settings.videoUrl.split( '/' ).pop(),
iframe = document.createElement( 'iframe' );
this._paused = true;
handler.setVideo( iframe );
iframe.id = 'wp-custom-header-video';
iframe.src = 'https://player.vimeo.com/video/' + videoId + '?api=1&autopause=0&autoplay=0&background=1&badge=0&byline=0&loop=1&player_id=' + iframe.id + '&portrait=0&title=0';
this.iframe = iframe;
window.addEventListener( 'message', function( e ) {
var data;
if ( handler.origin !== e.origin ) {
return;
}
try {
data = JSON.parse( e.data );
} catch ( ex ) {
return;
}
if ( 'wp-custom-header-video' !== data.player_id ) {
return;
}
if ( 'ready' === data.event ) {
handler.postMessage( 'addEventListener', 'pause' );
handler.postMessage( 'addEventListener', 'play' );
handler.postMessage( 'setVolume', 0 );
handler.play();
handler.showControls();
} else if ( 'pause' === data.event ) {
handler._paused = true;
handler.trigger( data.event );
} else if ( 'play' === data.event ) {
handler._paused = false;
handler.trigger( data.event );
}
});
},
isPaused: function() {
return this._paused;
},
pause: function() {
this.postMessage( 'pause' );
},
play: function() {
this.postMessage( 'play' );
},
postMessage: function( method, params ) {
var data = JSON.stringify({
method: method,
value: params
});
this.iframe.contentWindow.postMessage( data, this.origin );
}
});
wp.customHeader.handlers.vimeo = new VimeoHandler();
})( window, wp );