Restrict sticky header to specified namespaces

- Hardcode allowable namespaces, actions for now in relevant js.

Bug: T290347
Change-Id: If482505be5de4b3e5bf130530f27f0d917ecaaa0
This commit is contained in:
Clare Ming 2021-09-15 15:21:24 -06:00 committed by Jdlrobson
parent d9a002574c
commit bdad84a7c9
1 changed files with 29 additions and 1 deletions

View File

@ -167,6 +167,30 @@ function setupSearchIfNeeded( header ) {
// } );
}
/**
* 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.
var allowedNamespaceNumbers = [ 0, 2, 4, 10, 12, 14, 100, 828 ];
return allowedNamespaceNumbers.indexOf( namespaceNumber ) > -1;
}
/**
* Determines if sticky header should be visible for a given action.
*
* @param {string} action
* @return {boolean}
*/
function isAllowedAction( action ) {
var disallowedActions = [ 'history', 'edit' ],
hasDiffId = mw.config.get( 'wgDiffOldId' );
return disallowedActions.indexOf( action ) < 0 && !hasDiffId;
}
module.exports = function initStickyHeader() {
var header = document.getElementById( STICKY_HEADER_ID ),
stickyIntersection = document.getElementById(
@ -175,7 +199,9 @@ module.exports = function initStickyHeader() {
userMenu = document.getElementById( USER_MENU_ID ),
userMenuStickyContainer = document.getElementsByClassName(
STICKY_HEADER_USER_MENU_CONTAINER_CLASS
)[ 0 ];
)[ 0 ],
allowedNamespace = isAllowedNamespace( mw.config.get( 'wgNamespaceNumber' ) ),
allowedAction = isAllowedAction( mw.config.get( 'wgAction' ) );
if ( !(
header &&
@ -183,6 +209,8 @@ module.exports = function initStickyHeader() {
stickyIntersection &&
userMenu &&
userMenuStickyContainer &&
allowedNamespace &&
allowedAction &&
'IntersectionObserver' in window ) ) {
return;
}