All files / mframejs/attribute repeatAttributeSubscriberHelpers.ts

100% Statements 58/58
86.67% Branches 13/15
100% Functions 9/9
100% Lines 58/58

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  29x                                 29x             30x   30x             36x 36x     1x   1x 1x   1x 1x       30x 30x   30x 298x       31x   1x 1x   1x 1x   3x 3x                               29x         30x 30x   30x               28x     19x         1x   1x   2x 2x       17x 17x   17x 15x     17x 17x   17x           3x 3x 3x   3x       6x 6x 6x   3x                             29x           6x 6x                     8x   8x 8x 322x             2x 2x       4x   5x                
import { RepeatAttribute } from './repeatAttribute';
import { BindingEngine } from '../binding/exported';
import { IListener } from '../interface/exported';
import { ArrayObserverHandler } from '../binding/array/arrayObserverHandler';
import { PropertyObserverHandler } from '../binding/property/propertyObserverHandler';
 
 
/***************************************************************
 *  Helpers for repeat for attribute when array changes
 *  ArrayMethodCallHandler & ArrayPropertyChange & PropertyChangeSimple
 ***************************************************************/
 
 
 
/**
 *  Handles events/methods on array, like push/splice etc
 *
 */
export class ArrayMethodCallHandler implements IListener {
    public name: string;
    public caller: ArrayObserverHandler | PropertyObserverHandler;
 
 
 
    constructor(
        private repeat: RepeatAttribute
    ) {
        this.name = 'repeat' + this.repeat.value;
    }
 
 
 
    public call(events: any) {
 
        let sort = true;
        events.forEach((event: any) => {
            switch (event.event) {
                case 'push':
                    let i = 0;
                    while (i < event.args.length) {
                        this.repeat.push(event.args[i]);
                        i++;
                    }
                    this.repeat.updateInternals();
                    break;
                case 'reverse':
                case 'sort':
                    if (sort) {
                        sort = false;
                        const array = this.repeat.$array; // BindingEngine.evaluateExpression(this.repeat.arrayExpression, this.repeat.$bindingContext);
                        if (array) {
                            array.forEach((ctx: any, i: number) => {
                                this.repeat.templateArray[i].ctx.$context[this.repeat.rowInstanceName] = ctx;
                            });
                        }
                    }
                    break;
                case 'shift':
                    this.repeat.shift();
                    break;
                case 'pop':
                    this.repeat.pop();
                    break;
                case 'splice':
                    this.repeat.splice(event.args);
                    break;
                default:
            }
        });
 
 
 
    }
}
 
 
 
/**
 *  Handles changes to array when its replaced
 *
 */
export class ArrayPropertyChange implements IListener {
    public name: string;
    public caller: ArrayObserverHandler | PropertyObserverHandler;
 
    constructor(
        private repeat: RepeatAttribute,
        public expression?: boolean
    ) {
        this.name = 'repeat' + this.repeat.value;
    }
 
 
 
    public call() {
        if (this.repeat.isAttached) {
 
            const array = BindingEngine.evaluateExpression(this.repeat.arrayExpression, this.repeat.$bindingContext);
 
            if (Array.isArray(array) && this.repeat.templateArray.length !== array.length) {
                this.repeat.$array = array;
                if (this.repeat.templateArray.length !== 0 && array.length !== 0) {
 
                    if (this.repeat.templateArray.length > array.length) {
 
                        this.repeat.loopBinded(true, this.repeat.templateArray.length - array.length, 0);
                    } else {
                        this.repeat.loopBinded(true, 0, array.length - this.repeat.templateArray.length);
                    }
                    BindingEngine.unSubscribeClassArray(this.repeat.$bindingContext, this.repeat.arrayMethodCallHandler);
                    this.repeat.subscribeArray();
 
 
                } else {
                    this.repeat.clearTemplateArray();
                    let array = BindingEngine.evaluateExpression(this.repeat.arrayExpression, this.repeat.$bindingContext);
                    if (Array.isArray(array)) {
                        array.forEach((ctx: any) => {
                            this.repeat.push(ctx);
                        });
 
                        BindingEngine.unSubscribeClassArray(this.repeat.$bindingContext, this.repeat.arrayMethodCallHandler);
                        this.repeat.subscribeArray();
 
                        array = null;
                    }
                }
            } else {
                if (Array.isArray(array)) {
                    // array is same size, we just need to replace data
                    this.repeat.$array = array;
                    this.repeat.loopBinded();
                    BindingEngine.unSubscribeClassArray(this.repeat.$bindingContext, this.repeat.arrayMethodCallHandler);
                    if (array) {
                        this.repeat.subscribeArray();
                    }
                } else {
                    // array is null/undefined, we need to clear array
                    this.repeat.$array = Array.isArray(array) ? array : [];
                    this.repeat.clearTemplateArray();
                    BindingEngine.unSubscribeClassArray(this.repeat.$bindingContext, this.repeat.arrayMethodCallHandler);
                    if (array) {
                        this.repeat.subscribeArray();
                    }
                }
 
            }
        }
    }
}
 
 
 
/**
 *  Handles changes to property(not array when this is used) when its replaced  (simple array, not object)
 *
 */
export class PropertyChangeSimple implements IListener {
    public name: string;
    public caller: ArrayObserverHandler | PropertyObserverHandler;
 
 
 
    constructor(private repeat: RepeatAttribute) {
        this.name = 'repeat' + this.repeat.value;
    }
 
 
 
    public call(newValue: any) {
        if (this.repeat.isAttached) {
            if (this.repeat.arrayType === 'string') {
                // TODO: this is slow if sub router and many
                // I think this will ever be used..much
                // need to think about implementing this better like numbers
                const stringLength = typeof newValue === 'string' ? newValue.length : 0;
                if (this.repeat.templateArray.length !== stringLength) {
                    this.repeat.clearTemplateArray();
                    for (let i = 0; i < stringLength; i++) {
                        this.repeat.push(newValue[i]);
                    }
                }
            }
            if (this.repeat.arrayType === 'number') {
 
                if (typeof newValue !== 'number') {
                    console.warn('repeat not number:', newValue);
                    newValue = 0;
                }
                if (this.repeat.templateArray.length !== newValue) {
                    if (this.repeat.templateArray.length < newValue) {
                        this.repeat.loopArrayNumber(true, 0, newValue - this.repeat.templateArray.length);
                    } else {
                        this.repeat.loopArrayNumber(true, this.repeat.templateArray.length - newValue, 0);
                    }
                }
            }
        }
    }
}