]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/angular/videos/components/watch/videos-watch.component.ts
Make the sort/results bar less ugly
[github/Chocobozzz/PeerTube.git] / client / angular / videos / components / watch / videos-watch.component.ts
CommitLineData
230809ef
C
1import { Component, OnInit, ElementRef } from '@angular/core';
2import { RouteParams, CanDeactivate, ComponentInstruction } from '@angular/router-deprecated';
8140a704 3
230809ef 4import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
dc8bc31b 5
98b01bac 6// TODO import it with systemjs
dc8bc31b
C
7declare var WebTorrent: any;
8
501bc6c2
C
9import { Video } from '../../video';
10import { VideosService } from '../../videos.service';
dc8bc31b
C
11
12@Component({
13 selector: 'my-video-watch',
14 templateUrl: 'app/angular/videos/components/watch/videos-watch.component.html',
8cfecb2a
C
15 styleUrls: [ 'app/angular/videos/components/watch/videos-watch.component.css' ],
16 pipes: [ BytesPipe ]
dc8bc31b
C
17})
18
98b01bac 19export class VideosWatchComponent implements OnInit, CanDeactivate {
dc8bc31b 20 video: Video;
8cfecb2a
C
21 downloadSpeed: number;
22 uploadSpeed: number;
23 numPeers: number;
da932efc 24 loading: boolean = false;
dc8bc31b 25
f03996da 26 private _interval: NodeJS.Timer;
dc8bc31b
C
27 private client: any;
28
29 constructor(
30 private _videosService: VideosService,
31 private _routeParams: RouteParams,
32 private _elementRef: ElementRef
33 ) {
98b01bac 34 // TODO: use a service
dc8bc31b
C
35 this.client = new WebTorrent({ dht: false });
36 }
37
38 ngOnInit() {
39 let id = this._routeParams.get('id');
40 this._videosService.getVideo(id).subscribe(
41 video => this.loadVideo(video),
42 error => alert(error)
43 );
44 }
45
46 loadVideo(video: Video) {
da932efc 47 this.loading = true;
dc8bc31b 48 this.video = video;
2c4a0b5d 49 console.log('Adding ' + this.video.magnetUri + '.');
dc8bc31b 50 this.client.add(this.video.magnetUri, (torrent) => {
da932efc 51 this.loading = false;
2c4a0b5d 52 console.log('Added ' + this.video.magnetUri + '.');
cb11e775 53 torrent.files[0].appendTo(this._elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
dc8bc31b
C
54 if (err) {
55 alert('Cannot append the file.');
56 console.error(err);
57 }
44124980 58 });
8cfecb2a
C
59
60 // Refresh each second
61 this._interval = setInterval(() => {
62 this.downloadSpeed = torrent.downloadSpeed;
63 this.uploadSpeed = torrent.uploadSpeed;
64 this.numPeers = torrent.numPeers;
65 }, 1000);
44124980 66 });
dc8bc31b 67 }
98b01bac
C
68
69 routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) : any {
70 console.log('Removing video from webtorrent.');
e9a2578e 71 clearInterval(this._interval);
98b01bac
C
72 this.client.remove(this.video.magnetUri);
73 return true;
74 }
dc8bc31b 75}