Do not prefix private attributes

This commit is contained in:
Chocobozzz 2016-05-27 17:25:52 +02:00
parent 41a2aee38c
commit ccf6ed16f1
15 changed files with 112 additions and 93 deletions

View File

@ -51,16 +51,15 @@ import {
export class AppComponent {
isLoggedIn: boolean;
search_field: string = name;
choices = [ ];
constructor(private _friendService: FriendService,
private _authService: AuthService,
private _router: Router
choices = [];
constructor(private friendService: FriendService,
private authService: AuthService,
private router: Router
) {
this.isLoggedIn = this._authService.isLoggedIn();
this.isLoggedIn = this.authService.isLoggedIn();
this._authService.loginChanged$.subscribe(
this.authService.loginChangedSource.subscribe(
status => {
if (status === AuthStatus.LoggedIn) {
this.isLoggedIn = true;
@ -75,9 +74,9 @@ export class AppComponent {
search: search.value,
field: search.field
};
this._router.navigate(['VideosList', params]);
this.router.navigate(['VideosList', params]);
} else {
this._router.navigate(['VideosList']);
this.router.navigate(['VideosList']);
}
}
@ -86,7 +85,7 @@ export class AppComponent {
}
makeFriends() {
this._friendService.makeFriends().subscribe(
this.friendService.makeFriends().subscribe(
status => {
if (status === 409) {
alert('Already made friends!');
@ -99,7 +98,7 @@ export class AppComponent {
}
quitFriends() {
this._friendService.quitFriends().subscribe(
this.friendService.quitFriends().subscribe(
status => {
alert('Quit friends!');
},

View File

@ -4,23 +4,23 @@ import { Observable } from 'rxjs/Rx';
@Injectable()
export class FriendService {
private _baseFriendsUrl = '/api/v1/pods/';
private static BASE_FRIEND_URL: string = '/api/v1/pods/';
constructor (private http: Http) {}
makeFriends() {
return this.http.get(this._baseFriendsUrl + 'makefriends')
.map(res => <number> res.status)
return this.http.get(FriendService.BASE_FRIEND_URL + 'makefriends')
.map(res => res.status)
.catch(this.handleError);
}
quitFriends() {
return this.http.get(this._baseFriendsUrl + 'quitfriends')
.map(res => <number> res.status)
return this.http.get(FriendService.BASE_FRIEND_URL + 'quitfriends')
.map(res => res.status)
.catch(this.handleError);
}
private handleError (error: Response) {
private handleError (error: Response): Observable<number> {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}

View File

@ -12,12 +12,13 @@ import { SearchField } from './search-field.type';
})
export class SearchComponent {
@Output() search: EventEmitter<Search> = new EventEmitter<Search>();
@Output() search = new EventEmitter<Search>();
searchCriterias: Search = {
field: 'name',
value: ''
};
fieldChoices = {
name: 'Name',
author: 'Author',
@ -29,18 +30,18 @@ export class SearchComponent {
return Object.keys(this.fieldChoices);
}
getStringChoice(choiceKey: SearchField): string {
getStringChoice(choiceKey: SearchField) {
return this.fieldChoices[choiceKey];
}
choose($event:MouseEvent, choice: SearchField) {
choose($event: MouseEvent, choice: SearchField) {
$event.preventDefault();
$event.stopPropagation();
this.searchCriterias.field = choice;
}
doSearch(): void {
doSearch() {
this.search.emit(this.searchCriterias);
}

View File

@ -10,17 +10,17 @@ import { AuthService, AuthStatus, User } from '../shared/index';
})
export class UserLoginComponent {
constructor(private _authService: AuthService, private _router: Router) {}
constructor(private authService: AuthService, private router: Router) {}
login(username: string, password: string) {
this._authService.login(username, password).subscribe(
this.authService.login(username, password).subscribe(
result => {
const user = new User(username, result);
user.save();
this._authService.setStatus(AuthStatus.LoggedIn);
this.authService.setStatus(AuthStatus.LoggedIn);
this._router.navigate(['VideosList']);
this.router.navigate(['VideosList']);
},
error => {
if (error.error === 'invalid_grant') {

View File

@ -7,27 +7,28 @@ import { User } from './user.model';
@Injectable()
export class AuthService {
loginChanged$;
private static BASE_LOGIN_URL = '/api/v1/users/token';
private static BASE_CLIENT_URL = '/api/v1/users/client';
private _loginChanged;
private _baseLoginUrl = '/api/v1/users/token';
private _baseClientUrl = '/api/v1/users/client';
private _clientId = '';
private _clientSecret = '';
loginChangedSource: Observable<AuthStatus>;
constructor (private http: Http) {
this._loginChanged = new Subject<AuthStatus>();
this.loginChanged$ = this._loginChanged.asObservable();
private loginChanged: Subject<AuthStatus>;
private clientId: string;
private clientSecret: string;
constructor(private http: Http) {
this.loginChanged = new Subject<AuthStatus>();
this.loginChangedSource = this.loginChanged.asObservable();
// Fetch the client_id/client_secret
// FIXME: save in local storage?
this.http.get(this._baseClientUrl)
this.http.get(AuthService.BASE_CLIENT_URL)
.map(res => res.json())
.catch(this.handleError)
.subscribe(
result => {
this._clientId = result.client_id;
this._clientSecret = result.client_secret;
this.clientId = result.client_id;
this.clientSecret = result.client_secret;
console.log('Client credentials loaded.');
},
error => {
@ -38,8 +39,8 @@ export class AuthService {
login(username: string, password: string) {
let body = new URLSearchParams();
body.set('client_id', this._clientId);
body.set('client_secret', this._clientSecret);
body.set('client_id', this.clientId);
body.set('client_secret', this.clientSecret);
body.set('response_type', 'code');
body.set('grant_type', 'password');
body.set('scope', 'upload');
@ -53,7 +54,7 @@ export class AuthService {
headers: headers
};
return this.http.post(this._baseLoginUrl, body.toString(), options)
return this.http.post(AuthService.BASE_LOGIN_URL, body.toString(), options)
.map(res => res.json())
.catch(this.handleError);
}
@ -62,7 +63,7 @@ export class AuthService {
// TODO make HTTP request
}
getRequestHeader(): Headers {
getRequestHeader() {
return new Headers({ 'Authorization': `${this.getTokenType()} ${this.getToken()}` });
}
@ -70,11 +71,11 @@ export class AuthService {
return new RequestOptions({ headers: this.getRequestHeader() });
}
getToken(): string {
getToken() {
return localStorage.getItem('access_token');
}
getTokenType(): string {
getTokenType() {
return localStorage.getItem('token_type');
}
@ -88,7 +89,7 @@ export class AuthService {
return user;
}
isLoggedIn(): boolean {
isLoggedIn() {
if (this.getToken()) {
return true;
} else {
@ -97,7 +98,7 @@ export class AuthService {
}
setStatus(status: AuthStatus) {
this._loginChanged.next(status);
this.loginChanged.next(status);
}
private handleError (error: Response) {

View File

@ -3,7 +3,7 @@ export class Token {
refresh_token: string;
token_type: string;
static load(): Token {
static load() {
return new Token({
access_token: localStorage.getItem('access_token'),
refresh_token: localStorage.getItem('refresh_token'),
@ -11,10 +11,11 @@ export class Token {
});
}
constructor (hash?: any) {
constructor(hash?: any) {
if (hash) {
this.access_token = hash.access_token;
this.refresh_token = hash.refresh_token;
if (hash.token_type === 'bearer') {
this.token_type = 'Bearer';
} else {
@ -23,7 +24,7 @@ export class Token {
}
}
save():void {
save() {
localStorage.setItem('access_token', this.access_token);
localStorage.setItem('refresh_token', this.refresh_token);
localStorage.setItem('token_type', this.token_type);

View File

@ -4,16 +4,16 @@ export class User {
username: string;
token: Token;
static load(): User {
static load() {
return new User(localStorage.getItem('username'), Token.load());
}
constructor (username: string, hash_token: any) {
constructor(username: string, hash_token: any) {
this.username = username;
this.token = new Token(hash_token);
}
save(): void {
save() {
localStorage.setItem('username', this.username);
this.token.save();
}

View File

@ -11,7 +11,7 @@ export class Video {
by: string;
duration: string;
private static createDurationString(duration: number): string {
private static createDurationString(duration: number) {
const minutes = Math.floor(duration / 60);
const seconds = duration % 60;
const minutes_padding = minutes >= 10 ? '' : '0';
@ -20,7 +20,7 @@ export class Video {
return minutes_padding + minutes.toString() + ':' + seconds_padding + seconds.toString();
}
private static createByString(author: string, podUrl: string): string {
private static createByString(author: string, podUrl: string) {
let [ host, port ] = podUrl.replace(/^https?:\/\//, '').split(':');
if (port === '80' || port === '443') {
@ -57,7 +57,7 @@ export class Video {
this.by = Video.createByString(hash.author, hash.podUrl);
}
isRemovableBy(user): boolean {
isRemovableBy(user) {
return this.isLocal === true && user && this.author === user.username;
}
}

View File

@ -10,30 +10,30 @@ import { Video } from './video.model';
@Injectable()
export class VideoService {
private _baseVideoUrl = '/api/v1/videos/';
private static BASE_VIDEO_URL = '/api/v1/videos/';
constructor (private http: Http, private _authService: AuthService) {}
constructor(private http: Http, private authService: AuthService) {}
getVideos(pagination: Pagination, sort: SortField) {
const params = this.createPaginationParams(pagination);
if (sort) params.set('sort', sort);
return this.http.get(this._baseVideoUrl, { search: params })
return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
.map(res => res.json())
.map(this.extractVideos)
.catch(this.handleError);
}
getVideo(id: string) {
return this.http.get(this._baseVideoUrl + id)
return this.http.get(VideoService.BASE_VIDEO_URL + id)
.map(res => <Video> res.json())
.catch(this.handleError);
}
removeVideo(id: string) {
const options = this._authService.getAuthRequestOptions();
return this.http.delete(this._baseVideoUrl + id, options)
const options = this.authService.getAuthRequestOptions();
return this.http.delete(VideoService.BASE_VIDEO_URL + id, options)
.map(res => <number> res.status)
.catch(this.handleError);
}
@ -44,13 +44,13 @@ export class VideoService {
if (search.field) params.set('field', search.field);
if (sort) params.set('sort', sort);
return this.http.get(this._baseVideoUrl + 'search/' + encodeURIComponent(search.value), { search: params })
return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
.map(res => res.json())
.map(this.extractVideos)
.catch(this.handleError);
}
private extractVideos (body: any) {
private extractVideos(body: any) {
const videos_json = body.data;
const totalVideos = body.total;
const videos = [];
@ -61,7 +61,7 @@ export class VideoService {
return { videos, totalVideos };
}
private handleError (error: Response) {
private handleError(error: Response) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}

View File

@ -7,7 +7,7 @@ import { PROGRESSBAR_DIRECTIVES } from 'ng2-bootstrap/components/progressbar';
import { AuthService, User } from '../../users/index';
// TODO: import it with systemjs
declare var jQuery:any;
declare var jQuery: any;
@Component({
selector: 'my-videos-add',

View File

@ -31,26 +31,26 @@ export class VideoListComponent implements OnInit {
total: 0
};
sort: SortField;
loading: boolean = false;
loading = false;
private search: Search;
constructor(
private _authService: AuthService,
private _videoService: VideoService,
private _routeParams: RouteParams,
private _router: Router
private authService: AuthService,
private videoService: VideoService,
private routeParams: RouteParams,
private router: Router
) {
this.search = {
value: this._routeParams.get('search'),
field: <SearchField>this._routeParams.get('field')
value: this.routeParams.get('search'),
field: <SearchField>this.routeParams.get('field')
};
this.sort = <SortField>this._routeParams.get('sort') || '-createdDate';
this.sort = <SortField>this.routeParams.get('sort') || '-createdDate';
}
ngOnInit() {
if (this._authService.isLoggedIn()) {
if (this.authService.isLoggedIn()) {
this.user = User.load();
}
@ -64,9 +64,9 @@ export class VideoListComponent implements OnInit {
let observable = null;
if (this.search.value !== null) {
observable = this._videoService.searchVideos(this.search, this.pagination, this.sort);
observable = this.videoService.searchVideos(this.search, this.pagination, this.sort);
} else {
observable = this._videoService.getVideos(this.pagination, this.sort);
observable = this.videoService.getVideos(this.pagination, this.sort);
}
observable.subscribe(
@ -79,7 +79,7 @@ export class VideoListComponent implements OnInit {
);
}
onRemoved(video: Video): void {
onRemoved(video: Video) {
this.videos.splice(this.videos.indexOf(video), 1);
}
@ -95,7 +95,7 @@ export class VideoListComponent implements OnInit {
params.field = this.search.field;
}
this._router.navigate(['VideosList', params]);
this.router.navigate(['VideosList', params]);
this.getVideos();
}
}

View File

@ -19,9 +19,9 @@ export class VideoMiniatureComponent {
@Input() video: Video;
@Input() user: User;
hovering: boolean = false;
hovering = false;
constructor(private _videoService: VideoService) {}
constructor(private videoService: VideoService) {}
onHover() {
this.hovering = true;
@ -31,13 +31,13 @@ export class VideoMiniatureComponent {
this.hovering = false;
}
displayRemoveIcon(): boolean {
displayRemoveIcon() {
return this.hovering && this.video.isRemovableBy(this.user);
}
removeVideo(id: string) {
if (confirm('Do you really want to remove this video?')) {
this._videoService.removeVideo(id).subscribe(
this.videoService.removeVideo(id).subscribe(
status => this.removed.emit(true),
error => alert(error)
);

View File

@ -26,7 +26,7 @@ export class VideoSortComponent {
return Object.keys(this.sortChoices);
}
getStringChoice(choiceKey: SortField): string {
getStringChoice(choiceKey: SortField) {
return this.sortChoices[choiceKey];
}

View File

@ -23,21 +23,21 @@ export class VideoWatchComponent implements OnInit, CanDeactivate {
numPeers: number;
loading: boolean = false;
private _interval: NodeJS.Timer;
private interval: NodeJS.Timer;
private client: any;
constructor(
private _videoService: VideoService,
private _routeParams: RouteParams,
private _elementRef: ElementRef
private videoService: VideoService,
private routeParams: RouteParams,
private elementRef: ElementRef
) {
// TODO: use a service
this.client = new WebTorrent({ dht: false });
}
ngOnInit() {
let id = this._routeParams.get('id');
this._videoService.getVideo(id).subscribe(
let id = this.routeParams.get('id');
this.videoService.getVideo(id).subscribe(
video => this.loadVideo(video),
error => alert(error)
);
@ -50,7 +50,7 @@ export class VideoWatchComponent implements OnInit, CanDeactivate {
this.client.add(this.video.magnetUri, (torrent) => {
this.loading = false;
console.log('Added ' + this.video.magnetUri + '.');
torrent.files[0].appendTo(this._elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
if (err) {
alert('Cannot append the file.');
console.error(err);
@ -58,7 +58,7 @@ export class VideoWatchComponent implements OnInit, CanDeactivate {
});
// Refresh each second
this._interval = setInterval(() => {
this.interval = setInterval(() => {
this.downloadSpeed = torrent.downloadSpeed;
this.uploadSpeed = torrent.uploadSpeed;
this.numPeers = torrent.numPeers;
@ -66,9 +66,9 @@ export class VideoWatchComponent implements OnInit, CanDeactivate {
});
}
routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) : any {
routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
console.log('Removing video from webtorrent.');
clearInterval(this._interval);
clearInterval(this.interval);
this.client.remove(this.video.magnetUri);
return true;
}

View File

@ -51,6 +51,23 @@
"use-pipe-transform-interface": true,
"pipe-naming": [true, "camelCase", "my"],
"component-class-suffix": true,
"directive-class-suffix": true
"directive-class-suffix": true,
"typedef-whitespace": [ true,
{
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
}
],
"whitespace": [ true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
]
}
}