]>
Commit | Line | Data |
---|---|---|
9af61e84 | 1 | import { Directive, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core' |
8cac1b64 | 2 | import 'rxjs/add/operator/debounceTime' |
0cd4344f | 3 | import 'rxjs/add/operator/distinct' |
3bcfff7f | 4 | import 'rxjs/add/operator/distinctUntilChanged' |
8cac1b64 C |
5 | import 'rxjs/add/operator/filter' |
6 | import 'rxjs/add/operator/map' | |
9af61e84 | 7 | import 'rxjs/add/operator/share' |
0cd4344f | 8 | import 'rxjs/add/operator/startWith' |
a9ca764e | 9 | import 'rxjs/add/operator/throttleTime' |
0cd4344f | 10 | import { fromEvent } from 'rxjs/observable/fromEvent' |
9af61e84 | 11 | import { Subscription } from 'rxjs/Subscription' |
0cd4344f C |
12 | |
13 | @Directive({ | |
14 | selector: '[myInfiniteScroller]' | |
15 | }) | |
9af61e84 | 16 | export class InfiniteScrollerDirective implements OnInit, OnDestroy { |
0cd4344f C |
17 | private static PAGE_VIEW_TOP_MARGIN = 500 |
18 | ||
19 | @Input() containerHeight: number | |
20 | @Input() pageHeight: number | |
21 | @Input() percentLimit = 70 | |
22 | @Input() autoLoading = false | |
23 | ||
24 | @Output() nearOfBottom = new EventEmitter<void>() | |
25 | @Output() nearOfTop = new EventEmitter<void>() | |
26 | @Output() pageChanged = new EventEmitter<number>() | |
27 | ||
28 | private decimalLimit = 0 | |
29 | private lastCurrentBottom = -1 | |
30 | private lastCurrentTop = 0 | |
9af61e84 C |
31 | private scrollDownSub: Subscription |
32 | private scrollUpSub: Subscription | |
33 | private pageChangeSub: Subscription | |
0cd4344f C |
34 | |
35 | constructor () { | |
36 | this.decimalLimit = this.percentLimit / 100 | |
37 | } | |
38 | ||
39 | ngOnInit () { | |
40 | if (this.autoLoading === true) return this.initialize() | |
41 | } | |
42 | ||
9af61e84 C |
43 | ngOnDestroy () { |
44 | if (this.scrollDownSub) this.scrollDownSub.unsubscribe() | |
45 | if (this.scrollUpSub) this.scrollUpSub.unsubscribe() | |
46 | if (this.pageChangeSub) this.pageChangeSub.unsubscribe() | |
47 | } | |
48 | ||
0cd4344f | 49 | initialize () { |
6a6d92b1 | 50 | // Emit the last value |
2c6bbd97 | 51 | const throttleOptions = { leading: true, trailing: true } |
6a6d92b1 | 52 | |
0cd4344f C |
53 | const scrollObservable = fromEvent(window, 'scroll') |
54 | .startWith(true) | |
6a6d92b1 | 55 | .throttleTime(200, undefined, throttleOptions) |
0cd4344f | 56 | .map(() => ({ current: window.scrollY, maximumScroll: document.body.clientHeight - window.innerHeight })) |
2c6bbd97 | 57 | .distinctUntilChanged((o1, o2) => o1.current === o2.current) |
6a6d92b1 | 58 | .share() |
0cd4344f C |
59 | |
60 | // Scroll Down | |
9af61e84 | 61 | this.scrollDownSub = scrollObservable |
0cd4344f C |
62 | // Check we scroll down |
63 | .filter(({ current }) => { | |
64 | const res = this.lastCurrentBottom < current | |
65 | ||
66 | this.lastCurrentBottom = current | |
67 | return res | |
68 | }) | |
69 | .filter(({ current, maximumScroll }) => maximumScroll <= 0 || (current / maximumScroll) > this.decimalLimit) | |
0cd4344f C |
70 | .subscribe(() => this.nearOfBottom.emit()) |
71 | ||
72 | // Scroll up | |
9af61e84 | 73 | this.scrollUpSub = scrollObservable |
0cd4344f C |
74 | // Check we scroll up |
75 | .filter(({ current }) => { | |
76 | const res = this.lastCurrentTop > current | |
77 | ||
78 | this.lastCurrentTop = current | |
79 | return res | |
80 | }) | |
81 | .filter(({ current, maximumScroll }) => { | |
82 | return current !== 0 && (1 - (current / maximumScroll)) > this.decimalLimit | |
83 | }) | |
0cd4344f C |
84 | .subscribe(() => this.nearOfTop.emit()) |
85 | ||
86 | // Page change | |
9af61e84 | 87 | this.pageChangeSub = scrollObservable |
0cd4344f | 88 | .distinct() |
6a6d92b1 | 89 | .map(({ current }) => this.calculateCurrentPage(current)) |
0cd4344f C |
90 | .distinctUntilChanged() |
91 | .subscribe(res => this.pageChanged.emit(res)) | |
92 | } | |
93 | ||
6a6d92b1 C |
94 | private calculateCurrentPage (current: number) { |
95 | return Math.max(1, Math.round((current + InfiniteScrollerDirective.PAGE_VIEW_TOP_MARGIN) / this.pageHeight)) | |
96 | } | |
0cd4344f | 97 | } |