MediaWiki:Common.js: Difference between revisions
Content deleted Content added
mNo edit summary |
rm SE script Tag: Manual revert |
||
(199 intermediate revisions by 52 users not shown) | |||
Line 1: | Line 1: | ||
/* Any JavaScript here will be loaded for all users on every page load. */ |
/* Any JavaScript here will be loaded for all users on every page load. */ |
||
/* |
/** |
||
* Collapsible tables |
|||
//Fix bug in http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/skins/common/wikibits.js : |
|||
* |
|||
//for the sorting order, take the whole number, not the number before the second comma. |
|||
* Allows tables to be collapsed, showing only the header. See [[Wikipedia:NavFrame]]. |
|||
//Also, remove spaces. |
|||
* @maintainer [[User:R. Koot]] (on Wikipedia) |
|||
function ts_parseFloat(num) { |
|||
*/ |
|||
if (!num) return 0; |
|||
num = removeSpaces(num); |
|||
var autoCollapse = 2; |
|||
num = parseFloat(num.replace(/,/g, "")); |
|||
var collapseCaption = 'hide'; |
|||
return (isNaN(num) ? 0 : num); |
|||
var expandCaption = 'show'; |
|||
function hasClass( element, className ) { |
|||
var Classes = ( element && element.className && element.className.split( " " ) ) || []; |
|||
for ( var i = 0; i < Classes.length; i++ ) { |
|||
if ( Classes[i] == className ) { |
|||
return true; |
|||
} |
|||
} |
|||
return false; |
|||
} |
} |
||
function |
function collapseTable( tableIndex ) { |
||
var i; |
|||
var tstring = ""; |
|||
var Button = document.getElementById( 'collapseButton' + tableIndex ); |
|||
string = '' + string; |
|||
var Table = document.getElementById( 'collapsibleTable' + tableIndex ); |
|||
splitstring = string.split(" "); |
|||
for(i = 0; i < splitstring.length; i++) |
|||
if ( !Table || !Button ) { |
|||
tstring += splitstring[i]; |
|||
return false; |
|||
} |
|||
var Rows = Table.getElementsByTagName( 'tr' ); |
|||
if ( Button.firstChild.data == collapseCaption ) { |
|||
for ( i = 1; i < Rows.length; i++ ) { |
|||
Rows[i].style.display = 'none'; |
|||
} |
|||
Button.firstChild.data = expandCaption; |
|||
} else { |
|||
for ( i = 1; i < Rows.length; i++ ) { |
|||
Rows[i].style.display = Rows[0].style.display; |
|||
} |
|||
Button.firstChild.data = collapseCaption; |
|||
} |
|||
} |
} |
||
function |
function createCollapseButtons() { |
||
var i; |
|||
// get the span |
|||
var tableIndex = 0; |
|||
var span = lnk.getElementsByTagName('span')[0]; |
|||
var NavigationBoxes = {}; |
|||
var Tables = document.getElementsByTagName( 'table' ); |
|||
for ( i = 0; i < Tables.length; i++ ) { |
|||
var td = lnk.parentNode; |
|||
if ( hasClass( Tables[i], 'collapsible' ) ) { |
|||
var tr = td.parentNode; |
|||
NavigationBoxes[ tableIndex ] = Tables[i]; |
|||
var column = td.cellIndex; |
|||
Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex ); |
|||
var Button = document.createElement( 'span' ); |
|||
var table = tr.parentNode; |
|||
var ButtonLink = document.createElement( 'a' ); |
|||
while (table && !(table.tagName && table.tagName.toLowerCase() == 'table')) |
|||
var ButtonText = document.createTextNode( collapseCaption ); |
|||
table = table.parentNode; |
|||
if (!table) return; |
|||
Button.style.styleFloat = 'right'; |
|||
// Work out a type for the column |
|||
Button.style.cssFloat = 'right'; |
|||
if (table.rows.length <= 1) return; |
|||
Button.style.fontWeight = 'normal'; |
|||
Button.style.textAlign = 'right'; |
|||
Button.style.width = '6em'; |
|||
ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex ); |
|||
// Skip the first row if that's where the headings are |
|||
ButtonLink.setAttribute( 'href', 'javascript:collapseTable(' + tableIndex + ');' ); |
|||
var rowStart = (table.tHead && table.tHead.rows.length > 0 ? 0 : 1); |
|||
ButtonLink.appendChild( ButtonText ); |
|||
Button.appendChild( document.createTextNode( '[' ) ); |
|||
var itm = ""; |
|||
Button.appendChild( ButtonLink ); |
|||
for (var i = rowStart; i < table.rows.length; i++) { |
|||
Button.appendChild( document.createTextNode( ']' ) ); |
|||
if (table.rows[i].cells.length > column) { |
|||
itm = ts_getInnerText(table.rows[i].cells[column]); |
|||
var Header = Tables[i].getElementsByTagName( 'tr' )[0].getElementsByTagName( 'th' )[0]; |
|||
itm = itm.replace(/^[\s\xa0]+/, "").replace(/[\s\xa0]+$/, ""); |
|||
/* only add button and increment count if there is a header row to work with */ |
|||
if (itm != "") break; |
|||
if (Header) { |
|||
Header.insertBefore( Button, Header.childNodes[0] ); |
|||
tableIndex++; |
|||
} |
|||
} |
} |
||
} |
} |
||
for ( i = 0; i < tableIndex; i++ ) { |
|||
sortfn = ts_sort_caseinsensitive; |
|||
if ( hasClass( NavigationBoxes[i], 'collapsed' ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], 'autocollapse' ) ) ) { |
|||
if (itm.match(/^\d\d[\/. -][a-zA-Z]{3}[\/. -]\d\d\d\d$/)) |
|||
collapseTable( i ); |
|||
sortfn = ts_sort_date; |
|||
} |
|||
if (itm.match(/^\d\d[\/.-]\d\d[\/.-]\d\d\d\d$/)) |
|||
} |
|||
sortfn = ts_sort_date; |
|||
} |
|||
if (itm.match(/^\d\d[\/.-]\d\d[\/.-]\d\d$/)) |
|||
sortfn = ts_sort_date; |
|||
if (itm.match(/^[\u00a3$\u20ac\u00a5]/)) // pound dollar euro yen |
|||
sortfn = ts_sort_currency; |
|||
itm = removeSpaces(itm); |
|||
if (itm.match(/^[\d\.\,\-\+]+\%?$/)) sortfn = ts_sort_numeric; |
|||
if (itm.match(/^[\d\.\,\-\+]+[eE][\d\-\+]+\%?$/)) sortfn = ts_sort_numeric; |
|||
if (itm.match(/^[\d\.\,\-\+]+e[\d\-\+]+x10<sup>[\d\-\+]+<\/sup>\%?$/)) sortfn = ts_sort_numeric; |
|||
var reverse = (span.getAttribute("sortdir") == 'down'); |
|||
$( createCollapseButtons ); |
|||
var newRows = new Array(); |
|||
for (var j = rowStart; j < table.rows.length; j++) { |
|||
var row = table.rows[j]; |
|||
var keyText = ts_getInnerText(row.cells[column]); |
|||
var oldIndex = (reverse ? -j : j); |
|||
// Shuffle for election candidates |
|||
newRows[newRows.length] = new Array(row, keyText, oldIndex); |
|||
function dshuf() { |
|||
var shufsets = {}; |
|||
var rx = new RegExp('dshuf' + '\\s+(dshufset\\d+)', 'i'); |
|||
var divs = document.getElementsByTagName('div'); |
|||
var i = divs.length; |
|||
while (i--) { |
|||
if (rx.test(divs[i].className)) { |
|||
if (typeof shufsets[RegExp.$1] === 'undefined') { |
|||
shufsets[RegExp.$1] = {}; |
|||
shufsets[RegExp.$1].inner = []; |
|||
shufsets[RegExp.$1].member = []; |
|||
} |
|||
shufsets[RegExp.$1].inner.push({ |
|||
key: Math.random(), |
|||
html: divs[i].innerHTML |
|||
}); |
|||
shufsets[RegExp.$1].member.push(divs[i]); |
|||
} |
|||
} |
} |
||
for (shufset in shufsets) { |
|||
shufsets[shufset].inner.sort(function(a, b) { |
|||
return a.key - b.key; |
|||
}); |
|||
i = shufsets[shufset].member.length; |
|||
while (i--) { |
|||
shufsets[shufset].member[i].innerHTML = shufsets[shufset].inner[i].html; |
|||
shufsets[shufset].member[i].style.display = 'block'; |
|||
} |
|||
} |
|||
} |
|||
$(dshuf); |
|||
// Import local or interwiki page as script |
|||
newRows.sort(sortfn); |
|||
// |
|||
// Use of custom 'lang' parameter is deprecated. Use mw.loader.load(url) instead. |
|||
var _importScript = window.importScript; |
|||
window.importScript = function importScript(page, lang) { |
|||
if (!lang) { |
|||
return _importScript(page); |
|||
} |
|||
var query = '?title=' + encodeURIComponent(page.replace(' ','_')) + '&action=raw&ctype=text/javascript'; |
|||
mw.loader.load('//' + lang + '.wikipedia.org/w/index.php' + query); |
|||
}; |
|||
( function () { |
|||
var arrowHTML; |
|||
var conf = mw.config.get( [ |
|||
if (reverse) { |
|||
'wgCanonicalSpecialPageName', |
|||
arrowHTML = '<img src="'+ ts_image_path + ts_image_down + '" alt="↓"/>'; |
|||
'wgUserGroups', |
|||
newRows.reverse(); |
|||
'wgPageName', |
|||
span.setAttribute('sortdir','up'); |
|||
'wgUserName' |
|||
} else { |
|||
] ); |
|||
arrowHTML = '<img src="'+ ts_image_path + ts_image_up + '" alt="↑"/>'; |
|||
span.setAttribute('sortdir','down'); |
|||
} |
|||
// Multilingual description.js from commons |
|||
// We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones |
|||
mw.loader.load('//commons.wikimedia.org/w/index.php?title=MediaWiki:Gadget-LanguageSelect.js&action=raw&ctype=text/javascript'); |
|||
// don't do sortbottom rows |
|||
for (var i = 0; i < newRows.length; i++) { |
|||
// Tabs |
|||
if ((" "+newRows[i][0].className+" ").indexOf(" sortbottom ") == -1) |
|||
mw.loader.load('//meta.wikimedia.org/w/index.php?title=MediaWiki:Tabs.js&action=raw&ctype=text/javascript'); |
|||
table.tBodies[0].appendChild(newRows[i][0]); |
|||
}() ); |
|||
} |
|||
// do sortbottom rows only |
|||
/** |
|||
for (var i = 0; i < newRows.length; i++) { |
|||
* Snippets/Load JS and CSS by URL |
|||
if ((" "+newRows[i][0].className+" ").indexOf(" sortbottom ") != -1) |
|||
* @source https://www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL |
|||
table.tBodies[0].appendChild(newRows[i][0]); |
|||
* @revision 5022668 dated 2022-01-17 |
|||
*/ |
|||
mw.loader.using( ['mediawiki.util'], function () { |
|||
var extraCSS = mw.util.getParamValue( 'withCSS' ), |
|||
extraJS = mw.util.getParamValue( 'withJS' ), |
|||
extraModule = mw.util.getParamValue( 'withModule' ); |
|||
if ( extraCSS ) { |
|||
// WARNING: DO NOT REMOVE THIS "IF" - REQUIRED FOR SECURITY (against XSS/CSRF attacks) |
|||
if ( /^MediaWiki:[^&<>=%#]*\.css$/.test( extraCSS ) ) { |
|||
mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraCSS ) + '&action=raw&ctype=text/css', 'text/css' ); |
|||
} else { |
|||
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } ); |
|||
} |
|||
} |
} |
||
if ( extraJS ) { |
|||
// Delete any other arrows there may be showing |
|||
// WARNING: DO NOT REMOVE THIS "IF" - REQUIRED FOR SECURITY (against XSS/CSRF attacks) |
|||
var spans = getElementsByClassName(tr, "span", "sortarrow"); |
|||
if ( /^MediaWiki:[^&<>=%#]*\.js$/.test( extraJS ) ) { |
|||
for (var i = 0; i < spans.length; i++) { |
|||
mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraJS ) + '&action=raw&ctype=text/javascript' ); |
|||
spans[i].innerHTML = '<img src="'+ ts_image_path + ts_image_none + '" alt="↓"/>'; |
|||
} else { |
|||
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } ); |
|||
} |
|||
} |
} |
||
span.innerHTML = arrowHTML; |
|||
if ( extraModule ) { |
|||
ts_alternate(table); |
|||
if ( /^ext\.gadget\.[^,\|]+$/.test( extraModule ) ) { |
|||
} |
|||
mw.loader.load( extraModule ); |
|||
} else { |
|||
/*</pre>*/ |
|||
mw.notify( 'Only gadget modules are allowed.', { title: 'Invalid withModule value' } ); |
|||
} |
|||
} |
|||
}); |
Latest revision as of 17:19, 27 February 2024
/* Any JavaScript here will be loaded for all users on every page load. */
/**
* Collapsible tables
*
* Allows tables to be collapsed, showing only the header. See [[Wikipedia:NavFrame]].
* @maintainer [[User:R. Koot]] (on Wikipedia)
*/
var autoCollapse = 2;
var collapseCaption = 'hide';
var expandCaption = 'show';
function hasClass( element, className ) {
var Classes = ( element && element.className && element.className.split( " " ) ) || [];
for ( var i = 0; i < Classes.length; i++ ) {
if ( Classes[i] == className ) {
return true;
}
}
return false;
}
function collapseTable( tableIndex ) {
var i;
var Button = document.getElementById( 'collapseButton' + tableIndex );
var Table = document.getElementById( 'collapsibleTable' + tableIndex );
if ( !Table || !Button ) {
return false;
}
var Rows = Table.getElementsByTagName( 'tr' );
if ( Button.firstChild.data == collapseCaption ) {
for ( i = 1; i < Rows.length; i++ ) {
Rows[i].style.display = 'none';
}
Button.firstChild.data = expandCaption;
} else {
for ( i = 1; i < Rows.length; i++ ) {
Rows[i].style.display = Rows[0].style.display;
}
Button.firstChild.data = collapseCaption;
}
}
function createCollapseButtons() {
var i;
var tableIndex = 0;
var NavigationBoxes = {};
var Tables = document.getElementsByTagName( 'table' );
for ( i = 0; i < Tables.length; i++ ) {
if ( hasClass( Tables[i], 'collapsible' ) ) {
NavigationBoxes[ tableIndex ] = Tables[i];
Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex );
var Button = document.createElement( 'span' );
var ButtonLink = document.createElement( 'a' );
var ButtonText = document.createTextNode( collapseCaption );
Button.style.styleFloat = 'right';
Button.style.cssFloat = 'right';
Button.style.fontWeight = 'normal';
Button.style.textAlign = 'right';
Button.style.width = '6em';
ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
ButtonLink.setAttribute( 'href', 'javascript:collapseTable(' + tableIndex + ');' );
ButtonLink.appendChild( ButtonText );
Button.appendChild( document.createTextNode( '[' ) );
Button.appendChild( ButtonLink );
Button.appendChild( document.createTextNode( ']' ) );
var Header = Tables[i].getElementsByTagName( 'tr' )[0].getElementsByTagName( 'th' )[0];
/* only add button and increment count if there is a header row to work with */
if (Header) {
Header.insertBefore( Button, Header.childNodes[0] );
tableIndex++;
}
}
}
for ( i = 0; i < tableIndex; i++ ) {
if ( hasClass( NavigationBoxes[i], 'collapsed' ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], 'autocollapse' ) ) ) {
collapseTable( i );
}
}
}
$( createCollapseButtons );
// Shuffle for election candidates
function dshuf() {
var shufsets = {};
var rx = new RegExp('dshuf' + '\\s+(dshufset\\d+)', 'i');
var divs = document.getElementsByTagName('div');
var i = divs.length;
while (i--) {
if (rx.test(divs[i].className)) {
if (typeof shufsets[RegExp.$1] === 'undefined') {
shufsets[RegExp.$1] = {};
shufsets[RegExp.$1].inner = [];
shufsets[RegExp.$1].member = [];
}
shufsets[RegExp.$1].inner.push({
key: Math.random(),
html: divs[i].innerHTML
});
shufsets[RegExp.$1].member.push(divs[i]);
}
}
for (shufset in shufsets) {
shufsets[shufset].inner.sort(function(a, b) {
return a.key - b.key;
});
i = shufsets[shufset].member.length;
while (i--) {
shufsets[shufset].member[i].innerHTML = shufsets[shufset].inner[i].html;
shufsets[shufset].member[i].style.display = 'block';
}
}
}
$(dshuf);
// Import local or interwiki page as script
//
// Use of custom 'lang' parameter is deprecated. Use mw.loader.load(url) instead.
var _importScript = window.importScript;
window.importScript = function importScript(page, lang) {
if (!lang) {
return _importScript(page);
}
var query = '?title=' + encodeURIComponent(page.replace(' ','_')) + '&action=raw&ctype=text/javascript';
mw.loader.load('//' + lang + '.wikipedia.org/w/index.php' + query);
};
( function () {
var conf = mw.config.get( [
'wgCanonicalSpecialPageName',
'wgUserGroups',
'wgPageName',
'wgUserName'
] );
// Multilingual description.js from commons
mw.loader.load('//commons.wikimedia.org/w/index.php?title=MediaWiki:Gadget-LanguageSelect.js&action=raw&ctype=text/javascript');
// Tabs
mw.loader.load('//meta.wikimedia.org/w/index.php?title=MediaWiki:Tabs.js&action=raw&ctype=text/javascript');
}() );
/**
* Snippets/Load JS and CSS by URL
* @source https://www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
* @revision 5022668 dated 2022-01-17
*/
mw.loader.using( ['mediawiki.util'], function () {
var extraCSS = mw.util.getParamValue( 'withCSS' ),
extraJS = mw.util.getParamValue( 'withJS' ),
extraModule = mw.util.getParamValue( 'withModule' );
if ( extraCSS ) {
// WARNING: DO NOT REMOVE THIS "IF" - REQUIRED FOR SECURITY (against XSS/CSRF attacks)
if ( /^MediaWiki:[^&<>=%#]*\.css$/.test( extraCSS ) ) {
mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraCSS ) + '&action=raw&ctype=text/css', 'text/css' );
} else {
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
}
}
if ( extraJS ) {
// WARNING: DO NOT REMOVE THIS "IF" - REQUIRED FOR SECURITY (against XSS/CSRF attacks)
if ( /^MediaWiki:[^&<>=%#]*\.js$/.test( extraJS ) ) {
mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraJS ) + '&action=raw&ctype=text/javascript' );
} else {
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
}
}
if ( extraModule ) {
if ( /^ext\.gadget\.[^,\|]+$/.test( extraModule ) ) {
mw.loader.load( extraModule );
} else {
mw.notify( 'Only gadget modules are allowed.', { title: 'Invalid withModule value' } );
}
}
});