Debian Codegraph @

resources/src/mediawiki.page.ready/ready.js

raw symbols SCIP
1 const checkboxShift = require( './checkboxShift.js' );
2 const config = require( './config.json' );
3 const teleportTarget = require( './teleportTarget.js' );
4
5 // Break out of framesets
6 if ( mw.config.get( 'wgBreakFrames' ) ) {
7 // Note: In IE < 9 strict comparison to window is non-standard (the standard didn't exist yet)
8 // it works only comparing to window.self or window.window (https://stackoverflow.com/q/4850978/319266)
9 if ( window.top !== window.self ) {
10 // Un-trap us from framesets
11 window.top.location.href = location.href;
12 }
13 }
14
15 mw.hook( 'wikipage.content' ).add( ( $content ) => {
16 const modules = [];
17
18 let $collapsible;
19 if ( config.collapsible ) {
20 $collapsible = $content.find( '.mw-collapsible' );
21 if ( $collapsible.length ) {
22 modules.push( 'jquery.makeCollapsible' );
23 }
24 }
25
26 let $sortable;
27 if ( config.sortable ) {
28 $sortable = $content.find( 'table.sortable' );
29 if ( $sortable.length ) {
30 modules.push( 'jquery.tablesorter' );
31 }
32 }
33
34 if ( modules.length ) {
35 // Both modules are preloaded by Skin::getDefaultModules()
36 mw.loader.using( modules ).then( () => {
37 // For tables that are both sortable and collapsible,
38 // it must be made sortable first and collapsible second.
39 // This is because jquery.tablesorter stumbles on the
40 // elements inserted by jquery.makeCollapsible (T64878)
41 if ( $sortable && $sortable.length ) {
42 $sortable.tablesorter();
43 }
44 if ( $collapsible && $collapsible.length ) {
45 $collapsible.makeCollapsible();
46 }
47 } );
48 }
49 if ( $content[ 0 ] && $content[ 0 ].isConnected === false ) {
50 mw.log.warn( 'wikipage.content hook should not be fired on unattached content' );
51 }
52
53 checkboxShift( $content.find( 'input[type="checkbox"]:not(.noshiftselect)' ) );
54 } );
55
56 // Add toolbox portlet to toggle all collapsibles if there are any
57 require( './toggleAllCollapsibles.js' );
58
59 // Handle elements outside the wikipage content
60 $( () => {
61 /**
62 * There is a bug on iPad and maybe other browsers where if initial-scale is not set
63 * the page cannot be zoomed. If the initial-scale is set on the server side, this will result
64 * in an unwanted zoom on mobile devices. To avoid this we check innerWidth and set the
65 * initial-scale on the client where needed. The width must be synced with the value in
66 * Skin::initPage.
67 * More information on this bug in [[phab:T311795]].
68 *
69 * @ignore
70 */
71 function fixViewportForTabletDevices() {
72 const $viewport = $( 'meta[name=viewport]' );
73 const content = $viewport.attr( 'content' );
74 const scale = window.outerWidth / window.innerWidth;
75 // This adjustment is limited to tablet devices. It must be a non-zero value to work.
76 // (these values correspond to @min-width-breakpoint-tablet and @min-width-breakpoint-desktop
77 // See https://doc.wikimedia.org/codex/main/design-tokens/breakpoint.html
78 if ( window.innerWidth >= 640 && window.innerWidth < 1120 &&
79 content && content.indexOf( 'initial-scale' ) === -1
80 ) {
81 // Note:
82 // - The `width` value must be equal to @min-width-breakpoint-desktop above
83 // - If `initial-scale` value is 1 the font-size adjust feature will not work on iPad
84 $viewport.attr( 'content', 'width=1120,initial-scale=' + scale );
85 }
86 }
87
88 // Add accesskey hints to the tooltips
89 $( '[accesskey]' ).updateTooltipAccessKeys();
90
91 const node = document.querySelector( '.mw-indicators' );
92 if ( node && node.children.length ) {
93 /**
94 * Fired when a page's status indicators are being added to the DOM.
95 *
96 * @event ~'wikipage.indicators'
97 * @memberof Hooks
98 * @param {jQuery} $content jQuery object with the elements of the indicators
99 * @see https://www.mediawiki.org/wiki/Special:MyLanguage/Help:Page_status_indicators
100 */
101 mw.hook( 'wikipage.indicators' ).fire( $( node.children ) );
102 }
103
104 const $content = $( '#mw-content-text' );
105 // Avoid unusable events, and the errors they cause, for custom skins that
106 // do not display any content (T259577).
107 if ( $content.length ) {
108 /**
109 * Fired when wiki content has been added to the DOM.
110 *
111 * This should only be fired after $content has been attached.
112 *
113 * This includes the ready event on a page load (including post-edit loads)
114 * and when content has been previewed with LivePreview.
115 *
116 * @event ~'wikipage.content'
117 * @memberof Hooks
118 * @param {jQuery} $content The most appropriate element containing the content,
119 * such as #mw-content-text (regular content root) or #wikiPreview (live preview
120 * root)
121 */
122 mw.hook( 'wikipage.content' ).fire( $content );
123 }
124
125 let $nodes = $( '.catlinks[data-mw="interface"]' );
126 if ( $nodes.length ) {
127 /**
128 * Fired when categories are being added to the DOM.
129 *
130 * It is encouraged to fire it before the main DOM is changed (when $content
131 * is still detached). However, this order is not defined either way, so you
132 * should only rely on $content itself.
133 *
134 * This includes the ready event on a page load (including post-edit loads)
135 * and when content has been previewed with LivePreview.
136 *
137 * @event ~'wikipage.categories'
138 * @memberof Hooks
139 * @param {jQuery} $content The most appropriate element containing the content,
140 * such as .catlinks
141 */
142 mw.hook( 'wikipage.categories' ).fire( $nodes );
143 }
144
145 $nodes = $( 'table.diff[data-mw="interface"]' );
146 if ( $nodes.length ) {
147 /**
148 * Fired when the diff is added to a page containing a diff.
149 *
150 * Similar to the {@link Hooks~'wikipage.content' wikipage.content hook}
151 * $diff may still be detached when the hook is fired.
152 *
153 * @event ~'wikipage.diff'
154 * @memberof Hooks
155 * @param {jQuery} $diff The root element of the MediaWiki diff (`table.diff`).
156 */
157 mw.hook( 'wikipage.diff' ).fire( $nodes.eq( 0 ) );
158 }
159
160 $( '#t-print a' ).on( 'click', ( e ) => {
161 window.print();
162 e.preventDefault();
163 } );
164
165 const $permanentLink = $( '#t-permalink a' );
166 function updatePermanentLinkHash() {
167 if ( mw.util.getTargetFromFragment() ) {
168 $permanentLink[ 0 ].hash = location.hash;
169 } else {
170 $permanentLink[ 0 ].hash = '';
171 }
172 }
173 if ( $permanentLink.length ) {
174 $( window ).on( 'hashchange', updatePermanentLinkHash );
175 updatePermanentLinkHash();
176 }
177
178 /**
179 * Fired when a trusted UI element to perform a logout has been activated.
180 *
181 * This will end the user session, and either redirect to the given URL
182 * on success, or queue an error message via {@link mw.notification}.
183 *
184 * @event ~'skin.logout'
185 * @memberof Hooks
186 * @param {string} href Full URL
187 */
188 const LOGOUT_EVENT = 'skin.logout';
189 function logoutViaPost( href ) {
190 mw.notify(
191 mw.message( 'logging-out-notify' ),
192 { tag: 'logout', autoHide: false }
193 );
194 const api = new mw.Api();
195 if ( mw.user.isTemp() ) {
196 // Indicate to the success page that the user was previously a temporary account, so that the success
197 // message can be customised appropriately.
198 const url = new URL( href );
199 url.searchParams.append( 'wasTempUser', 1 );
200 href = url;
201 }
202 api.postWithToken( 'csrf', {
203 action: 'logout'
204 } ).then(
205 () => {
206 location.href = href;
207 },
208 ( err, data ) => {
209 mw.notify(
210 api.getErrorMessage( data ),
211 { type: 'error', tag: 'logout', autoHide: false }
212 );
213 }
214 );
215 }
216 // Turn logout to a POST action
217 mw.hook( LOGOUT_EVENT ).add( logoutViaPost );
218 $( config.selectorLogoutLink ).on( 'click', function ( e ) {
219 mw.hook( LOGOUT_EVENT ).fire( this.href );
220 e.preventDefault();
221 } );
222 fixViewportForTabletDevices();
223
224 teleportTarget.attach();
225 } );
226
227 /**
228 * @private
229 * @param {HTMLElement} element
230 * @return {boolean} Whether the element is a search input.
231 */
232 function isSearchInput( element ) {
233 return element.id === 'searchInput' ||
234 element.classList.contains( 'mw-searchInput' );
235 }
236
237 /**
238 * Load a given module when a search input is focused.
239 *
240 * @memberof module:mediawiki.page.ready
241 * @param {string} moduleName Name of a module
242 */
243 function loadSearchModule( moduleName ) {
244 // T251544: Collect search performance metrics to compare Vue search with
245 // mediawiki.searchSuggest performance. Marks and Measures will only be
246 // recorded on the Vector skin.
247 //
248 // Vue search isn't loaded through this function so we are only collecting
249 // legacy search performance metrics here.
250
251 const shouldTestSearch = !!( moduleName === 'mediawiki.searchSuggest' &&
252 mw.config.get( 'skin' ) === 'vector' &&
253 window.performance &&
254 performance.mark &&
255 performance.measure &&
256
257 performance.getEntriesByName ),
258 loadStartMark = 'mwVectorLegacySearchLoadStart',
259 loadEndMark = 'mwVectorLegacySearchLoadEnd';
260
261 function requestSearchModule() {
262 if ( shouldTestSearch ) {
263 performance.mark( loadStartMark );
264 }
265 mw.loader.using( moduleName, () => {
266 if ( shouldTestSearch && performance.getEntriesByName( loadStartMark ).length ) {
267 performance.mark( loadEndMark );
268 performance.measure( 'mwVectorLegacySearchLoadStartToLoadEnd', loadStartMark, loadEndMark );
269 }
270 } );
271 }
272
273 // Load the module once a search input is focussed.
274 function eventListener( e ) {
275 if ( isSearchInput( e.target ) ) {
276 requestSearchModule();
277
278 document.removeEventListener( 'focusin', eventListener );
279 }
280 }
281
282 // Load the module now if the search input is already focused,
283 // because the user started typing before the JavaScript arrived.
284 if ( document.activeElement && isSearchInput( document.activeElement ) ) {
285 requestSearchModule();
286 return;
287 }
288
289 document.addEventListener( 'focusin', eventListener );
290 }
291
292 // Skins may decide to disable this behaviour or use an alternative module.
293 if ( config.search ) {
294 loadSearchModule( 'mediawiki.searchSuggest' );
295 }
296
297 try {
298 // Load the post-edit notification module if a notification has been scheduled.
299 // Use `sessionStorage` directly instead of 'mediawiki.storage' to minimize dependencies.
300 if ( sessionStorage.getItem( 'mw-PostEdit' + mw.config.get( 'wgPageName' ) ) ) {
301 mw.loader.load( 'mediawiki.action.view.postEdit' );
302 }
303 } catch ( err ) {}
304
305 /**
306 * @exports mediawiki.page.ready
307 */
308 module.exports = {
309 loadSearchModule,
310 /** @type {module:mediawiki.page.ready.CheckboxHack} */
311 checkboxHack: require( './checkboxHack.js' ),
312 /**
313 * A container for displaying elements that overlay the page, such as dialogs.
314 *
315 * @type {HTMLElement}
316 */
317 teleportTarget: teleportTarget.target
318 };