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

121 lines
2.8 KiB
JavaScript
Raw Normal View History

2022-05-03 20:50:57 +02:00
'use strict'
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
import $ from 'jquery'
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +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 = (($) => {
2022-05-03 20:50:57 +02:00
const NAME = 'jsVideoPreviewUI'
const DATA_KEY = NAME
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
const G = window
const D = document
2019-06-08 17:20:51 +02:00
class VideoPreviewUI {
2022-05-03 20:50:57 +02:00
constructor (el) {
const ui = this
ui.$_el = $(el)
ui.innerHTML = ui.$_el[0].innerHTML
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
ui.$_el.data(DATA_KEY, this)
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\/)/
2022-05-03 20:50:57 +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]
2022-05-03 20:50:57 +02:00
: parsedURL[0]
}
2019-07-10 20:59:57 +02:00
2022-05-03 20:50:57 +02:00
let video
2019-06-08 17:20:51 +02:00
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
))
) {
2022-05-03 20:50:57 +02:00
let video_id
2019-06-08 17:20:51 +02:00
2020-06-16 03:20:10 +02:00
if (
2022-05-03 20:50:57 +02:00
video[1] === 'youtube' ||
video[1] === 'youtube-nocookie' ||
video[1] === 'youtu'
2020-06-16 03:20:10 +02:00
) {
2022-05-03 20:50:57 +02:00
video_id = YouTubeGetID(href)
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
if (video[1] == 'vimeo') {
video_id = video[3]
ui.$_el.addClass('loading')
2019-06-08 17:20:51 +02:00
$.ajax({
2022-05-03 20:50:57 +02:00
type: 'GET',
2020-06-16 03:20:10 +02:00
url: `https://vimeo.com/api/v2/video/${video_id}.json`,
2022-05-03 20:50:57 +02:00
jsonp: 'callback',
dataType: 'jsonp',
2020-06-16 03:20:10 +02:00
success: function (data) {
2022-05-03 20:50:57 +02:00
const thumbnail_src = data[0].thumbnail_large
ui.show(thumbnail_src)
ui.$_el.removeClass('loading')
2019-06-08 17:20:51 +02:00
},
2022-05-03 20:50:57 +02:00
})
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
return
2019-06-08 17:20:51 +02:00
}
if (video_id) {
2022-05-03 20:50:57 +02:00
ui.show(`//i3.ytimg.com/vi/${video_id}/0.jpg`)
2019-06-08 17:20:51 +02:00
}
}
}
2022-05-03 20:50:57 +02:00
show (src) {
const ui = this
ui.$_el[0].innerHTML = ''
ui.$_el.append(`<img src="${src}" alt="Video" />`)
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
static dispose () {
console.log(`${NAME}: dispose`)
ui.$_el[0].innerHTML = ui.innerHTML
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
static _jQueryInterface () {
2019-06-08 17:20:51 +02:00
return this.each((i, el) => {
// attach functionality to element
2022-05-03 20:50:57 +02:00
const $el = $(el)
let data = $el.data(DATA_KEY)
2019-06-08 17:20:51 +02:00
if (!data) {
2022-05-03 20:50:57 +02:00
data = new VideoPreviewUI(el)
$el.data(DATA_KEY, data)
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
})
2019-06-08 17:20:51 +02:00
}
}
// jQuery interface
2022-05-03 20:50:57 +02:00
$.fn[NAME] = VideoPreviewUI._jQueryInterface
$.fn[NAME].Constructor = VideoPreviewUI
2019-06-08 17:20:51 +02:00
$.fn[NAME].noConflict = () => {
2022-05-03 20:50:57 +02:00
$.fn[NAME] = JQUERY_NO_CONFLICT
return VideoPreviewUI._jQueryInterface
}
2019-06-08 17:20:51 +02:00
// auto-apply
2020-06-16 03:20:10 +02:00
$(window).on(`${Events.LODEDANDREADY}`, () => {
2022-05-03 20:50:57 +02:00
console.log(`${NAME}: init`)
2020-06-16 03:20:10 +02:00
$('[data-video-preview="true"]').each((i, el) => {
2022-05-03 20:50:57 +02:00
$(el).jsVideoPreviewUI()
})
})
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
return VideoPreviewUI
})($)
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
export default VideoPreviewUI