| 1 |
const features = require( './features.js' ); |
| 2 |
const PINNED_HEADER_CLASS = 'vector-pinnable-header-pinned'; |
| 3 |
const UNPINNED_HEADER_CLASS = 'vector-pinnable-header-unpinned'; |
| 4 |
const popupNotification = require( './popupNotification.js' ); |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
function disablePinningAtBreakpoint( header, e ) { |
| 15 |
const { |
| 16 |
pinnableElementId, |
| 17 |
pinnedContainerId, |
| 18 |
unpinnedContainerId, |
| 19 |
featureName |
| 20 |
} = header.dataset; |
| 21 |
const savedPinnedState = JSON.parse( header.dataset.savedPinnedState || 'false' ); |
| 22 |
|
| 23 |
|
| 24 |
if ( !( pinnableElementId && unpinnedContainerId && pinnedContainerId && featureName ) ) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
if ( e.matches && savedPinnedState === true ) { |
| 29 |
features.toggleDocClasses( featureName, false ); |
| 30 |
movePinnableElement( pinnableElementId, unpinnedContainerId ); |
| 31 |
} |
| 32 |
|
| 33 |
if ( !e.matches && savedPinnedState === true ) { |
| 34 |
features.toggleDocClasses( featureName, true ); |
| 35 |
movePinnableElement( pinnableElementId, pinnedContainerId ); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
function setSavedPinnableState( header ) { |
| 50 |
header.dataset.savedPinnedState = String( isPinned( header ) ); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
function togglePinnableClasses( header ) { |
| 59 |
const featureName = ( header.dataset.featureName ); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
features.toggle( featureName ); |
| 64 |
|
| 65 |
|
| 66 |
header.classList.toggle( PINNED_HEADER_CLASS ); |
| 67 |
header.classList.toggle( UNPINNED_HEADER_CLASS ); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
function addPinnableElementIndicator( pinnableElementId ) { |
| 76 |
const dropdownSelector = document.querySelector( `#${ pinnableElementId }-dropdown` ); |
| 77 |
const container = dropdownSelector && dropdownSelector.parentElement; |
| 78 |
if ( container ) { |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
const message = mw.msg( `${ pinnableElementId }-unpinned-popup` ); |
| 83 |
popupNotification.add( container, message, pinnableElementId ) |
| 84 |
.then( ( popupWidget ) => { |
| 85 |
if ( popupWidget ) { |
| 86 |
popupNotification.show( popupWidget ); |
| 87 |
} |
| 88 |
} ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
function pinnableElementClickHandler( header ) { |
| 100 |
const { |
| 101 |
pinnableElementId, |
| 102 |
pinnedContainerId, |
| 103 |
unpinnedContainerId |
| 104 |
} = header.dataset; |
| 105 |
|
| 106 |
togglePinnableClasses( header ); |
| 107 |
|
| 108 |
const isPinnedElement = isPinned( header ); |
| 109 |
|
| 110 |
|
| 111 |
if ( pinnableElementId && pinnedContainerId && unpinnedContainerId ) { |
| 112 |
setSavedPinnableState( header ); |
| 113 |
const newContainerId = isPinnedElement ? pinnedContainerId : unpinnedContainerId; |
| 114 |
movePinnableElement( pinnableElementId, newContainerId ); |
| 115 |
window.dispatchEvent( new Event( 'resize' ) ); |
| 116 |
setFocusAfterToggle( pinnableElementId ); |
| 117 |
if ( !isPinnedElement ) { |
| 118 |
addPinnableElementIndicator( pinnableElementId ); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
function setFocusAfterToggle( pinnableElementId ) { |
| 130 |
let focusElement; |
| 131 |
const pinnableElement = document.getElementById( pinnableElementId ); |
| 132 |
const header = ( pinnableElement && pinnableElement.querySelector( '.vector-pinnable-header' ) ); |
| 133 |
if ( !pinnableElement || !header ) { |
| 134 |
return; |
| 135 |
} |
| 136 |
if ( isPinned( header ) ) { |
| 137 |
focusElement = ( pinnableElement.querySelector( '.vector-pinnable-header-unpin-button' ) ); |
| 138 |
} else { |
| 139 |
const dropdown = pinnableElement.closest( '.vector-dropdown' ); |
| 140 |
focusElement = ( dropdown && dropdown.querySelector( '.vector-menu-checkbox' ) ); |
| 141 |
} |
| 142 |
if ( focusElement ) { |
| 143 |
focusElement.focus(); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
function bindPinnableToggleButtons( header ) { |
| 154 |
const toggleButtons = header.querySelectorAll( '.vector-pinnable-header-toggle-button' ); |
| 155 |
toggleButtons.forEach( ( button ) => { |
| 156 |
button.addEventListener( 'click', pinnableElementClickHandler.bind( null, header ) ); |
| 157 |
} ); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
function bindPinnableBreakpoint( header ) { |
| 167 |
const { pinnedContainerId, unpinnedContainerId } = header.dataset; |
| 168 |
if ( !unpinnedContainerId || !pinnedContainerId ) { |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
const pinnableBreakpoint = window.matchMedia( '(max-width: 1119px)' ); |
| 173 |
|
| 174 |
setSavedPinnableState( header ); |
| 175 |
|
| 176 |
disablePinningAtBreakpoint( header, pinnableBreakpoint ); |
| 177 |
|
| 178 |
|
| 179 |
if ( pinnableBreakpoint.addEventListener ) { |
| 180 |
pinnableBreakpoint.addEventListener( 'change', disablePinningAtBreakpoint.bind( null, header ) ); |
| 181 |
} else { |
| 182 |
|
| 183 |
|
| 184 |
pinnableBreakpoint.addListener( disablePinningAtBreakpoint.bind( null, header ) ); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
function isPinned( header ) { |
| 193 |
const featureName = ( header.dataset.featureName ); |
| 194 |
return features.isEnabled( featureName ); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
function updatePinnableHeaderClass( pinnableElement ) { |
| 204 |
const header = pinnableElement.querySelector( '.vector-pinnable-header' ); |
| 205 |
|
| 206 |
|
| 207 |
if ( !header || !( header instanceof HTMLElement ) ) { |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
if ( isPinned( header ) ) { |
| 213 |
header.classList.add( PINNED_HEADER_CLASS ); |
| 214 |
header.classList.remove( UNPINNED_HEADER_CLASS ); |
| 215 |
} else { |
| 216 |
header.classList.remove( PINNED_HEADER_CLASS ); |
| 217 |
header.classList.add( UNPINNED_HEADER_CLASS ); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
function movePinnableElement( pinnableElementId, newContainerId ) { |
| 226 |
const pinnableElem = document.getElementById( pinnableElementId ); |
| 227 |
const newContainer = document.getElementById( newContainerId ); |
| 228 |
const currContainer = ( pinnableElem && pinnableElem.parentElement ); |
| 229 |
|
| 230 |
if ( !pinnableElem || !newContainer || !currContainer ) { |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
if ( currContainer.id !== newContainerId ) { |
| 236 |
newContainer.insertAdjacentElement( 'beforeend', pinnableElem ); |
| 237 |
updatePinnableHeaderClass( pinnableElem ); |
| 238 |
} |
| 239 |
|
| 240 |
popupNotification.hideAll(); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
function updatePinnableElementLocation( header ) { |
| 251 |
const newContainerId = isPinned( header ) ? |
| 252 |
header.dataset.pinnedContainerId : |
| 253 |
header.dataset.unpinnedContainerId; |
| 254 |
if ( header.dataset.pinnableElementId && newContainerId ) { |
| 255 |
movePinnableElement( header.dataset.pinnableElementId, newContainerId ); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
function initPinnableElement() { |
| 260 |
const pinnableHeader = ( document.querySelectorAll( '.vector-pinnable-header' ) ); |
| 261 |
pinnableHeader.forEach( ( header ) => { |
| 262 |
if ( header.dataset.featureName && header.dataset.pinnableElementId ) { |
| 263 |
bindPinnableToggleButtons( header ); |
| 264 |
bindPinnableBreakpoint( header ); |
| 265 |
updatePinnableElementLocation( header ); |
| 266 |
} |
| 267 |
} ); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
function hasPinnedElements() { |
| 278 |
const suffixesToCheck = [ 'pinned-clientpref-1', 'pinned-enabled' ]; |
| 279 |
const htmlElement = document.documentElement; |
| 280 |
return Array.from( htmlElement.classList ).some( |
| 281 |
( className ) => suffixesToCheck.some( ( suffix ) => className.endsWith( suffix ) ) |
| 282 |
); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
function analyticsPinnedState() { |
| 293 |
const htmlElement = document.documentElement; |
| 294 |
return htmlElement.classList.contains( 'vector-feature-main-menu-pinned-enabled' ) || htmlElement.classList.contains( 'vector-feature-page-tools-pinned-enabled' ); |
| 295 |
} |
| 296 |
|
| 297 |
module.exports = { |
| 298 |
|
| 299 |
hasPinnedElements, |
| 300 |
analyticsPinnedState, |
| 301 |
initPinnableElement, |
| 302 |
movePinnableElement, |
| 303 |
setFocusAfterToggle, |
| 304 |
isPinned, |
| 305 |
PINNED_HEADER_CLASS, |
| 306 |
UNPINNED_HEADER_CLASS |
| 307 |
}; |