All files / mframejs/view elementController.ts

83.33% Statements 70/84
72.73% Branches 8/11
84.62% Functions 11/13
83.33% Lines 70/84

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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 30129x 29x 29x 29x 29x 29x 29x             29x       252x   252x                               252x 252x           252x 252x   237x   15x   252x     252x 252x       22x 22x                                                                         252x 252x 252x 252x                                 252x   252x   252x   252x 252x 252x   252x 252x     252x             26x   26x 26x       286x       22x     252x   252x 252x   343x     252x 252x           252x                       252x       252x                       252x   2x                         252x           5x                           252x                                     22x   230x                                     114x           114x   4x   4x       4x     114x 114x 114x 114x 114x 114x 114x 114x 114x 114x            
import { IElement, IControllerArray, CONSTANTS, IBindingContext } from '../interface/exported';
import { View } from './view';
import { ViewController } from './viewController';
import { ContainerClasses } from '../container/exported';
import { BindingEngine, createBindingContext } from '../binding/exported';
import { Logger } from '../utils/exported';
import { DOM } from '../utils/exported';
 
 
/**
 * Element controller adds view and controls the lifecycle of element
 *
 */
export class ElementController {
    public classInstance: IElement;
    private templateString: string;
    private template: Node;
    private _process = true;
    private logger: Logger;
    private internalAttached = false;
    private elements: Node[];
    private anchorNode: Node;
    private viewController: ViewController;
    private shadowDom: boolean;
 
    /**
     * Creates instance of ElementController
     * @param bindingContext - Binding context to use
     * @param htmlNode - html node
     * @param classInstance - class instance, optional..
     * @param elementName - name of custom element
     * @param templateString - optional template string
     * @param parentViewController - parent viewController
     */
    constructor(
        private bindingContext: IBindingContext,
        private htmlNode: Node,
        classInstance: IElement | null,
        elementName: string,
        templateString: string | null,
        parentViewController: ViewController) {
 
        this.logger = Logger.getLogger((htmlNode as any).tagName, 'element');
        this.templateString = templateString;
        if (!classInstance) {
            this.logger.log('constructor');
        } else {
            this.logger.log('constructor - using supplied class');
        }
        this.classInstance = classInstance || ContainerClasses.get(elementName);
 
        // every element is a controller in it self, so create a new view
        this.viewController = new ViewController(htmlNode, parentViewController);
        this.viewController.addElement(this);
 
 
        if ((this.classInstance as any).__proto__[CONSTANTS.CONTAINER_FREE]) {
            this.anchorNode = DOM.document.createComment('containerless-anchor');
            this.elements = [];
        }
 
        if ((this.classInstance as any).__proto__[CONSTANTS.SHADOW_DOM]) {
            this.shadowDom = true;
        }
    }
 
 
 
    /**
     * 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 $view
     *
     */
    public getView(): ViewController {
        return this.viewController;
    }
 
 
 
