ng-track-by
Version:
trackBy and trackById directives which provide TrackByFunction to ngForTrackBy
113 lines (105 loc) • 5.17 kB
JavaScript
import * as i1 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { Directive, NgModule, Input } from '@angular/core';
import { not } from 'logical-not';
function changeTrackBy(ngForOf, trackBy, property) {
ngForOf.ngForTrackBy = trackBy;
patch(ngForOf, trackBy, property);
}
function patch(ngForOf, trackBy, property) {
const differ = ngForOf._differ;
differ._trackByFn = trackBy;
const linkedRecordsMap = differ._linkedRecords.map;
differ.forEachItem((record) => {
const trackById = record.item[property];
linkedRecordsMap.set(trackById, linkedRecordsMap.get(record.trackById));
linkedRecordsMap.delete(record.trackById);
record.trackById = record.item[property];
});
}
const markedKey = Symbol();
function marked(ngForOf) {
const marked = markedKey in ngForOf;
ngForOf[markedKey] = undefined;
return marked;
}
const trackById = (_, item) => {
return item.id;
};
class TrackByIdDirective {
constructor(ngForOf) {
this.ngForOf = ngForOf;
}
ngOnInit() {
if (not(marked(this.ngForOf)))
changeTrackBy(this.ngForOf, trackById, "id");
}
}
TrackByIdDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.0", ngImport: i0, type: TrackByIdDirective, deps: [{ token: i1.NgForOf }], target: i0.ɵɵFactoryTarget.Directive });
TrackByIdDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "12.2.0", type: TrackByIdDirective, selector: "[trackById]", ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.0", ngImport: i0, type: TrackByIdDirective, decorators: [{
type: Directive,
args: [{
selector: "[trackById]",
}]
}], ctorParameters: function () { return [{ type: i1.NgForOf }]; } });
class TrackByIdModule {
}
TrackByIdModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.0", ngImport: i0, type: TrackByIdModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
TrackByIdModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.2.0", ngImport: i0, type: TrackByIdModule, declarations: [TrackByIdDirective], imports: [CommonModule], exports: [TrackByIdDirective] });
TrackByIdModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.2.0", ngImport: i0, type: TrackByIdModule, imports: [[CommonModule]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.0", ngImport: i0, type: TrackByIdModule, decorators: [{
type: NgModule,
args: [{
imports: [CommonModule],
exports: [TrackByIdDirective],
declarations: [TrackByIdDirective],
}]
}] });
class TrackByDirective {
constructor(ngForOf) {
this.ngForOf = ngForOf;
}
ngOnInit() {
if (not(marked(this.ngForOf)))
changeTrackBy(this.ngForOf, this.createTrackByFn(), this.trackBy);
}
createTrackByFn() {
const key = this.trackBy;
return function (_, item) {
return item[key];
};
}
}
TrackByDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.0", ngImport: i0, type: TrackByDirective, deps: [{ token: i1.NgForOf }], target: i0.ɵɵFactoryTarget.Directive });
TrackByDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "12.2.0", type: TrackByDirective, selector: "[trackBy]", inputs: { trackBy: "trackBy" }, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.0", ngImport: i0, type: TrackByDirective, decorators: [{
type: Directive,
args: [{
selector: "[trackBy]",
}]
}], ctorParameters: function () { return [{ type: i1.NgForOf }]; }, propDecorators: { trackBy: [{
type: Input
}] } });
class TrackByModule {
}
TrackByModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.0", ngImport: i0, type: TrackByModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
TrackByModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.2.0", ngImport: i0, type: TrackByModule, declarations: [TrackByDirective], imports: [CommonModule], exports: [TrackByDirective] });
TrackByModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.2.0", ngImport: i0, type: TrackByModule, imports: [[CommonModule]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.0", ngImport: i0, type: TrackByModule, decorators: [{
type: NgModule,
args: [{
imports: [CommonModule],
exports: [TrackByDirective],
declarations: [TrackByDirective],
}]
}] });
/*
* Public API Surface of ng-track-by
*/
/**
* Generated bundle index. Do not edit.
*/
export { TrackByDirective, TrackByIdDirective, TrackByIdModule, TrackByModule };
//# sourceMappingURL=ng-track-by.js.map