1 import { Component, OnInit, ElementRef } from 'angular2/core';
2 import { RouteParams, CanDeactivate, ComponentInstruction } from 'angular2/router';
4 // TODO import it with systemjs
5 declare var WebTorrent: any;
7 import { Video } from '../../models/video';
8 import { VideosService } from '../../services/videos.service';
11 selector: 'my-video-watch',
12 templateUrl: 'app/angular/videos/components/watch/videos-watch.component.html',
13 styleUrls: [ 'app/angular/videos/components/watch/videos-watch.component.css' ]
16 export class VideosWatchComponent implements OnInit, CanDeactivate {
22 private _videosService: VideosService,
23 private _routeParams: RouteParams,
24 private _elementRef: ElementRef
26 // TODO: use a service
27 this.client = new WebTorrent({ dht: false });
31 let id = this._routeParams.get('id');
32 this._videosService.getVideo(id).subscribe(
33 video => this.loadVideo(video),
38 loadVideo(video: Video) {
40 console.log('Adding ' + this.video.magnetUri + '.');
41 this.client.add(this.video.magnetUri, (torrent) => {
42 console.log('Added ' + this.video.magnetUri + '.');
43 torrent.files[0].appendTo(this._elementRef.nativeElement, (err) => {
45 alert('Cannot append the file.');
52 routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) : any {
53 console.log('Removing video from webtorrent.');
54 this.client.remove(this.video.magnetUri);