VTepL/src/Core.js

57 lines
1.6 KiB
JavaScript

class VTepLCore {
constructor({path, suffix, template, cache} = {}) {
this.templates = {};
this.dir = path || '/tpl/';
this.suffix = suffix || 'tpl';
this.path = template || `${this.dir}%s.${this.suffix}`;
this.cache = cache === undefined ? true : cache;
}
// Add download queue!
async loadTemplate(name) {
if (this.templates[name]) {
return null;
}
this.templates[name] = true; // small hack to prevent double loading :)
let path = this.path.replace('%s', name);
let data = await Network.requestUrl(path)
this.addTpl(name, data);
loader.addLoadedItems(1);
return null;
}
async loadArray(names) {
loader.addToLoadItem(names.length)
const promises = []
for (const name of names) {
promises.push(this.loadTemplate(name));
}
await Promise.all(promises)
}
addTpl(name, content) {
let temp = this.templates[name] = new VTepLTemplate(name, content, this);
temp.parseContent(this.cache);
}
async renderOn(name, data) {
if (this.templates[name]) {
return await this.templates[name].render(data);
}
PrettyConsole.warn("VTepLRender", `Template: "${name}" dont exist`);
return '';
}
renderTo(element, name, data = {}) {
this.renderOn(name, data).then(html => {
element.innerHTML = html;
})
}
async renderToAsync(element, name, data = {}) {
return this.renderOn(name, data).then(html => {
element.innerHTML = html;
})
}
}