pragma-views2
Version:
90 lines (76 loc) • 3.55 kB
JavaScript
export async function parseElements(elements, callerObj, bindingContext) {
return new Promise(resolve => {
const promises = [];
const itterator = Array.from(elements);
for (let element of itterator) {
promises.push(parseElement(element, callerObj, bindingContext));
}
Promise.all(promises).then(() => resolve());
}).catch(error => console.error(error));
}
export async function parseElement(element, callerObj, bindingContext) {
return new Promise(resolve => {
const promises = [];
if (element.attributes != undefined) {
if (element.attributes.length > 0) {
promises.push(parseAttributes(element.attributes, callerObj, bindingContext));
}
}
if (element.innerText != undefined && element.innerText.length > 0) {
promises.push(parseInnerText(element, callerObj, bindingContext));
}
if (element.children.length > 0) {
promises.push(parseElements(element.children, callerObj, bindingContext));
}
Promise.all(promises).then(() => resolve());
}).catch(error => console.error(error));
}
const allowedAttr = ["repeat", "behaviours"];
export async function parseAttributes(attributes, callerObj, bindingContext) {
return new Promise(resolve => {
const promises = [];
const collection = Array.from(attributes).filter(item => item.name.indexOf(".") > -1 || allowedAttr.indexOf(item.name) != -1 || item.nodeValue.indexOf("#") != -1);
for (let attribute of collection) {
promises.push(parseAttribute(attribute, callerObj, bindingContext));
}
Promise.all(promises).then(() => resolve());
}).catch(error => console.error(error));
}
export async function parseAttribute(attribute, callerObj, bindingContext) {
return new Promise(resolve => {
if (allowedAttr.indexOf(attribute.name) != -1) {
performCallbackForAttribute(callerObj, attribute, attribute.name, attribute.value, bindingContext);
resolve();
}
if (attribute.nodeValue.indexOf("#") != -1) {
performCallbackForAttribute(callerObj, attribute, "bind", attribute.name, bindingContext);
resolve();
}
else {
const index = attribute.name.indexOf(".");
if (index != -1) {
const bin = attribute.name.split(".");
performCallbackForAttribute(callerObj, attribute, bin[1], bin[0], bindingContext);
resolve();
}
}
}).catch(error => console.error(error));
}
function performCallbackForAttribute(callerObj, attribute, fn, prop, bindingContext) {
if (callerObj[fn] != undefined) {
callerObj[fn](prop, attribute, bindingContext);
}
}
export async function parseInnerText(element, callerObj, bindingContext) {
return new Promise(async resolve => {
if (element.children.length > 0 || element.innerText == undefined) return resolve();
if ((element.innerText.indexOf("${") == -1) && (element.innerText.indexOf("#") == -1)) return resolve();
const attr = {
ownerElement: element,
name: "innerhtml",
nodeValue: element.innerText.trim()
};
performCallbackForAttribute(callerObj, attr, "bind", "innerhtml", bindingContext);
resolve();
}).catch(error => console.error(error));
}