|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {ChangeDetectionStrategy, Component, Input, ViewEncapsulation} from '@angular/core'; |
| 10 | + |
| 11 | +/** Quality of the placeholder image. */ |
| 12 | +export type PlaceholderImageQuality = 'high' | 'standard' | 'low'; |
| 13 | + |
| 14 | +@Component({ |
| 15 | + selector: 'youtube-player-placeholder', |
| 16 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 17 | + encapsulation: ViewEncapsulation.None, |
| 18 | + template: ` |
| 19 | + <button type="button" class="youtube-player-placeholder-button" [attr.aria-label]="buttonLabel"> |
| 20 | + <svg |
| 21 | + height="100%" |
| 22 | + version="1.1" |
| 23 | + viewBox="0 0 68 48" |
| 24 | + focusable="false" |
| 25 | + aria-hidden="true"> |
| 26 | + <path d="M66.52,7.74c-0.78-2.93-2.49-5.41-5.42-6.19C55.79,.13,34,0,34,0S12.21,.13,6.9,1.55 C3.97,2.33,2.27,4.81,1.48,7.74C0.06,13.05,0,24,0,24s0.06,10.95,1.48,16.26c0.78,2.93,2.49,5.41,5.42,6.19 C12.21,47.87,34,48,34,48s21.79-0.13,27.1-1.55c2.93-0.78,4.64-3.26,5.42-6.19C67.94,34.95,68,24,68,24S67.94,13.05,66.52,7.74z" fill="#f00"></path> |
| 27 | + <path d="M 45,24 27,14 27,34" fill="#fff"></path> |
| 28 | + </svg> |
| 29 | + </button> |
| 30 | + `, |
| 31 | + standalone: true, |
| 32 | + styleUrl: 'youtube-player-placeholder.css', |
| 33 | + host: { |
| 34 | + 'class': 'youtube-player-placeholder', |
| 35 | + '[class.youtube-player-placeholder-loading]': 'isLoading', |
| 36 | + '[style.background-image]': '_getBackgroundImage()', |
| 37 | + '[style.width.px]': 'width', |
| 38 | + '[style.height.px]': 'height', |
| 39 | + }, |
| 40 | +}) |
| 41 | +export class YouTubePlayerPlaceholder { |
| 42 | + /** ID of the video for which to show the placeholder. */ |
| 43 | + @Input() videoId: string; |
| 44 | + |
| 45 | + /** Width of the video for which to show the placeholder. */ |
| 46 | + @Input() width: number; |
| 47 | + |
| 48 | + /** Height of the video for which to show the placeholder. */ |
| 49 | + @Input() height: number; |
| 50 | + |
| 51 | + /** Whether the video is currently being loaded. */ |
| 52 | + @Input() isLoading: boolean; |
| 53 | + |
| 54 | + /** Accessible label for the play button. */ |
| 55 | + @Input() buttonLabel: string; |
| 56 | + |
| 57 | + /** Quality of the placeholder image. */ |
| 58 | + @Input() quality: PlaceholderImageQuality; |
| 59 | + |
| 60 | + /** Gets the background image showing the placeholder. */ |
| 61 | + protected _getBackgroundImage(): string | undefined { |
| 62 | + let url: string; |
| 63 | + |
| 64 | + if (this.quality === 'low') { |
| 65 | + url = `https://i.ytimg.com/vi/${this.videoId}/hqdefault.jpg`; |
| 66 | + } else if (this.quality === 'high') { |
| 67 | + url = `https://i.ytimg.com/vi/${this.videoId}/maxresdefault.jpg`; |
| 68 | + } else { |
| 69 | + url = `https://i.ytimg.com/vi_webp/${this.videoId}/sddefault.webp`; |
| 70 | + } |
| 71 | + |
| 72 | + return `url(${url})`; |
| 73 | + } |
| 74 | +} |
0 commit comments