diff --git a/app/app.component.ts b/app/app.component.ts index 450620b..dc913bf 100644 --- a/app/app.component.ts +++ b/app/app.component.ts @@ -1,19 +1,26 @@ import {Component} from "@angular/core"; +import {OnInit}from "@angular/core"; -import {InputareaComponent} from "./inputarea.component"; -// import {ConversionType} from "./conversiontype"; -//import { SelectorComponent } from "./selector.component"; +import {ConverterregistryService} from "./converterregistry.service"; +import {Converter} from "./converter"; @Component({ moduleId: module.id, selector: "den-app", templateUrl: "app.component.html", - styleUrls: ["app.component.css"] + styleUrls: ["app.component.css"], + providers: [ConverterregistryService] }) -export class AppComponent { - // private inputAreas:InputareaComponent[] = []; - // - // constructor() { - // this.inputAreas.push(new InputareaComponent()); - // } +export class AppComponent extends OnInit { + constructor(private converterregistryService:ConverterregistryService) { + super(); + } + + ngOnInit():void { + let converters:Converter[] = this.converterregistryService.getConverters(); + console.log("Number of registered converters: " + converters.length); + for (let i = 0; i < converters.length; i++) { + console.log("Converter " + converters[i].getId() + ": " + converters[i].getDisplayname()); + } + } } diff --git a/app/base64decoder.ts b/app/base64decoder.ts new file mode 100644 index 0000000..eba3379 --- /dev/null +++ b/app/base64decoder.ts @@ -0,0 +1,15 @@ +import {Converter} from './converter'; + +export class Base64Decoder implements Converter { + getDisplayname():string { + return "Decode Base 64"; + } + + getId():string { + return "base64decode"; + } + + convert(input:string):string { + return atob(input); + } +} diff --git a/app/base64encoder.ts b/app/base64encoder.ts new file mode 100644 index 0000000..bb9de8f --- /dev/null +++ b/app/base64encoder.ts @@ -0,0 +1,15 @@ +import {Converter} from './converter'; + +export class Base64Encoder implements Converter { + getDisplayname():string { + return "Encode Base 64"; + } + + getId():string { + return "base64encode"; + } + + convert(input:string):string { + return btoa(input); + } +} diff --git a/app/converter.ts b/app/converter.ts new file mode 100644 index 0000000..66706f9 --- /dev/null +++ b/app/converter.ts @@ -0,0 +1,5 @@ +export interface Converter { + getDisplayname():string; + getId():string; + convert(input:string):string; +} diff --git a/app/converterregistry.service.ts b/app/converterregistry.service.ts new file mode 100644 index 0000000..44ad46d --- /dev/null +++ b/app/converterregistry.service.ts @@ -0,0 +1,41 @@ +import {Injectable} from '@angular/core'; + +import {Converter} from './converter'; +import {Base64Encoder} from "./base64encoder"; +import {Base64Decoder} from "./base64decoder"; + +@Injectable() +export class ConverterregistryService { + private converters:Converter[] = []; + + constructor() { + this.init(); + } + + public getConverters():Converter[] { + return this.converters; + } + + public getConverter(id:string):Converter { + for (let i = 0; i < this.converters.length; i++) { + if (this.converters[i].getId() == id) { + return this.converters[i]; + } + } + return undefined; + } + + private init():void { + this.registerConverter(new Base64Encoder()); + this.registerConverter(new Base64Decoder()); + } + + private registerConverter(converter:Converter):void { + this.converters.forEach((c:Converter) => { + if (c.getId() == converter.getId()) { + throw new Error("Converter-ID " + converter.getId() + " is already registered!"); + } + }); + this.converters.push(converter); + } +} diff --git a/app/main.ts b/app/main.ts index af2e54a..244ca48 100644 --- a/app/main.ts +++ b/app/main.ts @@ -2,5 +2,4 @@ import {platformBrowserDynamic} from "@angular/platform-browser-dynamic"; import {AppModule} from "./app.module"; -const platform = platformBrowserDynamic(); -platform.bootstrapModule(AppModule); +platformBrowserDynamic().bootstrapModule(AppModule);