All files / mframejs/attribute ifAttribute.ts

96.61% Statements 57/59
100% Branches 12/12
100% Functions 7/7
96.36% Lines 53/55

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 20629x   29x 29x 29x                 29x                                             12x     12x 12x       12x 12x         12x     12x                             12x       12x     12x   12x             12x 12x 12x   8x 8x           12x                   2x 2x   2x 2x                       12x                       12x   12x     1x 1x 1x 1x   1x 1x 1x   1x   1x       12x   1x 1x 2x     11x       12x   14x     12x                         9x 9x 9x       1x   3x         16x     21x              
import { customAttribute } from '../decorator/exported';
import { IAttribute, IListener, IBindingContext } from '../interface/exported';
import { ViewController, View, AttributeController } from '../view/exported';
import { BindingEngine } from '../binding/exported';
import { DOM } from '../utils/exported';
 
 
 
/**
 * removes dom when expression is undefined/null''0
 *
 */
@customAttribute('if.bind')
export class IfAttribute implements IAttribute {
    public $element: HTMLElement;
    public $attribute: Attr;
    public $bindingContext: IBindingContext;
    public $controller: AttributeController;
    private value: string | null | undefined;
    private elementClone: Node;
    private anchor: Node;
    private added: boolean;
    private subscribeCall: IListener;
    private $view: ViewController;
    private templateElement: boolean;
    private childNodes: Node[];
    private $templateView: ViewController;
 
 
    /**
     * created
     *
     */
    public created() {
 
        // get current value;
        this.value = this.$attribute.value;
 
        // remove the if.bind attribute
        this.$element.attributes.removeNamedItem('if.bind');
        this.$view = this.$controller.getView();
 
 
        // clone node, we need this clone later when we add it
        this.elementClone = this.$element.cloneNode(true);
        this.templateElement = (this.elementClone as any).tagName === 'TEMPLATE' ? true : false;
 
 
 
        // hide it for now, we will remove it when we are attached to parent
        this.$element.style.display = 'None';
 
        // set variable for later
        this.added = false;
 
 
    }
 
 
 
    /**
     * attached
     *
     */
    public attached() {
 
        // create anchor so where know where to inject it
        // this needs to be after we attched it since we need parentNode to do this
        this.anchor = DOM.document.createComment('mf-if-bind');
 
 
        // add anchor
        this.$element.parentNode.insertBefore(this.anchor, this.$element);
 
        // remove main element
        this.remove();
 
        this.subscribeCall = {
            name: 'ifAttribute',
            value: this.value,
            call: (newValue: any) => {
 
                switch (true) {
                    case newValue && !this.added:
                        this.added = true;
                        this.add();
                        break;
                    case !newValue && this.added:
                        this.remove();
                        break;
                }
            }
        } as IListener;
 
 
        BindingEngine.subscribeClassProperty(this.$bindingContext, this.value, this.subscribeCall);
    }
 
 
 
    /**
     * detached
     *
     */
    public detached(): void {
        BindingEngine.unSubscribeClassProperty(this.$bindingContext, this.subscribeCall);
        this.remove();
        if (this.anchor) {
            this.anchor.parentNode.removeChild(this.anchor);
            this.anchor = null;
        }
    }
 
 
 
    /**
     * shows element/content
     *
     */
    public add() {
        // create clone
        const template = this.elementClone.cloneNode(true);
        if (this.templateElement) {
            // IE11
            if (!(<any>template).content) {
                (<any>template).content = DOM.document.createDocumentFragment();
                while (template.childNodes[0]) {
                    (<any>template).content.appendChild(template.childNodes[0]);
                }
            }
        }
 
        // set clone as current element
        this.$element = (template as any);
        let temp: any;
        this.$templateView = new ViewController(template, this.$view);
 
        if (this.templateElement) {
            temp = DOM.document.createElement('div');
            temp.appendChild((template as any).content);
            this.childNodes = [];
            let anchor = DOM.document.createComment('ifbind-template-anchor-start');
 
            this.childNodes.push(anchor);
            for (let i = 0, ii = temp.childNodes.length; i < ii; i++) {
                this.childNodes.push(temp.childNodes[i]);
            }
            anchor = DOM.document.createComment('ifbind-template-anchor-end');
 
            this.childNodes.push(anchor);
        }
 
        // get attach event for elements
        let controllers = View.parseTemplate(this.templateElement ? temp : this.$element, this.$bindingContext, this.$templateView);
        if (this.templateElement) {
            this.anchor.parentNode.insertBefore(this.childNodes[0], this.anchor);
            for (let i = 1, ii = this.childNodes.length; i < ii; i++) {
                this.anchor.parentNode.insertBefore(this.childNodes[i], this.childNodes[i - 1].nextSibling);
            }
        } else {
            this.anchor.parentNode.insertBefore(this.$element, this.anchor);
        }
 
        // if this even used after last rebuild?
        controllers.forEach((x) => {
            if (x.attached) {
                x.attached();
            }
        });
        controllers = null;
 
 
    }
 
 
 
    /**
     * removes content
     *
     */
    private remove() {
        if (this.added) {
            this.added = false;
            this.$templateView.clearView();
            this.$templateView = null;
        }
        if (this.$element) {
            if (this.templateElement && this.childNodes) {
                for (let i = 0, ii = this.childNodes.length; i < ii; i++) {
                    if (this.childNodes[i].parentNode === this.anchor.parentNode) {
                        this.anchor.parentNode.removeChild(this.childNodes[i]);
                    }
                }
            } else {
                if (this.anchor.parentNode === this.$element.parentNode) {
                    this.anchor.parentNode.removeChild(this.$element);
                }
            }
            this.$element = null;
        }
    }
 
 
 
}