added the ConverterregistryService and two converters (base64 de- and

encoder)
This commit is contained in:
Manuel Friedli 2016-09-15 20:43:37 +02:00
parent dd4ffd9989
commit b30c698546
6 changed files with 94 additions and 12 deletions

View File

@ -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());
}
}
}

15
app/base64decoder.ts Normal file
View File

@ -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);
}
}

15
app/base64encoder.ts Normal file
View File

@ -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);
}
}

5
app/converter.ts Normal file
View File

@ -0,0 +1,5 @@
export interface Converter {
getDisplayname():string;
getId():string;
convert(input:string):string;
}

View File

@ -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);
}
}

View File

@ -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);