-
Notifications
You must be signed in to change notification settings - Fork 22
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: lecture-5 HW #122
base: master
Are you sure you want to change the base?
feat: lecture-5 HW #122
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,9 @@ export class ProductsListComponent implements OnInit { | |
// eslint-disable-next-line no-console | ||
console.log(event); | ||
} | ||
|
||
onLoad(event: unknown) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему в типизации поставил There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это была заглушка про которую я потом забыл, исправлю |
||
// eslint-disable-next-line no-console | ||
console.log(event); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
import {NgModule} from '@angular/core'; | ||
import {CommonModule} from '@angular/common'; | ||
import {MatProgressSpinnerModule} from '@angular/material/progress-spinner'; | ||
import {ScrollWithLoadModule} from 'src/app/shared/scroll-with-load/scroll-with-load.module'; | ||
import {ProductsListComponent} from './products-list.component'; | ||
import {CardModule} from './card/card.module'; | ||
import {DumpNgIfModule} from '../../shared/dump-ng-if/dump-ng-if.module'; | ||
|
||
@NgModule({ | ||
declarations: [ProductsListComponent], | ||
imports: [CommonModule, CardModule, DumpNgIfModule, MatProgressSpinnerModule], | ||
imports: [ | ||
CommonModule, | ||
CardModule, | ||
DumpNgIfModule, | ||
MatProgressSpinnerModule, | ||
ScrollWithLoadModule, | ||
], | ||
exports: [ProductsListComponent], | ||
}) | ||
export class ProductsListModule {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum LoadDirection { | ||
UP, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше значениям There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Спасибо за совет, думаю так действительно лучше |
||
DOWN, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const SCROLL_CONST = 100; |
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 {ScrollWithLoadingDirective} from './scroll-with-loading.directive'; | ||
|
||
@NgModule({ | ||
declarations: [ScrollWithLoadingDirective], | ||
imports: [CommonModule], | ||
exports: [ScrollWithLoadingDirective], | ||
}) | ||
export class ScrollWithLoadModule {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {ScrollWithLoadingDirective} from './scroll-with-loading.directive'; | ||
|
||
describe('ScrollWithLoadingDirective', () => { | ||
it('should create an instance', () => { | ||
const directive = new ScrollWithLoadingDirective(); | ||
|
||
expect(directive).toBeTruthy(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import {Directive, Output, EventEmitter, HostListener} from '@angular/core'; | ||
import {LoadDirection} from './load-direction'; | ||
import {bottomLoadCondition} from './utils/bottom-load-condition'; | ||
import {upLoadCondition} from './utils/up-load-condition'; | ||
|
||
@Directive({ | ||
selector: '[appScrollWithLoading]', | ||
}) | ||
export class ScrollWithLoadingDirective { | ||
@Output() loadData = new EventEmitter<LoadDirection>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я бы There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Принял |
||
|
||
private prevScrollTop = -1; // Значение требуется для сравнения, из него можно вычислить направление скролла. | ||
|
||
@HostListener('scroll', ['$event.target']) scrollAction({ | ||
scrollHeight, | ||
clientHeight, | ||
scrollTop, | ||
}: HTMLElement) { | ||
const prevScrollTop = this.prevScrollTop; // Сохраняем предыдущее значение скролла | ||
|
||
this.prevScrollTop = scrollTop; // Заменяем текущим значением скролла | ||
|
||
const lowerScrollPosition = scrollHeight - clientHeight; // Нижнее значение на которое может быть промотан скролл. lowerScrollPosition + clientHeight = scrollHeight | ||
|
||
const loadDown = bottomLoadCondition(scrollTop, lowerScrollPosition, prevScrollTop); | ||
|
||
if (loadDown) { | ||
this.loadData.emit(LoadDirection.DOWN); | ||
|
||
return; | ||
} | ||
|
||
const loadUp = upLoadCondition(scrollTop, prevScrollTop); | ||
|
||
if (loadUp) { | ||
this.loadData.emit(LoadDirection.UP); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {SCROLL_CONST} from '../scroll-const'; | ||
|
||
export function bottomLoadCondition( | ||
scrollTop: number, | ||
lowerScrollPosition: number, | ||
prevScrollTop: number, | ||
): boolean { | ||
const nearBotttomBorder = lowerScrollPosition - scrollTop < SCROLL_CONST; | ||
// true, когда до нижней границы останется меньше значения SCROLL_CONST; | ||
const bottomScrollContinue = scrollTop > prevScrollTop; | ||
// Проверяет направление промотки, и не даёт сработать если промотка идёт вверх. | ||
|
||
return nearBotttomBorder && bottomScrollContinue; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {SCROLL_CONST} from '../scroll-const'; | ||
|
||
export function upLoadCondition(scrollTop: number, prevScrollTop: number): boolean { | ||
const nearUpBorder = scrollTop < SCROLL_CONST; | ||
// true, когда попадаем в диапазон 0 - SCROLL_CONST | ||
const scrollUpContinue = scrollTop < prevScrollTop; | ||
// проверяет направление, выбросит false если промотка направлена вниз | ||
|
||
return nearUpBorder && scrollUpContinue; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Неплохо было бы подчистить)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Принял