All files / mframejs/view addAttribute.ts

20% Statements 4/20
0% Branches 0/8
0% Functions 0/1
20% Lines 4/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 6329x   29x 29x                           29x                                                                                          
import { ContainerAttributes } from '../container/exported';
import { IControllerArray, IBindingContext } from '../interface/exported';
import { AttributeController } from './attributeController';
import { InterpolateController } from './interpolateController';
import { ViewController } from './viewController';
 
/**
 * Helper class to add custom attribute element
 *
 * @public
 * @param htmlNode - node you want to add use/ have attribute
 * @param bindingContext - binding context to use
 * @param attributeName - attribute name to add
 * @param value - value to use
 * @param viewController - viewcontroller to use
 * @param autoAttach - call attached when created ? defaults to true if not set
 */
export function addAttribute(
    htmlNode: HTMLElement,
    bindingContext: IBindingContext,
    attributeName: string,
    value: string,
    viewController: ViewController,
    autoAttach?: boolean) {
    autoAttach = autoAttach === undefined ? true : autoAttach;
    const arr: IControllerArray = [];
    if ((htmlNode as any).getAttribute) {
        htmlNode.setAttribute(attributeName, value);
        const attributeNode = htmlNode.getAttributeNode(attributeName);
        let customAttribute = ContainerAttributes.findAttribute(attributeNode.name);
        if (!customAttribute && attributeNode.name) {
            customAttribute = ContainerAttributes.findAttribute(attributeNode.name.replace('.bind', ''));
            if (!customAttribute && attributeNode.name) {
                customAttribute = ContainerAttributes.findAttribute(attributeNode.name.replace(/([@a-z]\w+\.)/g, '#VARIABLE#'));
            }
        }
 
        if (customAttribute) {
            const instance = new AttributeController(bindingContext, htmlNode, attributeNode, customAttribute, viewController);
            instance.init();
            if (instance) {
                if (autoAttach) {
                    instance.attached();
                }
                arr.push(instance);
            }
        } else {
            if (attributeNode.value.indexOf('${') !== -1 || attributeNode.value.indexOf('@{') !== -1) {
                if (attributeNode.name.indexOf('.bind') === -1) {
                    const interpolateController = new InterpolateController(bindingContext, attributeNode, viewController, true);
                    interpolateController.init();
                    if (autoAttach) {
                        interpolateController.attached();
                    }
                    arr.push(interpolateController);
                }
            }
        }
    }
 
    return arr;
}