UNPKG

mframejs

Version:
133 lines 5.64 kB
import { BindingEngine } from '../binding/exported'; export class ArrayMethodCallHandler { constructor(repeat) { this.repeat = repeat; this.name = 'repeat' + this.repeat.value; } call(events) { let sort = true; events.forEach((event) => { 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; if (array) { array.forEach((ctx, i) => { 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: } }); } } export class ArrayPropertyChange { constructor(repeat, expression) { this.repeat = repeat; this.expression = expression; this.name = 'repeat' + this.repeat.value; } 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) => { this.repeat.push(ctx); }); BindingEngine.unSubscribeClassArray(this.repeat.$bindingContext, this.repeat.arrayMethodCallHandler); this.repeat.subscribeArray(); array = null; } } } else { if (Array.isArray(array)) { this.repeat.$array = array; this.repeat.loopBinded(); BindingEngine.unSubscribeClassArray(this.repeat.$bindingContext, this.repeat.arrayMethodCallHandler); if (array) { this.repeat.subscribeArray(); } } else { this.repeat.$array = Array.isArray(array) ? array : []; this.repeat.clearTemplateArray(); BindingEngine.unSubscribeClassArray(this.repeat.$bindingContext, this.repeat.arrayMethodCallHandler); if (array) { this.repeat.subscribeArray(); } } } } } } export class PropertyChangeSimple { constructor(repeat) { this.repeat = repeat; this.name = 'repeat' + this.repeat.value; } call(newValue) { if (this.repeat.isAttached) { if (this.repeat.arrayType === 'string') { 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); } } } } } } //# sourceMappingURL=repeatAttributeSubscriberHelpers.js.map