UNPKG

jpush-ui

Version:

极光大数据前端Angular组件(^6.1.0)

118 lines 4.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var core_1 = require("@angular/core"); var range_model_1 = require("./range.model"); /* tslint:disable:no-input-rename */ var RangeDirective = /** @class */ (function () { function RangeDirective(viewcontainer, templateRef) { this.viewcontainer = viewcontainer; this.templateRef = templateRef; this.last = { start: 0, end: 0, }; } RangeDirective.prototype.ngOnInit = function () { if (this.end > this.start) { // this.applyChanges(); } else { console.warn('to后的参数不应该比from小'); return; } }; RangeDirective.prototype.ngOnChanges = function (changes) { if ('start' in changes || 'end' in changes) { if (this.start === undefined) { if (this.end < 0) { console.warn('from默认为0,此时to后的参数不能为负'); return; } this.start = 0; } if (this.end > this.start) { this.applyChanges(); } else { console.warn('to后的参数不应该比from小(from默认为0)'); } } }; RangeDirective.prototype.applyChanges = function () { var lastStart = this.last.start; var lastEnd = this.last.end; var start = this.start; var end = this.end; if (lastStart !== this.start || lastEnd !== this.end) { // 找出要添加或删除的项(只可能操作两头) if (start > lastEnd || end < lastStart) { this.viewcontainer.clear(); this.updateList(start, end); } else { // 前面判断 if (start < lastStart) { // 前面加 this.updateList(start, lastStart); } else if (start > lastStart) { // 前面减 this.updateList(lastStart, start, false); } // 后面判断 if (end < lastEnd) { // 后面减 this.updateList(end, lastEnd, false, false); } else if (end > lastEnd) { // 后面加 this.updateList(lastEnd, end, true, false); } } // 最后修改last值 this.last.start = this.start; this.last.end = this.end; } }; RangeDirective.prototype.updateList = function (start, end, isAdd, isFront) { if (isAdd === void 0) { isAdd = true; } if (isFront === void 0) { isFront = true; } var index = 0; if (!isFront) { index = this.viewcontainer.length; } for (var i = start; i < end; i++) { if (isAdd) { // 添加View var view = this.viewcontainer.createEmbeddedView(this.templateRef, new range_model_1.RangeContext(i), index); index++; // 不管面还是后面,都要加 // view.detectChanges(); } else { // 删除View if (!isFront) { --index; // 反着删 } this.viewcontainer.remove(index); } } }; RangeDirective.decorators = [ { type: core_1.Directive, args: [{ selector: '[range][rangeTo]' },] }, ]; /** @nocollapse */ RangeDirective.ctorParameters = function () { return [ { type: core_1.ViewContainerRef, }, { type: core_1.TemplateRef, }, ]; }; RangeDirective.propDecorators = { "start": [{ type: core_1.Input, args: ['rangeFrom',] },], "end": [{ type: core_1.Input, args: ['rangeTo',] },], }; return RangeDirective; }()); exports.RangeDirective = RangeDirective; //# sourceMappingURL=range.directive.js.map