-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScroller.js
282 lines (249 loc) · 9.4 KB
/
Scroller.js
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* Requires
*/
import { EventDispatcher } from '../Events/EventDispatcher.js';
import { docReady } from '../Events/docReady.js';
import { scrollComplete } from './scrollComplete.js';
import { scrollTo } from './scrollTo.js';
import { mergeObject } from '../Object/mergeObject.js';
import { isPojo } from '../Object/isPojo.js';
import { normalizePath } from '../String/normalizePath';
/**
* @typedef {Object} ScrollerOptions
* @property {number|HTMLElement|Function|Array} offset - Offset pixels, element, Function or Array of arguments, default: null
* @property {boolean} bind - Bind scrollTo links, default: true
* @property {document.body|HTMLElement} context - Context to select scrollTo links from, default: document.body
* @property {string} selector - Scroll to link selector, default: [href^="#"]
* @property {null|document.documentElement|HTMLElement} scrollContext - Context to scroll
* @property {boolean} autoTop - Scroll to top when using only # or #top without an actual element target
* @property {boolean} capture - Capture initial scroll, default: true
* @property {number|'ready'|Array} initial - Initial scroll delay after capture
* @property {null|Function} complete - Complete callback for local scrollTo
*/
/**
* Scroller
* @class
* @extends EventDispatcher
*/
export class Scroller extends EventDispatcher {
/**
* Get url with new hash value
* @public
* @static
* @param {string} hash - New hash string excluding #
* @param {null|string} url - Custom url, default uses location.href
* @return {string} - Updated href string
*/
static getUrlWithHash( hash, url = null ) {
return ( url || window.location.href ).split( '#' )[ 0 ] + '#' + hash;
}
/**
* Config
* @public
* @property
* @type {null|Object|ScrollerOptions}
*/
config = null;
/**
* Initial scroll target
* @public
* @property
* @type {null|HTMLElement}
*/
initial = null;
/**
* Constructor
* @constructor
* @param {Object|ScrollerOptions} options - Scroller options
* @param {null|console|Object} debug - Debug object
*/
constructor( options = {}, debug = null ) {
super( window, null, debug );
// Set default config
this.config = {
offset : 0,
bind : true,
context : document.body,
selector : '[href^="#"], [href*="#"]',
scrollContext : null,
autoTop : false,
capture : true,
initial : 1000,
complete : null,
};
// Update config
if ( isPojo( options ) ) {
mergeObject( this.config, options );
}
// Capture and bind
if ( this.config.capture ) this.#capture();
if ( this.config.bind ) this.bind();
}
/**
* Scroll to wrapper
* @public
* @param {HTMLElement} element - Target element
* @param {null|Function} complete - Complete callback
* @param {number|HTMLElement|Function|Array} offset - Offset
* @param {null|document.documentElement|HTMLElement} context - Scroll context
* @return {void}
*/
scrollTo( element, complete, offset = null, context = null ) {
let params = offset ?? this.config.offset;
if ( !( params instanceof Array ) ) params = [ params ];
params.unshift( context ?? this.config.scrollContext );
params.unshift( element );
if ( typeof complete === 'undefined' ) complete = this.config.complete;
const not_cancelled = this.dispatchEvent( 'scroll.before', {
scrollTarget : element,
params : params,
}, true, true );
if ( not_cancelled ) {
scrollComplete( () => {
this.dispatchEvent( 'scroll.after', { scrollTarget : element } );
if ( typeof complete === 'function' ) complete( element );
} );
scrollTo( ...params );
}
}
/**
* Event scroll to click
* @private
* @param {Event} event - Click event
* @return {void}
*/
#event_scrollToClick( event ) {
// Check if action is disabled
if ( event.currentTarget.getAttribute( 'data-scrollto' ) !== 'true' ) {
return;
}
// Find the target id
let id = null;
const href = event.currentTarget.getAttribute( 'href' );
const hash = href.indexOf( '#' );
if ( hash === 0 ) {
id = href.substring( 1 );
} else if ( hash > 0 ) {
const parts = href.split( '#' );
if ( normalizePath( location.pathname ) === normalizePath( parts[ 0 ] ) ) id = parts[ 1 ] || '';
}
// Find the target
let target = document.getElementById( id );
if ( this.config.autoTop && !target && ( id === '' || id === 'top' ) ) {
target = document.body;
}
// Scroll to target or warn in debug mode
if ( target ) {
this.scrollTo( target );
event.preventDefault();
} else if ( this.debug ) {
this.debug.warn( this.constructor.name + '::event_scrollToClick No valid target for: ', id, event.currentTarget );
}
}
/**
* Bind scroll to events
* @public
* @param {null|document.body|HTMLElement} context - Context to select scrollTo links from, default: document.body
* @param {null|string} selector - Scroll to link selector, default: [href^="#"]
* @return {void}
*/
bind( context = null, selector = null ) {
// Get config defaults
context = context || this.config.context;
selector = selector || this.config.selector;
// Find links
const links = this.config.context.querySelectorAll( selector );
if ( !links.length && this.debug ) {
this.debug.warn( this.constructor.name + '::bind No scrollTo links found in context:', context );
}
// Bind all unbound links
for ( let i = 0; i < links.length; i++ ) {
if ( !links[ i ].hasAttribute( 'data-scrollto' ) ) {
links[ i ].addEventListener( 'click', ( event ) => { this.#event_scrollToClick( event ); } );
links[ i ].setAttribute( 'data-scrollto', 'true' );
}
}
}
/**
* Capture hash scroll
* @private
* @return {void}
*/
#capture() {
const hash = window.location.hash;
// Catch hash and prevent native scroll, to allow initial smooth scroll
if ( hash && hash.length > 1 ) {
// Only update if an actual target is found
// TODO: and the target must be visible
this.initial = document.getElementById( hash.substring( 1 ) );
if ( this.initial ) {
history.replaceState( null, document.title, this.constructor.getUrlWithHash( 's2:' + hash.substring( 1 ) ) );
}
}
// Scroll when ready
if ( this.config.initial === 'ready' ) {
docReady( () => { this.#initial_scroll( hash ); } );
} else if ( this.config.initial instanceof Array ) {
this.#initial_bind_custom( [ ...this.config.initial ] );
} else if ( typeof this.config.initial === 'number' ) {
// Delayed initial scroll
window.setTimeout( () => { this.#initial_scroll( hash ); }, this.config.initial );
} else {
// Instant scroll
this.#initial_scroll( hash );
}
}
/**
* Bind custom initial event
* @private
* @param {string} hash - Hash reference
* @param {Array} params - Arguments
* @return {void}
*/
#initial_bind_custom( hash, params ) {
// Get event target
const target = params.shift();
// Verify options
if ( typeof target.addEventListener !== 'function' ) {
throw new Error( this.constructor.name + '::capture() First initial argument must be an event target' );
}
if ( typeof params[ 0 ] !== 'string' || !params[ 0 ].length ) {
throw new Error( this.constructor.name + '::capture() Second initial argument must be an event name' );
}
if ( typeof params[ 1 ] !== 'function' ) {
throw new Error( this.constructor.name + '::capture() Thrid initial argument must be an event handler' );
}
// Wrap callback
const callback = params[ 1 ];
params[ 1 ] = ( event ) => {
callback( event, hash, () => { this.#initial_complete( hash ); } );
};
// By default add once option
if ( typeof params[ 2 ] === 'undefined' ) {
params[ 2 ] = { once : true };
}
target.addEventListener( ...params );
}
/**
* Scroll to initial element and reset hash
* @private
* @param {string} hash - Hash to reset to
* @return {void}
*/
#initial_scroll( hash ) {
if ( this.initial instanceof HTMLElement ) {
// Scroll to initial target and restore hash after scroll complete
this.scrollTo( this.initial, () => { this.#initial_complete( hash ); } );
}
}
/**
* Reset hash
* @private
* @param {string} hash - Hash to reset to
* @return {void}
*/
#initial_complete( hash ) {
history.replaceState( null, document.title, this.constructor.getUrlWithHash( hash.substring( 1 ) ) );
this.dispatchEvent( 'scroll.initial.complete', { initial : this.initial } );
}
}