All files / mframejs/view attributeController.ts

97.44% Statements 38/39
100% Branches 2/2
85.71% Functions 6/7
97.44% Lines 38/39

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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 14029x 29x 29x   29x         29x                           135x 135x 135x       135x 135x 135x       43x 43x   92x 92x                         135x                                     43x                   135x 135x 135x 135x 135x   135x     134x                         135x   94x                     24x     24x     24x   24x 24x 24x 24x 24x 24x 24x 24x 24x              
import { ViewController } from './viewController';
import { ContainerClasses } from '../container/exported';
import { BindingEngine } from '../binding/exported';
import { IAttribute, IBindingContext } from '../interface/exported';
import { Logger } from '../utils/exported';
 
/**
 * Creates and controlls custom attributes
 */
export class AttributeController {
    public classInstance: IAttribute;
    private logger: Logger;
    private viewController: ViewController;
 
    /**
     * Creates instance of AttributeController
     * @param bindingContext - bindingContext to use
     * @param htmlNode - html node
     * @param attributeNode - attibute node
     * @param attributeName - name of registered attribute to use
     * @param viewController - viewController to use
     */
    constructor(
        private bindingContext: IBindingContext,
        private htmlNode: Node,
        private attributeNode: Attr,
        attributeName: string,
        viewController: ViewController) {
 
        this.logger = Logger.getLogger(attributeNode.name, 'attribute');
        this.classInstance = ContainerClasses.get(attributeName);
        this.htmlNode = htmlNode;
 
        if (attributeNode.name === 'if.bind' || attributeNode.name === 'repeat.for') {
            // TODO: needs to be more dynamic, either as a param when creating or look and check if its regeistered as a controller
            this.viewController = new ViewController(htmlNode, viewController);
            this.viewController.addAttribute(this);
        } else {
            viewController.addAttribute(this);
            this.viewController = viewController;
        }
 
 
    }
 
 
 
    /**
     * Start element life cycle
     *
     */
    public init(): void {
        this.create();
    }
 
 
 
    /**
     * Get class instance from parent
     * @param _customElement custom element class you want to try and search for
     */
    public searchForInstance<T>(_customElement: T): T | null {
        return this.viewController.searchForInstance(_customElement);
    }
 
 
    /**
     * Get viewController
     *
     */
    public getView(): ViewController {
        return this.viewController;
    }
 
 
    /**
     * Sets vars and calls create on attribute
     * @internal
     *
     */
    public create(): void {
        this.logger.log('created', this.attributeNode.name, this.attributeNode.value);
        this.classInstance.$element = this.htmlNode;
        this.classInstance.$bindingContext = this.bindingContext;
        this.classInstance.$attribute = this.attributeNode;
        this.classInstance.$controller = this;
 
        BindingEngine.subscribeClassMetaBinding(this.classInstance);
 
        if (this.classInstance.created) {
            this.classInstance.created();
        }
 
 
    }
 
 
 
    /**
     * calls elements attached() if it exist
     *
     */
    public attached(): void {
        this.logger.log('attached', this.attributeNode.name, this.attributeNode.value);
        if (this.classInstance.attached) {
            this.classInstance.attached();
        }
    }
 
 
 
    /**
     *  calls elements detached() if it exist and clears it
     *
     */
    public detached(): void {
        this.logger.log('detached', this.attributeNode.name, this.attributeNode.value);
 
        if (this.classInstance.detached) {
            this.classInstance.detached();
        }
 
        BindingEngine.unSubscribeClassMetaBinding(this.classInstance);
 
        this.bindingContext = null;
        this.htmlNode = null;
        this.attributeNode = null;
        this.classInstance.$element = null;
        this.classInstance.$bindingContext = null;
        this.classInstance.$attribute = null;
        this.classInstance.$controller = null;
        this.classInstance = null;
        this.viewController = null;
 
    }
 
 
 
}