Fix command callback delegation

This commit is contained in:
Kristóf Tóth 2018-02-15 16:22:11 +01:00
parent aceb544496
commit 735ea8d4e0

View File

@ -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<SourceCode>(this.anchor_id).subscribe((event) => {
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;
}
if (event.data.command === 'write') { this.saveButtonState = 'SAVED'; }
if (event.data.command === 'reload') { this.requestCode(); }
selectHandler(event: any) {
this.updateFileData(event);
}
this.changeDetectorRef.detectChanges();
});
reloadHandler(event: any) {
this.requestCode();
}
readHandler(event: any) {
this.updateFileData(event);
}
writeHandler(event: any) {
this.saveButtonState = 'SAVED';
}
setButtonStateDirty() {
setTimeout(() => {this.saveButtonState = 'DIRTY'; }, 0);
}