| 1 |
/* Background Video — frontend */ |
| 2 |
(function () { |
| 3 |
'use strict'; |
| 4 |
|
| 5 |
var reducedMotion = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches; |
| 6 |
|
| 7 |
/* ── Hosted video ─────────────────────────────────────────────────────── */ |
| 8 |
function initHosted(outer, media, cfg) { |
| 9 |
var video = document.createElement('video'); |
| 10 |
video.src = cfg.url; |
| 11 |
video.muted = true; |
| 12 |
video.autoplay = false; |
| 13 |
video.loop = cfg.loop; |
| 14 |
video.playsInline = true; |
| 15 |
video.playbackRate = cfg.rate; |
| 16 |
video.style.cssText = 'position:absolute;inset:0;width:100%;height:100%;object-fit:' + cfg.fit + ';object-position:' + cfg.pos + ';pointer-events:none;'; |
| 17 |
media.appendChild(video); |
| 18 |
|
| 19 |
var playing = false; |
| 20 |
|
| 21 |
function tryPlay() { |
| 22 |
if (playing) { return; } |
| 23 |
video.play().then(function () { |
| 24 |
playing = true; |
| 25 |
outer.classList.add('bkbv--playing'); |
| 26 |
}).catch(function () {}); |
| 27 |
} |
| 28 |
function tryPause() { |
| 29 |
if (!playing) { return; } |
| 30 |
video.pause(); |
| 31 |
playing = false; |
| 32 |
} |
| 33 |
|
| 34 |
if (cfg.inView) { |
| 35 |
var obs = new IntersectionObserver(function (entries) { |
| 36 |
entries.forEach(function (e) { |
| 37 |
if (e.isIntersecting) { tryPlay(); } |
| 38 |
else { tryPause(); } |
| 39 |
}); |
| 40 |
}, { threshold: 0.1 }); |
| 41 |
obs.observe(outer); |
| 42 |
} else { |
| 43 |
tryPlay(); |
| 44 |
} |
| 45 |
|
| 46 |
if (cfg.pauseHover) { |
| 47 |
outer.addEventListener('mouseenter', function () { video.pause(); playing = false; }); |
| 48 |
outer.addEventListener('mouseleave', function () { tryPlay(); }); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/* ── YouTube iframe ───────────────────────────────────────────────────── */ |
| 53 |
function initYouTube(outer, media, cfg) { |
| 54 |
var id = cfg.youtubeId; |
| 55 |
if (!id) { return; } |
| 56 |
|
| 57 |
var params = [ |
| 58 |
'autoplay=1', 'mute=1', 'controls=0', 'disablekb=1', 'fs=0', |
| 59 |
'iv_load_policy=3', 'modestbranding=1', 'rel=0', 'showinfo=0', |
| 60 |
'loop=' + (cfg.loop ? '1' : '0'), |
| 61 |
cfg.loop ? 'playlist=' + id : '', |
| 62 |
].filter(Boolean).join('&'); |
| 63 |
|
| 64 |
var iframe = document.createElement('iframe'); |
| 65 |
iframe.src = 'https://www.youtube-nocookie.com/embed/' + id + '?' + params; |
| 66 |
iframe.allow = 'autoplay; encrypted-media'; |
| 67 |
iframe.setAttribute('allowfullscreen', ''); |
| 68 |
iframe.setAttribute('tabindex', '-1'); |
| 69 |
iframe.setAttribute('aria-hidden', 'true'); |
| 70 |
media.appendChild(iframe); |
| 71 |
|
| 72 |
iframe.addEventListener('load', function () { |
| 73 |
outer.classList.add('bkbv--playing'); |
| 74 |
}); |
| 75 |
} |
| 76 |
|
| 77 |
/* ── Vimeo iframe ─────────────────────────────────────────────────────── */ |
| 78 |
function initVimeo(outer, media, cfg) { |
| 79 |
var id = cfg.vimeoId; |
| 80 |
if (!id) { return; } |
| 81 |
|
| 82 |
var params = [ |
| 83 |
'autoplay=1', 'muted=1', 'controls=0', 'loop=' + (cfg.loop ? '1' : '0'), |
| 84 |
'background=1', 'dnt=1', |
| 85 |
].join('&'); |
| 86 |
|
| 87 |
var iframe = document.createElement('iframe'); |
| 88 |
iframe.src = 'https://player.vimeo.com/video/' + id + '?' + params; |
| 89 |
iframe.allow = 'autoplay; fullscreen; picture-in-picture'; |
| 90 |
iframe.setAttribute('tabindex', '-1'); |
| 91 |
iframe.setAttribute('aria-hidden', 'true'); |
| 92 |
media.appendChild(iframe); |
| 93 |
|
| 94 |
iframe.addEventListener('load', function () { |
| 95 |
outer.classList.add('bkbv--playing'); |
| 96 |
}); |
| 97 |
} |
| 98 |
|
| 99 |
/* ── init one block ───────────────────────────────────────────────────── */ |
| 100 |
function initBlock(outer) { |
| 101 |
if (outer._bkbvInit) { return; } |
| 102 |
outer._bkbvInit = true; |
| 103 |
|
| 104 |
if (reducedMotion) { return; } |
| 105 |
|
| 106 |
var media = outer.querySelector('.bkbv-media'); |
| 107 |
if (!media) { return; } |
| 108 |
|
| 109 |
var cfg = { |
| 110 |
type: outer.getAttribute('data-video-type') || 'hosted', |
| 111 |
url: outer.getAttribute('data-video-url') || '', |
| 112 |
youtubeId: outer.getAttribute('data-youtube-id') || '', |
| 113 |
vimeoId: outer.getAttribute('data-vimeo-id') || '', |
| 114 |
loop: outer.getAttribute('data-loop') !== '0', |
| 115 |
inView: outer.getAttribute('data-in-view') !== '0', |
| 116 |
pauseHover:outer.getAttribute('data-pause-hover') === '1', |
| 117 |
rate: parseFloat(outer.getAttribute('data-rate')) || 1, |
| 118 |
fit: outer.getAttribute('data-object-fit') || 'cover', |
| 119 |
pos: outer.getAttribute('data-object-pos') || 'center center', |
| 120 |
}; |
| 121 |
|
| 122 |
if (cfg.type === 'hosted' && cfg.url) { |
| 123 |
initHosted(outer, media, cfg); |
| 124 |
} else if (cfg.type === 'youtube' && cfg.youtubeId) { |
| 125 |
initYouTube(outer, media, cfg); |
| 126 |
} else if (cfg.type === 'vimeo' && cfg.vimeoId) { |
| 127 |
initVimeo(outer, media, cfg); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/* ── boot ────────────────────────────────────────────────────────────── */ |
| 132 |
function init() { |
| 133 |
document.querySelectorAll('.bkbv-outer[data-video-type]').forEach(initBlock); |
| 134 |
} |
| 135 |
|
| 136 |
if (document.readyState === 'loading') { |
| 137 |
document.addEventListener('DOMContentLoaded', init); |
| 138 |
} else { |
| 139 |
init(); |
| 140 |
} |
| 141 |
}()); |
| 142 |
|