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 | 1x 1x 1x 1x 3x 1x 2x 1x 1x 1x 2x 2x 2x 2x 2x 1x 2x 2x 1x 1x 1x 2x 4x 4x | import {LitElement, css, html} from 'lit';
import {property, query} from 'lit/decorators.js';
import {ScriptLoader} from '../loader/script-loader';
const YOUTUBE_IFRAME_API = 'https://www.youtube.com/iframe_api?trustedtypes=1';
enum YoutubeInlineEvent {
STATE_CHANGE = 'stateChange',
}
/**
* # Degu Youtube Inline Component
* Automatically adds an youtube iframe to your page.
*
* Usage:
*
* ```ts
* import { DeguYouTubeInline} from '@blinkk/degu/lib/components/youtube-inline';
*
* window.customElements.define('degu-youtube-inline', DeguYouTubeInline);
* ```
*
* ```html
* <degu-youtube-inline
* video-id="5qap5aO4i9A"
* style="aspect-ratio: 16/9"></degu-youtube-inline>
* ```
*
*
* Advanced Example:
* Here is an example of programmatically updating the youtube video.
*
* ```html
* <degu-youtube-inline id="myVideo"
* style="aspect-ratio: 16/9"></degu-youtube-inline>
*
* <button id="change-youtube-video">Change Video</button>
* ```
*
* ```ts
* import { DeguYouTubeInline} from '@blinkk/degu/lib/components/youtube-inline';
* window.customElements.define('degu-youtube-inline', DeguYouTubeInline);
*
* // Initially load a video
* const youtubeInlineTest = document.getElementById('myVideo');
* youtubeInlineTest.load('t_cKM_JYtbs')
*
* let index = 0;
* const youtubeIds = [
* '_tV5LEBDs7w',
* 'hGIW2fDb0jg',
* '4pcNRDx6KrE',
* 'UtWaBEn_x8c'
* ]
*
* // On button clicks change the youtube video.
* document.getElementById('change-youtube-video').addEventListener('click', ()=> {
* index++;
* if(index >= youtubeIds.length - 1) {
* index = 0;
* }
* youtubeInlineTest.load(youtubeIds[index], true)
* })
* ```
*
*
* ### Custom Script Loaders
* You can set your own custom script loader (subclass degu ScriptLoader)
*
* ```ts
* import { DeguYouTubeInline} from '@blinkk/degu/lib/components/youtube-inline';
* window.customElements.define('degu-youtube-inline', DeguYouTubeInline);
* DeguYouTubeInline.scriptLoader = new MyLoader();
* ```
*/
export class DeguYouTubeInline extends LitElement {
static scriptLoader = new ScriptLoader();
@property({type: String, attribute: 'video-id', reflect: true})
private videoId: string;
@property({type: Boolean, attribute: 'enable-js-api', reflect: true})
enableJsApi? = false;
@query('.container__player')
playerElement: HTMLElement;
public player?: YT.Player;
private loadedVideoId?: string;
connectedCallback() {
super.connectedCallback();
}
static styles = css`
:host {
height: 100%;
width: 100%;
display: block;
}
.container {
align-items: center;
cursor: pointer;
display: inline-flex;
justify-content: center;
position: relative;
width: 100%;
height: 100%;
}
.container__player {
bottom: 0;
height: 100%;
left: 0;
position: absolute;
right: 0;
top: 0;
width: 100%;
z-index: 1;
}
`;
pause() {
this.player && this.player.pauseVideo && this.player.pauseVideo();
}
private play() {
this.player && this.player.playVideo && this.player.playVideo();
}
/**
* Loads and attempt to play a youtube video.
*
* Note about tryPlay.
* This is an "attempted play". Without prior browser interaction the video
* will only load and therefore, the playing is not guaranteed to work without
* prior interaction. Defaults to false.
*/
async load(videoId: string, tryPlay = false) {
this.loadedVideoId = videoId;
this.videoId = videoId;
Iif (this.player) {
if (tryPlay) {
this.player.loadVideoById(this.videoId);
this.play();
} else {
this.player.cueVideoById(this.videoId);
this.pause();
}
return;
}
await DeguYouTubeInline.scriptLoader.load(
YOUTUBE_IFRAME_API + `&origin=${encodeURIComponent(location.origin)}`,
{
test: () => !!window.YT && window.YT['loaded'] === 1,
}
);
const config = {
autohide: 1,
autoplay: tryPlay ? 1 : 0,
fs: 1,
modestbranding: 1,
rel: 0,
showinfo: 0,
iv_load_policy: 3,
enablejsapi: this.enableJsApi ? 1 : 0,
origin: location.origin,
};
const playerVars = Object.assign({}, config);
const playerOptions = {
videoId: this.videoId,
playerVars: playerVars,
events: {
onReady: (e: any) => {
this.player = e.target;
if (tryPlay) {
this.play();
} else E{
this.pause();
}
},
onStateChange: (e: any) => {
const event = new CustomEvent(YoutubeInlineEvent.STATE_CHANGE, {
detail: {
event: e,
},
bubbles: true,
composed: true,
});
this.dispatchEvent(event);
},
},
};
new YT.Player(this.playerElement, playerOptions);
}
render() {
Iif (this.videoId && this.videoId !== this.loadedVideoId) {
this.load(this.videoId);
}
return html`<div class="container">
<div class="container__player"></div>
</div>`;
}
}
|