taggedjs
Version:
tagged template reactive html
115 lines • 5.36 kB
JavaScript
// taggedjs-no-compile
import { specialAttribute } from '../../interpolations/attributes/specialAttribute.js';
import { isFunction } from '../../isInstance.js';
import { bindSubjectCallback } from '../../interpolations/attributes/bindSubjectCallback.function.js';
import { paintContent } from '../paint.function.js';
import { processNonDynamicAttr } from '../../interpolations/attributes/processNameValueAttribute.function.js';
import { getNewContext } from '../addOneContext.function.js';
import { processAttributeFunction } from '../../interpolations/attributes/processAttributeCallback.function.js';
import { createDynamicArrayAttribute, createDynamicAttribute } from './createDynamicAttribute.function.js';
import { getTagJsTag } from './getTagJsTag.function.js';
import { processStandAloneAttribute } from './processStandAloneAttribute.function.js';
import { processTagJsTagAttribute } from './processTagJsAttribute.function.js';
/** MAIN FUNCTION. Sets attribute value, subscribes to value updates */
export function processAttribute(attrName, value, values, // all the variables inside html``
element, support, howToSet, // = howToSetInputValue
contexts, parentContext, isSpecial) {
// TODO: Next 3 lines might be obsolete
const varIndex = getTagJsTag(attrName);
let isNameVar = varIndex >= 0 || (value === undefined && typeof (attrName) !== 'string');
let valueInValues = values[varIndex];
// value or name from bolt?
if (value?.tagJsType) {
valueInValues = value; // the value is a TagJsTag
}
else if (attrName?.tagJsType) {
isNameVar = true;
valueInValues = attrName; // the name is a TagJsTag
value = attrName;
}
else if (typeof attrName === 'function') {
isNameVar = true;
valueInValues = attrName; // the name is a TagJsTag
value = attrName;
}
const tagJsVar = valueInValues;
if (tagJsVar?.tagJsType) {
return processTagJsTagAttribute(value,
// [], // contexts,
contexts, parentContext, tagJsVar, varIndex, support, attrName, element, isNameVar);
}
if (isNameVar) {
// old way of setting by html``
if (varIndex === -1 && isNameVar) {
valueInValues = attrName; // its a name only value attribute
}
const contextItem = getNewContext(valueInValues, [], // contexts,
true, parentContext);
contextItem.description = 'processAttribute';
contextItem.isAttr = true;
contextItem.target = element;
contextItem.isNameOnly = true;
contextItem.howToSet = howToSet;
// single/stand alone attributes
processStandAloneAttribute(values, valueInValues, element, support, howToSet, contexts, parentContext, contextItem);
return contextItem;
}
if (Array.isArray(value)) {
return createDynamicArrayAttribute(attrName, value, element, [], // contexts,
howToSet, values, support.context);
}
const valueVar = getTagJsTag(value);
if (valueVar >= 0) {
const value = values[valueVar];
return createDynamicAttribute(attrName, value, element, [], // contexts,
parentContext, howToSet, support, isSpecial, valueVar);
}
// simple name/value attribute
return processNonDynamicAttr(attrName, value, element, howToSet, isSpecial, parentContext);
}
/** Only used during updates */
export function processAttributeEmit(newAttrValue, attrName, subject, element, support, howToSet, isSpecial) {
// should the function be wrapped so every time its called we re-render?
if (isFunction(newAttrValue)) {
return callbackFun(support, newAttrValue, element, attrName);
}
return processAttributeSubjectValue(newAttrValue, element, attrName, isSpecial, howToSet, support);
}
/** figure out what type of attribute we are dealing with and/or feed value into handler to figure how to update */
export function processAttributeSubjectValue(newAttrValue, element, attrName, special, howToSet, support) {
// process adding/removing style. class. (false means remove)
if (special !== false) {
specialAttribute(attrName, newAttrValue, element, special);
return;
}
switch (newAttrValue) {
case undefined:
case false:
case null:
paintContent.push([paintContentPush, [element, attrName]]);
return;
}
if (isFunction(newAttrValue)) {
return processAttributeFunction(element, newAttrValue, support, attrName);
}
// value is 0
howToSet(element, attrName, newAttrValue);
}
function callbackFun(support, newAttrValue, element, attrName) {
return processTagCallbackFun(
// subject,
newAttrValue, support, attrName, element);
}
export function processTagCallbackFun(
// subject: AttributeContextItem,
newAttrValue, support, attrName, element) {
// tag has state and will need all functions wrapped to cause re-renders
newAttrValue = bindSubjectCallback(newAttrValue, support);
// const TagJsTag = subject.tagJsVar // = valueToTagJsVar(newAttrValue)
// TagJsTag.processUpdate = processUpdateAttrContext
return processAttributeFunction(element, newAttrValue, support, attrName);
}
function paintContentPush(element, attrName) {
element.removeAttribute(attrName);
}
//# sourceMappingURL=processAttribute.function.js.map