vee-dom
Version:
Small and easy to use virtual dom library
62 lines (46 loc) • 1.25 kB
JavaScript
;
const Element = require('./element');
const TypeMismatchException = require('../exceptions/type-mismatch');
class DomRoot {
constructor(rootHtmlElement, elements) {
let self = this;
self.rootHtmlElement = rootHtmlElement;
if (typeof elements != 'undefined') {
if (!Array.isArray(elements)) {
elements = [ elements ];
}
elements.forEach(self.addElement);
}
}
addElement(element) {
if (!(element instanceof Element)) {
throw new TypeMismatchException('The given object is not instace of the \'Element\' class!', element);
}
if (typeof this.elements == 'undefined') {
this.elements = [];
}
this.elements.push(element);
}
removeElement(element) {
if (!(element instanceof Element)) {
throw new TypeMismatchException('The given object is not instace of the \'Element\' class!', element);
}
this.elements = this.elements.filter(function(el) {
return el.uniqueElementId !== element.uniqueElementId;
});
}
attach() {
let self = this;
self.elements.forEach(function(el) {
el.parentElement = self.rootHtmlElement;
el.attach();
});
}
detach() {
let self = this;
self.elements.forEach(function(el) {
el.detach();
});
}
}
module.exports = DomRoot;