-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscroll_controller.js
More file actions
164 lines (101 loc) · 3.86 KB
/
scroll_controller.js
File metadata and controls
164 lines (101 loc) · 3.86 KB
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
/*
An ES6 singleton scroll controller for handling the window's scrollbar.
*/
const DEBUG = false;
class ScrollController {
constructor() {
if ( DEBUG ) console.log( 'ScrollController initialised.' );
this.activated = false;
this._window = window;
this._document = document;
this._body = document.body;
this._expander = document.createElement( 'div' );
this._expander.style.cssText = 'position: absolute; left: 0; top: 0; width: 1px;';
this._position = 0;
this._height = 0;
this._position_stack = [];
this._onWindowScroll = this._onWindowScroll.bind( this );
}
// Accessors
getPosition() {
return this._position;
}
getCapturePosition() {
return this._position_stack[ 0 ];
}
getHeight() {
return this._height;
}
// Bindings
_attachEvents() {
if ( DEBUG ) console.log( 'ScrollController._attachEvents' );
this._window.addEventListener( 'scroll', this._onWindowScroll, false );
}
_detachEvents() {
if ( DEBUG ) console.log( 'ScrollController._detachEvents' );
this._window.removeEventListener( 'scroll', this._onWindowScroll, false );
}
_onWindowScroll() {
let y = this._fetchScrollPosition();
if ( DEBUG ) console.log( 'ScrollController._onWindowScroll: ', y );
this._updatePosition( y );
}
// Internal state
_activate( height ) {
if ( DEBUG ) console.log( 'ScrollController._activate - requested...' );
if ( this.activated ) { return; }
this.activated = true;
if ( DEBUG ) console.log( 'ScrollController._activate - ACTIVATED!' );
this._attachEvents();
this._updateHeight( height );
this._body.appendChild( this._expander );
}
_deactivate() {
if ( DEBUG ) console.log( 'ScrollController._deactivate - requested...' );
if ( !this.activated ) { return; }
this.activated = false;
if ( DEBUG ) console.log( 'ScrollController._deactivate - DEACTIVATED!' );
this._detachEvents();
this._updateHeight( 0 );
this._body.removeChild( this._expander );
}
// Helpers
_fetchScrollPosition() {
return this._window.pageYOffset || this._document.documentElement.scrollTop;
}
_updatePosition(y) {
if ( y === undefined ) { console.warn( 'ScrollController._updatePosition called without the "y" argument' ); }
this._position = y;
this._window.scrollTo( 0, y );
}
_updateHeight(h) {
if ( h === undefined ) { console.warn( 'ScrollController._updateHeight called without the "h" argument' ); }
this._height = h;
this._expander.style.height = h + 'px';
}
// Public methods
// -------------------------------------------------------------------------
capture(height = 0) {
// Store the previous scroll position
this._position_stack.push( this._fetchScrollPosition() );
// Start listening to scroll and update the expander
this._activate( height );
}
release (revert = true) {
// Stop listening to scroll and remove the expander
this._deactivate();
// Return the scroll position to where it was before capture
if ( revert ) { this._updatePosition( this._position_stack.pop() ); }
}
resize(height) {
if ( !this.activated ) { return; }
// Update the expander
this._updateHeight( height );
}
reposition(y) {
if ( !this.activated ) { return; }
this._updatePosition( y );
}
}
let instance = new ScrollController();
export default function () { return instance };