PeerTube/client/angular/videos/components/watch/videos-watch.component.ts

59 lines
1.6 KiB
TypeScript
Raw Normal View History

2016-03-14 13:50:19 +01:00
/// <reference path='../../../../typings/browser/ambient/webtorrent/webtorrent.d.ts' />
import { Component, OnInit, ElementRef } from 'angular2/core';
2016-03-14 22:16:43 +01:00
import { RouteParams, CanDeactivate, ComponentInstruction } from 'angular2/router';
2016-03-14 13:50:19 +01:00
2016-03-14 22:16:43 +01:00
// TODO import it with systemjs
2016-03-14 13:50:19 +01:00
declare var WebTorrent: any;
import { Video } from '../../models/video';
import { VideosService } from '../../services/videos.service';
@Component({
selector: 'my-video-watch',
templateUrl: 'app/angular/videos/components/watch/videos-watch.component.html',
styleUrls: [ 'app/angular/videos/components/watch/videos-watch.component.css' ]
})
2016-03-14 22:16:43 +01:00
export class VideosWatchComponent implements OnInit, CanDeactivate {
2016-03-14 13:50:19 +01:00
video: Video;
private client: any;
constructor(
private _videosService: VideosService,
private _routeParams: RouteParams,
private _elementRef: ElementRef
) {
2016-03-14 22:16:43 +01:00
// TODO: use a service
2016-03-14 13:50:19 +01:00
this.client = new WebTorrent({ dht: false });
}
ngOnInit() {
let id = this._routeParams.get('id');
this._videosService.getVideo(id).subscribe(
video => this.loadVideo(video),
error => alert(error)
);
}
loadVideo(video: Video) {
this.video = video;
this.client.add(this.video.magnetUri, (torrent) => {
torrent.files[0].appendTo(this._elementRef.nativeElement, (err) => {
if (err) {
alert('Cannot append the file.');
console.error(err);
}
})
})
}
2016-03-14 22:16:43 +01:00
routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) : any {
console.log('Removing video from webtorrent.');
this.client.remove(this.video.magnetUri);
return true;
}
2016-03-14 13:50:19 +01:00
}