I am building a desktop app using node+electron. I needed a way to containerize my UI elements in the DOM.
I came up with this solution that can be extended by other Typescript DOM elements.
My code runs effectively. Is this an appropriate use of Typescript?
Am I rewriting a library that someone else has already written? I didn't use React because it doesn't allow object scoped global variables.
Is it bad form to parse HTML on the front end?
class Glyph {
//field
uuid:string;
//constructor
constructor(parent:string, html:string) {
this.uuid = this.guid();
var regex = /(<([^>]+>))/ig
var tags = html.match(regex);
var position = tags[0].search(/>/ig);
tags[0] = [tags[0].slice(0, position), ' id="'+ this.uuid +'"', tags[0].slice(position)].join('');
html = tags.join('');
//appends child to the provided parent id
document.getElementById(parent).innerHTML += html;
}
guid():string {
return this.s4() + this.s4() + '-' + this.s4() + '-' + this.s4() + '-' +
this.s4() + '-' + this.s4() + this.s4() + this.s4();
}
s4():string {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
replaceElement(html):void {
var currentElement = document.getElementById(this.uuid);
currentElement.outerHTML = html;
}
deleteElement():void {
var child = document.getElementById(this.uuid);
var parent = document.getElementById(this.uuid).parentElement;
parent.removeChild(child);
}
}