Debian Codegraph @

skins/Vector/resources/skins.vector.js/scrollObserver.js

raw symbols SCIP
1 const
2 SCROLL_TITLE_HOOK = 'vector.page_title_scroll',
3 SCROLL_TITLE_CONTEXT_ABOVE = 'scrolled-above-page-title',
4 SCROLL_TITLE_CONTEXT_BELOW = 'scrolled-below-page-title',
5 SCROLL_TITLE_ACTION = 'scroll-to-top';
6
7 /**
8 * Fire a hook to be captured by WikimediaEvents for scroll event logging.
9 *
10 * @param {string} direction the scroll direction
11 */
12 function firePageTitleScrollHook( direction ) {
13 /**
14 * For use by WikimediaEvents only.
15 *
16 * @event vector.page_title_scroll
17 * @internal
18 * @property {string} context
19 * @property {string} action
20 */
21 if ( direction === 'down' ) {
22 mw.hook( SCROLL_TITLE_HOOK ).fire( {
23 context: SCROLL_TITLE_CONTEXT_BELOW
24 } );
25 } else {
26 mw.hook( SCROLL_TITLE_HOOK ).fire( {
27 context: SCROLL_TITLE_CONTEXT_ABOVE,
28 action: SCROLL_TITLE_ACTION
29 } );
30 }
31 }
32
33 /**
34 * Create an observer for showing/hiding feature and for firing scroll event hooks.
35 *
36 * @param {Function} show functionality for when feature is visible
37 * @param {Function} hide functionality for when feature is hidden
38 * @return {IntersectionObserver}
39 */
40 function initScrollObserver( show, hide ) {
41 return new IntersectionObserver( ( entries ) => {
42 if ( !entries[ 0 ].isIntersecting && entries[ 0 ].boundingClientRect.top < 0 ) {
43 // Viewport has crossed the bottom edge of the target element.
44 show();
45 } else {
46 // Viewport is above the bottom edge of the target element.
47 hide();
48 }
49 } );
50 }
51
52 module.exports = {
53 initScrollObserver,
54 firePageTitleScrollHook
55 };