harbor/src/ui_ng/src/app/project/project.component.ts

159 lines
4.5 KiB
TypeScript
Raw Normal View History

import { Component, OnInit, ViewChild, OnDestroy } from '@angular/core';
2017-02-21 07:54:42 +01:00
import { Router } from '@angular/router';
2017-02-21 07:54:42 +01:00
2017-02-24 11:06:08 +01:00
import { Project } from './project';
import { ProjectService } from './project.service';
2017-02-21 07:54:42 +01:00
import { CreateProjectComponent } from './create-project/create-project.component';
2017-02-24 11:06:08 +01:00
import { ListProjectComponent } from './list-project/list-project.component';
import { MessageService } from '../global-message/message.service';
2017-02-28 05:46:15 +01:00
import { Message } from '../global-message/message';
2017-02-24 11:06:08 +01:00
2017-02-28 05:46:15 +01:00
import { AlertType } from '../shared/shared.const';
import { Response } from '@angular/http';
2017-02-24 11:06:08 +01:00
import { ConfirmationDialogService } from '../shared/confirmation-dialog/confirmation-dialog.service';
import { ConfirmationMessage } from '../shared/confirmation-dialog/confirmation-message';
import { ConfirmationTargets, ConfirmationState } from '../shared/shared.const';
2017-03-07 10:20:33 +01:00
import { Subscription } from 'rxjs/Subscription';
2017-03-13 11:20:45 +01:00
import { State } from 'clarity-angular';
2017-03-07 10:20:33 +01:00
const types: {} = { 0: 'PROJECT.MY_PROJECTS', 1: 'PROJECT.PUBLIC_PROJECTS' };
2017-03-07 10:20:33 +01:00
2017-02-21 07:54:42 +01:00
@Component({
moduleId: module.id,
selector: 'project',
templateUrl: 'project.component.html',
styleUrls: ['./project.component.css']
2017-02-21 07:54:42 +01:00
})
export class ProjectComponent implements OnInit, OnDestroy {
2017-02-24 11:06:08 +01:00
selected = [];
changedProjects: Project[];
projectTypes = types;
2017-02-24 11:06:08 +01:00
@ViewChild(CreateProjectComponent)
creationProject: CreateProjectComponent;
2017-02-21 07:54:42 +01:00
2017-02-24 11:06:08 +01:00
@ViewChild(ListProjectComponent)
listProject: ListProjectComponent;
2017-02-21 07:54:42 +01:00
2017-02-24 11:06:08 +01:00
currentFilteredType: number = 0;
2017-02-21 07:54:42 +01:00
2017-03-07 10:20:33 +01:00
subscription: Subscription;
2017-03-13 11:20:45 +01:00
projectName: string;
isPublic: number;
page: number = 1;
2017-03-22 13:54:22 +01:00
pageSize: number = 15;
2017-03-13 11:20:45 +01:00
totalPage: number;
totalRecordCount: number;
2017-03-07 10:20:33 +01:00
constructor(
private projectService: ProjectService,
private messageService: MessageService,
private deletionDialogService: ConfirmationDialogService) {
this.subscription = deletionDialogService.confirmationConfirm$.subscribe(message => {
if (message &&
message.state === ConfirmationState.CONFIRMED &&
message.source === ConfirmationTargets.PROJECT) {
let projectId = message.data;
this.projectService
.deleteProject(projectId)
.subscribe(
response => {
console.log('Successful delete project with ID:' + projectId);
this.retrieve();
},
error => this.messageService.announceMessage(error.status, error, AlertType.WARNING)
);
}
});
}
2017-02-21 07:54:42 +01:00
2017-02-24 11:06:08 +01:00
ngOnInit(): void {
2017-03-13 11:20:45 +01:00
this.projectName = '';
this.isPublic = 0;
2017-02-24 11:06:08 +01:00
}
2017-02-21 07:54:42 +01:00
ngOnDestroy(): void {
if (this.subscription) {
this.subscription.unsubscribe();
}
}
2017-03-13 11:20:45 +01:00
retrieve(state?: State): void {
if (state) {
2017-03-13 11:20:45 +01:00
this.page = state.page.to + 1;
}
2017-02-24 11:06:08 +01:00
this.projectService
.listProjects(this.projectName, this.isPublic, this.page, this.pageSize)
.subscribe(
response => {
this.totalRecordCount = response.headers.get('x-total-count');
this.totalPage = Math.ceil(this.totalRecordCount / this.pageSize);
console.log('TotalRecordCount:' + this.totalRecordCount + ', totalPage:' + this.totalPage);
this.changedProjects = response.json();
},
error => this.messageService.announceAppLevelMessage(error.status, error, AlertType.WARNING)
);
2017-02-24 11:06:08 +01:00
}
2017-02-21 07:54:42 +01:00
2017-02-24 11:06:08 +01:00
openModal(): void {
this.creationProject.newProject();
}
2017-02-24 11:06:08 +01:00
createProject(created: boolean) {
if (created) {
2017-03-13 11:20:45 +01:00
this.retrieve();
2017-02-21 07:54:42 +01:00
}
2017-02-24 11:06:08 +01:00
}
2017-02-21 07:54:42 +01:00
2017-02-24 11:06:08 +01:00
doSearchProjects(projectName: string): void {
console.log('Search for project name:' + projectName);
2017-03-13 11:20:45 +01:00
this.projectName = projectName;
this.retrieve();
2017-02-24 11:06:08 +01:00
}
2017-02-21 07:54:42 +01:00
2017-02-24 11:06:08 +01:00
doFilterProjects(filteredType: number): void {
console.log('Filter projects with type:' + types[filteredType]);
2017-03-13 11:20:45 +01:00
this.isPublic = filteredType;
this.currentFilteredType = filteredType;
2017-03-13 11:20:45 +01:00
this.retrieve();
2017-02-24 11:06:08 +01:00
}
2017-02-21 07:54:42 +01:00
2017-02-24 11:06:08 +01:00
toggleProject(p: Project) {
2017-03-07 10:20:33 +01:00
if (p) {
p.public === 0 ? p.public = 1 : p.public = 0;
this.projectService
2017-02-24 11:06:08 +01:00
.toggleProjectPublic(p.project_id, p.public)
.subscribe(
response => console.log('Successful toggled project_id:' + p.project_id),
error => this.messageService.announceMessage(error.status, error, AlertType.WARNING)
2017-02-24 11:06:08 +01:00
);
2017-03-07 10:20:33 +01:00
}
2017-02-24 11:06:08 +01:00
}
deleteProject(p: Project) {
let deletionMessage = new ConfirmationMessage(
2017-03-07 10:20:33 +01:00
'PROJECT.DELETION_TITLE',
'PROJECT.DELETION_SUMMARY',
p.name,
p.project_id,
ConfirmationTargets.PROJECT
2017-03-07 10:20:33 +01:00
);
this.deletionDialogService.openComfirmDialog(deletionMessage);
}
refresh(): void {
2017-03-13 11:20:45 +01:00
this.retrieve();
2017-02-24 11:06:08 +01:00
}
2017-02-21 07:54:42 +01:00
}