Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add component scroll loading #96

Open
wants to merge 1 commit into
base: lecture-5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {HeaderModule} from './components/header/header.module';
import {ProductsListModule} from './pages/products-list/products-list.module';
import {SidenavModule} from './components/sidenav/sidenav.module';
import {InsertShadowModule} from './shared/insert-shadow/insert-shadow.module';
import {ScrollLoadingDirective} from './shared/scroll-loading/scroll-loading.directive';

@NgModule({
declarations: [AppComponent],
Expand All @@ -22,6 +23,7 @@ import {InsertShadowModule} from './shared/insert-shadow/insert-shadow.module';
SidenavModule,
MatListModule,
InsertShadowModule,
ScrollLoadingDirective,
],
bootstrap: [AppComponent],
})
Expand Down
14 changes: 7 additions & 7 deletions src/app/pages/products-list/products-list.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

<ng-template let-value [ngIf]="products" [ngIfElse]="loaderTemplate">
<!-- Раскомментировать div при выполнении ДЗ -->
<!-- <div appScrollWithLoading class="scroll-container" (loadData)="onLoad($event)"> -->
<app-card class="card" [product]="value[0] || null"></app-card>
<app-card class="card" [product]="value[1] || null"></app-card>
<app-card class="card" [product]="value[2] || null"></app-card>
<app-card class="card" [product]="value[3] || null"></app-card>
<app-card class="card" [product]="value[4] || null"></app-card>
<!-- </div> -->
<div appScrollLoading class="scroll-container" (loadData)="onLoad($event)">
<app-card class="card" [product]="value[0] || null"></app-card>
<app-card class="card" [product]="value[1] || null"></app-card>
<app-card class="card" [product]="value[2] || null"></app-card>
<app-card class="card" [product]="value[3] || null"></app-card>
<app-card class="card" [product]="value[4] || null"></app-card>
</div>
</ng-template>

<ng-template #loaderTemplate>
Expand Down
6 changes: 6 additions & 0 deletions src/app/pages/products-list/products-list.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Component, OnInit} from '@angular/core';
import {Direction} from 'src/app/shared/scroll-loading/scroll-loading.const';
import {productsMock} from '../../shared/products/products.mock';
import {IProduct} from '../../shared/products/product.interface';

Expand All @@ -22,4 +23,9 @@ export class ProductsListComponent implements OnInit {
this.productsStore = productsMock;
}, 4000);
}

onLoad(event: Direction) {
/* eslint no-console:0 */
console.log('event', event);
}
}
3 changes: 2 additions & 1 deletion src/app/pages/products-list/products-list.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {MatProgressSpinnerModule} from '@angular/material/progress-spinner';
import {ProductsListComponent} from './products-list.component';
import {CardModule} from './card/card.module';
import {IfModule} from '../../shared/if/if.module';
import {ScrollLoadingDirective} from '../../shared/scroll-loading/scroll-loading.directive';

@NgModule({
declarations: [ProductsListComponent],
imports: [CardModule, IfModule, CommonModule, MatProgressSpinnerModule],
imports: [CardModule, IfModule, CommonModule, MatProgressSpinnerModule, ScrollLoadingDirective],
exports: [ProductsListComponent],
})
export class ProductsListModule {}
6 changes: 6 additions & 0 deletions src/app/shared/scroll-loading/scroll-loading.const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const offset = 100;

export enum Direction {
Top = 'top',
Bottom = 'bottom',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {ScrollLoadingDirective} from './scroll-loading.directive';

describe('ScrollLoadingDirective', () => {
it('should create an instance', () => {
const directive = new ScrollLoadingDirective();

expect(directive).toBeTruthy();
});
});
31 changes: 31 additions & 0 deletions src/app/shared/scroll-loading/scroll-loading.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {Directive, EventEmitter, HostListener, Output} from '@angular/core';
import {Direction, offset} from './scroll-loading.const';

@Directive({
selector: '[appScrollLoading]',
standalone: true,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему поставил stadealone: true?

})
export class ScrollLoadingDirective {
@Output() loadData = new EventEmitter<Direction>();

protected scrollTop = 0;

@HostListener('scroll', ['$event'])
onMouseScroll(event: Event) {
const {scrollTop, clientHeight, scrollHeight} = event.target as HTMLElement;

// Проверяем, что спускаемся вниз
if (this.scrollTop < scrollTop) {
if (clientHeight + scrollTop > scrollHeight - offset) {
// Генерируем событие с направлением вниз
this.loadData.emit(Direction.Bottom);
}
} else if (scrollTop < offset) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Мы с тобой не обсуждали, что else/if - не лучшая практика?)
Если нет - могу подкинуть аргументов, если интересно, только дай знать🙃

// Или поднимаемся наверх
// Генерируем событие с направлением вверх
this.loadData.emit(Direction.Top);
}

this.scrollTop = scrollTop;
}
}
10 changes: 10 additions & 0 deletions src/app/shared/scroll-loading/scroll-loading.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {ScrollLoadingDirective} from './scroll-loading.directive';

@NgModule({
declarations: [ScrollLoadingDirective],
exports: [ScrollLoadingDirective],
imports: [CommonModule],
})
export class ScrollLoadingModule {}