· 8 years ago · Feb 05, 2018, 09:52 PM
1/**
2 * Keep code in MediaWiki:Common.js to a minimum as it is unconditionally
3 * loaded for all users on every wiki page. If possible create a gadget that is
4 * enabled by default instead of adding it here (since gadgets are fully
5 * optimized ResourceLoader modules with possibility to add dependencies etc.)
6 *
7 * Since Common.js isn't a gadget, there is no place to declare its
8 * dependencies, so we have to lazy load them with mw.loader.using on demand and
9 * then execute the rest in the callback. In most cases these dependencies will
10 * be loaded (or loading) already and the callback will not be delayed. In case a
11 * dependency hasn't arrived yet it'll make sure those are loaded before this.
12 */
13
14/* global mw, $ */
15/* jshint strict:false, browser:true */
16
17mw.loader.using( ['mediawiki.user', 'mediawiki.util', 'mediawiki.notify'] ).done( function () {
18/* Begin of mw.loader.using callback */
19
20/**
21 * Main Page layout fixes
22 *
23 * Description: Adds an additional link to the complete list of languages available.
24 * Maintainers: [[User:AzaToth]], [[User:R. Koot]], [[User:Alex Smotrov]]
25 */
26if ( mw.config.get( 'wgPageName' ) === 'Main_Page' || mw.config.get( 'wgPageName' ) === 'Talk:Main_Page' ) {
27 $( function () {
28 mw.util.addPortletLink( 'p-lang', '//meta.wikimedia.org/wiki/List_of_Wikipedias',
29 'Complete list', 'interwiki-completelist', 'Complete list of Wikipedias' );
30 } );
31}
32
33/**
34 * Redirect User:Name/skin.js and skin.css to the current skin's pages
35 * (unless the 'skin' page really exists)
36 * @source: http://www.mediawiki.org/wiki/Snippets/Redirect_skin.js
37 * @rev: 2
38 */
39if ( mw.config.get( 'wgArticleId' ) === 0 && mw.config.get( 'wgNamespaceNumber' ) === 2 ) {
40 var titleParts = mw.config.get( 'wgPageName' ).split( '/' );
41 /* Make sure there was a part before and after the slash
42 and that the latter is 'skin.js' or 'skin.css' */
43 if ( titleParts.length == 2 ) {
44 var userSkinPage = titleParts.shift() + '/' + mw.config.get( 'skin' );
45 if ( titleParts.slice( -1 ) == 'skin.js' ) {
46 window.location.href = mw.util.getUrl( userSkinPage + '.js' );
47 } else if ( titleParts.slice( -1 ) == 'skin.css' ) {
48 window.location.href = mw.util.getUrl( userSkinPage + '.css' );
49 }
50 }
51}
52
53/**
54 * Map addPortletLink to mw.util
55 * @deprecated: Use mw.util.addPortletLink instead.
56 */
57mw.log.deprecate( window, 'addPortletLink', mw.util.addPortletLink, 'Use mw.util.addPortletLink instead' );
58
59/**
60 * Extract a URL parameter from the current URL
61 * @deprecated: Use mw.util.getParamValue with proper escaping
62 */
63mw.log.deprecate( window, 'getURLParamValue', mw.util.getParamValue, 'Use mw.util.getParamValue instead' );
64
65/**
66 * Test if an element has a certain class
67 * @deprecated: Use $(element).hasClass() instead.
68 */
69mw.log.deprecate( window, 'hasClass', function ( element, className ) {
70 return $( element ).hasClass( className );
71}, 'Use jQuery.hasClass() instead' );
72
73/**
74 * @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
75 * @rev 6
76 */
77var extraCSS = mw.util.getParamValue( 'withCSS' ),
78 extraJS = mw.util.getParamValue( 'withJS' );
79
80if ( extraCSS ) {
81 if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
82 mw.loader.load( '/w/index.php?title=' + extraCSS + '&action=raw&ctype=text/css', 'text/css' );
83 } else {
84 mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
85 }
86}
87
88if ( extraJS ) {
89 if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
90 mw.loader.load( '/w/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript' );
91 } else {
92 mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
93 }
94}
95
96/**
97 * WikiMiniAtlas
98 *
99 * Description: WikiMiniAtlas is a popup click and drag world map.
100 * This script causes all of our coordinate links to display the WikiMiniAtlas popup button.
101 * The script itself is located on meta because it is used by many projects.
102 * See [[Meta:WikiMiniAtlas]] for more information.
103 * Note - use of this service is recommended to be repalced with mw:Help:Extension:Kartographer
104 */
105( function () {
106 var require_wikiminiatlas = false;
107 var coord_filter = /geohack/;
108 $( function () {
109 $( 'a.external.text' ).each( function( key, link ) {
110 if ( link.href && coord_filter.exec( link.href ) ) {
111 require_wikiminiatlas = true;
112 // break from loop
113 return false;
114 }
115 } );
116 if ( $( 'div.kmldata' ).length ) {
117 require_wikiminiatlas = true;
118 }
119 if ( require_wikiminiatlas ) {
120 mw.loader.load( '//meta.wikimedia.org/w/index.php?title=MediaWiki:Wikiminiatlas.js&action=raw&ctype=text/javascript' );
121 }
122 } );
123} )();
124
125/**
126 * Collapsible tables
127 *
128 * Allows tables to be collapsed, showing only the header. See [[Help:Collapsing]].
129 *
130 * @version 2.0.3 (2014-03-14)
131 * @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
132 * @author [[User:R. Koot]]
133 * @author [[User:Krinkle]]
134 * @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which
135 * is supported in MediaWiki core.
136 */
137
138var autoCollapse = 2;
139var collapseCaption = 'hide';
140var expandCaption = 'show';
141var tableIndex = 0;
142
143function collapseTable( tableIndex ) {
144 var Button = document.getElementById( 'collapseButton' + tableIndex );
145 var Table = document.getElementById( 'collapsibleTable' + tableIndex );
146
147 if ( !Table || !Button ) {
148 return false;
149 }
150
151 var Rows = Table.rows;
152 var i;
153 var $row0 = $(Rows[0]);
154
155 if ( Button.firstChild.data === collapseCaption ) {
156 for ( i = 1; i < Rows.length; i++ ) {
157 Rows[i].style.display = 'none';
158 }
159 Button.firstChild.data = expandCaption;
160 } else {
161 for ( i = 1; i < Rows.length; i++ ) {
162 Rows[i].style.display = $row0.css( 'display' );
163 }
164 Button.firstChild.data = collapseCaption;
165 }
166}
167
168function createClickHandler( tableIndex ) {
169 return function ( e ) {
170 e.preventDefault();
171 collapseTable( tableIndex );
172 };
173}
174
175function createCollapseButtons( $content ) {
176 var NavigationBoxes = {};
177 var $Tables = $content.find( 'table' );
178 var i;
179
180 $Tables.each( function( i, table ) {
181 if ( $(table).hasClass( 'collapsible' ) ) {
182
183 /* only add button and increment count if there is a header row to work with */
184 var HeaderRow = table.getElementsByTagName( 'tr' )[0];
185 if ( !HeaderRow ) {
186 return;
187 }
188 var Header = table.getElementsByTagName( 'th' )[0];
189 if ( !Header ) {
190 return;
191 }
192
193 NavigationBoxes[ tableIndex ] = table;
194 table.setAttribute( 'id', 'collapsibleTable' + tableIndex );
195
196 var Button = document.createElement( 'span' );
197 var ButtonLink = document.createElement( 'a' );
198 var ButtonText = document.createTextNode( collapseCaption );
199 // Styles are declared in [[MediaWiki:Common.css]]
200 Button.className = 'collapseButton';
201
202 ButtonLink.style.color = Header.style.color;
203 ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
204 ButtonLink.setAttribute( 'href', '#' );
205 $( ButtonLink ).on( 'click', createClickHandler( tableIndex ) );
206 ButtonLink.appendChild( ButtonText );
207
208 Button.appendChild( document.createTextNode( '[' ) );
209 Button.appendChild( ButtonLink );
210 Button.appendChild( document.createTextNode( ']' ) );
211
212 Header.insertBefore( Button, Header.firstChild );
213 tableIndex++;
214 }
215 } );
216
217 for ( i = 0; i < tableIndex; i++ ) {
218 if ( $( NavigationBoxes[i] ).hasClass( 'collapsed' ) ||
219 ( tableIndex >= autoCollapse && $( NavigationBoxes[i] ).hasClass( 'autocollapse' ) )
220 ) {
221 collapseTable( i );
222 }
223 else if ( $( NavigationBoxes[i] ).hasClass ( 'innercollapse' ) ) {
224 var element = NavigationBoxes[i];
225 while ((element = element.parentNode)) {
226 if ( $( element ).hasClass( 'outercollapse' ) ) {
227 collapseTable ( i );
228 break;
229 }
230 }
231 }
232 }
233}
234
235mw.hook( 'wikipage.content' ).add( createCollapseButtons );
236
237/**
238 * Add support to mw-collapsible for autocollapse, innercollapse and outercollapse
239 *
240 * Maintainers: TheDJ
241 */
242function mwCollapsibleSetup( $collapsibleContent ) {
243 var $element,
244 $toggle,
245 autoCollapseThreshold = 2;
246 $.each( $collapsibleContent, function (index, element) {
247 $element = $( element );
248 if ( $collapsibleContent.length > autoCollapseThreshold && $element.hasClass( 'autocollapse' ) ) {
249 $element.data( 'mw-collapsible' ).collapse();
250 } else if ( $element.hasClass( 'innercollapse' ) ) {
251 if ( $element.parents( '.outercollapse' ).length > 0 ) {
252 $element.data( 'mw-collapsible' ).collapse();
253 }
254 }
255 $toggle = $element.find( '.mw-collapsible-toggle' );
256 if ( $toggle.length ) {
257 // Make the toggle inherit text color
258 if( $toggle.parent()[0].style.color ) {
259 $toggle.find( 'a' ).css( 'color', 'inherit' );
260 }
261 }
262 } );
263}
264
265mw.hook( 'wikipage.collapsibleContent' ).add( mwCollapsibleSetup );
266
267/**
268 * Dynamic Navigation Bars (experimental)
269 *
270 * Description: See [[Wikipedia:NavFrame]].
271 * Maintainers: UNMAINTAINED
272 */
273
274/* set up the words in your language */
275var NavigationBarHide = '[' + collapseCaption + ']';
276var NavigationBarShow = '[' + expandCaption + ']';
277var indexNavigationBar = 0;
278
279/**
280 * Shows and hides content and picture (if available) of navigation bars
281 * Parameters:
282 * indexNavigationBar: the index of navigation bar to be toggled
283 **/
284window.toggleNavigationBar = function ( indexNavigationBar, event ) {
285 var NavToggle = document.getElementById( 'NavToggle' + indexNavigationBar );
286 var NavFrame = document.getElementById( 'NavFrame' + indexNavigationBar );
287 var NavChild;
288
289 if ( !NavFrame || !NavToggle ) {
290 return false;
291 }
292
293 /* if shown now */
294 if ( NavToggle.firstChild.data === NavigationBarHide ) {
295 for ( NavChild = NavFrame.firstChild; NavChild !== null; NavChild = NavChild.nextSibling ) {
296 if ( $( NavChild ).hasClass( 'NavContent' ) || $( NavChild ).hasClass( 'NavPic' ) ) {
297 NavChild.style.display = 'none';
298 }
299 }
300 NavToggle.firstChild.data = NavigationBarShow;
301
302 /* if hidden now */
303 } else if ( NavToggle.firstChild.data === NavigationBarShow ) {
304 for ( NavChild = NavFrame.firstChild; NavChild !== null; NavChild = NavChild.nextSibling ) {
305 if ( $( NavChild ).hasClass( 'NavContent' ) || $( NavChild ).hasClass( 'NavPic' ) ) {
306 NavChild.style.display = 'block';
307 }
308 }
309 NavToggle.firstChild.data = NavigationBarHide;
310 }
311
312 event.preventDefault();
313};
314
315/* adds show/hide-button to navigation bars */
316function createNavigationBarToggleButton( $content ) {
317 var NavChild;
318 /* iterate over all < div >-elements */
319 var $divs = $content.find( 'div' );
320 $divs.each( function ( i, NavFrame ) {
321 /* if found a navigation bar */
322 if ( $( NavFrame ).hasClass( 'NavFrame' ) ) {
323
324 indexNavigationBar++;
325 var NavToggle = document.createElement( 'a' );
326 NavToggle.className = 'NavToggle';
327 NavToggle.setAttribute( 'id', 'NavToggle' + indexNavigationBar );
328 NavToggle.setAttribute( 'href', '#' );
329 $( NavToggle ).on( 'click', $.proxy( window.toggleNavigationBar, window, indexNavigationBar ) );
330
331 var isCollapsed = $( NavFrame ).hasClass( 'collapsed' );
332 /**
333 * Check if any children are already hidden. This loop is here for backwards compatibility:
334 * the old way of making NavFrames start out collapsed was to manually add style="display:none"
335 * to all the NavPic/NavContent elements. Since this was bad for accessibility (no way to make
336 * the content visible without JavaScript support), the new recommended way is to add the class
337 * "collapsed" to the NavFrame itself, just like with collapsible tables.
338 */
339 for ( NavChild = NavFrame.firstChild; NavChild !== null && !isCollapsed; NavChild = NavChild.nextSibling ) {
340 if ( $( NavChild ).hasClass( 'NavPic' ) || $( NavChild ).hasClass( 'NavContent' ) ) {
341 if ( NavChild.style.display === 'none' ) {
342 isCollapsed = true;
343 }
344 }
345 }
346 if ( isCollapsed ) {
347 for ( NavChild = NavFrame.firstChild; NavChild !== null; NavChild = NavChild.nextSibling ) {
348 if ( $( NavChild ).hasClass( 'NavPic' ) || $( NavChild ).hasClass( 'NavContent' ) ) {
349 NavChild.style.display = 'none';
350 }
351 }
352 }
353 var NavToggleText = document.createTextNode( isCollapsed ? NavigationBarShow : NavigationBarHide );
354 NavToggle.appendChild( NavToggleText );
355
356 /* Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked) */
357 for( var j = 0; j < NavFrame.childNodes.length; j++ ) {
358 if ( $( NavFrame.childNodes[j] ).hasClass( 'NavHead' ) ) {
359 NavToggle.style.color = NavFrame.childNodes[j].style.color;
360 NavFrame.childNodes[j].appendChild( NavToggle );
361 }
362 }
363 NavFrame.setAttribute( 'id', 'NavFrame' + indexNavigationBar );
364 }
365 } );
366}
367
368mw.hook( 'wikipage.content' ).add( createNavigationBarToggleButton );
369
370/**
371 * Uploadwizard_newusers
372 * Switches in a message for non-autoconfirmed users at [[Wikipedia:Upload]]
373 *
374 * Maintainers: [[User:Krimpet]]
375 */
376function uploadwizard_newusers() {
377 if ( mw.config.get( 'wgNamespaceNumber' ) === 4 && mw.config.get( 'wgTitle' ) === 'Upload' && mw.config.get( 'wgAction' ) === 'view' ) {
378 var oldDiv = document.getElementById( 'autoconfirmedusers' ),
379 newDiv = document.getElementById( 'newusers' );
380 if ( oldDiv && newDiv ) {
381 var userGroups = mw.config.get( 'wgUserGroups' );
382 if ( userGroups ) {
383 for ( var i = 0; i < userGroups.length; i++ ) {
384 if ( userGroups[i] === 'autoconfirmed' ) {
385 oldDiv.style.display = 'block';
386 newDiv.style.display = 'none';
387 return;
388 }
389 }
390 }
391 oldDiv.style.display = 'none';
392 newDiv.style.display = 'block';
393 return;
394 }
395 }
396}
397
398$(uploadwizard_newusers);
399
400/**
401 * Magic editintros ****************************************************
402 *
403 * Description: Adds editintros on disambiguation pages and BLP pages.
404 * Maintainers: [[User:RockMFR]]
405 */
406function addEditIntro( name ) {
407 $( '.mw-editsection, #ca-edit' ).find( 'a' ).each( function ( i, el ) {
408 el.href = $( this ).attr( 'href' ) + '&editintro=' + name;
409 } );
410}
411
412if ( mw.config.get( 'wgNamespaceNumber' ) === 0 ) {
413 $( function () {
414 if ( document.getElementById( 'disambigbox' ) ) {
415 addEditIntro( 'Template:Disambig_editintro' );
416 }
417 } );
418
419 $( function () {
420 var cats = mw.config.get('wgCategories');
421 if ( !cats ) {
422 return;
423 }
424 if ( $.inArray( 'Living people', cats ) !== -1 || $.inArray( 'Possibly living people', cats ) !== -1 ) {
425 addEditIntro( 'Template:BLP_editintro' );
426 }
427 } );
428}
429
430/* Actions specific to the edit page */
431if ( mw.config.get( 'wgAction' ) === 'edit' || mw.config.get( 'wgAction' ) === 'submit' ) {
432 /**
433 * Fix edit summary prompt for undo
434 *
435 * Fixes the fact that the undo function combined with the "no edit summary prompter"
436 * complains about missing editsummary, if leaving the edit summary unchanged.
437 * Added by [[User:Deskana]], code by [[User:Tra]].
438 * See also [[phab:T10912]].
439 */
440 $(function () {
441 if (document.location.search.indexOf('undo=') !== -1 && document.getElementsByName('wpAutoSummary')[0]) {
442 document.getElementsByName('wpAutoSummary')[0].value = '1';
443 }
444 });
445}
446
447/* End of mw.loader.using callback */
448} );
449/* DO NOT ADD CODE BELOW THIS LINE */