@nent/core
Version:
122 lines (118 loc) • 4.9 kB
JavaScript
/*!
* NENT 2022
*/
;
const expressions = require('./expressions-531d82f5.js');
const tokens = require('./tokens-4f8bcd42.js');
/* istanbul ignore file */
/**
* It resolves all the child elements of the given element that have an x-* attribute
* @param {HTMLElement} element - The element to resolve the attributes for.
*/
function resolveChildElementXAttributes(element) {
resolveChildXHideWhenAttributes(element);
resolveChildXShowWhenAttributes(element);
resolveChildXClassWhenAttributes(element);
resolveChildXValueFromAttributes(element);
}
/**
* It finds all the elements with the `n-hide-when` attribute and resolves them
* @param {Element} element - The element to resolve the attribute on.
*/
function resolveChildXHideWhenAttributes(element) {
element.querySelectorAll('[n-hide-when]').forEach(async (el) => {
await resolveXHideWhenAttribute(el);
});
}
/**
* It evaluates the expression in the `n-hide-when` attribute and sets the `hidden` attribute on the
* element if the expression evaluates to `true`
* @param {Element} element - The element to resolve the attribute on.
* @returns A function that takes an element as an argument.
*/
async function resolveXHideWhenAttribute(element) {
const expression = element.getAttribute('n-hide-when');
if (!expression)
return;
const hide = await expressions.evaluatePredicate(expression);
element.toggleAttribute('hidden', hide);
}
/**
* It resolves the `n-show-when` attribute of all child elements of the given element
* @param {Element} element - The element to resolve the n-show-when attribute on.
*/
function resolveChildXShowWhenAttributes(element) {
element.querySelectorAll('[n-show-when]').forEach(async (el) => {
await resolveXShowWhenAttribute(el);
});
}
/**
* It evaluates the expression in the `n-show-when` attribute and sets the `hidden` attribute on the
* element if the expression evaluates to `false`
* @param {Element} element - The element that has the n-show-when attribute.
* @returns A function that takes an element as an argument.
*/
async function resolveXShowWhenAttribute(element) {
const expression = element.getAttribute('n-show-when');
if (!expression)
return;
const show = await expressions.evaluatePredicate(expression);
element.toggleAttribute('hidden', !show);
}
/**
* It resolves the `n-class-when` attribute on all child elements of the given element
* @param {Element} element - The element to resolve the attribute on.
*/
function resolveChildXClassWhenAttributes(element) {
element.querySelectorAll('[n-class-when]').forEach(async (el) => {
await resolveXClassWhenAttribute(el);
});
}
/**
* It evaluates the expression in the `n-class-when` attribute, and if the result is truthy, it adds
* the class name in the `n-class` attribute to the element
* @param {Element} element - The element that the attribute is on.
* @returns A function that takes an element as an argument.
*/
async function resolveXClassWhenAttribute(element) {
const expression = element.getAttribute('n-class-when');
const className = element.getAttribute('n-class');
if (!expression || !className)
return;
const when = await expressions.evaluatePredicate(expression);
element.classList.toggle(className, when);
}
/**
* It finds all elements with the `n-value-from` attribute and calls the `resolveXValueFromAttribute`
* function on each of them.
* @param {Element} element - The element to resolve the attribute on.
*/
function resolveChildXValueFromAttributes(element) {
element.querySelectorAll('[n-value-from]').forEach(async (el) => {
await resolveXValueFromAttribute(el);
});
}
/**
* It takes an element, checks if it has an attribute called `n-value-from`, and if it does, it
* resolves the tokens in the attribute's value and sets the element's `value` attribute to the
* resolved value
* @param {Element} element - The element that we're resolving the value for.
*/
async function resolveXValueFromAttribute(element) {
const expression = element.getAttribute('n-value-from');
if (expression && tokens.hasToken(expression)) {
const value = await tokens.resolveTokens(expression);
if (value) {
element.setAttribute('value', value);
}
}
}
exports.resolveChildElementXAttributes = resolveChildElementXAttributes;
exports.resolveChildXClassWhenAttributes = resolveChildXClassWhenAttributes;
exports.resolveChildXHideWhenAttributes = resolveChildXHideWhenAttributes;
exports.resolveChildXShowWhenAttributes = resolveChildXShowWhenAttributes;
exports.resolveChildXValueFromAttributes = resolveChildXValueFromAttributes;
exports.resolveXClassWhenAttribute = resolveXClassWhenAttribute;
exports.resolveXHideWhenAttribute = resolveXHideWhenAttribute;
exports.resolveXShowWhenAttribute = resolveXShowWhenAttribute;
exports.resolveXValueFromAttribute = resolveXValueFromAttribute;