|
| 1 | +// Copyright © 2020-present Techassi |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | +// vue-youtube-iframe 1.0.0 |
| 5 | +(function (global, factory) { |
| 6 | + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('vue')) : |
| 7 | + typeof define === 'function' && define.amd ? define(['exports', 'vue'], factory) : |
| 8 | + (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.VueYouTubeEmbed = {}, global.Vue)); |
| 9 | +}(this, (function (exports, vue) { 'use strict'; |
| 10 | + |
| 11 | + const manager = { |
| 12 | + factory: null, |
| 13 | + players: [], |
| 14 | + events: {}, |
| 15 | + uid: 0, |
| 16 | + |
| 17 | + // registerFactory registers the YT Factory (e.g. create a new Player) |
| 18 | + registerFactory(YT) { |
| 19 | + this.factory = YT; |
| 20 | + }, |
| 21 | + |
| 22 | + // registerEvents registers the YT Player events to be emitted by the component |
| 23 | + registerEvents() { |
| 24 | + const { PlayerState } = this.factory; |
| 25 | + this.events[PlayerState.ENDED] = 'ended'; |
| 26 | + this.events[PlayerState.PLAYING] = 'playing'; |
| 27 | + this.events[PlayerState.PAUSED] = 'paused'; |
| 28 | + this.events[PlayerState.BUFFERING] = 'buffering'; |
| 29 | + this.events[PlayerState.CUED] = 'cued'; |
| 30 | + }, |
| 31 | + |
| 32 | + // register registers a new player to be initalizied and runs callback |
| 33 | + register(callback) { |
| 34 | + this.uid += 1; |
| 35 | + if (this.factory) { |
| 36 | + callback(this.factory, `vue-youtube-iframe-${this.uid}`); |
| 37 | + return; |
| 38 | + } |
| 39 | + this.players.push(callback); |
| 40 | + }, |
| 41 | + |
| 42 | + // runBacklog runs backlogged initializations (when the YT factory wasn't set yet) |
| 43 | + runBacklog() { |
| 44 | + this.players.forEach((callback) => { |
| 45 | + callback(this.factory, `vue-youtube-iframe-${this.uid}`); |
| 46 | + }); |
| 47 | + this.players = []; |
| 48 | + }, |
| 49 | + }; |
| 50 | + |
| 51 | + const player = { |
| 52 | + name: 'YoutubeIframe', |
| 53 | + props: { |
| 54 | + playerWidth: { |
| 55 | + type: [String, Number], |
| 56 | + default: '1280', |
| 57 | + }, |
| 58 | + playerHeight: { |
| 59 | + type: [String, Number], |
| 60 | + default: '720', |
| 61 | + }, |
| 62 | + playerParameters: { |
| 63 | + type: Object, |
| 64 | + default: () => ({ |
| 65 | + autpplay: 0, |
| 66 | + time: 0, |
| 67 | + mute: 0, |
| 68 | + }), |
| 69 | + }, |
| 70 | + videoId: { |
| 71 | + type: String, |
| 72 | + default: '', |
| 73 | + }, |
| 74 | + }, |
| 75 | + data() { |
| 76 | + return { |
| 77 | + elementId: '', |
| 78 | + player: {}, |
| 79 | + }; |
| 80 | + }, |
| 81 | + render() { |
| 82 | + return vue.h('div', [vue.h('div', { id: this.elementId })]); |
| 83 | + }, |
| 84 | + template: '<div><div :id="elementId"></div></div>', |
| 85 | + mounted() { |
| 86 | + const { playerHeight, playerWidth, playerParameters, videoId } = this; |
| 87 | + |
| 88 | + manager.register((factory, uid) => { |
| 89 | + this.elementId = uid; |
| 90 | + vue.nextTick().then(() => { |
| 91 | + this.player = new factory.Player(this.elementId, { |
| 92 | + width: playerWidth, |
| 93 | + height: playerHeight, |
| 94 | + ...playerParameters, |
| 95 | + videoId, |
| 96 | + host: 'https://www.youtube.com', |
| 97 | + events: { |
| 98 | + onReady: (event) => { |
| 99 | + const p = event.target; |
| 100 | + if (playerParameters.autoplay === 1) { |
| 101 | + p.mute(); |
| 102 | + if (playerParameters?.start !== 0) { |
| 103 | + p.seekTo(playerParameters.start); |
| 104 | + } else { |
| 105 | + p.playVideo(); |
| 106 | + } |
| 107 | + } |
| 108 | + this.$emit('ready', event); |
| 109 | + }, |
| 110 | + onStateChange: (event) => { |
| 111 | + if (event.data !== -1) { |
| 112 | + this.$emit(manager.events[event.data], event); |
| 113 | + } |
| 114 | + }, |
| 115 | + onError: (event) => { |
| 116 | + this.$emit('error', event); |
| 117 | + }, |
| 118 | + }, |
| 119 | + }); |
| 120 | + }); |
| 121 | + }); |
| 122 | + }, |
| 123 | + }; |
| 124 | + |
| 125 | + const plugin = { |
| 126 | + install: (app) => { |
| 127 | + if (typeof window !== 'undefined' && typeof document !== 'undefined') { |
| 128 | + const tag = document.createElement('script'); |
| 129 | + tag.src = 'https://www.youtube.com/player_api'; |
| 130 | + |
| 131 | + const firstScriptTag = document.getElementsByTagName('script')[0]; |
| 132 | + firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); |
| 133 | + |
| 134 | + app.component('youtube-iframe', player); |
| 135 | + |
| 136 | + window.onYouTubeIframeAPIReady = () => { |
| 137 | + manager.registerFactory(window.YT); |
| 138 | + manager.registerEvents(); |
| 139 | + manager.runBacklog(); |
| 140 | + }; |
| 141 | + } |
| 142 | + }, |
| 143 | + }; |
| 144 | + |
| 145 | + exports.YouTubePlayer = player; |
| 146 | + exports.default = plugin; |
| 147 | + |
| 148 | + Object.defineProperty(exports, '__esModule', { value: true }); |
| 149 | + |
| 150 | +}))); |
0 commit comments