    /**
     * start element life cycle
     *
     */
    public init() {
 
 
        this.classInstance.$element = this.htmlNode;
        this.classInstance.$bindingContext = this.bindingContext;
        this.classInstance.$attributes = (this.htmlNode as any).attributes;
        this.classInstance.$controller = this;
 
        if (this.shadowDom) {
            if ((this.classInstance as any).__proto__[CONSTANTS.CONTAINER_FREE]) {
                console.warn('containerfree can not be used with showdow dom');
            } else {
                const mode = (this.classInstance as any).__proto__[CONSTANTS.SHADOW_DOM].mode;
                try {
                    this.htmlNode = this.classInstance.$shadowdom;
                    this.classInstance.$shadowdom = (this.htmlNode as any).attachShadow({ mode: mode });
                } catch (e) {
                    console.warn('error adding showdow dom:', e);
                }
            }
 
        }
 
        BindingEngine.subscribeClassMetaBinding(this.classInstance);
 
        const result = this.loadTemplate();
 
        Promise.resolve(result).then((template: string) => {
 
            this.templateString = this.templateString || template || '<template></template>';
            let arr: IControllerArray = [];
            this.template = View.createTemplate(this.templateString);
 
            this.create();
            this.processContent();
 
            if (this._process) {
                arr = View.parseTemplate(this.template, createBindingContext(this.classInstance), this.viewController);
            }
 
            // task is started, lets look children and append them, if we have process content
            if (this._process) {
                while (this.template.childNodes.length) {
                    if (this.anchorNode) {
                        this.htmlNode.parentNode.insertBefore(this.anchorNode, this.htmlNode.nextSibling);
 
                        this.elements.push(this.template.firstChild);
                        this.anchorNode.parentNode.insertBefore(this.template.firstChild, this.anchorNode.nextSibling);
 
 
                    } else {
                        this.htmlNode.appendChild(this.template.firstChild);
                    }
                }
                if (this.anchorNode) {
                    this.htmlNode.parentNode.removeChild(this.htmlNode);
                }
            }
            arr.reverse();
 
            this.contentProcessed(arr);
            arr.forEach((a) => {
                if (a.attached) {
                    a.attached();
                }
            });
            this.internalAttached = true;
            this.attached();
 
 
        });
 
 
        return this;
 
    }
 
 
 
    /**
     * calls loadtemplate if stemplate string isnt set
     * @internal
     *
     */
    public loadTemplate(): string | Promise<string> {
        this.logger.log('loadTemplate');
        if (this.templateString) {
            return this.templateString;
        } else {
            return this.classInstance.loadTemplate && this.classInstance.loadTemplate();
        }
    }
 
 
 
    /**
     * sets vars and calls create on element
     * @internal
     *
     */
    public create(): void {
        this.logger.log('create');
        if (this.classInstance.created) {
            this.classInstance.created();
        }
    }
 
 
 
    /**
     * calls elements processContent() if it exist
     * here someone can modify the template
     * @internal
     *
     */
    public processContent(): void {
        this.logger.log('processContent');
 
        if (this.classInstance.processContent) {
            this._process = this.classInstance.processContent(this.template);
        } else {
            while (this.htmlNode.childNodes[0]) {
                this.template.appendChild(this.htmlNode.childNodes[0]);
            }
        }
    }
 
 
 
    /**
     * calls elements contentProcessed() if it exist
     * is called after template is processed and added to element
     * @internal
     *
     */
    public contentProcessed(controllers: IControllerArray): void {
        this.logger.log('contentProcessed');
 
        if (this.classInstance.contentProcessed && this._process) {
            this.classInstance.contentProcessed(controllers);
        }
    }
 
 
 
    /**
     * calls elements attached() if it exist
     * when attached to parent
     * @public
     *
     */
    public attached(): void {
 
        if (this.internalAttached) {
            if (!this.classInstance.$element.parentNode) {
                this.logger.log('attached', 'missing parent node');
            } else {
                this.logger.log('attached');
            }
        }
 
        if (this.classInstance.attached && this.internalAttached) {
            this.internalAttached = false;
            this.classInstance.attached();
        }
    }
 
 
 
    /**
     * calls elements detached() if it exist and clears it
     * when about to be detached
     * @public
     *
     */
    public detached(): void {
        this.logger.log('detached');
 
        if (this.classInstance.detached) {
            this.classInstance.detached();
        }
 
        BindingEngine.unSubscribeClassMetaBinding(this.classInstance);
        if (this.anchorNode) {
            this.elements.forEach((element: any) => {
                if (element.parentNode) { // if repeat or if.bind is here it might just be a dummy ref
                    element.parentNode.removeChild(element);
                }
            });
            if (this.anchorNode.parentNode) {
                this.anchorNode.parentNode.removeChild(this.anchorNode);
            }
        }
        this.anchorNode = null;
        this.elements = null;
        this.bindingContext = null;
        this.htmlNode = null;
        this.classInstance.$element = null;
        this.classInstance.$bindingContext = null;
        this.classInstance.$attributes = null;
        this.classInstance.$controller = null;
        this.classInstance = null;
        this.viewController = null;
 
 
    }
 
}