Merge pull request #7 from avatao-content/reload_fix

Fix command callback delegation
This commit is contained in:
Bokros Bálint 2018-02-15 17:19:22 +01:00 committed by GitHub
commit 95d616225f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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