aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/plugins/vimeo.js
blob: 8c7f2b04b3d4482bd86fcc178a0df33b91a2ed0b (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// ==========================================================================
// Vimeo plugin
// ==========================================================================

import utils from './../utils';
import captions from './../captions';
import ui from './../ui';

const vimeo = {
    // Setup YouTube
    setup() {
        // Remove old containers
        const containers = utils.getElements.call(this, `[id^="${this.type}-"]`);
        Array.from(containers).forEach(utils.removeElement);

        // Add embed class for responsive
        utils.toggleClass(this.elements.wrapper, this.config.classNames.embed, true);

        // Set ID
        this.media.setAttribute('id', utils.generateId(this.type));

        // Load the API if not already
        if (!utils.is.object(window.Vimeo)) {
            utils.loadScript(this.config.urls.vimeo.api);
            // Wait for load
            const vimeoTimer = window.setInterval(() => {
                if (utils.is.object(window.Vimeo)) {
                    window.clearInterval(vimeoTimer);
                    vimeo.ready.call(this);
                }
            }, 50);
        } else {
            vimeo.ready.call(this);
        }
    },

    // Ready
    ready() {
        const player = this;

        // Get Vimeo params for the iframe
        const options = {
            loop: this.config.loop.active,
            autoplay: this.config.autoplay,
            byline: false,
            portrait: false,
            title: false,
            transparent: 0,
        };
        const params = utils.buildUrlParameters(options);
        const id = utils.parseVimeoId(this.embedId);

        // Build an iframe
        const iframe = utils.createElement('iframe');
        const src = `https://player.vimeo.com/video/${id}?${params}`;
        iframe.setAttribute('src', src);
        iframe.setAttribute('allowfullscreen', '');
        player.media.appendChild(iframe);

        // Setup instance
        // https://github.com/vimeo/this.js
        player.embed = new window.Vimeo.Player(iframe);

        // Create a faux HTML5 API using the Vimeo API
        player.media.play = () => {
            player.embed.play();
            player.media.paused = false;
        };
        player.media.pause = () => {
            player.embed.pause();
            player.media.paused = true;
        };
        player.media.stop = () => {
            player.embed.stop();
            player.media.paused = true;
        };

        player.media.paused = true;
        player.media.currentTime = 0;

        // Seeking
        let { currentTime } = player.media;
        Object.defineProperty(player.media, 'currentTime', {
            get() {
                return currentTime;
            },
            set(time) {
                // Get current paused state
                // Vimeo will automatically play on seek
                const { paused } = player.media;

                // Set seeking flag
                player.media.seeking = true;

                // Trigger seeking
                utils.dispatchEvent.call(player, player.media, 'seeking');

                // Seek after events
                player.embed.setCurrentTime(time);

                // Restore pause state
                if (paused) {
                    this.pause();
                }
            },
        });

        // Playback speed
        // Not currently supported in Vimeo
        Object.defineProperty(player.media, 'playbackRate', {
            get() {
                return null;
            },
            set() {},
        });

        // Volume
        let { volume } = player.media;
        Object.defineProperty(player.media, 'volume', {
            get() {
                return volume;
            },
            set(input) {
                volume = input;
                player.embed.setVolume(input);
                utils.dispatchEvent.call(player, player.media, 'volumechange');
            },
        });

        // Rebuild UI
        window.setTimeout(() => ui.build.call(player), 0);

        // Get title
        player.embed.getVideoTitle().then(title => {
            player.config.title = title;
        });

        // Get current time
        player.embed.getCurrentTime().then(value => {
            currentTime = value;
            utils.dispatchEvent.call(this, this.media, 'timeupdate');
        });

        // Get duration
        player.embed.getDuration().then(value => {
            player.media.duration = value;
            utils.dispatchEvent.call(player, player.media, 'durationchange');
        });

        // Get captions
        player.embed.getTextTracks().then(tracks => {
            player.captions.tracks = tracks;
            captions.setup.call(player);
        });

        player.embed.on('cuechange', data => {
            let cue = null;

            if (data.cues.length) {
                cue = utils.stripHTML(data.cues[0].text);
            }

            captions.set.call(player, cue);
        });

        player.embed.on('loaded', () => {
            if (utils.is.htmlElement(player.embed.element) && player.supported.ui) {
                const frame = player.embed.element;

                // Fix keyboard focus issues
                // https://github.com/sampotts/plyr/issues/317
                frame.setAttribute('tabindex', -1);
            }
        });

        player.embed.on('play', () => {
            player.media.paused = false;
            utils.dispatchEvent.call(player, player.media, 'play');
            utils.dispatchEvent.call(player, player.media, 'playing');
        });

        player.embed.on('pause', () => {
            player.media.paused = true;
            utils.dispatchEvent.call(player, player.media, 'pause');
        });

        this.embed.on('timeupdate', data => {
            this.media.seeking = false;
            currentTime = data.seconds;
            utils.dispatchEvent.call(this, this.media, 'timeupdate');
        });

        this.embed.on('progress', data => {
            this.media.buffered = data.percent;
            utils.dispatchEvent.call(this, this.media, 'progress');

            if (parseInt(data.percent, 10) === 1) {
                // Trigger event
                utils.dispatchEvent.call(this, this.media, 'canplaythrough');
            }
        });

        this.embed.on('seeked', () => {
            this.media.seeking = false;
            utils.dispatchEvent.call(this, this.media, 'seeked');
            utils.dispatchEvent.call(this, this.media, 'play');
        });

        this.embed.on('ended', () => {
            this.media.paused = true;
            utils.dispatchEvent.call(this, this.media, 'ended');
        });
    },
};

export default vimeo;