]>
Commit | Line | Data |
---|---|---|
dc8bc31b C |
1 | /// <reference path='../../../../typings/browser/ambient/webtorrent/webtorrent.d.ts' /> |
2 | ||
3 | import { Component, OnInit, ElementRef } from 'angular2/core'; | |
98b01bac | 4 | import { RouteParams, CanDeactivate, ComponentInstruction } from 'angular2/router'; |
dc8bc31b | 5 | |
98b01bac | 6 | // TODO import it with systemjs |
dc8bc31b C |
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 | ||
98b01bac | 18 | export class VideosWatchComponent implements OnInit, CanDeactivate { |
dc8bc31b C |
19 | video: Video; |
20 | ||
21 | private client: any; | |
22 | ||
23 | constructor( | |
24 | private _videosService: VideosService, | |
25 | private _routeParams: RouteParams, | |
26 | private _elementRef: ElementRef | |
27 | ) { | |
98b01bac | 28 | // TODO: use a service |
dc8bc31b C |
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 | ||
43 | this.client.add(this.video.magnetUri, (torrent) => { | |
44 | torrent.files[0].appendTo(this._elementRef.nativeElement, (err) => { | |
45 | if (err) { | |
46 | alert('Cannot append the file.'); | |
47 | console.error(err); | |
48 | } | |
49 | }) | |
50 | }) | |
51 | } | |
98b01bac C |
52 | |
53 | routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) : any { | |
54 | console.log('Removing video from webtorrent.'); | |
55 | this.client.remove(this.video.magnetUri); | |
56 | return true; | |
57 | } | |
dc8bc31b | 58 | } |