From 735ea8d4e092aa822bf04e9d26778b7dc09d3d59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 15 Feb 2018 16:22:11 +0100 Subject: [PATCH] Fix command callback delegation --- src/app/webide/webide.component.ts | 40 ++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/app/webide/webide.component.ts b/src/app/webide/webide.component.ts index 10ca71b..07f7f6e 100644 --- a/src/app/webide/webide.component.ts +++ b/src/app/webide/webide.component.ts @@ -32,25 +32,49 @@ export class WebideComponent implements OnInit { theme = 'monokai'; files: string[]; saveButtonState = 'DIRTY'; + command_handlers = { 'reload': this.reloadHandler.bind(this), + 'read': this.readHandler.bind(this), + 'select': this.selectHandler.bind(this), + 'write': this.writeHandler.bind(this)}; constructor(private webSocketService: WebSocketService, private changeDetectorRef: ChangeDetectorRef) { } ngOnInit() { + this.subscribeWS(); + this.requestCode(); + } + + subscribeWS() { this.webSocketService.observeAnchor(this.anchor_id).subscribe((event) => { - this.filename = event.data.filename; - this.code = event.data.content; - this.language = modelist.getModeForPath(this.filename).name; - this.files = event.data.files; - - if (event.data.command === 'write') { this.saveButtonState = 'SAVED'; } - if (event.data.command === 'reload') { this.requestCode(); } - + this.command_handlers[event.data.command](event); this.changeDetectorRef.detectChanges(); }); + } + + updateFileData(event: any) { + this.filename = event.data.filename; + this.code = event.data.content; + this.language = modelist.getModeForPath(this.filename).name; + this.files = event.data.files; + } + + selectHandler(event: any) { + this.updateFileData(event); + } + + reloadHandler(event: any) { this.requestCode(); } + readHandler(event: any) { + this.updateFileData(event); + } + + writeHandler(event: any) { + this.saveButtonState = 'SAVED'; + } + setButtonStateDirty() { setTimeout(() => {this.saveButtonState = 'DIRTY'; }, 0); }