1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-06-01 20:41:36 +00:00
8bitworkshop/src/windows.ts

91 lines
2.2 KiB
TypeScript
Raw Normal View History

"use strict";
import $ = require("jquery");
2018-07-08 14:07:19 +00:00
import { CodeProject } from "./project";
import { WorkerError } from "./workertypes";
2018-07-07 14:55:27 +00:00
import { ProjectView } from "./views";
type WindowCreateFunction = (id:string) => ProjectView;
2018-07-08 03:10:51 +00:00
export class ProjectWindows {
containerdiv:HTMLElement;
project:CodeProject;
id2window : {[id:string]:ProjectView} = {};
id2createfn : {[id:string]:WindowCreateFunction} = {};
id2div : {[id:string]:HTMLElement} = {};
activewnd : ProjectView;
activediv : HTMLElement;
lasterrors : WorkerError[];
constructor(containerdiv:HTMLElement, project:CodeProject) {
this.containerdiv = containerdiv;
this.project = project;
}
// TODO: delete windows ever?
2018-07-08 03:10:51 +00:00
setCreateFunc(id:string, createfn:WindowCreateFunction) {
this.id2createfn[id] = createfn;
}
2018-07-08 03:10:51 +00:00
createOrShow(id:string) {
var wnd = this.id2window[id];
if (!wnd) {
2018-07-08 03:10:51 +00:00
wnd = this.id2window[id] = this.id2createfn[id](id);
}
2018-07-08 03:10:51 +00:00
var div = this.id2div[id];
if (!div) {
2018-07-08 03:10:51 +00:00
var data = this.project.getFile(id)+""; // TODO: binary files
div = this.id2div[id] = wnd.createDiv(this.containerdiv, data);
}
2018-07-08 03:10:51 +00:00
if (this.activewnd != wnd) {
if (this.activediv)
$(this.activediv).hide();
this.activediv = div;
this.activewnd = wnd;
$(div).show();
this.refresh();
this.refreshErrors();
}
return wnd;
}
2018-07-08 03:10:51 +00:00
put(id:string, window:ProjectView) {
this.id2window[id] = window;
}
2018-07-08 03:10:51 +00:00
refresh() {
if (this.activewnd && this.activewnd.refresh)
this.activewnd.refresh();
}
2018-07-08 03:10:51 +00:00
tick() {
if (this.activewnd && this.activewnd.tick)
this.activewnd.tick();
}
2018-07-08 03:10:51 +00:00
setErrors(errors:WorkerError[]) {
this.lasterrors = errors;
this.refreshErrors();
}
2018-07-08 03:10:51 +00:00
refreshErrors() {
if (this.activewnd && this.activewnd.markErrors) {
if (this.lasterrors && this.lasterrors.length)
this.activewnd.markErrors(this.lasterrors);
else
2018-07-08 03:10:51 +00:00
this.activewnd.clearErrors();
}
}
2018-07-08 03:10:51 +00:00
getActive() : ProjectView { return this.activewnd; }
2018-07-08 03:10:51 +00:00
getCurrentText() : string {
if (this.activewnd && this.activewnd.getValue)
return this.activewnd.getValue();
else
alert("Please switch to an editor window.");
}
};