All files / mframejs/attribute repeatAttribute.ts

83.67% Statements 164/196
88.46% Branches 23/26
100% Functions 26/26
84.95% Lines 158/186

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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 65329x   29x 29x 29x 29x 29x 29x                           29x                                     36x       36x                         1663x 1663x 1663x 1663x 1663x                     36x   36x     36x     36x     36x 36x                   36x     36x 36x                     36x     36x         36x 72x       36x 36x 36x   36x 36x   36x 36x               5x 5x     1x 1x                       30x 30x                           5x       1x 1x     3x     7x 7x 7x         1x 1x     3x     7x         3x   20x       5x                   9x       5x 5x   320x 320x 320x         4x 4x   310x                     9x                         7x       2x         2x                 5x         7x 7x     7x 7x 7x 7x 7x 7x                         36x     36x 36x 36x   30x 264x         5x 5x 22x         1x   1x 1x 5x         36x                       30x   55x                     30x       30x                                                   6x   6x                       948x                       948x         948x 948x 948x   36x     948x       18x 18x 18x   18x 162x     18x 18x       946x 946x           946x     16x 16x 16x 160x     2x 2x 20x         880x   48x           2x                                       2x                   948x       1058x                           1x 1x 1x           56x 717x                   1x 1x 1x                       3x 3x 3x 3x   2x         1x 1x 1x 1x       3x 2x   3x                         80x 80x           481x           80x 481x       80x     80x                   36x     36x                                         330x   330x                  
import { customAttribute } from '../decorator/exported';
import { IAttribute, IBindingContext, ITemplateCache } from '../interface/exported';
import { View, AttributeController } from '../view/exported';
import { ViewController } from '../view/exported';
import { BindingEngine } from '../binding/exported';
import { ArrayMethodCallHandler, ArrayPropertyChange, PropertyChangeSimple } from './repeatAttributeSubscriberHelpers';
import { DOM } from '../utils/exported';
import { createBindingContext } from '../binding/createBindingContext';
 
 
export interface ITemplateArray {
    template: Node | Element | Node[] | Element[];
    ctx: IBindingContext;
    $view: ViewController;
}
 
/**
 *  repeat for attribute
 *
 */
@customAttribute('repeat.for')
export class RepeatAttribute implements IAttribute {
 
    // added by template engine
    public $element: HTMLElement;
    public $attribute: Attr;
    public $bindingContext: IBindingContext;
    public $controller: AttributeController;
 
    // internal vars
    public value: any;
    public isAttached: boolean;
    private elementClone: Node;
    private anchor: Node;
    public arrayVariableName: string;
    public arrayExpression: string;
    public rowInstanceName: string;
    public arrayMethodCallHandler: ArrayMethodCallHandler;
    public arrayPropertyChangeHandler: ArrayPropertyChange;
    private propertyChangeHandlerSimple: PropertyChangeSimple;
    public templateArray: ITemplateArray[] = [];
    public loopBinded: Function;
    private repeatForArray: string[];
    private $view: ViewController;
    public arrayType = 'object';
    public arrayPropertyChangeHandlerLocal: ArrayPropertyChange;
    public $array: any;
    private templateElement: boolean;
    private cache: ITemplateCache[];
 
 
 
    /**
     * sets local variables in the context of each row
     *
     */
    public setArrayLocalVariables(ctx: any, i: number) {
        ctx.$index = i;
        ctx.$even = i % 2 === 0 ? true : false;
        ctx.$odd = i % 2 === 0 ? false : true;
        ctx.$last = i === this.templateArray.length - 1 ? true : false;
        ctx.$first = i === 0 ? true : false;
    }
 
 
 
