Skip to content

Commit 17105db

Browse files
committed
feat(core): added new directive for virtual scroll
1 parent efd4765 commit 17105db

8 files changed

Lines changed: 76 additions & 5 deletions

File tree

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngxf",
3-
"version": "6.0.8",
3+
"version": "6.0.9",
44
"scripts": {
55
"ng": "ng",
66
"tsc": "tsc",
@@ -32,6 +32,7 @@
3232
"@angular/pwa": "^0.8.4",
3333
"@angular/router": "7.2.1",
3434
"@angular/service-worker": "7.2.1",
35+
"@ngxd/core": "7.0.1",
3536
"core-js": "^2.5.4",
3637
"hammerjs": "^2.0.8",
3738
"rxjs": "6.3.3",

projects/platform/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ngxf/platform",
3-
"version": "6.0.8",
3+
"version": "6.0.9",
44
"description": "NGXF - Non-State Management for Angular",
55
"keywords": [
66
"non-ngxs",
@@ -40,6 +40,7 @@
4040
"@angular/common": "^6.0.0-rc.0 || ^6.0.0 || ^7.0.0",
4141
"@angular/core": "^6.0.0-rc.0 || ^6.0.0 || ^7.0.0",
4242
"@angular/router": "^6.0.0-rc.0 || ^6.0.0 || ^7.0.0",
43+
"@ngxd/core": "^7.0.1",
4344
"rxjs": ">=6.0.0 || ^5.6.0-forward-compat.4"
4445
}
4546
}

projects/platform/src/lib/directives/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export * from './timeout.directive';
1313
export * from './use-reducer.directive';
1414
export * from './use-state.directive';
1515
export * from './use-effect.directive';
16+
export * from './virtual.directive';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ChangeDetectorRef, Directive, ElementRef, Input, OnDestroy } from '@angular/core';
2+
import { VirtualHandler } from '../tools';
3+
4+
@Directive({ selector: '[virtual]' })
5+
export class VirtualDirective implements OnDestroy {
6+
@Input() virtual: any;
7+
8+
constructor(
9+
private handler: VirtualHandler,
10+
private elementRef: ElementRef,
11+
private cd: ChangeDetectorRef
12+
) {
13+
this.handler.register(this.elementRef.nativeElement, cd);
14+
}
15+
16+
ngOnDestroy() {
17+
this.handler.unregister(this.elementRef.nativeElement);
18+
}
19+
}

projects/platform/src/lib/platform.module.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
TimeoutDirective,
1616
UseEffectDirective,
1717
UseReducerDirective,
18-
UseStateDirective
18+
UseStateDirective,
19+
VirtualDirective
1920
} from './directives';
2021
import { CallPipe } from './pipes';
2122

@@ -34,7 +35,8 @@ const DIRECTIVES = [
3435
TimeoutDirective,
3536
UseReducerDirective,
3637
UseStateDirective,
37-
UseEffectDirective
38+
UseEffectDirective,
39+
VirtualDirective
3840
];
3941

4042
const PIPES = [
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
export * from './cookies.tools';
21
export * from './recompose';
32
export * from './utils';
3+
export * from './cookies.tools';
4+
export * from './virtual.handler';
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Injectable, ChangeDetectorRef, OnDestroy } from '@angular/core';
2+
3+
@Injectable({ providedIn: 'root' })
4+
export class VirtualHandler implements OnDestroy {
5+
6+
private elementLink = new Map<Element, ChangeDetectorRef>();
7+
private observer = new IntersectionObserver(entries => {
8+
entries.forEach(entry => {
9+
const cd = this.elementLink.get(entry.target as Element);
10+
entry.isIntersecting ? attachCD(cd) : detachCD(cd);
11+
});
12+
});
13+
14+
register(element: Element, cd: ChangeDetectorRef) {
15+
this.elementLink.set(element, cd);
16+
this.observer.observe(element);
17+
}
18+
19+
unregister(element: Element) {
20+
this.elementLink.delete(element);
21+
this.observer.unobserve(element);
22+
}
23+
24+
ngOnDestroy() {
25+
this.elementLink.forEach((cd, e) => this.observer.unobserve(e));
26+
this.elementLink.clear();
27+
}
28+
29+
}
30+
31+
function attachCD(cd: ChangeDetectorRef) {
32+
cd.reattach();
33+
cd.detectChanges();
34+
}
35+
36+
function detachCD(cd: ChangeDetectorRef) {
37+
cd.detach();
38+
}

0 commit comments

Comments
 (0)