aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-06-03 22:08:03 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-06-03 22:08:03 +0200
commit4a6995be18b15de1834a39c8921a0e4109671bb6 (patch)
treeb659661cea33687fcc6bd8fc2251cb7a15ab9f9d /client/src/app/shared
parent468892541175f9662f8b1b977e819dc1a496f282 (diff)
downloadPeerTube-4a6995be18b15de1834a39c8921a0e4109671bb6.tar.gz
PeerTube-4a6995be18b15de1834a39c8921a0e4109671bb6.tar.zst
PeerTube-4a6995be18b15de1834a39c8921a0e4109671bb6.zip
First draft to use webpack instead of systemjs
Diffstat (limited to 'client/src/app/shared')
-rw-r--r--client/src/app/shared/index.ts2
-rw-r--r--client/src/app/shared/search/index.ts3
-rw-r--r--client/src/app/shared/search/search-field.type.ts1
-rw-r--r--client/src/app/shared/search/search.component.html17
-rw-r--r--client/src/app/shared/search/search.component.ts46
-rw-r--r--client/src/app/shared/search/search.model.ts6
-rw-r--r--client/src/app/shared/users/auth-status.model.ts4
-rw-r--r--client/src/app/shared/users/auth.service.ts108
-rw-r--r--client/src/app/shared/users/index.ts4
-rw-r--r--client/src/app/shared/users/token.model.ts32
-rw-r--r--client/src/app/shared/users/user.model.ts20
11 files changed, 243 insertions, 0 deletions
diff --git a/client/src/app/shared/index.ts b/client/src/app/shared/index.ts
new file mode 100644
index 000000000..0cab7dad0
--- /dev/null
+++ b/client/src/app/shared/index.ts
@@ -0,0 +1,2 @@
1export * from './search';
2export * from './users'
diff --git a/client/src/app/shared/search/index.ts b/client/src/app/shared/search/index.ts
new file mode 100644
index 000000000..a49a4f1a9
--- /dev/null
+++ b/client/src/app/shared/search/index.ts
@@ -0,0 +1,3 @@
1export * from './search-field.type';
2export * from './search.component';
3export * from './search.model';
diff --git a/client/src/app/shared/search/search-field.type.ts b/client/src/app/shared/search/search-field.type.ts
new file mode 100644
index 000000000..846236290
--- /dev/null
+++ b/client/src/app/shared/search/search-field.type.ts
@@ -0,0 +1 @@
export type SearchField = "name" | "author" | "podUrl" | "magnetUri";
diff --git a/client/src/app/shared/search/search.component.html b/client/src/app/shared/search/search.component.html
new file mode 100644
index 000000000..fb13ac72e
--- /dev/null
+++ b/client/src/app/shared/search/search.component.html
@@ -0,0 +1,17 @@
1<div class="input-group">
2 <div class="input-group-btn" dropdown>
3 <button id="simple-btn-keyboard-nav" type="button" class="btn btn-default" dropdownToggle>
4 {{ getStringChoice(searchCriterias.field) }} <span class="caret"></span>
5 </button>
6 <ul class="dropdown-menu" role="menu" aria-labelledby="simple-btn-keyboard-nav">
7 <li *ngFor="let choice of choiceKeys" class="dropdown-item">
8 <a class="dropdown-item" href="#" (click)="choose($event, choice)">{{ getStringChoice(choice) }}</a>
9 </li>
10 </ul>
11 </div>
12
13 <input
14 type="text" id="search-video" name="search-video" class="form-control" placeholder="Search a video..." class="form-control"
15 [(ngModel)]="searchCriterias.value" (keyup.enter)="doSearch()"
16 >
17</div>
diff --git a/client/src/app/shared/search/search.component.ts b/client/src/app/shared/search/search.component.ts
new file mode 100644
index 000000000..31f8b1535
--- /dev/null
+++ b/client/src/app/shared/search/search.component.ts
@@ -0,0 +1,46 @@
1import { Component, EventEmitter, Output } from '@angular/core';
2
3import { DROPDOWN_DIRECTIVES} from 'ng2-bootstrap/components/dropdown';
4
5import { Search } from './search.model';
6import { SearchField } from './search-field.type';
7
8@Component({
9 selector: 'my-search',
10 template: require('./search.component.html'),
11 directives: [ DROPDOWN_DIRECTIVES ]
12})
13
14export class SearchComponent {
15 @Output() search = new EventEmitter<Search>();
16
17 fieldChoices = {
18 name: 'Name',
19 author: 'Author',
20 podUrl: 'Pod Url',
21 magnetUri: 'Magnet Uri'
22 };
23 searchCriterias: Search = {
24 field: 'name',
25 value: ''
26 };
27
28 get choiceKeys() {
29 return Object.keys(this.fieldChoices);
30 }
31
32 choose($event: MouseEvent, choice: SearchField) {
33 $event.preventDefault();
34 $event.stopPropagation();
35
36 this.searchCriterias.field = choice;
37 }
38
39 doSearch() {
40 this.search.emit(this.searchCriterias);
41 }
42
43 getStringChoice(choiceKey: SearchField) {
44 return this.fieldChoices[choiceKey];
45 }
46}
diff --git a/client/src/app/shared/search/search.model.ts b/client/src/app/shared/search/search.model.ts
new file mode 100644
index 000000000..932a6566c
--- /dev/null
+++ b/client/src/app/shared/search/search.model.ts
@@ -0,0 +1,6 @@
1import { SearchField } from './search-field.type';
2
3export interface Search {
4 field: SearchField;
5 value: string;
6}
diff --git a/client/src/app/shared/users/auth-status.model.ts b/client/src/app/shared/users/auth-status.model.ts
new file mode 100644
index 000000000..f646bd4cf
--- /dev/null
+++ b/client/src/app/shared/users/auth-status.model.ts
@@ -0,0 +1,4 @@
1export enum AuthStatus {
2 LoggedIn,
3 LoggedOut
4}
diff --git a/client/src/app/shared/users/auth.service.ts b/client/src/app/shared/users/auth.service.ts
new file mode 100644
index 000000000..d63fe38f3
--- /dev/null
+++ b/client/src/app/shared/users/auth.service.ts
@@ -0,0 +1,108 @@
1import { Injectable } from '@angular/core';
2import { Headers, Http, RequestOptions, Response, URLSearchParams } from '@angular/http';
3import { Observable, Subject } from 'rxjs/Rx';
4
5import { AuthStatus } from './auth-status.model';
6import { User } from './user.model';
7
8@Injectable()
9export class AuthService {
10 private static BASE_CLIENT_URL = '/api/v1/users/client';
11 private static BASE_LOGIN_URL = '/api/v1/users/token';
12
13 loginChangedSource: Observable<AuthStatus>;
14
15 private clientId: string;
16 private clientSecret: string;
17 private loginChanged: Subject<AuthStatus>;
18
19 constructor(private http: Http) {
20 this.loginChanged = new Subject<AuthStatus>();
21 this.loginChangedSource = this.loginChanged.asObservable();
22
23 // Fetch the client_id/client_secret
24 // FIXME: save in local storage?
25 this.http.get(AuthService.BASE_CLIENT_URL)
26 .map(res => res.json())
27 .catch(this.handleError)
28 .subscribe(
29 result => {
30 this.clientId = result.client_id;
31 this.clientSecret = result.client_secret;
32 console.log('Client credentials loaded.');
33 },
34 error => {
35 alert(error);
36 }
37 );
38 }
39
40 getAuthRequestOptions(): RequestOptions {
41 return new RequestOptions({ headers: this.getRequestHeader() });
42 }
43
44 getRequestHeader() {
45 return new Headers({ 'Authorization': `${this.getTokenType()} ${this.getToken()}` });
46 }
47
48 getToken() {
49 return localStorage.getItem('access_token');
50 }
51
52 getTokenType() {
53 return localStorage.getItem('token_type');
54 }
55
56 getUser(): User {
57 if (this.isLoggedIn() === false) {
58 return null;
59 }
60
61 const user = User.load();
62
63 return user;
64 }
65
66 isLoggedIn() {
67 if (this.getToken()) {
68 return true;
69 } else {
70 return false;
71 }
72 }
73
74 login(username: string, password: string) {
75 let body = new URLSearchParams();
76 body.set('client_id', this.clientId);
77 body.set('client_secret', this.clientSecret);
78 body.set('response_type', 'code');
79 body.set('grant_type', 'password');
80 body.set('scope', 'upload');
81 body.set('username', username);
82 body.set('password', password);
83
84 let headers = new Headers();
85 headers.append('Content-Type', 'application/x-www-form-urlencoded');
86
87 let options = {
88 headers: headers
89 };
90
91 return this.http.post(AuthService.BASE_LOGIN_URL, body.toString(), options)
92 .map(res => res.json())
93 .catch(this.handleError);
94 }
95
96 logout() {
97 // TODO make HTTP request
98 }
99
100 setStatus(status: AuthStatus) {
101 this.loginChanged.next(status);
102 }
103
104 private handleError (error: Response) {
105 console.error(error);
106 return Observable.throw(error.json() || { error: 'Server error' });
107 }
108}
diff --git a/client/src/app/shared/users/index.ts b/client/src/app/shared/users/index.ts
new file mode 100644
index 000000000..c6816b3c6
--- /dev/null
+++ b/client/src/app/shared/users/index.ts
@@ -0,0 +1,4 @@
1export * from './auth-status.model';
2export * from './auth.service';
3export * from './token.model';
4export * from './user.model';
diff --git a/client/src/app/shared/users/token.model.ts b/client/src/app/shared/users/token.model.ts
new file mode 100644
index 000000000..021c83fad
--- /dev/null
+++ b/client/src/app/shared/users/token.model.ts
@@ -0,0 +1,32 @@
1export class Token {
2 access_token: string;
3 refresh_token: string;
4 token_type: string;
5
6 static load() {
7 return new Token({
8 access_token: localStorage.getItem('access_token'),
9 refresh_token: localStorage.getItem('refresh_token'),
10 token_type: localStorage.getItem('token_type')
11 });
12 }
13
14 constructor(hash?: any) {
15 if (hash) {
16 this.access_token = hash.access_token;
17 this.refresh_token = hash.refresh_token;
18
19 if (hash.token_type === 'bearer') {
20 this.token_type = 'Bearer';
21 } else {
22 this.token_type = hash.token_type;
23 }
24 }
25 }
26
27 save() {
28 localStorage.setItem('access_token', this.access_token);
29 localStorage.setItem('refresh_token', this.refresh_token);
30 localStorage.setItem('token_type', this.token_type);
31 }
32}
diff --git a/client/src/app/shared/users/user.model.ts b/client/src/app/shared/users/user.model.ts
new file mode 100644
index 000000000..ca0a5f26c
--- /dev/null
+++ b/client/src/app/shared/users/user.model.ts
@@ -0,0 +1,20 @@
1import { Token } from './token.model';
2
3export class User {
4 username: string;
5 token: Token;
6
7 static load() {
8 return new User(localStorage.getItem('username'), Token.load());
9 }
10
11 constructor(username: string, hash_token: any) {
12 this.username = username;
13 this.token = new Token(hash_token);
14 }
15
16 save() {
17 localStorage.setItem('username', this.username);
18 this.token.save();
19 }
20}