webpack-bootstrap-ui-kit/src/js_old/_components/_ui.video.preview.js

121 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-08-18 20:51:15 +02:00
"use strict";
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
import $ from "jquery";
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
import MainUI from "../_main";
import Events from "../_events";
import SpinnerUI from "./_ui.spinner";
2019-06-08 17:20:51 +02:00
const VideoPreviewUI = (($) => {
2021-08-18 20:51:15 +02:00
const NAME = "jsVideoPreviewUI";
2019-06-08 17:20:51 +02:00
const DATA_KEY = NAME;
const G = window;
const D = document;
class VideoPreviewUI {
constructor(el) {
const ui = this;
ui.$_el = $(el);
ui.innerHTML = ui.$_el[0].innerHTML;
ui.$_el.data(DATA_KEY, this);
2021-08-18 20:51:15 +02:00
const href = ui.$_el.attr("href") || ui.$_el.data("href");
2019-07-10 20:59:57 +02:00
const YouTubeGetID = (url) => {
const parsedURL = url.split(
2021-08-18 20:51:15 +02:00
/(vi\/|v%3D|v=|\/v\/|youtu\.be\/|\/embed\/)/
);
2020-09-09 17:40:58 +02:00
console.log(`${NAME}: ${parsedURL}`);
2020-06-16 03:20:10 +02:00
return undefined !== parsedURL[2]
? parsedURL[2].split(/[^0-9a-z_-]/i)[0]
: parsedURL[0];
2019-07-10 20:59:57 +02:00
};
2019-06-08 17:20:51 +02:00
let video;
2020-06-16 03:20:10 +02:00
if (
(video = href.match(
2021-08-18 20:51:15 +02:00
/(youtube|youtube-nocookie|youtu|vimeo)\.(com|be)\/(watch\?v=([\w-]+)|([\w-]+))/
2020-06-16 03:20:10 +02:00
))
) {
2019-06-08 17:20:51 +02:00
let video_id;
2020-06-16 03:20:10 +02:00
if (
2021-08-18 20:51:15 +02:00
video[1] === "youtube" ||
video[1] === "youtube-nocookie" ||
video[1] === "youtu"
2020-06-16 03:20:10 +02:00
) {
2019-07-10 20:59:57 +02:00
video_id = YouTubeGetID(href);
2019-06-08 17:20:51 +02:00
}
2021-08-18 20:51:15 +02:00
if (video[1] == "vimeo") {
2019-06-08 17:20:51 +02:00
video_id = video[3];
2021-08-18 20:51:15 +02:00
ui.$_el.addClass("loading");
2019-06-08 17:20:51 +02:00
$.ajax({
2021-08-18 20:51:15 +02:00
type: "GET",
2020-06-16 03:20:10 +02:00
url: `https://vimeo.com/api/v2/video/${video_id}.json`,
2021-08-18 20:51:15 +02:00
jsonp: "callback",
dataType: "jsonp",
2020-06-16 03:20:10 +02:00
success: function (data) {
2019-06-08 17:20:51 +02:00
const thumbnail_src = data[0].thumbnail_large;
ui.show(thumbnail_src);
2021-08-18 20:51:15 +02:00
ui.$_el.removeClass("loading");
2019-06-08 17:20:51 +02:00
},
});
return;
}
if (video_id) {
ui.show(`//i3.ytimg.com/vi/${video_id}/0.jpg`);
}
}
}
show(src) {
const ui = this;
2021-08-18 20:51:15 +02:00
ui.$_el[0].innerHTML = "";
2019-06-08 17:20:51 +02:00
ui.$_el.append(`<img src="${src}" alt="Video" />`);
}
static dispose() {
2020-09-09 17:40:58 +02:00
console.log(`${NAME}: dispose`);
2019-06-08 17:20:51 +02:00
ui.$_el[0].innerHTML = ui.innerHTML;
}
static _jQueryInterface() {
return this.each((i, el) => {
// attach functionality to element
const $el = $(el);
let data = $el.data(DATA_KEY);
if (!data) {
data = new VideoPreviewUI(el);
$el.data(DATA_KEY, data);
}
});
}
}
// jQuery interface
$.fn[NAME] = VideoPreviewUI._jQueryInterface;
$.fn[NAME].Constructor = VideoPreviewUI;
$.fn[NAME].noConflict = () => {
$.fn[NAME] = JQUERY_NO_CONFLICT;
return VideoPreviewUI._jQueryInterface;
};
// auto-apply
2020-06-16 03:20:10 +02:00
$(window).on(`${Events.LODEDANDREADY}`, () => {
2020-09-09 17:40:58 +02:00
console.log(`${NAME}: init`);
2020-06-16 03:20:10 +02:00
$('[data-video-preview="true"]').each((i, el) => {
$(el).jsVideoPreviewUI();
});
2019-06-08 17:20:51 +02:00
});
return VideoPreviewUI;
})($);
export default VideoPreviewUI;