Skip to content

demo(youtube-player): add auto video resizing #17648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/dev-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"@angular/material-moment-adapter": ["../material-moment-adapter/public-api.ts"],
"@angular/google-maps": ["../google-maps"],
"@angular/components-examples": ["../components-examples"],
"@angular/components-examples/*": ["../components-examples/*"]
"@angular/components-examples/*": ["../components-examples/*"],
"@angular/youtube-player": ["../youtube-player"]
}
},
"include": ["./**/*.ts"]
Expand Down
7 changes: 4 additions & 3 deletions src/dev-app/youtube-player/youtube-player-demo.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<div>
<div #demoYouTubePlayer class="demo-youtube-player">
<h1>Basic Example</h1>
<section>
<div class="demo-video-selection">
<label>Pick the video:</label>
<mat-radio-group aria-label="Select a video" [(ngModel)]="video">
<mat-radio-group aria-label="Select a video" [(ngModel)]="selectedVideo">
<mat-radio-button *ngFor="let video of videos" [value]="video">
{{video.name}}
</mat-radio-button>
<mat-radio-button [value]="undefined">Unset</mat-radio-button>
</mat-radio-group>
</div>
<youtube-player [videoId]="video && video.id"></youtube-player>
<youtube-player [videoId]="selectedVideo && selectedVideo.id"
[width]="videoWidth" [height]="videoHeight"></youtube-player>
</section>
</div>
2 changes: 1 addition & 1 deletion src/dev-app/youtube-player/youtube-player-demo.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.demo-video-selection {
margin-bottom: 20px;

mat-radio-button {
.mat-radio-button {
margin: 8px;
}
}
35 changes: 31 additions & 4 deletions src/dev-app/youtube-player/youtube-player-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Component} from '@angular/core';
import {
AfterViewInit,
ChangeDetectorRef,
Component,
ElementRef,
OnDestroy,
ViewChild
} from '@angular/core';

interface Video {
id: string;
Expand All @@ -33,8 +40,28 @@ const VIDEOS: Video[] = [
templateUrl: 'youtube-player-demo.html',
styleUrls: ['youtube-player-demo.css'],
})
export class YouTubePlayerDemo {
video: Video | undefined = VIDEOS[0];
export class YouTubePlayerDemo implements AfterViewInit, OnDestroy {
@ViewChild('demoYouTubePlayer') demoYouTubePlayer: ElementRef<HTMLDivElement>;
selectedVideo: Video | undefined = VIDEOS[0];
videos = VIDEOS;
apiLoaded = false;
videoWidth: number | undefined;
videoHeight: number | undefined;

constructor(private _changeDetectorRef: ChangeDetectorRef) {}

ngAfterViewInit(): void {
this.onResize();
window.addEventListener('resize', this.onResize);
}

onResize = (): void => {
// Automatically expand the video to fit the page up to 1200px x 720px
this.videoWidth = Math.min(this.demoYouTubePlayer.nativeElement.clientWidth, 1200);
this.videoHeight = this.videoWidth * 0.6;
this._changeDetectorRef.detectChanges();
}

ngOnDestroy(): void {
window.removeEventListener('resize', this.onResize);
}
}