All files / mframejs/view viewController.ts

84.38% Statements 27/32
0% Branches 0/2
77.78% Functions 7/9
83.87% Lines 26/31

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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162                  29x     1278x     1278x   1255x                                                                     252x   252x   252x                   135x   77x   135x                   1049x   711x   1049x                                 320x   1255x                       938x   810x                               128x 128x             994x 994x           938x     948x 948x 948x 948x              
import { AttributeController } from './attributeController';
import { ElementController } from './elementController';
import { InterpolateController } from './interpolateController';
import { IControllerObject } from '../interface/exported';
 
/**
 * Holds all elements/attributes and interpolates, so we can clear them later
 *
 */
export class ViewController {
    private items: IControllerObject;
    private childViewControllers: ViewController[];
    private count = 0;
 
 
    constructor(private htmlNode: Node, private viewController?: any) {
        if (viewController) {
            this.viewController.addChildView(this);
        }
    }
 
 
    /**
     * Get class instance from parent
     * @param _customElement custom element class you want to try and search for
     */
    public searchForInstance(_customElement: any): any | null {
        for (const k in this.items) {
            if (this.items && this.items[k]) {
                if (this.items[k].classInstance) {
                    if (this.items[k].classInstance instanceof _customElement) {
                        return this.items[k].classInstance;
                    }
                }
            }
        }
        if (this.viewController) {
            const y = this.viewController.searchForInstance(_customElement);
            if (y) {
                return y;
            }
        }
 
        return null;
    }
 
 
    /**
     * adds to internal object so we can call detached later
     *
     */
    public addElement(_class: ElementController) {
        this.count++;
        if (!this.items) {
            this.items = {};
        }
        this.items['e' + this.count] = _class;
    }
 
 
 
    /**
     * adds to internal object so we can call detached later
     *
     */
    public addAttribute(attibuteController: AttributeController): void {
        this.count++;
        if (!this.items) {
            this.items = {};
        }
        this.items['a' + this.count] = attibuteController;
    }
 
 
 
    /**
     * adds to internal object so we can call detached later
     *
     */
    public addInterpolate(interpolateController: InterpolateController): void {
        this.count++;
        if (!this.items) {
            this.items = {};
        }
        this.items['i' + this.count] = interpolateController;
    }
 
 
 
    /**
     * returns current element
     *
     */
    public getElement(): Node {
        return this.htmlNode;
    }
 
 
    public addChildView(viewController: ViewController) {
 
        if (!this.childViewControllers) {
            this.childViewControllers = [];
        }
        this.childViewControllers.push(viewController);
    }
 
 
 
    /**
     * remove it self from parent so we dont get memory leak
     *
     */
    public removeChildView(viewController: ViewController) {
 
        if (this.childViewControllers) {
            const i = this.childViewControllers.indexOf(viewController);
            if (i !== -1) {
                this.childViewControllers.splice(i, 1);
            }
        }
 
    }
 
 
 
    /**
     * calls detached on all attributes/elements and interpolates binded on view
     *
     */
    public clearView(): void {
 
        if (this.childViewControllers) {
            while (this.childViewControllers.length) {
                const view = this.childViewControllers.pop();
                view.clearView();
            }
        }
 
        if (this.items) {
            for (const item in this.items) {
                if (this.items[item].detached) {
                    this.items[item].detached();
                    this.items[item] = null;
                }
            }
        }
 
        if (this.viewController) {
            this.viewController.removeChildView(this);
        }
 
        this.childViewControllers = null;
        this.items = null;
        this.htmlNode = null;
        this.viewController = null;
 
    }
 
 
 
}