    /**
     * created
     *
     */
    public created() {
 
        this.value = this.$attribute.value;
 
        this.loopBinded = this.loopArray.bind(this);
 
        // get view instance, will need this when we create rows
        this.$view = this.$controller.getView();
 
        // remove repeat attribute, so its not initiated again
        this.$element.attributes.removeNamedItem('repeat.for');
 
        // clone of our row dom
        this.elementClone = this.$element.cloneNode(true);
        this.templateElement = (this.elementClone as any).tagName === 'TEMPLATE' ? true : false;
        if ((this.elementClone as Element).getAttribute('if.bind') && this.templateElement) {
            // if we have if.bind we need to pass entire template over
            this.templateElement = false;
 
        }
 
 
 
        // hide it for now, we will remove it when we are attached to parent
        this.$element.style.display = 'None';
 
        // create dummy element, so viewparser can look at children
        const x = DOM.document.createElement('div'); // this is not needed if template tag (when I add)
        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]);
                }
            }
        }
        x.appendChild(this.templateElement ? (<any>template).content : template);
 
        // anchor so we know where to insert it, also add view to anchor, needed for viewparser to call detached
        this.anchor = DOM.document.createComment('mf-repeat-for');
 
 
 
        // need something better... but good enough for now
        this.repeatForArray = this.value.split(' of ');
        this.repeatForArray = this.repeatForArray.map(x => x.trim());
        if (this.repeatForArray.length !== 2) {
            console.error('unknown expression in repeat:' + this.value);
        } else {
            this.rowInstanceName = this.repeatForArray[0];
            this.arrayVariableName = this.repeatForArray[1];
            this.arrayExpression = this.repeatForArray[1];
            if (this.arrayVariableName.indexOf('|')) {
                const split = this.arrayVariableName.split('|').map(x => x.trim());
                this.arrayVariableName = split[0];
            }
            this.$array = BindingEngine.evaluateExpression(this.arrayExpression, this.$bindingContext);
            const propertyType = this.$array;
 
 
            if (!Array.isArray(propertyType)) {
                // not array, this can be a number or a string.
                // do I want to support object or date ?
                // map and set Im not going to bother atm..
                if (typeof propertyType === 'number') {
                    this.arrayType = 'number';
                    this.subscribePropSimple();
                } else {
                    if (typeof propertyType === 'string') {
                        this.arrayType = 'string';
                        this.subscribePropSimple();
                    } else {
                        // console.error('unknown type, only support array, string and number');
                        // object array
                        this.subscribeArray();
                        this.subscribePropArray();
                        // todo, add option for repeat.string, repeat.number
                    }
                }
 
            } else {
                // object array
                this.subscribeArray();
                this.subscribePropArray();
 
            }
        }
 
    }
 
 
 
    /**
     * loops array and updates values/context
     *
     */
    public loopArray(changed?: boolean, remove?: number, add?: number) {
        const array = this.$array;
        if (array) {
            if (changed) {
                if (remove) {
                    const syncLength = this.templateArray.length - remove;
                    for (let i = 0; i < this.templateArray.length; i++) {
                        if (i < syncLength) {
                            if (this.templateArray[i].ctx.$context[this.rowInstanceName] !== array[i]) {
                                this.templateArray[i].ctx.$context[this.rowInstanceName] = array[i];
                            }
                        } else {
                            const temp = this.templateArray.pop();
                            i--; // this isnt very elegant...
                            this.clearInRow(temp);
                        }
                    }
                }
                if (add) {
                    const syncLength = array.length - add;
                    for (let i = 0; i < array.length; i++) {
                        if (i < syncLength) {
                            if (this.templateArray[i].ctx.$context[this.rowInstanceName] !== this.$array[i]) {
                                this.templateArray[i].ctx.$context[this.rowInstanceName] = this.$array[i];
                            }
                        } else {
                            this.push(array[i]);
                        }
                    }
                }
            } else {
                for (let i = 0; i < this.$array.length; i++) {
                    if (this.templateArray[i].ctx.$context[this.rowInstanceName] !== array[i]) {
                        this.templateArray[i].ctx.$context[this.rowInstanceName] = array[i];
                    }
                }
            }
            this.updateInternals();
        }
    }
 
 
    /**
     * loops array/string and updates values/context
     *
     */
    public loopArrayNumber(changed?: boolean, remove?: number, add?: number) {
        const num = this.templateArray.length + 1;
        if (num) {
            if (changed) {
                if (remove) {
                    const syncLength = this.templateArray.length - remove;
                    for (let i = 0; i < this.templateArray.length; i++) {
                        if (i >= syncLength) {
                            const temp = this.templateArray.pop();
                            i--; // this isnt very elegant...
                            this.clearInRow(temp);
                        }
                    }
                }
                if (add) {
                    const syncLength = num;
                    for (let i = 0; i < num + add; i++) {
                        if (i >= syncLength) {
                            this.push(i);
                        }
                    }
                }
            } else {
                for (let i = 0; i < this.$array; i++) {
                    if (this.templateArray[i].ctx.$context[this.rowInstanceName] !== i) {
                        this.templateArray[i].ctx.$context[this.rowInstanceName] = i;
                    }
                }
            }
            this.updateInternals();
        }
    }
 
 
 
    /**
     * detached, unsubscribes events
     *
     */
    public detached() {
 
 
        this.clearTemplateArray();
 
 
        if (this.arrayMethodCallHandler) {
            BindingEngine.unSubscribeClassArray(this.$bindingContext, this.arrayMethodCallHandler);
        }
 
 
        if (this.arrayPropertyChangeHandler) {
            BindingEngine.unSubscribeClassProperty(this.$bindingContext, this.arrayPropertyChangeHandler);
        }
 
 
        if (this.arrayPropertyChangeHandlerLocal) {
            BindingEngine.unSubscribeClassProperty(this.$bindingContext, this.arrayPropertyChangeHandlerLocal);
        }
 
        if (this.propertyChangeHandlerSimple) {
            BindingEngine.unSubscribeClassProperty(this.$bindingContext, this.propertyChangeHandlerSimple);
        }
 
 
        if (this.anchor) {
            this.anchor.parentNode.removeChild(this.anchor);
            this.anchor = null;
        }
 
        this.$view = null;
        this.$array = null;
        this.arrayMethodCallHandler = null;
        this.arrayPropertyChangeHandler = null;
        this.arrayPropertyChangeHandlerLocal = null;
        this.propertyChangeHandlerSimple = null;
 
    }
 
 
 
    /**
     * attached, update rows
     *
     */
    public attached() {
 
        // replace dom with anchor
        this.remove();
 
        // var to set state
        this.isAttached = true;
        this.$array = BindingEngine.evaluateExpression(this.arrayExpression, this.$bindingContext);
        const array = this.$array;
        if (Array.isArray(array)) {
            array.forEach((ctx: any) => {
                this.push(ctx);
            });
        } else {
            if (this.arrayType === 'number') {
                if (this.templateArray.length !== array) {
                    this.clearTemplateArray();
                    for (let i = 0; i < array; i++) {
                        this.push(i + 1);
                    }
                }
            }
            if (this.arrayType === 'string') {
                const stringLength = typeof array === 'string' ? array.length : 0;
                if (this.templateArray.length !== stringLength) {
                    this.clearTemplateArray();
                    for (let i = 0; i < stringLength; i++) {
                        this.push(array[i]);
                    }
                }
            }
        }
        this.updateInternals();
 
    }
 
 
 
    /**
     * subscribe array in parent
     *
     */
    public subscribeArray() {
        if (!this.arrayMethodCallHandler) {
            this.arrayMethodCallHandler = new ArrayMethodCallHandler(this);
        }
        BindingEngine.subscribeClassArray(this.$bindingContext, this.arrayVariableName, this.arrayMethodCallHandler);
    }
 
 
 
    /**
     * subscribe array prop in parent
     *
     */
    public subscribePropArray() {
        if (!this.arrayPropertyChangeHandler) {
            this.arrayPropertyChangeHandler = new ArrayPropertyChange(this);
        } else {
            console.error('subscribePropArray fail, called when set', this.arrayExpression, this.arrayVariableName);
        }
        BindingEngine.subscribeClassProperty(this.$bindingContext, this.arrayVariableName, this.arrayPropertyChangeHandler);
 
        if (this.arrayVariableName !== this.arrayExpression) {
            // if expression is used we need to have a local handler
            // why local handler ?
            // - if we dont have a local handler we will be editing the main array, we need this for later
            // - if we do filter on main array and set it to the filtered array then we lose the original data
            if (!this.arrayPropertyChangeHandlerLocal) {
                this.arrayPropertyChangeHandlerLocal = new ArrayPropertyChange(this);
            }
            // replace original expression variable name with local "$array" used in this class
            // this will be the array we use for data
            const classKey = this.arrayExpression.replace(this.arrayVariableName, '$array');
            BindingEngine.subscribeClassProperty(createBindingContext(this), classKey, this.arrayPropertyChangeHandlerLocal);
        }
 
    }
 
 
 
    /**
     * subscribe array in parent
     *
     */
    public subscribePropSimple() {
        if (!this.propertyChangeHandlerSimple) {
            this.propertyChangeHandlerSimple = new PropertyChangeSimple(this);
        }
        BindingEngine.subscribeClassProperty(this.$bindingContext, this.arrayVariableName, this.propertyChangeHandlerSimple);
    }
 
 
 
    /**
     * pushes new row
     *
     */
    public push(ctx: any, i?: number) {
 
 
        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]);
                }
            }
        }
 
        // parse node for custom elements and attributes with context object
        const context = createBindingContext({ [this.rowInstanceName]: ctx }, createBindingContext(this.$bindingContext, this.$bindingContext));
 
 
 
        // append new child
        const temp = DOM.document.createElement('div');
        temp.appendChild(this.templateElement ? (<any>template).content : template);
        const $view = new ViewController(template, this.$view);
        if (!this.cache) {
            this.cache = View.createTemplateCache(temp);
        }
 
        const controllers = View.parseTemplateCache(temp, context, $view, this.cache);
 
        let childNodes: undefined | any[];
        if (this.templateElement) {
            childNodes = [];
            let anchor = DOM.document.createComment('repeat-template-row-anchor-start');
            childNodes.push(anchor);
 
            for (let i = 0, ii = temp.childNodes.length; i < ii; i++) {
                childNodes.push(temp.childNodes[i]);
            }
 
            anchor = DOM.document.createComment('repeat-template-row-anchor-end');
            childNodes.push(anchor);
        }
 
        if (i === undefined) {
            const length = this.templateArray.length;
            this.templateArray.push({
                ctx: context,
                template: this.templateElement ? childNodes : template,
                $view: $view
            });
 
            this.setArrayLocalVariables(context.$context, this.templateArray.length - 1);
            if (this.templateElement) {
                if (length) {
                    const u = (this.templateArray[length - 1].template as Element[] | Node[]).length - 1;
                    this.anchor.parentNode.insertBefore(childNodes[0], this.templateArray[length - 1].template[u].nextSibling);
                    for (let i = 1, ii = childNodes.length; i < ii; i++) {
                        this.anchor.parentNode.insertBefore(childNodes[i], childNodes[i - 1].nextSibling);
                    }
                } else {
                    this.anchor.parentNode.insertBefore(childNodes[0], this.anchor.nextSibling);
                    for (let i = 1, ii = childNodes.length; i < ii; i++) {
                        this.anchor.parentNode.insertBefore(childNodes[i], childNodes[i - 1].nextSibling);
                    }
                }
            } else {
                if (length) {
                    this.anchor.parentNode.insertBefore(template, (this.templateArray[length - 1].template as Element | Node).nextSibling);
                } else {
                    this.anchor.parentNode.insertBefore(template, this.anchor.nextSibling);
                }
            }
 
 
        } else {
            this.templateArray.splice(i, 0, {
                ctx: context,
                template: this.templateElement ? childNodes : template,
                $view: $view
            });
            if (this.templateElement) {
                if (this.templateArray[i + 1]) {
                    const u = (this.templateArray[length - 1].template as Element[] | Node[]).length - 1;
                    this.anchor.parentNode.insertBefore(childNodes[0], this.templateArray[length - 1].template[u].nextSibling);
                    for (let i = 1, ii = childNodes.length; i < ii; i++) {
                        this.anchor.parentNode.insertBefore(childNodes[i], childNodes[i - 1].nextSibling);
                    }
                } else {
                    this.anchor.parentNode.insertBefore(childNodes[0], this.anchor.nextSibling);
                    for (let i = 1, ii = childNodes.length; i < ii; i++) {
                        this.anchor.parentNode.insertBefore(childNodes[i], childNodes[i - 1].nextSibling);
                    }
                }
            } else {
                if (this.templateArray[i + 1]) {
                    this.anchor.parentNode.insertBefore(template, (this.templateArray[i + 1].template as Element | Node));
                } else {
                    this.anchor.parentNode.appendChild(template);
                }
            }
 
        }
 
 
        // call attached
        controllers.forEach((contr: any) => {
 
            // if it have attached method
            if (contr.attached) {
                contr.attached();
            }
        });
 
    }
 
 
 
    /**
     * pops row
     *
     */
    public pop() {
        if (this.templateArray.length > 0) {
            const temp = this.templateArray.pop();
            this.clearInRow(temp);
            this.updateInternals();
        }
    }
 
 
    public updateInternals() {
        this.templateArray.forEach((context: any, i: number) => {
            this.setArrayLocalVariables(context.ctx.$context, i);
        });
    }
 
    /**
     * shift row
     *
     */
    public shift() {
        if (this.templateArray.length > 0) {
            const temp = this.templateArray.shift();
            this.clearInRow(temp);
            this.updateInternals();
        }
    }
 
 
 
    /**
     * splice row
     *
     */
    public splice(args: any) {
        if (this.templateArray.length > 0) {
            const index = args[0];
            const deleted = args[0] === 0 && args[1] === undefined ? this.templateArray.length : args[1];
            const added = [];
            for (let i = 2; i < args.length; i++) {
                if (args[i]) {
                    added.push(args[i]);
                }
            }
 
            if (deleted) {
                const newIndex = index + deleted - 1;
                for (let i = newIndex; i > index - 1; i--) {
                    const temp = this.templateArray.splice(i, 1)[0];
                    this.clearInRow(temp);
                }
            }
 
            added.forEach((ctx) => {
                this.push(ctx, index);
            });
            this.updateInternals();
            // get our queue
        }
    }
 
 
 
    /**
     * clear template array
     *
     */
    public clearTemplateArray() {
        // create temp div
        let x = DOM.document.createElement('div');
        this.templateArray.forEach((temp: any) => {
            if (this.templateElement) {
                for (let i = 0, ii = temp.template.length; i < ii; i++) {
                    x.appendChild(temp.template[i]);
                }
            } else {
                x.appendChild(temp.template);
            }
 
        });
 
        // now clear all views
        this.templateArray.forEach((temp: any) => {
            temp.$view.clearView();
        });
 
        // clear all rows
        x = null;
 
        // reset template array
        this.templateArray = [];
    }
 
 
    /**
     * remove all
     *
     */
    private remove() {
        // clear if any
        this.clearTemplateArray();
 
        // my repeat fail fast if it have no parent, could this happend?
        this.$element.parentNode.replaceChild(this.anchor, this.$element);
 
    }
 
 
    /**
     * clear 1 row
     *
     */
    private clearInRow(rowData: any) {
        if (rowData) {
 
            if (this.templateElement) {
                for (let i = 0, ii = rowData.template.length; i < ii; i++) {
                    rowData.$view.clearView();
                    if (rowData.template[i].parentNode) {
                        rowData.template[i].parentNode.removeChild(rowData.template[i]);
                    }
                }
            } else {
                if (rowData.template.parentNode) {
                    rowData.template.parentNode.removeChild(rowData.template);
                }
                rowData.$view.clearView();
            }
 
 
 
        }
    }
 
}