Implement user requests autorizations in the client side
[oweals/peertube.git] / client / angular / videos / components / watch / videos-watch.component.ts
1 import { Component, OnInit, ElementRef } from 'angular2/core';
2 import { RouteParams, CanDeactivate, ComponentInstruction } from 'angular2/router';
3
4 // TODO import it with systemjs
5 declare var WebTorrent: any;
6
7 import { Video } from '../../models/video';
8 import { VideosService } from '../../services/videos.service';
9
10 @Component({
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' ]
14 })
15
16 export class VideosWatchComponent implements OnInit, CanDeactivate {
17   video: Video;
18
19   private client: any;
20
21   constructor(
22     private _videosService: VideosService,
23     private _routeParams: RouteParams,
24     private _elementRef: ElementRef
25   ) {
26     // TODO: use a service
27     this.client = new WebTorrent({ dht: false });
28   }
29
30   ngOnInit() {
31     let id = this._routeParams.get('id');
32     this._videosService.getVideo(id).subscribe(
33       video => this.loadVideo(video),
34       error => alert(error)
35     );
36   }
37
38   loadVideo(video: Video) {
39     this.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) => {
44         if (err) {
45           alert('Cannot append the file.');
46           console.error(err);
47         }
48       });
49     });
50   }
51
52   routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) : any {
53     console.log('Removing video from webtorrent.');
54     this.client.remove(this.video.magnetUri);
55     return true;
56   }
57 }