aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/source.js
blob: d0881675e097460cee38db858f437384b4d73f6c (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
// ==========================================================================
// Plyr source update
// ==========================================================================

import types from './types';
import utils from './utils';
import media from './media';
import ui from './ui';
import support from './support';

const source = {
    // Add elements to HTML5 media (source, tracks, etc)
    insertElements(type, attributes) {
        if (utils.is.string(attributes)) {
            utils.insertElement(type, this.media, {
                src: attributes,
            });
        } else if (utils.is.array(attributes)) {
            attributes.forEach(attribute => {
                utils.insertElement(type, this.media, attribute);
            });
        }
    },

    // Update source
    // Sources are not checked for support so be careful
    change(input) {
        if (!utils.is.object(input) || !('sources' in input) || !input.sources.length) {
            this.warn('Invalid source format');
            return;
        }

        // Cancel current network requests
        media.cancelRequests.call(this);

        // Destroy instance and re-setup
        this.destroy.call(
            this,
            () => {
                // TODO: Reset menus here

                // Remove elements
                utils.removeElement(this.media);
                this.media = null;

                // Reset class name
                if (utils.is.htmlElement(this.elements.container)) {
                    this.elements.container.removeAttribute('class');
                }

                // Set the type
                if ('type' in input) {
                    this.type = input.type;

                    // Get child type for video (it might be an embed)
                    if (this.type === 'video') {
                        const firstSource = input.sources[0];

                        if ('type' in firstSource && types.embed.includes(firstSource.type)) {
                            this.type = firstSource.type;
                        }
                    }
                }

                // Check for support
                this.supported = support.check(this.type, this.config.inline);

                // Create new markup
                switch (this.type) {
                    case 'video':
                        this.media = utils.createElement('video');
                        break;

                    case 'audio':
                        this.media = utils.createElement('audio');
                        break;

                    case 'youtube':
                    case 'vimeo':
                        this.media = utils.createElement('div');
                        this.embedId = input.sources[0].src;
                        break;

                    default:
                        break;
                }

                // Inject the new element
                this.elements.container.appendChild(this.media);

                // Autoplay the new source?
                if (utils.is.boolean(input.autoplay)) {
                    this.config.autoplay = input.autoplay;
                }

                // Set attributes for audio and video
                if (this.isHTML5) {
                    if (this.config.crossorigin) {
                        this.media.setAttribute('crossorigin', '');
                    }
                    if (this.config.autoplay) {
                        this.media.setAttribute('autoplay', '');
                    }
                    if ('poster' in input) {
                        this.media.setAttribute('poster', input.poster);
                    }
                    if (this.config.loop.active) {
                        this.media.setAttribute('loop', '');
                    }
                    if (this.config.muted) {
                        this.media.setAttribute('muted', '');
                    }
                    if (this.config.inline) {
                        this.media.setAttribute('playsinline', '');
                    }
                }

                // Restore class hooks
                utils.toggleClass(
                    this.elements.container,
                    this.config.classNames.captions.active,
                    this.supported.ui && this.captions.enabled
                );

                ui.addStyleHook.call(this);

                // Set new sources for html5
                if (this.isHTML5) {
                    source.insertElements.call(this, 'source', input.sources);
                }

                // Set video title
                this.config.title = input.title;

                // Set up from scratch
                media.setup.call(this);

                // HTML5 stuff
                if (this.isHTML5) {
                    // Setup captions
                    if ('tracks' in input) {
                        source.insertElements.call(this, 'track', input.tracks);
                    }

                    // Load HTML5 sources
                    this.media.load();
                }

                // If HTML5 or embed but not fully supported, setupInterface and call ready now
                if (this.isHTML5 || (this.isEmbed && !this.supported.ui)) {
                    // Setup interface
                    ui.build.call(this);
                }
            },
            true
        );
    },
};

export default source;