converter/src/app/app.component.ts

61 lines
2.0 KiB
TypeScript

import {Component, OnInit} from '@angular/core';
import {ConverterRegistryService} from './converterregistry.service';
import {InputComponentManagerService} from './inputcomponentmanager.service';
import {NativeLibraryWrapperService} from './nativelibrarywrapper.service';
import {Step} from './step';
import {Converter} from './converter/converter';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
providers: [ConverterRegistryService, InputComponentManagerService, NativeLibraryWrapperService]
})
export class AppComponent implements OnInit {
public steps: Step[] = [];
public converters: Converter[] = [];
constructor(private converterRegistryService: ConverterRegistryService,
private inputComponentManagerService: InputComponentManagerService) {
}
convert(step: Step, $event: any): void {
step.selectedConverter = this.converterRegistryService.getConverter($event.target.selectedOptions[0].id);
this.update(step);
}
update(step: Step): void {
const converter: Converter = step.selectedConverter;
if (converter !== undefined) {
const content: string = step.content;
let result: string;
try {
result = converter.convert(content);
} catch (error) {
if (typeof console === 'object' && typeof console.log === 'function') {
console.log(error);
}
step.message = error.message;
step.error = true;
result = null;
}
if (result !== null) {
step.message = '';
step.error = false;
if (result !== '') {
const nextComponent: Step = this.inputComponentManagerService.getNext(step);
nextComponent.content = result;
this.update(nextComponent);
}
}
}
}
ngOnInit(): void {
this.converters = this.converterRegistryService.getAllConverters();
this.steps = this.inputComponentManagerService.getAllComponents();
this.inputComponentManagerService.getFirst();
}
}