Skip to content

Commit

Permalink
fix rollup setting
Browse files Browse the repository at this point in the history
  • Loading branch information
kaorun343 committed Jul 6, 2018
1 parent 2fda4b3 commit a69895c
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 96 deletions.
108 changes: 62 additions & 46 deletions lib/vue-youtube-embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ if (!String.prototype.includes) {
};
}

const youtubeRegexp = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:['"][^<>]*>|<\/a>))[?=&+%\w.-]*/ig;
const timeRegexp = /t=(\d+)[ms]?(\d+)?s?/;
var youtubeRegexp = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:['"][^<>]*>|<\/a>))[?=&+%\w.-]*/ig;
var timeRegexp = /t=(\d+)[ms]?(\d+)?s?/;

/**
* get id from url
* @param {string} url url
* @return {string} id
*/
function getIdFromURL (url) {
let id = url.replace(youtubeRegexp, '$1');
var id = url.replace(youtubeRegexp, '$1');

if (id.includes(';')) {
const pieces = id.split(';');
var pieces = id.split(';');

if (pieces[1].includes('%')) {
const uriComponent = decodeURIComponent(pieces[1]);
id = `https://youtube.com${uriComponent}`.replace(youtubeRegexp, '$1');
var uriComponent = decodeURIComponent(pieces[1]);
id = ("https://youtube.com" + uriComponent).replace(youtubeRegexp, '$1');
} else {
id = pieces[0];
}
Expand All @@ -42,15 +42,18 @@ function getIdFromURL (url) {
* @param {string} url url
* @return {number} time
*/
function getTimeFromURL (url = '') {
const times = url.match(timeRegexp);
function getTimeFromURL (url) {
if ( url === void 0 ) url = '';

var times = url.match(timeRegexp);

if (!times) {
return 0
}

const [full] = times;
let [, minutes, seconds] = times;
var full = times[0];
var minutes = times[1];
var seconds = times[2];

if (typeof seconds !== 'undefined') {
seconds = parseInt(seconds, 10);
Expand All @@ -70,25 +73,29 @@ var container = {
scripts: [],
events: {},

run () {
this.scripts.forEach((callback) => {
callback(this.YT);
run: function run () {
var this$1 = this;

this.scripts.forEach(function (callback) {
callback(this$1.YT);
});
this.scripts = [];
},

register (callback) {
register: function register (callback) {
var this$1 = this;

if (this.YT) {
this.Vue.nextTick(() => {
callback(this.YT);
this.Vue.nextTick(function () {
callback(this$1.YT);
});
} else {
this.scripts.push(callback);
}
}
};

let pid = 0;
var pid = 0;

var YouTubePlayer = {
props: {
Expand All @@ -102,7 +109,7 @@ var YouTubePlayer = {
},
playerVars: {
type: Object,
default: () => ({ autoplay: 0, time: 0 })
default: function () { return ({ autoplay: 0, time: 0 }); }
},
videoId: {
type: String
Expand All @@ -112,7 +119,7 @@ var YouTubePlayer = {
default: false
}
},
render (h) {
render: function render (h) {
return h('div', [
h('div', { attrs: { id: this.elementId }})
])
Expand All @@ -124,26 +131,26 @@ var YouTubePlayer = {
videoId: 'update',
mute: 'setMute'
},
data () {
data: function data () {
pid += 1;
return {
elementId: `youtube-player-${pid}`,
elementId: ("youtube-player-" + pid),
player: {}
}
},
methods: {
setSize () {
setSize: function setSize () {
this.player.setSize(this.playerWidth, this.playerHeight);
},
setMute (value) {
setMute: function setMute (value) {
if (value) {
this.player.mute();
} else {
this.player.unMute();
}
},
update (videoId) {
const name = `${this.playerVars.autoplay ? 'load' : 'cue'}VideoById`;
update: function update (videoId) {
var name = (this.playerVars.autoplay ? 'load' : 'cue') + "VideoById";
if (this.player.hasOwnProperty(name)) {
this.player[name](videoId);
} else {
Expand All @@ -153,33 +160,39 @@ var YouTubePlayer = {
}
}
},
mounted () {
container.register((YouTube) => {
const { playerHeight, playerWidth, playerVars, videoId } = this;
mounted: function mounted () {
var this$1 = this;

this.player = new YouTube.Player(this.elementId, {
container.register(function (YouTube) {
var ref = this$1;
var playerHeight = ref.playerHeight;
var playerWidth = ref.playerWidth;
var playerVars = ref.playerVars;
var videoId = ref.videoId;

this$1.player = new YouTube.Player(this$1.elementId, {
height: playerHeight,
width: playerWidth,
playerVars,
videoId,
playerVars: playerVars,
videoId: videoId,
events: {
onReady: (event) => {
this.setMute(this.mute);
this.$emit('ready', event);
onReady: function (event) {
this$1.setMute(this$1.mute);
this$1.$emit('ready', event);
},
onStateChange: (event) => {
onStateChange: function (event) {
if (event.data !== -1) {
this.$emit(container.events[event.data], event);
this$1.$emit(container.events[event.data], event);
}
},
onError: (event) => {
this.$emit('error', event);
onError: function (event) {
this$1.$emit('error', event);
}
}
});
});
},
beforeDestroy () {
beforeDestroy: function beforeDestroy () {
if (this.player !== null && this.player.destroy) {
this.player.destroy();
}
Expand All @@ -188,35 +201,38 @@ var YouTubePlayer = {
};

var index = {
install (Vue, options = {}) {
install: function install (Vue, options) {
if ( options === void 0 ) options = {};

container.Vue = Vue;
YouTubePlayer.ready = YouTubePlayer.mounted;
const { global = true, componentId = 'youtube' } = options;
var global = options.global; if ( global === void 0 ) global = true;
var componentId = options.componentId; if ( componentId === void 0 ) componentId = 'youtube';

if (global) {
// if there is a global component with "youtube" identifier already taken
// then we should let user to pass a new identifier.
Vue.component(componentId, YouTubePlayer);
}
Vue.prototype.$youtube = { getIdFromURL, getTimeFromURL };
Vue.prototype.$youtube = { getIdFromURL: getIdFromURL, getTimeFromURL: getTimeFromURL };

if (typeof window !== 'undefined' && typeof document !== 'undefined') {
const tag = document.createElement('script');
var tag = document.createElement('script');
tag.src = 'https://www.youtube.com/player_api';
const firstScriptTag = document.getElementsByTagName('script')[0];
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

window.onYouTubeIframeAPIReady = function () {
container.YT = YT;
const { PlayerState } = YT;
var PlayerState = YT.PlayerState;

container.events[PlayerState.ENDED] = 'ended';
container.events[PlayerState.PLAYING] = 'playing';
container.events[PlayerState.PAUSED] = 'paused';
container.events[PlayerState.BUFFERING] = 'buffering';
container.events[PlayerState.CUED] = 'cued';

container.Vue.nextTick(() => {
container.Vue.nextTick(function () {
container.run();
});
};
Expand Down
Loading

0 comments on commit a69895c

Please sign in to comment.