From 98b01bac2c2c4536aa97d826b61516657f4d15f5 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 14 Mar 2016 22:16:43 +0100 Subject: Angular 2 : draft 2 --- .../videos/components/add/videos-add.component.ts | 15 +++++++----- .../components/list/videos-list.component.ts | 27 ++++++++++++++++------ .../components/watch/videos-watch.component.ts | 12 ++++++++-- client/angular/videos/services/videos.service.ts | 6 +++++ 4 files changed, 45 insertions(+), 15 deletions(-) (limited to 'client/angular/videos') diff --git a/client/angular/videos/components/add/videos-add.component.ts b/client/angular/videos/components/add/videos-add.component.ts index 97e3bb3b5..8ff6cfec8 100644 --- a/client/angular/videos/components/add/videos-add.component.ts +++ b/client/angular/videos/components/add/videos-add.component.ts @@ -1,9 +1,10 @@ -import {Component, ElementRef, Inject, OnInit} from 'angular2/core'; -import {Router} from 'angular2/router'; -import {NgForm} from 'angular2/common'; +import { Component, ElementRef, Inject, OnInit } from 'angular2/core'; +import { Router } from 'angular2/router'; +import { NgForm } from 'angular2/common'; -import {Video} from '../../models/video'; +import { Video } from '../../models/video'; +// TODO: import it with systemjs declare var jQuery:any; @Component({ @@ -22,9 +23,10 @@ export class VideosAddComponent implements OnInit { ngOnInit() { jQuery(this._elementRef.nativeElement).find('#input_video').fileupload({ + url: '/api/v1/videos', + dataType: 'json', singleFileUploads: true, multipart: true, - url: '/api/v1/videos', autoupload: false, add: (e, data) => { @@ -38,7 +40,8 @@ export class VideosAddComponent implements OnInit { }, done: (e, data) => { - console.log('finished'); + console.log('Video uploaded.'); + // Print all the videos once it's finished this._router.navigate(['VideosList']); } diff --git a/client/angular/videos/components/list/videos-list.component.ts b/client/angular/videos/components/list/videos-list.component.ts index e5af87448..eb23ed1ff 100644 --- a/client/angular/videos/components/list/videos-list.component.ts +++ b/client/angular/videos/components/list/videos-list.component.ts @@ -1,8 +1,8 @@ -import {Component, OnInit} from 'angular2/core'; -import {ROUTER_DIRECTIVES} from 'angular2/router'; +import { Component, OnInit } from 'angular2/core'; +import { ROUTER_DIRECTIVES, RouteParams } from 'angular2/router'; -import {VideosService} from '../../services/videos.service'; -import {Video} from '../../models/video'; +import { VideosService } from '../../services/videos.service'; +import { Video } from '../../models/video'; @Component({ selector: 'my-videos-list', @@ -14,16 +14,29 @@ import {Video} from '../../models/video'; export class VideosListComponent implements OnInit { videos: Video[]; + private search: string; + constructor( - private _videosService: VideosService - ) { } + private _videosService: VideosService, + routeParams: RouteParams + ) { + this.search = routeParams.get('search'); + } ngOnInit() { this.getVideos(); } getVideos() { - this._videosService.getVideos().subscribe( + let observable = null; + + if (this.search !== null) { + observable = this._videosService.searchVideos(this.search); + } else { + observable = this._videosService.getVideos() + } + + observable.subscribe( videos => this.videos = videos, error => alert(error) ); diff --git a/client/angular/videos/components/watch/videos-watch.component.ts b/client/angular/videos/components/watch/videos-watch.component.ts index e3a973820..da7f942db 100644 --- a/client/angular/videos/components/watch/videos-watch.component.ts +++ b/client/angular/videos/components/watch/videos-watch.component.ts @@ -1,8 +1,9 @@ /// import { Component, OnInit, ElementRef } from 'angular2/core'; -import { RouteParams } from 'angular2/router'; +import { RouteParams, CanDeactivate, ComponentInstruction } from 'angular2/router'; +// TODO import it with systemjs declare var WebTorrent: any; import { Video } from '../../models/video'; @@ -14,7 +15,7 @@ import { VideosService } from '../../services/videos.service'; styleUrls: [ 'app/angular/videos/components/watch/videos-watch.component.css' ] }) -export class VideosWatchComponent { +export class VideosWatchComponent implements OnInit, CanDeactivate { video: Video; private client: any; @@ -24,6 +25,7 @@ export class VideosWatchComponent { private _routeParams: RouteParams, private _elementRef: ElementRef ) { + // TODO: use a service this.client = new WebTorrent({ dht: false }); } @@ -47,4 +49,10 @@ export class VideosWatchComponent { }) }) } + + routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) : any { + console.log('Removing video from webtorrent.'); + this.client.remove(this.video.magnetUri); + return true; + } } diff --git a/client/angular/videos/services/videos.service.ts b/client/angular/videos/services/videos.service.ts index 784eec68d..17ae89c8b 100644 --- a/client/angular/videos/services/videos.service.ts +++ b/client/angular/videos/services/videos.service.ts @@ -30,6 +30,12 @@ export class VideosService { } } + searchVideos(search: string) { + return this.http.get(this._baseVideoUrl + 'search/' + search) + .map(res =>