Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | 1x 1x 8x 1x 7x 7x 7x 7x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 2x 2x | import * as dom from '../dom/dom';
import {DomWatcher} from '../dom/dom-watcher';
import {
type ElementVisibilityObject,
elementVisibility,
} from '../dom/element-visibility';
import * as func from '../func/func';
import * as is from '../is/is';
export const LazyVideoEvents = {
LOAD_START: 'LOAD_START',
LOAD_LOADED: 'LOAD_LOADED',
};
const VIDEO_MIME_TYPES: Record<string, string> = {
mp4: 'video/mp4',
m4v: 'video/mp4',
webm: 'video/webm',
ogv: 'video/ogg',
ogg: 'video/ogg',
mov: 'video/quicktime',
m3u8: 'application/x-mpegURL',
};
/**
* Infers a video MIME type from a URL's file extension (ignoring query string
* and hash). Returns null when the extension is unrecognized.
*/
export function detectVideoMimeType(url: string): string | null {
if (!url) {
return null;
}
const cleanUrl = url.split(/[?#]/, 1)[0];
const match = cleanUrl.match(/\.([a-z0-9]+)$/i);
Iif (!match) {
return null;
}
return VIDEO_MIME_TYPES[match[1].toLowerCase()] || null;
}
/**
* Vanilla Implementation of Lazy Video.
* This is a port of directive-lazy-video to a native implementation.
*
*
* Basic Usage Example:
*
*
* ```
* const lazyVideoElements: Array<HTMLElement> =
* Array.from(this.el.querySelectorAll('[lazy-video]'));
* lazyVideoElements.forEach((element:HTMLElement)=> {
* this.lazyVideos.push(
* new LazyVideo(element)
* );
* })
*
*
* <video playsinline disableremoteplayback muted title="hello"
* <source type="video/mp4" lazy-video="{{url}}"></source>
* <video>
* ```
*
*
*
* When the element comes into view, it will render as and load.
* ```
* <video>
* <source type="video/mp4" lazy-video="{{url}}" src="..."></source>
* <video>
* ```
*
*
* This class does display: none detection so will try to intelligently load
* videos ONLY when they are visible on the page.
*
* This allows you to have conditional videos such as mobile only.
*
* ````
* .only-mobile
* +md
* display: none
*
* <video class="only-mobile">
* <source type="video/mp4" lazy-video="{{url}}"></source>
* <video>
* ```
*
*
* # Autoplay
* By default, lazyvideo WILL not play your video but only load it.
* You should use the events fired by lazyvideo and handle the timing of playing.
*
* You can alternately, add `lazy-video-play-on-inview` to autoplay the video
* when it comes into the viewport.
*
* ```
* <video>
* <source type="video/mp4" lazy-video="{{url}}" lazy-video-play-on-inview></source>
* <video>
* ```
*
* # Forward Scalar
* Adjust the forward scalar to higher values to load videos more aggressively.
* <source lazy-video="{{url}}" lazy-video-forward-scalar="3"></source>
*
*
*
* # Listen for events.
* Events get fired on the video element.
*
* ```
* import { lazyVideoDirective, lazyVideoEvents } from 'degu/lib/angular/directive-lazy-video';
* document.getElementById("myvideo").addEventListener(lazyVideoEvents.LOAD_START, ()=. {
* // Loading started!
* }, { once: true});
*
*
* <video id="myvideo" class="only-mobile">
* <source type="video/mp4" lazy-video="{{url}}"></source>
* <video>
* ```
*
*/
export class LazyVideo {
/**
* The root video element.
*/
private el: HTMLElement;
/**
* The parent of the source element which is the <video> element.
*/
private video: HTMLVideoElement;
/**
* The url of the video to load.
*/
private url: string;
private setComplete: boolean;
// Ev used to trigger the load of the video.
private ev: ElementVisibilityObject;
// Ev used to play or pause the video.
private inviewEv: ElementVisibilityObject;
private watcher: DomWatcher;
// The amount of rootMargin offset to apply to lazyimage.
// 1 would result in a forward load of 1 * window.innerHeight.
// 0 would mean no forward load.
// Defaults to 1.
// Set to higher values if you want to load aggressively load video that are below
// the current fold.
private forwardLoadScalar: number;
/**
* Whether to play / stop the video on inview.
*/
private playOnInview = true;
constructor(el: HTMLElement) {
this.el = el;
this.video = this.el.parentElement as HTMLVideoElement;
this.watcher = new DomWatcher();
this.watcher.add({
element: window,
on: 'smartResize',
callback: func.debounce(this.onResize.bind(this), 500),
});
// Watch for force lazy load on root element to force start video load.
this.watcher.add({
element: this.el,
on: 'force-lazy-load',
callback: () => {
this.paint(true);
},
});
const forwardScalar = this.el.getAttribute('lazy-video-forward-scalar');
this.forwardLoadScalar = is.defined(forwardScalar) ? +forwardScalar! : 1;
/**
* The Video source.
*/
this.url = this.el.getAttribute('lazy-video') || '';
/**
* Whether this directive has finished setting the video.
*/
this.setComplete = false;
/**
* Check whether we should play this on inview.
* Add lazy-video-disable-inview-play to stop video from playing on inview.
*
* ```
* <video disableRemotePlayback="" playsinline="" muted autoplay="false">
* <source type="video/mp4" lazy-video="${src}" lazy-video-disable-inview-play>
* </video>
* ```
*/
this.playOnInview = !this.el.hasAttribute('lazy-video-disable-inview-play');
this.ev = elementVisibility.inview(
this.video,
{
rootMargin: window.innerHeight * this.forwardLoadScalar + 'px',
},
() => {
this.paint();
}
);
this.inviewEv = elementVisibility.inview(this.video, {}, () => {
this.paint();
});
// Immediately attempt to apint.
this.ev.readyPromise.then(() => {
this.paint();
});
}
public onResize(): void {
if (!this.setComplete) {
this.paint();
}
}
public paint(force = false): void {
Eif ((this.isPainted() && !this.setComplete) || force) {
this.setComplete = true;
if (!this.el.getAttribute('type')) {
const detectedType = detectVideoMimeType(this.url);
Eif (detectedType) {
this.el.setAttribute('type', detectedType);
}
}
this.el.setAttribute('src', this.url);
dom.whenVideosLoaded([this.video]).then(() => {
// Fire an event.
dom.event(this.video, LazyVideoEvents.LOAD_LOADED, {});
// Play the video if we opted to play on inview.
this.playOnInview && this.playVideo();
});
// Tell the parent element (video) to load.
this.video.load();
this.video.setAttribute('load', 'true');
this.video.pause();
// Fire an event.
dom.event(this.video, LazyVideoEvents.LOAD_START, {});
// Once it's loaded, dispose of this module.
this.dispose();
}
// If we have already loaded this, then reset the video on outview,
// replay on inview.
Eif (this.setComplete && this.playOnInview) {
if (!this.isPainted() && !this.inviewEv.state().inview) {
this.video.currentTime = 0;
this.video.pause();
} else E{
this.playVideo();
}
}
}
private playVideo() {
if (!dom.testVideoIsPlaying(this.video)) {
const playPromise = this.video.play();
if (playPromise) {
playPromise.catch(() => {});
}
}
}
/**
* Determines whether this element was painted (displayed on the screen).
* The basis of this is having a css class of display none. Other methods
* of hiding the element will return true.
*/
isPainted() {
return (
!dom.isDisplayNoneWithAncestors(this.video) && this.ev.state().inview
);
}
public dispose(): void {
this.ev.dispose();
this.watcher.dispose();
}
}
/**
* A utility class that helps quickly create and dispose lazy video elements on the page.
*/
export class LazyVideoManager {
private lazyVideos: Array<LazyVideo> = [];
constructor(rootElement: HTMLElement) {
const lazyVideoElements: Array<HTMLElement> = Array.from(
rootElement.querySelectorAll('[lazy-video]')
);
lazyVideoElements.forEach((element: HTMLElement) => {
this.lazyVideos.push(new LazyVideo(element));
});
}
dispose() {
this.lazyVideos &&
this.lazyVideos.forEach((lazyVideo) => {
lazyVideo.dispose();
});
}
}
|