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-16 19:27:10 +00:00
|
|
|
initSearchToggle = require( './searchToggle.js' ),
|
2022-03-23 20:35:27 +00:00
|
|
|
STICKY_HEADER_ID = 'vector-sticky-header',
|
2021-09-09 02:26:33 +00:00
|
|
|
STICKY_HEADER_APPENDED_ID = '-sticky-header',
|
2022-06-15 07:48:06 +00:00
|
|
|
STICKY_HEADER_APPENDED_PARAM = [ 'wvprov', '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',
|
2022-07-01 20:19:57 +00:00
|
|
|
TOC_ID = 'mw-panel-toc',
|
2021-09-09 02:26:33 +00:00
|
|
|
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',
|
2022-04-14 03:58:05 +00:00
|
|
|
STICKY_HEADER_EDIT_EXPERIMENT_NAME = 'vector.sticky_header_edit',
|
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.
|
|
|
|
*/
|
2022-05-05 20:54:20 +00:00
|
|
|
function show() {
|
|
|
|
document.body.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.
|
|
|
|
*/
|
2022-05-05 20:54:20 +00:00
|
|
|
function hide() {
|
|
|
|
document.body.classList.remove( STICKY_HEADER_VISIBLE_CLASS );
|
2022-03-23 20:35:27 +00:00
|
|
|
document.body.classList.add( ULS_HIDE_CLASS );
|
2021-12-03 19:19:27 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 20:19:57 +00:00
|
|
|
/**
|
|
|
|
* Moves the TOC element to a new parent container.
|
|
|
|
*
|
|
|
|
* @param {string} position The position to move the TOC into: sidebar or stickyheader
|
|
|
|
*/
|
|
|
|
function moveToc( position ) {
|
|
|
|
const toc = document.getElementById( TOC_ID );
|
|
|
|
const currTocContainer = toc && toc.parentElement;
|
|
|
|
if ( !toc || !currTocContainer ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-24 18:53:37 +00:00
|
|
|
// FIXME: Remove after Ia263c606dce5a6060b6b29fbaedc49cef3e17a5c has been in prod for 5 days
|
|
|
|
const isCachedHtml = document.querySelector( '.mw-table-of-contents-container.mw-sticky-header-element' );
|
|
|
|
|
2022-07-01 20:19:57 +00:00
|
|
|
let newTocContainer;
|
2022-08-24 18:53:37 +00:00
|
|
|
const sidebarTocContainerClass = isCachedHtml ? 'mw-table-of-contents-container' : 'vector-sticky-toc-container';
|
2022-07-01 20:19:57 +00:00
|
|
|
const stickyHeaderTocContainerClass = 'vector-menu-content';
|
|
|
|
// Avoid moving TOC if unnecessary
|
|
|
|
if ( !currTocContainer.classList.contains( sidebarTocContainerClass ) && position === 'sidebar' ) {
|
|
|
|
newTocContainer = document.querySelector( `.${sidebarTocContainerClass}` );
|
|
|
|
} else if ( !currTocContainer.classList.contains( stickyHeaderTocContainerClass ) && position === 'stickyheader' ) {
|
|
|
|
newTocContainer = document.querySelector( `.vector-sticky-header-toc .${stickyHeaderTocContainerClass}` );
|
|
|
|
}
|
2022-08-24 18:53:37 +00:00
|
|
|
|
2022-07-01 20:19:57 +00:00
|
|
|
if ( newTocContainer ) {
|
|
|
|
newTocContainer.insertAdjacentElement( 'beforeend', toc );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 07:48:06 +00:00
|
|
|
/**
|
|
|
|
* Suffixes the href attribute of a node with a value that indicates it
|
|
|
|
* relates to the sticky header to support tracking instrumentation.
|
|
|
|
*
|
|
|
|
* Distinct from suffixStickyAttribute as it's intended to support followed
|
|
|
|
* links recording their origin.
|
|
|
|
*
|
|
|
|
* @param {Element} node
|
|
|
|
*/
|
|
|
|
function suffixStickyHref( node ) {
|
|
|
|
/* eslint-disable compat/compat */
|
|
|
|
// @ts-ignore
|
|
|
|
const url = new URL( node.href );
|
|
|
|
if ( url && !url.searchParams.has( STICKY_HEADER_APPENDED_PARAM[ 0 ] ) ) {
|
|
|
|
url.searchParams.append(
|
|
|
|
STICKY_HEADER_APPENDED_PARAM[ 0 ], STICKY_HEADER_APPENDED_PARAM[ 1 ]
|
|
|
|
);
|
|
|
|
// @ts-ignore
|
|
|
|
node.href = url.toString();
|
|
|
|
}
|
|
|
|
/* eslint-enable compat/compat */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Undoes the effect of suffixStickyHref
|
|
|
|
*
|
|
|
|
* @param {Element} node
|
|
|
|
*/
|
|
|
|
function unsuffixStickyHref( node ) {
|
|
|
|
/* eslint-disable compat/compat */
|
|
|
|
// @ts-ignore
|
|
|
|
const url = new URL( node.href );
|
|
|
|
url.searchParams.delete( STICKY_HEADER_APPENDED_PARAM[ 0 ] );
|
|
|
|
// @ts-ignore
|
|
|
|
node.href = url.toString();
|
|
|
|
/* eslint-enable compat/compat */
|
|
|
|
}
|
|
|
|
|
2021-09-14 19:32:09 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
/**
|
2022-02-18 20:34:15 +00:00
|
|
|
* @param {Element} node
|
2021-09-24 21:00:32 +00:00
|
|
|
*/
|
|
|
|
function removeNode( node ) {
|
2022-02-18 20:34:15 +00:00
|
|
|
if ( node.parentNode ) {
|
|
|
|
node.parentNode.removeChild( node );
|
|
|
|
}
|
2021-09-24 21:00:32 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*
|
2022-02-18 20:34:15 +00:00
|
|
|
* @param {Element} watchSticky
|
2021-11-15 23:09:51 +00:00
|
|
|
* @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.
|
|
|
|
*
|
2022-03-23 20:35:27 +00:00
|
|
|
* @param {Element} header
|
2022-02-18 20:34:15 +00:00
|
|
|
* @param {Element|null} history
|
|
|
|
* @param {Element|null} talk
|
2022-07-22 21:21:51 +00:00
|
|
|
* @param {Element|null} subject
|
2022-02-18 20:34:15 +00:00
|
|
|
* @param {Element|null} watch
|
2021-09-14 16:53:35 +00:00
|
|
|
*/
|
2022-07-22 21:21:51 +00:00
|
|
|
function prepareIcons( header, history, talk, subject, watch ) {
|
2022-03-23 20:35:27 +00:00
|
|
|
const historySticky = header.querySelector( '#ca-history-sticky-header' ),
|
|
|
|
talkSticky = header.querySelector( '#ca-talk-sticky-header' ),
|
2022-07-22 21:21:51 +00:00
|
|
|
subjectSticky = header.querySelector( '#ca-subject-sticky-header' ),
|
2022-03-23 20:35:27 +00:00
|
|
|
watchSticky = header.querySelector( '#ca-watchstar-sticky-header' );
|
2021-09-14 16:53:35 +00:00
|
|
|
|
2022-07-22 21:21:51 +00:00
|
|
|
if ( !historySticky || !talkSticky || !subjectSticky || !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 {
|
2022-02-18 20:34:15 +00:00
|
|
|
removeNode( historySticky );
|
2021-09-14 16:53:35 +00:00
|
|
|
}
|
2022-07-22 21:21:51 +00:00
|
|
|
|
2021-09-14 16:53:35 +00:00
|
|
|
if ( talk ) {
|
2021-10-19 15:27:18 +00:00
|
|
|
copyButtonAttributes( talk, talkSticky );
|
2021-09-14 16:53:35 +00:00
|
|
|
} else {
|
2022-02-18 20:34:15 +00:00
|
|
|
removeNode( talkSticky );
|
2021-09-14 16:53:35 +00:00
|
|
|
}
|
2022-07-22 21:21:51 +00:00
|
|
|
|
|
|
|
if ( subject ) {
|
|
|
|
copyButtonAttributes( subject, subjectSticky );
|
|
|
|
} else {
|
|
|
|
removeNode( subjectSticky );
|
|
|
|
}
|
|
|
|
|
2022-02-18 20:34:15 +00:00
|
|
|
if ( watch && watch.parentNode instanceof Element ) {
|
2021-11-15 23:09:51 +00:00
|
|
|
const watchContainer = watch.parentNode;
|
|
|
|
copyButtonAttributes( watch, watchSticky );
|
|
|
|
updateStickyWatchlink(
|
|
|
|
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 {
|
2022-02-18 20:34:15 +00:00
|
|
|
removeNode( watchSticky );
|
2021-11-15 23:09:51 +00:00
|
|
|
}
|
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.
|
|
|
|
*
|
2022-03-23 20:35:27 +00:00
|
|
|
* @param {Element} header
|
2022-02-18 20:34:15 +00:00
|
|
|
* @param {Element|null} primaryEdit
|
2021-09-24 21:00:32 +00:00
|
|
|
* @param {boolean} isProtected
|
2022-02-18 20:34:15 +00:00
|
|
|
* @param {Element|null} secondaryEdit
|
2022-05-05 13:43:10 +00:00
|
|
|
* @param {Element|null} addSection
|
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(
|
2022-03-23 20:35:27 +00:00
|
|
|
header,
|
2021-09-24 21:00:32 +00:00
|
|
|
primaryEdit,
|
|
|
|
isProtected,
|
2021-10-19 19:14:30 +00:00
|
|
|
secondaryEdit,
|
2022-05-05 13:43:10 +00:00
|
|
|
addSection,
|
2021-10-19 19:14:30 +00:00
|
|
|
disableStickyHeader
|
2021-09-24 21:00:32 +00:00
|
|
|
) {
|
2021-10-20 19:10:42 +00:00
|
|
|
const
|
2022-03-23 20:35:27 +00:00
|
|
|
primaryEditSticky = header.querySelector(
|
2021-10-26 21:34:05 +00:00
|
|
|
'#ca-ve-edit-sticky-header'
|
|
|
|
),
|
2022-03-23 20:35:27 +00:00
|
|
|
protectedSticky = header.querySelector(
|
2022-02-18 20:34:15 +00:00
|
|
|
'#ca-viewsource-sticky-header'
|
2021-09-24 21:00:32 +00:00
|
|
|
),
|
2022-03-23 20:35:27 +00:00
|
|
|
wikitextSticky = header.querySelector(
|
2022-02-18 20:34:15 +00:00
|
|
|
'#ca-edit-sticky-header'
|
2022-05-05 13:43:10 +00:00
|
|
|
),
|
|
|
|
addSectionSticky = header.querySelector(
|
|
|
|
'#ca-addsection-sticky-header'
|
2021-09-24 21:00:32 +00:00
|
|
|
);
|
|
|
|
|
2022-05-05 13:43:10 +00:00
|
|
|
if ( addSectionSticky ) {
|
|
|
|
if ( addSection ) {
|
|
|
|
copyButtonAttributes( addSection, addSectionSticky );
|
|
|
|
suffixStickyHref( addSectionSticky );
|
|
|
|
} else {
|
|
|
|
removeNode( addSectionSticky );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-26 21:34:05 +00:00
|
|
|
// If no primary edit icon is present the feature is disabled.
|
2022-02-18 20:34:15 +00:00
|
|
|
if ( !primaryEditSticky || !wikitextSticky || !protectedSticky ) {
|
2021-10-26 21:34:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-04-14 03:58:05 +00:00
|
|
|
|
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 );
|
2022-06-15 07:48:06 +00:00
|
|
|
suffixStickyHref( protectedSticky );
|
2021-09-24 21:00:32 +00:00
|
|
|
} else {
|
|
|
|
removeNode( protectedSticky );
|
2021-10-19 15:27:18 +00:00
|
|
|
copyButtonAttributes( primaryEdit, primaryEditSticky );
|
2022-06-15 07:48:06 +00:00
|
|
|
suffixStickyHref( primaryEditSticky );
|
2021-10-19 19:14:30 +00:00
|
|
|
|
|
|
|
primaryEditSticky.addEventListener( 'click', function ( ev ) {
|
2022-02-18 20:34:15 +00:00
|
|
|
const target = ev.target;
|
2021-10-19 19:14:30 +00:00
|
|
|
const $ve = $( primaryEdit );
|
|
|
|
if ( target && $ve.length ) {
|
|
|
|
const event = $.Event( 'click' );
|
2022-06-15 07:48:06 +00:00
|
|
|
suffixStickyHref( $ve[ 0 ] );
|
2021-10-19 19:14:30 +00:00
|
|
|
$ve.trigger( event );
|
2022-06-15 07:48:06 +00:00
|
|
|
unsuffixStickyHref( $ve[ 0 ] );
|
2021-10-19 19:14:30 +00:00
|
|
|
// 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 );
|
2022-06-15 07:48:06 +00:00
|
|
|
suffixStickyHref( wikitextSticky );
|
2021-10-19 19:14:30 +00:00
|
|
|
wikitextSticky.addEventListener( 'click', function ( ev ) {
|
2022-02-18 20:34:15 +00:00
|
|
|
const target = ev.target;
|
2021-10-19 19:14:30 +00:00
|
|
|
if ( target ) {
|
|
|
|
const $edit = $( secondaryEdit );
|
|
|
|
if ( $edit.length ) {
|
|
|
|
const event = $.Event( 'click' );
|
2022-06-15 07:48:06 +00:00
|
|
|
suffixStickyHref( $edit[ 0 ] );
|
2021-10-19 19:14:30 +00:00
|
|
|
$edit.trigger( event );
|
2022-06-15 07:48:06 +00:00
|
|
|
unsuffixStickyHref( $edit[ 0 ] );
|
2021-10-19 19:14:30 +00:00
|
|
|
// 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.
|
|
|
|
*
|
2022-02-18 20:34:15 +00:00
|
|
|
* @param {Element} element
|
2021-09-24 21:00:32 +00:00
|
|
|
* @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.
|
|
|
|
*
|
2022-03-23 20:35:27 +00:00
|
|
|
* @param {Element} stickyIntersection intersection element
|
2021-10-26 23:37:56 +00:00
|
|
|
* @param {IntersectionObserver} observer
|
|
|
|
*/
|
2022-05-05 20:54:20 +00:00
|
|
|
function addVisualEditorHooks( stickyIntersection, 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( () => {
|
2022-05-05 20:54:20 +00:00
|
|
|
hide();
|
2022-03-23 20:35:27 +00:00
|
|
|
observer.unobserve( stickyIntersection );
|
2021-10-26 23:37:56 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
// 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( () => {
|
2022-03-23 20:35:27 +00:00
|
|
|
observer.observe( stickyIntersection );
|
2022-01-14 14:41:36 +00:00
|
|
|
} );
|
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( () => {
|
2022-03-23 20:35:27 +00:00
|
|
|
if ( !isInViewport( stickyIntersection ) ) {
|
2022-05-05 20:54:20 +00:00
|
|
|
show();
|
2022-03-23 20:35:27 +00:00
|
|
|
observer.observe( stickyIntersection );
|
2021-10-26 23:37:56 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2021-09-07 19:48:23 +00:00
|
|
|
/**
|
2022-02-18 20:34:15 +00:00
|
|
|
* @param {Element} userMenu
|
|
|
|
* @return {Element} 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
|
2022-02-18 20:34:15 +00:00
|
|
|
userMenuClone = /** @type {Element} */( 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.
|
|
|
|
*
|
2022-03-23 20:35:27 +00:00
|
|
|
* @param {Element} header
|
2022-02-18 20:34:15 +00:00
|
|
|
* @param {Element} userMenu
|
2022-02-18 16:47:44 +00:00
|
|
|
* @param {Element} userMenuStickyContainer
|
|
|
|
* @param {IntersectionObserver} stickyObserver
|
2022-02-18 20:34:15 +00:00
|
|
|
* @param {Element} stickyIntersection
|
2022-04-14 03:58:05 +00:00
|
|
|
* @param {boolean} disableEditIcons
|
2022-02-18 16:47:44 +00:00
|
|
|
*/
|
|
|
|
function makeStickyHeaderFunctional(
|
2022-03-23 20:35:27 +00:00
|
|
|
header,
|
2022-02-18 16:47:44 +00:00
|
|
|
userMenu,
|
|
|
|
userMenuStickyContainer,
|
|
|
|
stickyObserver,
|
2022-04-14 03:58:05 +00:00
|
|
|
stickyIntersection,
|
|
|
|
disableEditIcons
|
2022-02-18 16:47:44 +00:00
|
|
|
) {
|
|
|
|
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
|
|
|
|
2022-07-22 21:21:51 +00:00
|
|
|
let namespaceName = mw.config.get( 'wgCanonicalNamespace' );
|
|
|
|
const namespaceNumber = mw.config.get( 'wgNamespaceNumber' );
|
|
|
|
if ( namespaceNumber >= 0 && namespaceNumber % 2 === 1 ) {
|
|
|
|
// Remove '_talk' to get subject namespace
|
|
|
|
namespaceName = namespaceName.slice( 0, -5 );
|
|
|
|
}
|
|
|
|
// Title::getNamespaceKey()
|
|
|
|
let namespaceKey = namespaceName.toLowerCase() || 'main';
|
|
|
|
if ( namespaceKey === 'file' ) {
|
|
|
|
namespaceKey = 'image';
|
|
|
|
}
|
|
|
|
const namespaceTabId = 'ca-nstab-' + namespaceKey;
|
|
|
|
|
2022-03-23 20:35:27 +00:00
|
|
|
prepareIcons( header,
|
2021-09-14 16:53:35 +00:00
|
|
|
document.querySelector( '#ca-history a' ),
|
2022-04-19 17:42:13 +00:00
|
|
|
document.querySelector( '#ca-talk:not( .selected ) a' ),
|
2022-07-22 21:21:51 +00:00
|
|
|
document.querySelector( '#' + namespaceTabId + ':not( .selected ) a' ),
|
2021-11-15 23:09:51 +00:00
|
|
|
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;
|
2022-04-14 03:58:05 +00:00
|
|
|
// For sticky header edit A/B test, conditionally remove the edit icon by setting null.
|
|
|
|
// Otherwise, use either protected, ve, or source edit (in that order).
|
|
|
|
const primaryEdit = disableEditIcons ? null : protectedEdit || veEdit || ceEdit;
|
2021-10-20 19:10:42 +00:00
|
|
|
const secondaryEdit = veEdit ? ceEdit : null;
|
2021-10-19 19:14:30 +00:00
|
|
|
const disableStickyHeader = () => {
|
2022-05-05 20:54:20 +00:00
|
|
|
document.body.classList.remove( STICKY_HEADER_VISIBLE_CLASS );
|
2021-10-19 19:14:30 +00:00
|
|
|
stickyObserver.unobserve( stickyIntersection );
|
|
|
|
};
|
2022-05-05 13:43:10 +00:00
|
|
|
const addSection = document.querySelector( '#ca-addsection a' );
|
2021-09-24 21:00:32 +00:00
|
|
|
|
|
|
|
prepareEditIcons(
|
2022-03-23 20:35:27 +00:00
|
|
|
header,
|
2022-02-18 20:34:15 +00:00
|
|
|
primaryEdit,
|
2021-09-24 21:00:32 +00:00
|
|
|
isProtected,
|
2022-02-18 20:34:15 +00:00
|
|
|
secondaryEdit,
|
2022-05-05 13:43:10 +00:00
|
|
|
addSection,
|
2021-10-19 19:14:30 +00:00
|
|
|
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
|
|
|
/**
|
2022-03-23 20:35:27 +00:00
|
|
|
* @param {Element} header
|
2021-09-09 21:40:06 +00:00
|
|
|
*/
|
2022-03-23 20:35:27 +00:00
|
|
|
function setupSearchIfNeeded( header ) {
|
2021-10-20 19:10:42 +00:00
|
|
|
const
|
2022-03-23 20:35:27 +00:00
|
|
|
searchToggle = header.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 ) {
|
2022-04-19 17:42:13 +00:00
|
|
|
// Corresponds to Main, Main talk, User, User talk, Wikipedia,
|
|
|
|
// Template, Help, Category, Portal, Module.
|
|
|
|
const allowedNamespaceNumbers = [ 0, 1, 2, 3, 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
|
|
|
/**
|
2022-03-23 20:35:27 +00:00
|
|
|
* @typedef {Object} StickyHeaderProps
|
|
|
|
* @property {Element} header
|
|
|
|
* @property {Element} userMenu
|
|
|
|
* @property {IntersectionObserver} observer
|
|
|
|
* @property {Element} stickyIntersection
|
2022-04-14 03:58:05 +00:00
|
|
|
* @property {boolean} disableEditIcons
|
2021-10-26 23:37:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2022-03-23 20:35:27 +00:00
|
|
|
* @param {StickyHeaderProps} props
|
2021-10-26 23:37:56 +00:00
|
|
|
*/
|
2022-03-23 20:35:27 +00:00
|
|
|
function initStickyHeader( props ) {
|
|
|
|
const userMenuStickyContainer = document.getElementsByClassName(
|
|
|
|
STICKY_HEADER_USER_MENU_CONTAINER_CLASS
|
|
|
|
)[ 0 ];
|
2021-09-07 19:48:23 +00:00
|
|
|
|
2021-10-26 23:37:56 +00:00
|
|
|
makeStickyHeaderFunctional(
|
2022-03-23 20:35:27 +00:00
|
|
|
props.header,
|
|
|
|
props.userMenu,
|
2022-03-16 23:40:42 +00:00
|
|
|
userMenuStickyContainer,
|
2022-03-23 20:35:27 +00:00
|
|
|
props.observer,
|
2022-04-14 03:58:05 +00:00
|
|
|
props.stickyIntersection,
|
|
|
|
props.disableEditIcons
|
2021-10-26 23:37:56 +00:00
|
|
|
);
|
2022-03-23 20:35:27 +00:00
|
|
|
|
|
|
|
setupSearchIfNeeded( props.header );
|
2022-05-05 20:54:20 +00:00
|
|
|
addVisualEditorHooks( props.stickyIntersection, props.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.
|
2022-03-23 20:35:27 +00:00
|
|
|
const langBtn = props.header.querySelector( '#p-lang-btn-sticky-header' );
|
2021-12-03 19:36:15 +00:00
|
|
|
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-07-01 20:19:57 +00:00
|
|
|
moveToc,
|
2022-02-18 16:47:44 +00:00
|
|
|
prepareUserMenu,
|
2022-03-23 20:35:27 +00:00
|
|
|
isAllowedNamespace,
|
|
|
|
isAllowedAction,
|
2021-10-26 23:37:56 +00:00
|
|
|
initStickyHeader,
|
2022-03-23 20:35:27 +00:00
|
|
|
STICKY_HEADER_ID,
|
|
|
|
FIRST_HEADING_ID,
|
|
|
|
USER_MENU_ID,
|
2022-04-14 03:58:05 +00:00
|
|
|
STICKY_HEADER_EDIT_EXPERIMENT_NAME,
|
2021-10-26 23:37:56 +00:00
|
|
|
STICKY_HEADER_EXPERIMENT_NAME
|
2021-08-30 22:44:00 +00:00
|
|
|
};
|