Debian Codegraph @

skins/Vector/resources/skins.vector.js/pinnableElement.js

raw symbols SCIP
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 * Callback for matchMedia listener that overrides the pinnable header's stored state
8 * at a certain breakpoint and forces it to unpin.
9 * Usage of 'e.matches' assumes a `max-width` not `min-width` media query.
10 *
11 * @param {HTMLElement} header
12 * @param {MediaQueryList|MediaQueryListEvent} e
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 // (typescript null check)
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 * Saves the persistent pinnable state in the element's dataset
41 * so that it can be overridden at lower resolutions and the
42 * reverted to at wider resolutions.
43 *
44 * This is not necessarily the elements current state, but it
45 * seeks to represent the state of the saved user preference.
46 *
47 * @param {HTMLElement} header
48 */
49 function setSavedPinnableState( header ) {
50 header.dataset.savedPinnedState = String( isPinned( header ) );
51 }
52
53 /**
54 * Toggle classes on the body and pinnable element
55 *
56 * @param {HTMLElement} header pinnable element
57 */
58 function togglePinnableClasses( header ) {
59 const featureName = /** @type {string} */ ( header.dataset.featureName );
60
61 // Leverage features.js to toggle the body classes and persist the state
62 // for logged-in users.
63 features.toggle( featureName );
64
65 // Toggle pinned class
66 header.classList.toggle( PINNED_HEADER_CLASS );
67 header.classList.toggle( UNPINNED_HEADER_CLASS );
68 }
69
70 /**
71 * Create the indicators for the pinnable element
72 *
73 * @param {string} pinnableElementId
74 */
75 function addPinnableElementIndicator( pinnableElementId ) {
76 const dropdownSelector = document.querySelector( `#${ pinnableElementId }-dropdown` );
77 const container = dropdownSelector && dropdownSelector.parentElement;
78 if ( container ) {
79 // Possible messages include:
80 // * vector-page-tools-unpinned-popup
81 // * vector-main-menu-unpinned-popup
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 * Event handler that toggles the pinnable elements pinned state.
94 * Also moves the pinned element when those params are provided
95 * (via data attributes).
96 *
97 * @param {HTMLElement} header PinnableHeader element.
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 // Optional functionality of moving the pinnable element in the DOM
110 // to different containers based on it's pinned status
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 * Sets focus on the correct toggle button depending on the pinned state.
125 * Also opens the dropdown containing the unpinned element.
126 *
127 * @param {string} pinnableElementId
128 */
129 function setFocusAfterToggle( pinnableElementId ) {
130 let focusElement;
131 const pinnableElement = document.getElementById( pinnableElementId );
132 const header = /** @type {HTMLElement|null} */ ( pinnableElement && pinnableElement.querySelector( '.vector-pinnable-header' ) );
133 if ( !pinnableElement || !header ) {
134 return;
135 }
136 if ( isPinned( header ) ) {
137 focusElement = /** @type {HTMLElement|null} */ ( pinnableElement.querySelector( '.vector-pinnable-header-unpin-button' ) );
138 } else {
139 const dropdown = pinnableElement.closest( '.vector-dropdown' );
140 focusElement = /** @type {HTMLInputElement|null} */ ( dropdown && dropdown.querySelector( '.vector-menu-checkbox' ) );
141 }
142 if ( focusElement ) {
143 focusElement.focus();
144 }
145 }
146
147 /**
148 * Binds all the toggle buttons in a pinnableElement
149 * to the click handler that enables pinnability.
150 *
151 * @param {HTMLElement} header
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 * Binds pinnable breakpoint to allow automatic unpinning
162 * of pinnable elements with pinnedContainerId and unpinnedContainerId defined
163 *
164 * @param {HTMLElement} header
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 // Set saved pinned state for narrow breakpoint behaviour.
174 setSavedPinnableState( header );
175 // Check the breakpoint in case an override is needed on pageload.
176 disablePinningAtBreakpoint( header, pinnableBreakpoint );
177
178 // Add match media handler.
179 if ( pinnableBreakpoint.addEventListener ) {
180 pinnableBreakpoint.addEventListener( 'change', disablePinningAtBreakpoint.bind( null, header ) );
181 } else {
182 // Before Safari 14, MediaQueryList is based on EventTarget,
183 // so you must use addListener() and removeListener() to observe media query lists.
184 pinnableBreakpoint.addListener( disablePinningAtBreakpoint.bind( null, header ) );
185 }
186 }
187
188 /**
189 * @param {HTMLElement} header
190 * @return {boolean} Returns true if the element is pinned and false otherwise.
191 */
192 function isPinned( header ) {
193 const featureName = /** @type {string} */ ( header.dataset.featureName );
194 return features.isEnabled( featureName );
195 }
196
197 /**
198 * Ensures the header classes are in sync with the pinnable headers state
199 * in the case that it's moved via movePinnableElement().
200 *
201 * @param {HTMLElement} pinnableElement
202 */
203 function updatePinnableHeaderClass( pinnableElement ) {
204 const header = pinnableElement.querySelector( '.vector-pinnable-header' );
205
206 // Because Typescript
207 if ( !header || !( header instanceof HTMLElement ) ) {
208 return;
209 }
210
211 // Toggle header classes
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 * @param {string} pinnableElementId
223 * @param {string} newContainerId
224 */
225 function movePinnableElement( pinnableElementId, newContainerId ) {
226 const pinnableElem = document.getElementById( pinnableElementId );
227 const newContainer = document.getElementById( newContainerId );
228 const currContainer = /** @type {HTMLElement} */ ( pinnableElem && pinnableElem.parentElement );
229
230 if ( !pinnableElem || !newContainer || !currContainer ) {
231 return;
232 }
233
234 // Avoid moving element if unnecessary
235 if ( currContainer.id !== newContainerId ) {
236 newContainer.insertAdjacentElement( 'beforeend', pinnableElem );
237 updatePinnableHeaderClass( pinnableElem );
238 }
239
240 popupNotification.hideAll();
241 }
242
243 /**
244 * Update the pinnable element location in the DOM based off of whether its pinned or not.
245 * This is only necessary with pinnable elements that use client preferences (i.e. appearance menu)
246 * as all other pinnable elements should be serverside rendered in the correct location
247 *
248 * @param {HTMLElement} header
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 = /** @type {NodeListOf<HTMLElement>} */ ( 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 // T349924: Remove hasPinnedElements after one cycle of analyticsPinnedState() merge.
271 /**
272 * Checks if at least one of the elements in the HTML document is pinned based on CSS class names.
273 *
274 * @method
275 * @return {boolean} True if at least one pinned element is found, otherwise false.
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 * @stable for use in WikimediaEvents only.
287 * Checks if at least one of the elements in the HTML document is pinned based on CSS class names.
288 *
289 * @method
290 * @return {boolean} True if at least one pinned element is found, otherwise false.
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 // T349924: Remove hasPinnedElements.
299 hasPinnedElements,
300 analyticsPinnedState,
301 initPinnableElement,
302 movePinnableElement,
303 setFocusAfterToggle,
304 isPinned,
305 PINNED_HEADER_CLASS,
306 UNPINNED_HEADER_CLASS
307 };