forked from MapComplete/MapComplete
Further cleanup: further removal of the UIElement
This commit is contained in:
parent
09ba1b37c6
commit
785f57262e
15 changed files with 169 additions and 294 deletions
|
@ -1,37 +1,7 @@
|
|||
import {UIEventSource} from "../Logic/UIEventSource";
|
||||
import BaseUIElement from "./BaseUIElement";
|
||||
|
||||
export abstract class UIElement extends BaseUIElement{
|
||||
|
||||
private static nextId: number = 0;
|
||||
public readonly id: string;
|
||||
public readonly _source: UIEventSource<any>;
|
||||
|
||||
private lastInnerRender: string;
|
||||
|
||||
protected constructor(source: UIEventSource<any> = undefined) {
|
||||
super()
|
||||
this.id = `ui-${this.constructor.name}-${UIElement.nextId}`;
|
||||
this._source = source;
|
||||
UIElement.nextId++;
|
||||
this.ListenTo(source);
|
||||
}
|
||||
|
||||
public ListenTo(source: UIEventSource<any>) {
|
||||
if (source === undefined) {
|
||||
return this;
|
||||
}
|
||||
//console.trace("Got a listenTo in ", this.constructor.name)
|
||||
const self = this;
|
||||
source.addCallback(() => {
|
||||
self.lastInnerRender = undefined;
|
||||
if(self._constructedHtmlElement !== undefined){
|
||||
self.UpdateElement(self._constructedHtmlElement);
|
||||
}
|
||||
|
||||
})
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be overridden for specific HTML functionality
|
||||
|
@ -54,35 +24,7 @@ export abstract class UIElement extends BaseUIElement{
|
|||
}
|
||||
return el;
|
||||
}
|
||||
|
||||
protected UpdateElement(el: HTMLElement) : void{
|
||||
const innerRender = this.InnerRender();
|
||||
|
||||
if (typeof innerRender === "string") {
|
||||
if(el.innerHTML !== innerRender){
|
||||
el.innerHTML = innerRender
|
||||
}
|
||||
} else {
|
||||
const subElement = innerRender.ConstructElement();
|
||||
if(el.children.length === 1 && el.children[0] === subElement){
|
||||
return; // Nothing changed
|
||||
}
|
||||
|
||||
while (el.firstChild) {
|
||||
el.removeChild(el.firstChild);
|
||||
}
|
||||
|
||||
if (subElement === undefined) {
|
||||
return;
|
||||
}
|
||||
el.appendChild(subElement)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated The method should not be used
|
||||
*/
|
||||
|
||||
protected abstract InnerRender(): string | BaseUIElement;
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue