]>
Commit | Line | Data |
---|---|---|
dc8bc31b | 1 | import { Component, OnInit, ElementRef } from 'angular2/core'; |
98b01bac | 2 | import { RouteParams, CanDeactivate, ComponentInstruction } from 'angular2/router'; |
dc8bc31b | 3 | |
98b01bac | 4 | // TODO import it with systemjs |
dc8bc31b C |
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 | ||
98b01bac | 16 | export class VideosWatchComponent implements OnInit, CanDeactivate { |
dc8bc31b C |
17 | video: Video; |
18 | ||
19 | private client: any; | |
20 | ||
21 | constructor( | |
22 | private _videosService: VideosService, | |
23 | private _routeParams: RouteParams, | |
24 | private _elementRef: ElementRef | |
25 | ) { | |
98b01bac | 26 | // TODO: use a service |
dc8bc31b C |
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; | |
2c4a0b5d | 40 | console.log('Adding ' + this.video.magnetUri + '.'); |
dc8bc31b | 41 | this.client.add(this.video.magnetUri, (torrent) => { |
2c4a0b5d | 42 | console.log('Added ' + this.video.magnetUri + '.'); |
dc8bc31b C |
43 | torrent.files[0].appendTo(this._elementRef.nativeElement, (err) => { |
44 | if (err) { | |
45 | alert('Cannot append the file.'); | |
46 | console.error(err); | |
47 | } | |
44124980 C |
48 | }); |
49 | }); | |
dc8bc31b | 50 | } |
98b01bac C |
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 | } | |
dc8bc31b | 57 | } |