Code cleaned up

This commit is contained in:
Piotr Pekala
2019-11-13 07:30:06 -08:00
parent ce97ba085d
commit b6d74c58cb
8 changed files with 80 additions and 53 deletions

View File

@ -249,6 +249,7 @@ import { PageNotFoundComponent } from './components/page-not-found/page-not-foun
import { AlignHorizontallyActionComponent } from './components/project-map/context-menu/actions/align-horizontally/align-horizontally.component'; import { AlignHorizontallyActionComponent } from './components/project-map/context-menu/actions/align-horizontally/align-horizontally.component';
import { AlignVerticallyActionComponent } from './components/project-map/context-menu/actions/align_vertically/align-vertically.component'; import { AlignVerticallyActionComponent } from './components/project-map/context-menu/actions/align_vertically/align-vertically.component';
import { ConfirmationBottomSheetComponent } from './components/projects/confirmation-bottomsheet/confirmation-bottomsheet.component'; import { ConfirmationBottomSheetComponent } from './components/projects/confirmation-bottomsheet/confirmation-bottomsheet.component';
import { TemplateFilter } from './filters/templateFilter.pipe';
if (environment.production) { if (environment.production) {
Raven.config('https://b2b1cfd9b043491eb6b566fd8acee358@sentry.io/842726', { Raven.config('https://b2b1cfd9b043491eb6b566fd8acee358@sentry.io/842726', {
@ -362,6 +363,7 @@ if (environment.production) {
SearchFilter, SearchFilter,
DateFilter, DateFilter,
NameFilter, NameFilter,
TemplateFilter,
ProjectsFilter, ProjectsFilter,
ListOfSnapshotsComponent, ListOfSnapshotsComponent,
CustomAdaptersComponent, CustomAdaptersComponent,

View File

@ -112,7 +112,7 @@
</mat-menu> </mat-menu>
<mat-toolbar-row *ngIf="!readonly"> <mat-toolbar-row *ngIf="!readonly">
<app-template [server]="server" (onNodeCreation)="onNodeCreation($event)"></app-template> <app-template [server]="server" [project]="project" (onNodeCreation)="onNodeCreation($event)"></app-template>
</mat-toolbar-row> </mat-toolbar-row>
<mat-toolbar-row *ngIf="!readonly"> <mat-toolbar-row *ngIf="!readonly">

View File

@ -63,6 +63,7 @@ import { EthernetLinkWidget } from '../../cartography/widgets/links/ethernet-lin
import { SerialLinkWidget } from '../../cartography/widgets/links/serial-link'; import { SerialLinkWidget } from '../../cartography/widgets/links/serial-link';
import { NavigationDialogComponent } from '../projects/navigation-dialog/navigation-dialog.component'; import { NavigationDialogComponent } from '../projects/navigation-dialog/navigation-dialog.component';
import { ConfirmationBottomSheetComponent } from '../projects/confirmation-bottomsheet/confirmation-bottomsheet.component'; import { ConfirmationBottomSheetComponent } from '../projects/confirmation-bottomsheet/confirmation-bottomsheet.component';
import { NodeAddedEvent } from '../template/template-list-dialog/template-list-dialog.component';
@Component({ @Component({
@ -406,14 +407,13 @@ export class ProjectMapComponent implements OnInit, OnDestroy {
this.subscriptions.push(onInterfaceLabelContextMenu); this.subscriptions.push(onInterfaceLabelContextMenu);
this.mapChangeDetectorRef.detectChanges(); this.mapChangeDetectorRef.detectChanges();
} }
//here we should support adding multiple nodes on single click onNodeCreation(nodeAddedEvent: NodeAddedEvent) {
onNodeCreation(template: Template) { if(!nodeAddedEvent) {
if(!template) {
return; return;
} }
this.nodeService.createFromTemplate(this.server, this.project, template, 0, 0, 'local').subscribe(() => { this.nodeService.createFromTemplate(this.server, this.project, nodeAddedEvent.template, nodeAddedEvent.x, nodeAddedEvent.y, 'local').subscribe(() => {
this.projectService.nodes(this.server, this.project.project_id).subscribe((nodes: Node[]) => { this.projectService.nodes(this.server, this.project.project_id).subscribe((nodes: Node[]) => {
nodes.filter((node) => node.label.style === null).forEach((node) => { nodes.filter((node) => node.label.style === null).forEach((node) => {

View File

@ -7,7 +7,6 @@
<h6>Template</h6> <h6>Template</h6>
</div> </div>
<mat-form-field class="form-field" floatPlaceholder="never"> <mat-form-field class="form-field" floatPlaceholder="never">
<!-- <input matInput #filter placeholder="Filter templates by name" /> -->
<input matInput <input matInput
placeholder="Search by name" placeholder="Search by name"
[(ngModel)]="searchText" [(ngModel)]="searchText"
@ -30,21 +29,11 @@
placeholder="Choose template" placeholder="Choose template"
(selectionChange)="chooseTemplate($event)" (selectionChange)="chooseTemplate($event)"
[(ngModel)]="selectedTemplate"> [(ngModel)]="selectedTemplate">
<mat-option *ngFor="let template of templates" [value]="template.name"> <mat-option *ngFor="let template of filteredTemplates | templatefilter: searchText" [value]="template">
{{template.name}} {{template.name}}
</mat-option> </mat-option>
</mat-select> </mat-select>
</mat-form-field> </mat-form-field>
<!-- <mat-table class="mat-table" #table [dataSource]="dataSource">
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
<mat-cell *matCellDef="let row">
<a (click)="addNode(row)" href="javascript:void(0);" class="table-link">{{ row.name }}</a>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
</mat-table> -->
<div class="title-container"> <div class="title-container">
<h6>Configuration</h6> <h6>Configuration</h6>

View File

@ -1,14 +1,14 @@
import { Component, ElementRef, Inject, OnInit, ViewChild } from '@angular/core'; import { Component, ElementRef, Inject, OnInit, ViewChild } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material'; import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
import { DataSource } from '@angular/cdk/collections'; import { DataSource } from '@angular/cdk/collections';
import { Observable, BehaviorSubject, fromEvent, merge } from 'rxjs'; import { Observable, BehaviorSubject, fromEvent, merge } from 'rxjs';
import { debounceTime, distinctUntilChanged, map } from 'rxjs/operators'; import { debounceTime, distinctUntilChanged, map } from 'rxjs/operators';
import { Server } from '../../../models/server'; import { Server } from '../../../models/server';
import { TemplateService } from '../../../services/template.service'; import { TemplateService } from '../../../services/template.service';
import { Template } from '../../../models/template'; import { Template } from '../../../models/template';
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms'; import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';
import { ToasterService } from '../../../services/toaster.service';
import { Project } from '../../../models/project';
@Component({ @Component({
selector: 'app-template-list-dialog', selector: 'app-template-list-dialog',
@ -17,25 +17,25 @@ import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms'
}) })
export class TemplateListDialogComponent implements OnInit { export class TemplateListDialogComponent implements OnInit {
server: Server; server: Server;
templateDatabase: TemplateDatabase; project: Project;
dataSource: TemplateDataSource;
displayedColumns = ['name'];
templateTypes: string[] = ['cloud', 'ethernet_hub', 'ethernet_switch', 'docker', 'dynamips', 'vpcs', 'traceng', 'virtualbox', 'vmware', 'iou', 'qemu']; templateTypes: string[] = ['cloud', 'ethernet_hub', 'ethernet_switch', 'docker', 'dynamips', 'vpcs', 'traceng', 'virtualbox', 'vmware', 'iou', 'qemu'];
selectedType: string; selectedType: string;
configurationForm: FormGroup; configurationForm: FormGroup;
positionForm: FormGroup; positionForm: FormGroup;
templates: Template[]; templates: Template[];
searchText: string; filteredTemplates: Template[];
selectedTemplate: Template;
@ViewChild('filter', {static: true}) filter: ElementRef; searchText: string = '';
constructor( constructor(
public dialogRef: MatDialogRef<TemplateListDialogComponent>, public dialogRef: MatDialogRef<TemplateListDialogComponent>,
private templateService: TemplateService, private templateService: TemplateService,
private formBuilder: FormBuilder, private formBuilder: FormBuilder,
@Inject(MAT_DIALOG_DATA) public data: any @Inject(MAT_DIALOG_DATA) public data: any,
private toasterService: ToasterService
) { ) {
this.server = data['server']; this.server = data['server'];
this.project = data['project'];
this.configurationForm = this.formBuilder.group({ this.configurationForm = this.formBuilder.group({
name: new FormControl('new node', Validators.required), name: new FormControl('new node', Validators.required),
numberOfNodes: new FormControl(1, Validators.required) numberOfNodes: new FormControl(1, Validators.required)
@ -47,46 +47,60 @@ export class TemplateListDialogComponent implements OnInit {
} }
ngOnInit() { ngOnInit() {
// this.templateDatabase = new TemplateDatabase(this.server, this.templateService);
// this.dataSource = new TemplateDataSource(this.templateDatabase);
this.templateService.list(this.server).subscribe((listOfTemplates: Template[]) => { this.templateService.list(this.server).subscribe((listOfTemplates: Template[]) => {
this.filteredTemplates = listOfTemplates;
this.templates = listOfTemplates; this.templates = listOfTemplates;
}); });
// fromEvent(this.filter.nativeElement, 'keyup')
// .pipe(
// debounceTime(150),
// distinctUntilChanged()
// )
// .subscribe(() => {
// if (!this.dataSource) {
// return;
// }
// this.dataSource.filter = this.filter.nativeElement.value;
// });
} }
onNoClick(): void { onNoClick(): void {
this.dialogRef.close(); this.dialogRef.close();
} }
addNode(template: Template): void {
this.dialogRef.close(template);
}
filterTemplates(event) { filterTemplates(event) {
console.log('filter event ', event); let temporaryTemplates = this.templates.filter( item => {
return item.name.toLowerCase().includes(this.searchText.toLowerCase());
});
this.filteredTemplates = temporaryTemplates.filter(t => t.template_type === event.value.toString());
} }
chooseTemplate(event) { chooseTemplate(event) {
console.log('choose event ', event); this.selectedTemplate = event.value;
this.configurationForm.controls['name'].setValue(this.selectedTemplate.default_name_format);
} }
onAddClick(): void { onAddClick(): void {
if (!this.selectedTemplate) {
this.toasterService.error('Please firstly choose template.');
} else if (!this.positionForm.valid || !this.configurationForm.valid) {
this.toasterService.error('Please fill all required fields.');
} else {
let x: number = this.positionForm.get('left').value;
let y: number = this.positionForm.get('top').value;
if (x>(this.project.scene_width/2) || x<-(this.project.scene_width/2) || y>(this.project.scene_height/2) || y<-(this.project.scene_height)) {
this.toasterService.error('Please set correct position values.')
} else {
let event: NodeAddedEvent = {
template: this.selectedTemplate,
name: this.configurationForm.get('name').value,
numberOfNodes: this.configurationForm.get('numberOfNodes').value,
x: x,
y: y
};
this.dialogRef.close(event);
}
}
} }
} }
export interface NodeAddedEvent {
template: Template,
name: string,
numberOfNodes: number;
x: number;
y: number;
}
export class TemplateDatabase { export class TemplateDatabase {
dataChange: BehaviorSubject<Template[]> = new BehaviorSubject<Template[]>([]); dataChange: BehaviorSubject<Template[]> = new BehaviorSubject<Template[]>([]);

View File

@ -1,9 +1,10 @@
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { MatDialog } from '@angular/material'; import { MatDialog } from '@angular/material';
import { TemplateListDialogComponent } from './template-list-dialog/template-list-dialog.component'; import { TemplateListDialogComponent, NodeAddedEvent } from './template-list-dialog/template-list-dialog.component';
import { Server } from '../../models/server'; import { Server } from '../../models/server';
import { Template } from '../../models/template'; import { Template } from '../../models/template';
import { Project } from '../../models/project';
@Component({ @Component({
selector: 'app-template', selector: 'app-template',
@ -12,6 +13,7 @@ import { Template } from '../../models/template';
}) })
export class TemplateComponent implements OnInit { export class TemplateComponent implements OnInit {
@Input() server: Server; @Input() server: Server;
@Input() project: Project;
@Output() onNodeCreation = new EventEmitter<any>(); @Output() onNodeCreation = new EventEmitter<any>();
constructor(private dialog: MatDialog) {} constructor(private dialog: MatDialog) {}
@ -22,14 +24,15 @@ export class TemplateComponent implements OnInit {
const dialogRef = this.dialog.open(TemplateListDialogComponent, { const dialogRef = this.dialog.open(TemplateListDialogComponent, {
width: '600px', width: '600px',
data: { data: {
server: this.server server: this.server,
project: this.project
}, },
autoFocus: false autoFocus: false
}); });
dialogRef.afterClosed().subscribe((template: Template) => { dialogRef.afterClosed().subscribe((nodeAddedEvent: NodeAddedEvent) => {
if (template !== null) { if (nodeAddedEvent !== null) {
this.onNodeCreation.emit(template); this.onNodeCreation.emit(nodeAddedEvent);
} }
}); });
} }

View File

@ -0,0 +1,18 @@
import { Pipe, PipeTransform } from '@angular/core';
import { Template } from '../models/template';
@Pipe({
name: 'templatefilter'
})
export class TemplateFilter implements PipeTransform {
transform(items: Template[], searchText: string): any[] {
if(!items) return [];
if(!searchText) return items;
searchText = searchText.toLowerCase();
return items.filter( item => {
return item.name.toLowerCase().includes(searchText);
});
}
}

View File

@ -7,4 +7,5 @@ export class Template {
name: string; name: string;
node_type: string; node_type: string;
symbol: string; symbol: string;
template_type: string;
} }