Add typescript (and angular2) linter
[oweals/peertube.git] / client / angular / videos / components / watch / videos-watch.component.ts
1 /// <reference path='../../../../typings/browser/ambient/webtorrent/webtorrent.d.ts' />
2
3 import { Component, OnInit, ElementRef } from 'angular2/core';
4 import { RouteParams, CanDeactivate, ComponentInstruction } from 'angular2/router';
5
6 // TODO import it with systemjs
7 declare var WebTorrent: any;
8
9 import { Video } from '../../models/video';
10 import { VideosService } from '../../services/videos.service';
11
12 @Component({
13   selector: 'my-video-watch',
14   templateUrl: 'app/angular/videos/components/watch/videos-watch.component.html',
15   styleUrls: [ 'app/angular/videos/components/watch/videos-watch.component.css' ]
16 })
17
18 export class VideosWatchComponent implements OnInit, CanDeactivate {
19   video: Video;
20
21   private client: any;
22
23   constructor(
24     private _videosService: VideosService,
25     private _routeParams: RouteParams,
26     private _elementRef: ElementRef
27   ) {
28     // TODO: use a service
29     this.client = new WebTorrent({ dht: false });
30   }
31
32   ngOnInit() {
33     let id = this._routeParams.get('id');
34     this._videosService.getVideo(id).subscribe(
35       video => this.loadVideo(video),
36       error => alert(error)
37     );
38   }
39
40   loadVideo(video: Video) {
41     this.video = video;
42     console.log('Adding ' + this.video.magnetUri + '.');
43     this.client.add(this.video.magnetUri, (torrent) => {
44       console.log('Added ' + this.video.magnetUri + '.');
45       torrent.files[0].appendTo(this._elementRef.nativeElement, (err) => {
46         if (err) {
47           alert('Cannot append the file.');
48           console.error(err);
49         }
50       });
51     });
52   }
53
54   routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) : any {
55     console.log('Removing video from webtorrent.');
56     this.client.remove(this.video.magnetUri);
57     return true;
58   }
59 }