1 import { Component, OnInit, ElementRef } from 'angular2/core';
2 import { RouteParams, CanDeactivate, ComponentInstruction } from 'angular2/router';
3 import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
5 // TODO import it with systemjs
6 declare var WebTorrent: any;
8 import { Video } from '../../models/video';
9 import { VideosService } from '../../services/videos.service';
12 selector: 'my-video-watch',
13 templateUrl: 'app/angular/videos/components/watch/videos-watch.component.html',
14 styleUrls: [ 'app/angular/videos/components/watch/videos-watch.component.css' ],
18 export class VideosWatchComponent implements OnInit, CanDeactivate {
20 downloadSpeed: number;
23 loading: boolean = false;
25 private _interval: number;
29 private _videosService: VideosService,
30 private _routeParams: RouteParams,
31 private _elementRef: ElementRef
33 // TODO: use a service
34 this.client = new WebTorrent({ dht: false });
38 let id = this._routeParams.get('id');
39 this._videosService.getVideo(id).subscribe(
40 video => this.loadVideo(video),
45 loadVideo(video: Video) {
48 console.log('Adding ' + this.video.magnetUri + '.');
49 this.client.add(this.video.magnetUri, (torrent) => {
51 console.log('Added ' + this.video.magnetUri + '.');
52 torrent.files[0].appendTo(this._elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
54 alert('Cannot append the file.');
59 // Refresh each second
60 this._interval = setInterval(() => {
61 this.downloadSpeed = torrent.downloadSpeed;
62 this.uploadSpeed = torrent.uploadSpeed;
63 this.numPeers = torrent.numPeers;
68 routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) : any {
69 console.log('Removing video from webtorrent.');
70 clearInterval(this._interval);
71 this.client.remove(this.video.magnetUri);