Difference between revisions of "MediaWiki:Common.js"
Dinoguy1000 (talk | contribs) ((hopefully) fix card table image switcher script; indent with tabs; add missing semicolons) |
(→top: Restoring revision 4676729 by User:Dinoguy1000 on 2021-04-20 19:35:50. update LAST_LOG) (Tag: New user edit) |
||
(48 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | /*global mw, $, console | + | /*global mw, $, console, enableOldForumEdit */ |
/*jshint browser:true, curly:false, eqnull:true, strict:false */ | /*jshint browser:true, curly:false, eqnull:true, strict:false */ | ||
mw.loader.using(['mediawiki.util', 'jquery.client'], function () { | mw.loader.using(['mediawiki.util', 'jquery.client'], function () { | ||
/* Begin of mw.loader.using callback */ | /* Begin of mw.loader.using callback */ | ||
− | |||
− | |||
/** | /** | ||
* update this (replace the timestamp with 5 tildes) whenever this page is edited | * update this (replace the timestamp with 5 tildes) whenever this page is edited | ||
* this simplifies checking if new code has cleared server caches | * this simplifies checking if new code has cleared server caches | ||
*/ | */ | ||
− | console.log('MediaWiki:Common.js last updated: | + | var LAST_LOG = '19:35, 20 April 2021 (UTC)'; |
− | + | console.log('MediaWiki:Common.js last updated:', LAST_LOG); | |
− | |||
/** | /** | ||
− | * | + | * Customize wikitext highlighter colors. |
− | |||
*/ | */ | ||
− | window. | + | window.syntaxHighlighterSiteConfig = { |
− | + | wikilinkColor: "#DFEFFF", | |
− | + | externalLinkColor: "#DFEFFF", | |
− | + | listOrIndentColor: "#FFDAD0", | |
− | + | tableColor: "#E8BDF0", | |
− | + | templateColor: "#EAE4D7", | |
− | + | parameterColor: "#BAFFBF", | |
− | + | tagColor: "#FFDAD0", | |
− | + | entityColor: "#B5C3B5" | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
}; | }; | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
/** | /** | ||
* Cleanup excessive space in hlist elements | * Cleanup excessive space in hlist elements | ||
Line 194: | Line 29: | ||
items[i].innerHTML = items[i].innerHTML.trim(); | items[i].innerHTML = items[i].innerHTML.trim(); | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
/** | /** | ||
* Remove empty rows from {{Infobox}} transclusions | * Remove empty rows from {{Infobox}} transclusions | ||
Line 391: | Line 41: | ||
} | } | ||
}); | }); | ||
− | |||
/** | /** | ||
− | * | + | * The following code is currently maintained by [[User:Becasita]]. |
+ | * Contact [[User talk:Becasita]] if the need arises. | ||
*/ | */ | ||
− | + | ( function( window, $, mw ) { | |
− | + | /** | |
− | + | * Fix Special:Ask table headers when wikilinked. | |
− | + | * Address to «.JS-changed» to find what elements were changed/created. | |
− | + | */ | |
− | if ( | + | $( function _specialAskLabelsLinks() { |
− | + | if ( mw.config.get( 'wgPageName' ).split( /\s*\/\s*/ )[ 0 ] === 'Special:Ask' ) { | |
+ | $( '.smwtable' ).find( 'th' ).each( function( i, el ) { | ||
+ | $( el ).html( function() { | ||
+ | return $( this ).text().replace( /\[\[(.*?)]]/g, $( '<a>', { | ||
+ | class: 'JS-changed', | ||
+ | href: 'wiki/$1', | ||
+ | text: '$1' | ||
+ | } ).prop( 'outerHTML' ) ); | ||
+ | } ); | ||
+ | } ); | ||
} | } | ||
− | } | + | } ); |
− | + | /** End. */ | |
− | + | /** | |
− | /** | + | * Fix tabbers labels achors. |
− | + | */ | |
− | + | $(function _fixTabAnchors() { | |
− | $('. | + | // Wait for the tabbers to load: |
− | + | mw.loader.using( 'ext.tabber' ).then( function() { | |
− | + | mw.log( 'DEBUG: Tabbers loaded!' ); | |
− | + | $( '.tabbernav' ).each( function( i, el ) { | |
− | + | const $el = $( el ); | |
+ | const $parent = $el.parent(); | ||
+ | // Gets the tab content, given a tab label: | ||
+ | const getTab = function( $li ) { | ||
+ | return $parent.find( '.tabbertab' ).filter( function() { | ||
+ | return this.title.trim() === $li.find( 'a' ).attr( 'title' ).trim(); | ||
+ | }); | ||
+ | }; | ||
+ | // Remove the default click event: | ||
+ | $el.off( 'click' ); | ||
+ | // Create new click event | ||
+ | $el.children().click( function( e ) { | ||
+ | e.preventDefault(); | ||
+ | // Current tab: | ||
+ | const $currentLabel = $el.find( '.tabberactive' ); | ||
+ | const $currentContent = getTab( $currentLabel ); | ||
+ | // New tab: | ||
+ | const $newLabel = $( this ); | ||
+ | const $newContent = getTab( $newLabel ); | ||
+ | // Switch tabs: | ||
+ | $currentLabel.removeClass( 'tabberactive' ); | ||
+ | $newLabel.addClass( 'tabberactive' ); | ||
+ | $currentContent.hide(); | ||
+ | $newContent.show(); | ||
+ | } ); | ||
+ | } ); | ||
+ | } ); | ||
+ | } ); | ||
+ | /** End. */ | ||
+ | /** | ||
+ | * Styles for the edit textbox after wikitext highlight kicks in. | ||
+ | */ | ||
+ | mw.hook( 'wikipage.editform' ).add( function( $editform ) { | ||
+ | var times = 0; | ||
+ | var interval = window.setInterval(function() { | ||
+ | if ( mw.loader.getState( 'ext.gadget.DotsSyntaxHighlighter' ) === 'ready' ) { | ||
+ | mw.log( '[DotsSyntaxHighlighter styles] - modifing' ); | ||
+ | $editform.find( '#wpTextbox0, #wpTextbox1' ) | ||
+ | .css( { | ||
+ | 'border': 'unset', | ||
+ | 'line-height': '1.5em', | ||
+ | 'height': 'unset', | ||
+ | } ) | ||
+ | ; | ||
+ | window.clearInterval( interval ); | ||
+ | mw.log( '[DotsSyntaxHighlighter styles] - clearing: Managed to style it.' ); | ||
+ | return; | ||
+ | } | ||
+ | if ( times > 10 ) { | ||
+ | window.clearInterval( interval ); | ||
+ | mw.log( '[DotsSyntaxHighlighter styles] - clearing: Exceeding 10 times.' ); | ||
+ | return; | ||
} | } | ||
− | + | times++; | |
− | + | }, 250 ); | |
− | }); | + | } ); |
− | + | /** | |
− | + | * Automatically add alt attributes to thumbnail images using the image's caption. | |
− | }); | + | */ |
− | + | $('.thumbinner').each(function(){ | |
− | /** | + | var attr = $(this).find('img').attr('alt'); |
− | + | if (typeof attr !== typeof undefined && attr !== false) { | |
− | + | var caption = $(this).find('.thumbcaption'); | |
− | $('. | + | caption.attr('aria-hidden', 'true'); |
− | { | + | $(this).find('img').attr('alt', caption.text()); |
− | + | } | |
− | + | }); | |
− | + | /** | |
− | + | * Hide checkbox to prevent updating double redirects | |
− | + | * automatically after page move. | |
− | + | */ | |
− | + | $( '.mw-special-Movepage #wpFixRedirects' ) | |
− | + | .closest( '.oo-ui-layout' ).hide() | |
− | + | ; | |
− | + | /** End. */ | |
− | + | } )( window, window.jQuery, window.mediaWiki ); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | $ | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
/* End of mw.loader.using callback; DO NOT ADD CODE BELOW THIS LINE */ | /* End of mw.loader.using callback; DO NOT ADD CODE BELOW THIS LINE */ | ||
}); | }); |
Latest revision as of 14:56, 19 March 2023
/*global mw, $, console, enableOldForumEdit */
/*jshint browser:true, curly:false, eqnull:true, strict:false */
mw.loader.using(['mediawiki.util', 'jquery.client'], function () {
/* Begin of mw.loader.using callback */
/**
* update this (replace the timestamp with 5 tildes) whenever this page is edited
* this simplifies checking if new code has cleared server caches
*/
var LAST_LOG = '19:35, 20 April 2021 (UTC)';
console.log('MediaWiki:Common.js last updated:', LAST_LOG);
/**
* Customize wikitext highlighter colors.
*/
window.syntaxHighlighterSiteConfig = {
wikilinkColor: "#DFEFFF",
externalLinkColor: "#DFEFFF",
listOrIndentColor: "#FFDAD0",
tableColor: "#E8BDF0",
templateColor: "#EAE4D7",
parameterColor: "#BAFFBF",
tagColor: "#FFDAD0",
entityColor: "#B5C3B5"
};
/**
* Cleanup excessive space in hlist elements
*/
var items = document.querySelectorAll('.hlist li, .hlist dt, .hlist dd');
for (var i = items.length - 1; i >= 0; i--) {
items[i].innerHTML = items[i].innerHTML.trim();
}
/**
* Remove empty rows from {{Infobox}} transclusions
*/
$('.infobox tr').each(function () {
if (
!$.trim($(this).text()) &&
!$(this).find('img').length &&
!$(this).find('hr').length
) {
$(this).remove();
}
});
/**
* The following code is currently maintained by [[User:Becasita]].
* Contact [[User talk:Becasita]] if the need arises.
*/
( function( window, $, mw ) {
/**
* Fix Special:Ask table headers when wikilinked.
* Address to «.JS-changed» to find what elements were changed/created.
*/
$( function _specialAskLabelsLinks() {
if ( mw.config.get( 'wgPageName' ).split( /\s*\/\s*/ )[ 0 ] === 'Special:Ask' ) {
$( '.smwtable' ).find( 'th' ).each( function( i, el ) {
$( el ).html( function() {
return $( this ).text().replace( /\[\[(.*?)]]/g, $( '<a>', {
class: 'JS-changed',
href: 'wiki/$1',
text: '$1'
} ).prop( 'outerHTML' ) );
} );
} );
}
} );
/** End. */
/**
* Fix tabbers labels achors.
*/
$(function _fixTabAnchors() {
// Wait for the tabbers to load:
mw.loader.using( 'ext.tabber' ).then( function() {
mw.log( 'DEBUG: Tabbers loaded!' );
$( '.tabbernav' ).each( function( i, el ) {
const $el = $( el );
const $parent = $el.parent();
// Gets the tab content, given a tab label:
const getTab = function( $li ) {
return $parent.find( '.tabbertab' ).filter( function() {
return this.title.trim() === $li.find( 'a' ).attr( 'title' ).trim();
});
};
// Remove the default click event:
$el.off( 'click' );
// Create new click event
$el.children().click( function( e ) {
e.preventDefault();
// Current tab:
const $currentLabel = $el.find( '.tabberactive' );
const $currentContent = getTab( $currentLabel );
// New tab:
const $newLabel = $( this );
const $newContent = getTab( $newLabel );
// Switch tabs:
$currentLabel.removeClass( 'tabberactive' );
$newLabel.addClass( 'tabberactive' );
$currentContent.hide();
$newContent.show();
} );
} );
} );
} );
/** End. */
/**
* Styles for the edit textbox after wikitext highlight kicks in.
*/
mw.hook( 'wikipage.editform' ).add( function( $editform ) {
var times = 0;
var interval = window.setInterval(function() {
if ( mw.loader.getState( 'ext.gadget.DotsSyntaxHighlighter' ) === 'ready' ) {
mw.log( '[DotsSyntaxHighlighter styles] - modifing' );
$editform.find( '#wpTextbox0, #wpTextbox1' )
.css( {
'border': 'unset',
'line-height': '1.5em',
'height': 'unset',
} )
;
window.clearInterval( interval );
mw.log( '[DotsSyntaxHighlighter styles] - clearing: Managed to style it.' );
return;
}
if ( times > 10 ) {
window.clearInterval( interval );
mw.log( '[DotsSyntaxHighlighter styles] - clearing: Exceeding 10 times.' );
return;
}
times++;
}, 250 );
} );
/**
* Automatically add alt attributes to thumbnail images using the image's caption.
*/
$('.thumbinner').each(function(){
var attr = $(this).find('img').attr('alt');
if (typeof attr !== typeof undefined && attr !== false) {
var caption = $(this).find('.thumbcaption');
caption.attr('aria-hidden', 'true');
$(this).find('img').attr('alt', caption.text());
}
});
/**
* Hide checkbox to prevent updating double redirects
* automatically after page move.
*/
$( '.mw-special-Movepage #wpFixRedirects' )
.closest( '.oo-ui-layout' ).hide()
;
/** End. */
} )( window, window.jQuery, window.mediaWiki );
/* End of mw.loader.using callback; DO NOT ADD CODE BELOW THIS LINE */
});