2021-10-26 23:37:56 +00:00
|
|
|
/**
|
|
|
|
* Functions and variables to implement sticky header.
|
|
|
|
*/
|
2021-10-20 19:10:42 +00:00
|
|
|
const
|
2021-09-07 19:48:23 +00:00
|
|
|
STICKY_HEADER_ID = 'vector-sticky-header',
|
2021-12-03 19:19:27 +00:00
|
|
|
header = document.getElementById( STICKY_HEADER_ID ),
|
2021-09-16 19:27:10 +00:00
|
|
|
initSearchToggle = require( './searchToggle.js' ),
|
2021-09-09 02:26:33 +00:00
|
|
|
STICKY_HEADER_APPENDED_ID = '-sticky-header',
|
2021-09-07 19:48:23 +00:00
|
|
|
STICKY_HEADER_VISIBLE_CLASS = 'vector-sticky-header-visible',
|
2021-09-09 02:26:33 +00:00
|
|
|
STICKY_HEADER_USER_MENU_CONTAINER_CLASS = 'vector-sticky-header-icon-end',
|
|
|
|
FIRST_HEADING_ID = 'firstHeading',
|
|
|
|
USER_MENU_ID = 'p-personal',
|
2021-12-03 19:36:15 +00:00
|
|
|
ULS_STICKY_CLASS = 'uls-dialog-sticky',
|
|
|
|
ULS_HIDE_CLASS = 'uls-dialog-sticky-hide',
|
2021-09-09 02:26:33 +00:00
|
|
|
VECTOR_USER_LINKS_SELECTOR = '.vector-user-links',
|
2021-10-26 23:37:56 +00:00
|
|
|
SEARCH_TOGGLE_SELECTOR = '.vector-sticky-header-search-toggle',
|
2021-12-09 19:34:44 +00:00
|
|
|
STICKY_HEADER_EXPERIMENT_NAME = 'vector.sticky_header';
|
2021-09-07 19:48:23 +00:00
|
|
|
|
2021-09-14 16:53:35 +00:00
|
|
|
/**
|
|
|
|
* Copies attribute from an element to another.
|
|
|
|
*
|
|
|
|
* @param {Element} from
|
|
|
|
* @param {Element} to
|
|
|
|
* @param {string} attribute
|
|
|
|
*/
|
|
|
|
function copyAttribute( from, to, attribute ) {
|
2021-10-20 19:10:42 +00:00
|
|
|
const fromAttr = from.getAttribute( attribute );
|
2021-09-14 16:53:35 +00:00
|
|
|
if ( fromAttr ) {
|
|
|
|
to.setAttribute( attribute, fromAttr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 19:19:27 +00:00
|
|
|
/**
|
|
|
|
* Show the sticky header.
|
|
|
|
*/
|
|
|
|
function show() {
|
|
|
|
if ( header ) {
|
|
|
|
header.classList.add( STICKY_HEADER_VISIBLE_CLASS );
|
|
|
|
}
|
2021-12-03 19:36:15 +00:00
|
|
|
document.body.classList.remove( ULS_HIDE_CLASS );
|
2021-12-03 19:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hide the sticky header.
|
|
|
|
*/
|
|
|
|
function hide() {
|
|
|
|
if ( header ) {
|
|
|
|
header.classList.remove( STICKY_HEADER_VISIBLE_CLASS );
|
2021-12-03 19:36:15 +00:00
|
|
|
document.body.classList.add( ULS_HIDE_CLASS );
|
2021-12-03 19:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 15:27:18 +00:00
|
|
|
/**
|
|
|
|
* Copies attribute from an element to another.
|
|
|
|
*
|
|
|
|
* @param {Element} from
|
|
|
|
* @param {Element} to
|
|
|
|
*/
|
|
|
|
function copyButtonAttributes( from, to ) {
|
|
|
|
copyAttribute( from, to, 'href' );
|
|
|
|
copyAttribute( from, to, 'title' );
|
|
|
|
}
|
|
|
|
|
2021-09-14 19:32:09 +00:00
|
|
|
/**
|
|
|
|
* Suffixes an attribute with a value that indicates it
|
|
|
|
* relates to the sticky header to support click tracking instrumentation.
|
|
|
|
*
|
|
|
|
* @param {Element} node
|
|
|
|
* @param {string} attribute
|
|
|
|
*/
|
|
|
|
function suffixStickyAttribute( node, attribute ) {
|
2021-10-20 19:10:42 +00:00
|
|
|
const value = node.getAttribute( attribute );
|
2021-09-14 19:32:09 +00:00
|
|
|
if ( value ) {
|
|
|
|
node.setAttribute( attribute, value + STICKY_HEADER_APPENDED_ID );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a node trackable by our click tracking instrumentation.
|
|
|
|
*
|
|
|
|
* @param {Element} node
|
|
|
|
*/
|
|
|
|
function makeNodeTrackable( node ) {
|
|
|
|
suffixStickyAttribute( node, 'id' );
|
|
|
|
suffixStickyAttribute( node, 'data-event-name' );
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:00:32 +00:00
|
|
|
/**
|
|
|
|
*
|
2021-10-19 19:14:30 +00:00
|
|
|
* @param {null|HTMLElement|Node|EventTarget} node
|
2021-09-24 21:00:32 +00:00
|
|
|
* @return {HTMLElement}
|
|
|
|
*/
|
|
|
|
function toHTMLElement( node ) {
|
|
|
|
// @ts-ignore
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {HTMLElement} node
|
|
|
|
*/
|
|
|
|
function removeNode( node ) {
|
|
|
|
toHTMLElement( node.parentNode ).removeChild( node );
|
|
|
|
}
|
|
|
|
|
2021-10-14 15:00:04 +00:00
|
|
|
/**
|
|
|
|
* @param {NodeList} nodes
|
|
|
|
* @param {string} className
|
|
|
|
*/
|
|
|
|
function removeClassFromNodes( nodes, className ) {
|
|
|
|
Array.prototype.forEach.call( nodes, function ( node ) {
|
|
|
|
node.classList.remove( className );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2022-02-18 16:47:44 +00:00
|
|
|
/**
|
|
|
|
* @param {NodeList} nodes
|
|
|
|
*/
|
|
|
|
function removeNodes( nodes ) {
|
|
|
|
Array.prototype.forEach.call( nodes, function ( node ) {
|
|
|
|
node.parentNode.removeChild( node );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2021-11-15 23:09:51 +00:00
|
|
|
/**
|
|
|
|
* Ensures a sticky header button has the correct attributes
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} watchSticky
|
|
|
|
* @param {string} status 'watched', 'unwatched', or 'temporary'
|
|
|
|
*/
|
|
|
|
function updateStickyWatchlink( watchSticky, status ) {
|
|
|
|
watchSticky.classList.toggle( 'mw-ui-icon-wikimedia-star', status === 'unwatched' );
|
|
|
|
watchSticky.classList.toggle( 'mw-ui-icon-wikimedia-unStar', status === 'watched' );
|
|
|
|
watchSticky.classList.toggle( 'mw-ui-icon-wikimedia-halfStar', status === 'temporary' );
|
|
|
|
watchSticky.setAttribute( 'data-event-name', status === 'unwatched' ? 'watch-sticky-header' : 'unwatch-sticky-header' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for watchsar
|
|
|
|
*
|
|
|
|
* @param {jQuery} $link Watchstar link
|
|
|
|
* @param {boolean} isWatched The page is watched
|
|
|
|
* @param {string} [expiry] Optional expiry time
|
|
|
|
*/
|
|
|
|
function watchstarCallback( $link, isWatched, expiry ) {
|
|
|
|
updateStickyWatchlink(
|
|
|
|
// @ts-ignore
|
|
|
|
$link[ 0 ],
|
|
|
|
expiry !== 'infinity' ? 'temporary' :
|
|
|
|
isWatched ? 'watched' : 'unwatched'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-14 16:53:35 +00:00
|
|
|
/**
|
|
|
|
* Makes sticky header icons functional for modern Vector.
|
|
|
|
*
|
2021-12-03 19:19:27 +00:00
|
|
|
* @param {HTMLElement} headerElement
|
2021-09-14 16:53:35 +00:00
|
|
|
* @param {HTMLElement|null} history
|
|
|
|
* @param {HTMLElement|null} talk
|
2021-11-15 23:09:51 +00:00
|
|
|
* @param {HTMLElement|null} watch
|
2021-09-14 16:53:35 +00:00
|
|
|
*/
|
2021-11-15 23:09:51 +00:00
|
|
|
function prepareIcons( headerElement, history, talk, watch ) {
|
2021-12-03 19:19:27 +00:00
|
|
|
const historySticky = headerElement.querySelector( '#ca-history-sticky-header' ),
|
2021-11-15 23:09:51 +00:00
|
|
|
talkSticky = headerElement.querySelector( '#ca-talk-sticky-header' ),
|
|
|
|
watchSticky = headerElement.querySelector( '#ca-watchstar-sticky-header' );
|
2021-09-14 16:53:35 +00:00
|
|
|
|
2021-11-15 23:09:51 +00:00
|
|
|
if ( !historySticky || !talkSticky || !watchSticky ) {
|
2021-09-14 16:53:35 +00:00
|
|
|
throw new Error( 'Sticky header has unexpected HTML' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( history ) {
|
2021-10-19 15:27:18 +00:00
|
|
|
copyButtonAttributes( history, historySticky );
|
2021-09-14 16:53:35 +00:00
|
|
|
} else {
|
|
|
|
// @ts-ignore
|
|
|
|
historySticky.parentNode.removeChild( historySticky );
|
|
|
|
}
|
|
|
|
if ( talk ) {
|
2021-10-19 15:27:18 +00:00
|
|
|
copyButtonAttributes( talk, talkSticky );
|
2021-09-14 16:53:35 +00:00
|
|
|
} else {
|
|
|
|
// @ts-ignore
|
|
|
|
talkSticky.parentNode.removeChild( talkSticky );
|
|
|
|
}
|
2021-11-15 23:09:51 +00:00
|
|
|
if ( watch && watch.parentNode instanceof HTMLElement ) {
|
|
|
|
const watchContainer = watch.parentNode;
|
|
|
|
copyButtonAttributes( watch, watchSticky );
|
|
|
|
updateStickyWatchlink(
|
|
|
|
// @ts-ignore
|
|
|
|
watchSticky,
|
|
|
|
watchContainer.classList.contains( 'mw-watchlink-temp' ) ? 'temporary' :
|
|
|
|
watchContainer.getAttribute( 'id' ) === 'ca-watch' ? 'unwatched' : 'watched'
|
|
|
|
);
|
|
|
|
|
|
|
|
const watchLib = require( /** @type {string} */( 'mediawiki.page.watch.ajax' ) );
|
|
|
|
watchLib.watchstar( $( watchSticky ), mw.config.get( 'wgRelevantPageName' ), watchstarCallback );
|
|
|
|
} else {
|
|
|
|
// @ts-ignore
|
|
|
|
watchSticky.parentNode.removeChild( watchSticky );
|
|
|
|
}
|
2021-09-14 16:53:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 21:00:32 +00:00
|
|
|
/**
|
|
|
|
* Render sticky header edit or protected page icons for modern Vector.
|
|
|
|
*
|
2021-12-03 19:19:27 +00:00
|
|
|
* @param {HTMLElement} headerElement
|
2021-09-24 21:00:32 +00:00
|
|
|
* @param {HTMLElement|null} primaryEdit
|
|
|
|
* @param {boolean} isProtected
|
|
|
|
* @param {HTMLElement|null} secondaryEdit
|
2021-10-19 19:14:30 +00:00
|
|
|
* @param {Function} disableStickyHeader function to call to disable the sticky
|
|
|
|
* header.
|
2021-09-24 21:00:32 +00:00
|
|
|
*/
|
|
|
|
function prepareEditIcons(
|
2021-12-03 19:19:27 +00:00
|
|
|
headerElement,
|
2021-09-24 21:00:32 +00:00
|
|
|
primaryEdit,
|
|
|
|
isProtected,
|
2021-10-19 19:14:30 +00:00
|
|
|
secondaryEdit,
|
|
|
|
disableStickyHeader
|
2021-09-24 21:00:32 +00:00
|
|
|
) {
|
2021-10-20 19:10:42 +00:00
|
|
|
const
|
2021-12-03 19:19:27 +00:00
|
|
|
primaryEditStickyElement = headerElement.querySelector(
|
2021-10-26 21:34:05 +00:00
|
|
|
'#ca-ve-edit-sticky-header'
|
|
|
|
),
|
|
|
|
primaryEditSticky = primaryEditStickyElement ? toHTMLElement(
|
2021-12-03 19:19:27 +00:00
|
|
|
headerElement.querySelector(
|
2021-09-24 21:00:32 +00:00
|
|
|
'#ca-ve-edit-sticky-header'
|
|
|
|
)
|
2021-10-26 21:34:05 +00:00
|
|
|
) : null,
|
2021-09-24 21:00:32 +00:00
|
|
|
protectedSticky = toHTMLElement(
|
2021-12-03 19:19:27 +00:00
|
|
|
headerElement.querySelector(
|
2021-09-24 21:00:32 +00:00
|
|
|
'#ca-viewsource-sticky-header'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
wikitextSticky = toHTMLElement(
|
2021-12-03 19:19:27 +00:00
|
|
|
headerElement.querySelector(
|
2021-09-24 21:00:32 +00:00
|
|
|
'#ca-edit-sticky-header'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2021-10-26 21:34:05 +00:00
|
|
|
// If no primary edit icon is present the feature is disabled.
|
|
|
|
if ( !primaryEditSticky ) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-24 21:00:32 +00:00
|
|
|
if ( !primaryEdit ) {
|
|
|
|
removeNode( protectedSticky );
|
|
|
|
removeNode( wikitextSticky );
|
|
|
|
removeNode( primaryEditSticky );
|
|
|
|
return;
|
|
|
|
} else if ( isProtected ) {
|
|
|
|
removeNode( wikitextSticky );
|
|
|
|
removeNode( primaryEditSticky );
|
2021-10-19 15:27:18 +00:00
|
|
|
copyButtonAttributes( primaryEdit, protectedSticky );
|
2021-09-24 21:00:32 +00:00
|
|
|
} else {
|
|
|
|
removeNode( protectedSticky );
|
2021-10-19 15:27:18 +00:00
|
|
|
copyButtonAttributes( primaryEdit, primaryEditSticky );
|
2021-10-19 19:14:30 +00:00
|
|
|
|
|
|
|
primaryEditSticky.addEventListener( 'click', function ( ev ) {
|
|
|
|
const target = toHTMLElement( ev.target );
|
|
|
|
const $ve = $( primaryEdit );
|
|
|
|
|
|
|
|
if ( target && $ve.length ) {
|
|
|
|
const event = $.Event( 'click' );
|
|
|
|
$ve.trigger( event );
|
|
|
|
// The link has been progressively enhanced.
|
|
|
|
if ( event.isDefaultPrevented() ) {
|
|
|
|
disableStickyHeader();
|
|
|
|
ev.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
2021-09-24 21:00:32 +00:00
|
|
|
if ( secondaryEdit ) {
|
2021-10-19 15:27:18 +00:00
|
|
|
copyButtonAttributes( secondaryEdit, wikitextSticky );
|
2021-10-19 19:14:30 +00:00
|
|
|
wikitextSticky.addEventListener( 'click', function ( ev ) {
|
|
|
|
const target = toHTMLElement( ev.target );
|
|
|
|
if ( target ) {
|
|
|
|
const $edit = $( secondaryEdit );
|
|
|
|
if ( $edit.length ) {
|
|
|
|
const event = $.Event( 'click' );
|
|
|
|
$edit.trigger( event );
|
|
|
|
// The link has been progressively enhanced.
|
|
|
|
if ( event.isDefaultPrevented() ) {
|
|
|
|
disableStickyHeader();
|
|
|
|
ev.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
2021-09-24 21:00:32 +00:00
|
|
|
} else {
|
|
|
|
removeNode( wikitextSticky );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if element is in viewport.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} element
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
function isInViewport( element ) {
|
2021-10-20 19:10:42 +00:00
|
|
|
const rect = element.getBoundingClientRect();
|
2021-09-24 21:00:32 +00:00
|
|
|
return (
|
|
|
|
rect.top >= 0 &&
|
|
|
|
rect.left >= 0 &&
|
|
|
|
rect.bottom <= ( window.innerHeight || document.documentElement.clientHeight ) &&
|
|
|
|
rect.right <= ( window.innerWidth || document.documentElement.clientWidth )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-26 23:37:56 +00:00
|
|
|
/**
|
|
|
|
* Add hooks for sticky header when Visual Editor is used.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} targetIntersection intersection element
|
|
|
|
* @param {IntersectionObserver} observer
|
|
|
|
*/
|
2021-12-03 19:19:27 +00:00
|
|
|
function addVisualEditorHooks( targetIntersection, observer ) {
|
2021-10-26 23:37:56 +00:00
|
|
|
// When Visual Editor is activated, hide the sticky header.
|
2022-01-25 12:30:53 +00:00
|
|
|
mw.hook( 've.activationStart' ).add( () => {
|
2021-12-03 19:19:27 +00:00
|
|
|
hide();
|
2021-10-26 23:37:56 +00:00
|
|
|
observer.unobserve( targetIntersection );
|
|
|
|
} );
|
|
|
|
|
|
|
|
// When Visual Editor is deactivated (by clicking "Read" tab at top of page), show sticky header
|
|
|
|
// by re-triggering the observer.
|
|
|
|
mw.hook( 've.deactivationComplete' ).add( () => {
|
2022-01-14 14:41:36 +00:00
|
|
|
// Wait for the next repaint or we might calculate that
|
|
|
|
// sticky header should not be visible (T299114)
|
|
|
|
requestAnimationFrame( () => {
|
|
|
|
observer.observe( targetIntersection );
|
|
|
|
} );
|
2021-10-26 23:37:56 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
// After saving edits, re-apply the sticky header if the target is not in the viewport.
|
|
|
|
mw.hook( 'postEdit.afterRemoval' ).add( () => {
|
|
|
|
if ( !isInViewport( targetIntersection ) ) {
|
2021-12-03 19:19:27 +00:00
|
|
|
show();
|
2021-10-26 23:37:56 +00:00
|
|
|
observer.observe( targetIntersection );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2021-09-07 19:48:23 +00:00
|
|
|
/**
|
2021-09-09 02:26:33 +00:00
|
|
|
* @param {HTMLElement} userMenu
|
2022-02-18 16:47:44 +00:00
|
|
|
* @return {HTMLElement} cloned userMenu
|
2021-09-07 19:48:23 +00:00
|
|
|
*/
|
2022-02-18 16:47:44 +00:00
|
|
|
function prepareUserMenu( userMenu ) {
|
2021-10-20 19:10:42 +00:00
|
|
|
const
|
2021-09-14 19:24:26 +00:00
|
|
|
// Type declaration needed because of https://github.com/Microsoft/TypeScript/issues/3734#issuecomment-118934518
|
|
|
|
userMenuClone = /** @type {HTMLElement} */( userMenu.cloneNode( true ) ),
|
2022-02-18 16:47:44 +00:00
|
|
|
userMenuStickyElementsWithIds = userMenuClone.querySelectorAll( '[ id ], [ data-event-name ]' );
|
2021-09-09 02:26:33 +00:00
|
|
|
// Update all ids of the cloned user menu to make them unique.
|
2021-09-14 19:32:09 +00:00
|
|
|
makeNodeTrackable( userMenuClone );
|
|
|
|
userMenuStickyElementsWithIds.forEach( makeNodeTrackable );
|
2021-09-21 16:01:33 +00:00
|
|
|
// Remove portlet links added by gadgets using mw.util.addPortletLink, T291426
|
2022-02-18 16:47:44 +00:00
|
|
|
removeNodes( userMenuClone.querySelectorAll( '.mw-list-item-js' ) );
|
2021-10-14 15:00:04 +00:00
|
|
|
removeClassFromNodes(
|
|
|
|
userMenuClone.querySelectorAll( '.user-links-collapsible-item' ),
|
|
|
|
'user-links-collapsible-item'
|
|
|
|
);
|
2021-09-21 16:01:33 +00:00
|
|
|
// Prevents user menu from being focusable, T290201
|
2021-10-20 19:10:42 +00:00
|
|
|
const userMenuCheckbox = userMenuClone.querySelector( 'input' );
|
2021-09-21 16:01:33 +00:00
|
|
|
if ( userMenuCheckbox ) {
|
|
|
|
userMenuCheckbox.setAttribute( 'tabindex', '-1' );
|
|
|
|
}
|
2022-02-18 16:47:44 +00:00
|
|
|
return userMenuClone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes sticky header functional for modern Vector.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} headerElement
|
|
|
|
* @param {HTMLElement} userMenu
|
|
|
|
* @param {Element} userMenuStickyContainer
|
|
|
|
* @param {IntersectionObserver} stickyObserver
|
|
|
|
* @param {HTMLElement} stickyIntersection
|
|
|
|
*/
|
|
|
|
function makeStickyHeaderFunctional(
|
|
|
|
headerElement,
|
|
|
|
userMenu,
|
|
|
|
userMenuStickyContainer,
|
|
|
|
stickyObserver,
|
|
|
|
stickyIntersection
|
|
|
|
) {
|
|
|
|
const
|
|
|
|
userMenuStickyContainerInner = userMenuStickyContainer
|
|
|
|
.querySelector( VECTOR_USER_LINKS_SELECTOR );
|
2021-09-21 16:01:33 +00:00
|
|
|
|
2021-09-09 02:26:33 +00:00
|
|
|
// Clone the updated user menu to the sticky header.
|
2021-09-14 19:24:26 +00:00
|
|
|
if ( userMenuStickyContainerInner ) {
|
2022-02-18 16:47:44 +00:00
|
|
|
userMenuStickyContainerInner.appendChild( prepareUserMenu( userMenu ) );
|
2021-09-14 19:24:26 +00:00
|
|
|
}
|
2021-09-09 02:26:33 +00:00
|
|
|
|
2021-12-03 19:19:27 +00:00
|
|
|
prepareIcons( headerElement,
|
2021-09-14 16:53:35 +00:00
|
|
|
document.querySelector( '#ca-history a' ),
|
2021-11-15 23:09:51 +00:00
|
|
|
document.querySelector( '#ca-talk a' ),
|
|
|
|
document.querySelector( '#ca-watch a, #ca-unwatch a' )
|
2021-09-14 16:53:35 +00:00
|
|
|
);
|
2021-09-20 23:37:54 +00:00
|
|
|
|
2021-10-20 19:10:42 +00:00
|
|
|
const veEdit = document.querySelector( '#ca-ve-edit a' );
|
|
|
|
const ceEdit = document.querySelector( '#ca-edit a' );
|
|
|
|
const protectedEdit = document.querySelector( '#ca-viewsource a' );
|
|
|
|
const isProtected = !!protectedEdit;
|
|
|
|
const primaryEdit = protectedEdit || ( veEdit || ceEdit );
|
|
|
|
const secondaryEdit = veEdit ? ceEdit : null;
|
2021-10-19 19:14:30 +00:00
|
|
|
const disableStickyHeader = () => {
|
2021-12-03 19:19:27 +00:00
|
|
|
headerElement.classList.remove( STICKY_HEADER_VISIBLE_CLASS );
|
2021-10-19 19:14:30 +00:00
|
|
|
stickyObserver.unobserve( stickyIntersection );
|
|
|
|
};
|
2021-09-24 21:00:32 +00:00
|
|
|
|
|
|
|
prepareEditIcons(
|
2021-12-03 19:19:27 +00:00
|
|
|
headerElement,
|
2021-09-24 21:00:32 +00:00
|
|
|
toHTMLElement( primaryEdit ),
|
|
|
|
isProtected,
|
2021-10-19 19:14:30 +00:00
|
|
|
toHTMLElement( secondaryEdit ),
|
|
|
|
disableStickyHeader
|
2021-09-24 21:00:32 +00:00
|
|
|
);
|
2021-09-20 23:37:54 +00:00
|
|
|
|
2021-09-07 19:48:23 +00:00
|
|
|
stickyObserver.observe( stickyIntersection );
|
|
|
|
}
|
|
|
|
|
2021-09-09 21:40:06 +00:00
|
|
|
/**
|
2021-12-03 19:19:27 +00:00
|
|
|
* @param {HTMLElement} headerElement
|
2021-09-09 21:40:06 +00:00
|
|
|
*/
|
2021-12-03 19:19:27 +00:00
|
|
|
function setupSearchIfNeeded( headerElement ) {
|
2021-10-20 19:10:42 +00:00
|
|
|
const
|
2021-12-03 19:19:27 +00:00
|
|
|
searchToggle = headerElement.querySelector( SEARCH_TOGGLE_SELECTOR );
|
2021-09-09 21:40:06 +00:00
|
|
|
|
2021-10-20 19:10:42 +00:00
|
|
|
if ( !document.body.classList.contains( 'skin-vector-search-vue' ) ) {
|
2021-09-09 21:40:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-20 19:10:42 +00:00
|
|
|
if ( searchToggle ) {
|
|
|
|
initSearchToggle( searchToggle );
|
|
|
|
}
|
2021-09-09 21:40:06 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 21:21:24 +00:00
|
|
|
/**
|
|
|
|
* Determines if sticky header should be visible for a given namespace.
|
|
|
|
*
|
|
|
|
* @param {number} namespaceNumber
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
function isAllowedNamespace( namespaceNumber ) {
|
|
|
|
// Corresponds to Main, User, Wikipedia, Template, Help, Category, Portal, Module.
|
2021-10-20 19:10:42 +00:00
|
|
|
const allowedNamespaceNumbers = [ 0, 2, 4, 10, 12, 14, 100, 828 ];
|
2021-09-15 21:21:24 +00:00
|
|
|
return allowedNamespaceNumbers.indexOf( namespaceNumber ) > -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if sticky header should be visible for a given action.
|
|
|
|
*
|
|
|
|
* @param {string} action
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
function isAllowedAction( action ) {
|
2021-10-20 19:10:42 +00:00
|
|
|
const disallowedActions = [ 'history', 'edit' ],
|
2021-09-15 21:21:24 +00:00
|
|
|
hasDiffId = mw.config.get( 'wgDiffOldId' );
|
|
|
|
return disallowedActions.indexOf( action ) < 0 && !hasDiffId;
|
|
|
|
}
|
|
|
|
|
2021-10-26 23:37:56 +00:00
|
|
|
const
|
|
|
|
stickyIntersection = document.getElementById(
|
|
|
|
FIRST_HEADING_ID
|
|
|
|
),
|
|
|
|
userMenu = document.getElementById( USER_MENU_ID ),
|
|
|
|
userMenuStickyContainer = document.getElementsByClassName(
|
|
|
|
STICKY_HEADER_USER_MENU_CONTAINER_CLASS
|
|
|
|
)[ 0 ],
|
|
|
|
allowedNamespace = isAllowedNamespace( mw.config.get( 'wgNamespaceNumber' ) ),
|
|
|
|
allowedAction = isAllowedAction( mw.config.get( 'wgAction' ) );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if all conditions are met to enable sticky header
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
function isStickyHeaderAllowed() {
|
|
|
|
// @ts-ignore
|
|
|
|
return header &&
|
2021-09-09 02:26:33 +00:00
|
|
|
stickyIntersection &&
|
|
|
|
userMenu &&
|
|
|
|
userMenuStickyContainer &&
|
2021-09-15 21:21:24 +00:00
|
|
|
allowedNamespace &&
|
|
|
|
allowedAction &&
|
2021-10-26 23:37:56 +00:00
|
|
|
'IntersectionObserver' in window;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {IntersectionObserver} observer
|
|
|
|
*/
|
|
|
|
function initStickyHeader( observer ) {
|
2021-12-15 23:19:04 +00:00
|
|
|
if ( !isStickyHeaderAllowed() || !header || !userMenu || !stickyIntersection ) {
|
2021-08-30 22:44:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-09-07 19:48:23 +00:00
|
|
|
|
2021-10-26 23:37:56 +00:00
|
|
|
makeStickyHeaderFunctional(
|
|
|
|
header,
|
|
|
|
userMenu,
|
|
|
|
userMenuStickyContainer,
|
|
|
|
observer,
|
|
|
|
stickyIntersection
|
|
|
|
);
|
2021-09-09 21:40:06 +00:00
|
|
|
setupSearchIfNeeded( header );
|
2021-12-15 23:19:04 +00:00
|
|
|
addVisualEditorHooks( stickyIntersection, observer );
|
2021-12-03 19:36:15 +00:00
|
|
|
|
|
|
|
// Make sure ULS outside sticky header disables the sticky header behaviour.
|
|
|
|
// @ts-ignore
|
|
|
|
mw.hook( 'mw.uls.compact_language_links.open' ).add( function ( $trigger ) {
|
|
|
|
if ( $trigger.attr( 'id' ) !== 'p-lang-btn-sticky-header' ) {
|
|
|
|
const bodyClassList = document.body.classList;
|
|
|
|
bodyClassList.remove( ULS_HIDE_CLASS );
|
|
|
|
bodyClassList.remove( ULS_STICKY_CLASS );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Make sure ULS dialog is sticky.
|
|
|
|
// @ts-ignore
|
|
|
|
const langBtn = header.querySelector( '#p-lang-btn-sticky-header' );
|
|
|
|
if ( langBtn ) {
|
|
|
|
langBtn.addEventListener( 'click', function () {
|
|
|
|
const bodyClassList = document.body.classList;
|
|
|
|
bodyClassList.remove( ULS_HIDE_CLASS );
|
|
|
|
bodyClassList.add( ULS_STICKY_CLASS );
|
|
|
|
} );
|
|
|
|
}
|
2021-10-26 23:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2021-12-03 19:19:27 +00:00
|
|
|
show,
|
|
|
|
hide,
|
2022-02-18 16:47:44 +00:00
|
|
|
prepareUserMenu,
|
2021-10-26 23:37:56 +00:00
|
|
|
initStickyHeader,
|
|
|
|
isStickyHeaderAllowed,
|
|
|
|
header,
|
2021-12-01 22:31:48 +00:00
|
|
|
stickyIntersection,
|
2021-10-26 23:37:56 +00:00
|
|
|
STICKY_HEADER_EXPERIMENT_NAME
|
2021-08-30 22:44:00 +00:00
|
|
|
};
|