All files / src/custom-element/mixins ParentChildMixin.ts

100% Statements 17/17
90% Branches 18/20
100% Functions 5/5
100% Lines 16/16

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            15x           81x         81x       77x   69x         46x   46x         115x   114x   114x   149x   4x     145x     114x     115x       15x
/**
 * Establishes a relationship between a parent custom element and its children to
 * allow the parent to manage them
 * @param Base 
 * @returns 
 */
const ParentChildMixin = Base =>
 
    class ParentChild extends Base {
 
        static readonly _isCustomElement: boolean = true;
 
        private _adoptingParent = null;
 
        /**
         * The children elements of this one
         */
        protected adoptedChildren: Set<Node> = new Set<Node>();
 
        connectedCallback() {
 
            super.connectedCallback?.();
 
            (this.adoptingParent as any)?.adoptedChildren.add(this); // It might be null for the topmost custom element
        }
 
        disconnectedCallback() {
 
            super.disconnectedCallback?.();
 
            (this.adoptingParent as any)?.adoptedChildren.delete(this); // It might be null for the topmost custom element
        }
 
        protected get adoptingParent(): Node {
 
            if (this._adoptingParent === null) {
 
                let parent = this.parentNode;
 
                while (parent !== null) {
 
                    if (parent.constructor._isCustomElement) {  // It is a custom element
 
                        break;
                    }
 
                    parent = parent.parentNode;
                }
 
                this._adoptingParent = parent;
            }
 
            return this._adoptingParent;
        }
    }
 
export default ParentChildMixin;