/
scroll-collapse.directive.ts
222 lines (219 loc) · 6.2 KB
/
scroll-collapse.directive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import {
Directive,
ElementRef,
HostBinding,
Input,
NgZone,
AfterViewInit,
OnDestroy
} from '@angular/core';
import { Subject } from 'rxjs/Subject';
import {
debounceTime,
takeUntil,
bufferCount,
map,
startWith,
distinctUntilChanged,
tap
} from 'rxjs/operators';
import { fromEvent } from 'rxjs/observable/fromEvent';
import { merge } from 'rxjs/observable/merge';
import { WindowRef } from '@thisissoon/angular-inviewport';
import { Viewport, Direction } from './shared';
import * as eventData from './shared/event-data';
import * as classes from './shared/classes';
/**
* A simple lightweight library for Angular that detects scroll direction
* and adds a `sn-scrolling-up` or `sn-scrolling-down` class to the element.
* The library can also detect when the user has scrolled passed the element
* and apply a `sn-affix` class. Useful for make a element sticky when the
* user has scrolled beyond it. This library can will also apply `sn-minimise`
* class after the user has scrolled beyond the height of the element.
*
* @example
* ```
* <p snScrollCollapse [debounce]="0">Amet tempor excepteur occaecat nulla.</p>
* ```
*
*/
@Directive({
selector: '[snScrollCollapse]'
})
export class ScrollCollapseDirective implements AfterViewInit, OnDestroy {
/**
* The last scroll direction
*
* @memberof ScrollCollapseDirective
*/
private scrollDirection: Direction;
/**
* Original offsetTop of element
*
* @memberof ScrollCollapseDirective
*/
public originalTop: number;
/**
* Original offsetHeight of element
*
* @memberof ScrollCollapseDirective
*/
public originalHeight: number;
/**
* Completes on component destroy lifecycle event
* use to handle unsubscription from infinite observables
*
* @memberof ScrollCollapseDirective
*/
private ngUnsubscribe$ = new Subject<void>();
/**
* Amount of time in ms to wait for other scroll events
* before running event handler
*
* @default 0
* @memberof ScrollCollapseDirective
*/
@Input()
public debounce = 0;
/**
* Returns true if last scroll direction is UP
*
* @readonly
* @memberof ScrollCollapseDirective
*/
@HostBinding(classes.directionUpClass)
public get isScrollingUp(): boolean {
return this.scrollDirection === Direction.UP;
}
/**
* Returns true if last scroll direction is DOWN
*
* @readonly
* @memberof ScrollCollapseDirective
*/
@HostBinding(classes.directionDownClass)
public get isScrollingDown(): boolean {
return this.scrollDirection === Direction.DOWN;
}
/**
* Returns true if the user has scrolled pass the original `offsetTop`
* position of the element.
*
* @memberof ScrollCollapseDirective
*/
@HostBinding(classes.affixClass)
public affixMode = false;
/**
* Returns true if the user has scrolled pass the origin height of
* the element assuming the element is fixed at the top of the page
*
* @memberof ScrollCollapseDirective
*/
@HostBinding(classes.minimiseClass)
public minimiseMode = false;
/**
* Creates an instance of ScrollCollapseDirective.
* @memberof ScrollCollapseDirective
*/
constructor(
private el: ElementRef,
private ngZone: NgZone,
private windowRef: WindowRef
) { }
/**
* Subscribe to window resize events as an observable
* will calculate directive values
*
* @memberof ScrollCollapseDirective
*/
public ngAfterViewInit(): void {
const el: HTMLElement = this.el.nativeElement;
this.originalTop = el.offsetTop;
this.originalHeight = el.offsetHeight;
this.ngZone.runOutsideAngular(() => {
merge(
fromEvent(this.windowRef as any, eventData.eventScroll),
fromEvent(this.windowRef as any, eventData.eventResize)
)
.pipe(
startWith(null),
map(() => this.getViewport()),
bufferCount(2, 1),
distinctUntilChanged(),
// Do not apply debounce operator if debounce is set to 0
this.debounce ? debounceTime(this.debounce) : tap(null),
takeUntil(this.ngUnsubscribe$)
)
.subscribe((events: Viewport[]) =>
this.ngZone.run(() => this.onScrollOrResizeEvent(events))
);
});
}
/**
* Event handler for scroll and resize events
* Calculates values scroll direction, affix and
* minimise properties
*
* @memberof ScrollCollapseDirective
*/
public onScrollOrResizeEvent(events: Viewport[]): void {
const previousEvent = events[0];
const currentEvent = events[1];
this.calculateScrollDirection(events);
this.calculateMinimiseMode(currentEvent);
this.calculateAffixMode(currentEvent);
}
/**
* Calculate last scroll direction by comparing y scroll position
* of last two values of `viewport$` observable
*
* @memberof ScrollCollapseDirective
*/
public calculateScrollDirection(events: Viewport[]): void {
const pastEvent = events[0];
const currentEvent = events[1];
this.scrollDirection = (pastEvent.scrollY > currentEvent.scrollY) ?
Direction.UP : Direction.DOWN;
}
/**
* Calculate if the user has scrolled pass the origin height of
* the element assuming the element is fixed at the top of the page
*
* @memberof ScrollCollapseDirective
*/
public calculateMinimiseMode(viewport: Viewport): void {
this.minimiseMode = viewport.scrollY >= (this.originalHeight + this.originalTop);
}
/**
* Calculate if the user has scrolled pass the origin height of
* the element assuming the element is fixed at the top of the page
*
* @memberof ScrollCollapseDirective
*/
public calculateAffixMode(viewport: Viewport): void {
this.affixMode = viewport.scrollY >= this.originalTop;
}
/**
* Return current viewport values
*
* @memberof ScrollCollapseDirective
*/
public getViewport(): Viewport {
return {
height: this.windowRef.innerHeight,
width: this.windowRef.innerWidth,
scrollY: this.windowRef.scrollY,
scrollX: this.windowRef.scrollX
};
}
/**
* trigger `ngUnsubscribe` complete on
* component destroy lifecycle hook
*
* @memberof ScrollCollapseDirective
*/
public ngOnDestroy(): void {
this.ngUnsubscribe$.next();
this.ngUnsubscribe$.complete();
}
}