MediaWiki:Common.js

// Start overly-complicated collapsible tables // Todo: jQuery this thing /** Collapsible tables ********************************************************* * *  Description: Allows tables to be collapsed, showing only the header. See *              NavFrame. * Maintainers: User:R. Koot */ var hasClass = (function {    var reCache = {};    return function (element, className) {        return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);   }; });

var autoCollapse = 2; var collapseCaption = "hide"; var expandCaption = "show"; function collapseTable( tableIndex ) {    var Button = document.getElementById( "collapseButton" + tableIndex ); var Table = document.getElementById( "collapsibleTable" + tableIndex ); if ( !Table || !Button ) { return false; }    var Rows = Table.rows; if ( Button.firstChild.data == collapseCaption ) { for ( var i = 1; i < Rows.length; i++ ) { Rows[i].style.display = "none"; }        Button.firstChild.data = expandCaption; } else { for ( var i = 1; i < Rows.length; i++ ) { Rows[i].style.display = Rows[0].style.display; }        Button.firstChild.data = collapseCaption; } } function createCollapseButtons {    var tableIndex = 0; var NavigationBoxes = new Object; var Tables = document.getElementsByTagName( "table" ); for ( var i = 0; i < Tables.length; i++ ) { if ( hasClass( Tables[i], "collapsible" ) ) { /* only add button and increment count if there is a header row to work with */ var HeaderRow = Tables[i].getElementsByTagName( "tr" )[0]; if (!HeaderRow) continue; var Header = HeaderRow.getElementsByTagName( "th" )[0]; if (!Header) continue; 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.style.color = Header.style.color; 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( "]" ) ); Header.insertBefore( Button, Header.childNodes[0] ); tableIndex++; }    }     for ( var i = 0;  i < tableIndex; i++ ) { if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) { collapseTable( i ); }    } } $(createCollapseButtons);

// End collapsible tables

// Mouseover trick for multi-image item infoboxes $("#itemsmallimages img").mouseover(function {          $("#itemmainimage img:first").attr('src', $(this).attr('src'));       }   );

// Fancy diffs var fancyDiffs = { isBigDiff: false, isBigDiffThreshold: 72, toggle: function(element) { var expanded = element.hasClass('diff-expanded'); var contents = element.parent.children('.diff-contents'); if(expanded) { // Just collapse then element.removeClass('diff-expanded'); if(fancyDiffs.isBigDiff) { contents.hide; } else { contents.slideUp('fast'); }		} else if(element.hasClass('diff-data-loaded')) { // Stuff is already loaded, expand element.addClass('diff-expanded'); contents.slideDown('fast'); } else if(!element.hasClass('diff-data-requested')) { // Stuff is not loaded element.addClass('diff-data-requested'); var fileName = element.find('span').text.replace(/^\s+|\s+$/g); var patchName = element.closest('.diffname'); var diffName = wgPageName; if(patchName && patchName.length && patchName.attr('class')) { diffName = patchName.attr('class').substr(9); }			$.get('/index.php?title=Template:PatchDiff/' + encodeURIComponent(diffName.replace(/^Template:PatchDiff\//, '')) + '/' + encodeURIComponent(fileName) + '&action=raw', function(data) {				contents.html(data);				if(fancyDiffs.isBigDiff) {					contents.show;				} else {					contents.slideDown('fast');				}				element.removeClass('diff-data-requested').addClass('diff-data-loaded').addClass('diff-expanded');			}); }	},	init: function { var diffText = $('.diff-name-text'); if(diffText.length) { // Preload leetle gif $('body').append($(' ').attr('src', 'http://hydra-images.cursecdn.com/dota2.gamepedia.com/4/43/Patch_diff_loading.gif').css('display', 'none')); diffText.find('span').each(function {				$(this).text($(this).find('a').text.replace(/^\s+|\s+$/g));			}); diffText.click(function {				fancyDiffs.toggle($(this));				return false;			}); fancyDiffs.isBigDiff = $('.diff-file').length > fancyDiffs.isBigDiffThreshold; }	} }; $(fancyDiffs.init);

// End fancy diffs

// Wiki-logo correct lang links var languages = ["ar", "cs", "da", "de", "el", "es", "fi", "fr", "hu", "it", "ja", "ko", "nl", "no", "pl", "pt", "pt-br", "ro", "ru", "sv", "th", "tr", "vi", "zh-hans", "zh-hant"];

var lang = /\/([a-z-]+)\/?$/.exec(location.href); if (lang) { lang = lang[1] if (languages.indexOf(lang) != -1) { var logolink = $("#p-logo a"); logolink.attr("href", logolink.attr("href") + "/" + lang); } } // End Wiki-logo correct lang links

// EmbedVideo fixes var youtubeHelper = { chromeSize: 25, // This is the height (in pixels) of the chrome of YouTube's embedded video player. Update this whenever they release a new embedded video player maxWidth: 0.85, // Maximum fraction of the available width that the video may take infoboxes: ['.infobox', '.testchamber'], // Selectors of infobox-style boxes that should be deducted from the page's available width ratioR: /ratio-(\d+)x(\d+)/i, widthsR: /widths((?:\D+\d+)+)/i, setSize:function { var widths = youtubeHelper.widthsR.exec($(this).attr('class')); if(widths != null) { widths = widths[1].substr(1).split(/\D+/g); var availableWidth = $('#bodyContent').width; for(var i in youtubeHelper.infoboxes) { if($(youtubeHelper.infoboxes[i]).length) { availableWidth -= $(youtubeHelper.infoboxes[i]).width; }			}			availableWidth *= youtubeHelper.maxWidth; var intWidths = []; for(var w = 0; w < widths.length; w++) { intWidths[w] = parseInt(widths[w]); }			intWidths.sort(function(a, b){return b - a;}); for(var w = 0; w < intWidths.length; w++) { if(intWidths[w] <= availableWidth || w == intWidths.length-1) { youtubeHelper.setWidth(this, intWidths[w]); break; }			}		}		else { youtubeHelper.setWidth(this, parseFloat(obj.attr('width'))); }	},	setUrl:function { var obj = $(this).children('object'); if(!obj) return; obj.append($(' ')); obj.append($(' ')); var titleParts = wgPageName.split(/\//g); var lang = 'en'; if(titleParts.length == 2 && !wgCanonicalSpecialPageName) { lang = titleParts[titleParts.length-1]; }		var playerUrl = obj.children('param[name="movie"]').attr('value') + '&fs=1&theme=dark&color=white' + ($(this).hasClass('hd-on') ? '&hd=1' : '') + '&cc_load_policy=1&hl=' + lang + '&cc_lang_pref=' + lang; obj.children('param[name="movie"]').attr('value', playerUrl); obj.children('embed').attr('src', playerUrl).attr('allowscriptaccess', 'always').attr('allowfullscreen', 'true'); var resultHtml = $(this).html; $(this).html('').html(resultHtml); },	setWidth:function(youtube, width) { var obj = $(youtube).children('object'); if(!obj) return; if($(youtube).hasClass('youtube-audio')) { obj.attr('width', width).attr('height', youtubeHelper.chromeSize); // Set height obj.children('embed').attr('width', width).attr('height', youtubeHelper.chromeSize); // Set height }		else { var ratio = youtubeHelper.ratioR.exec($(youtube).attr('class')); if(ratio != null) { ratio = parseFloat(ratio[1])/parseFloat(ratio[2]); var newHeight = Math.round(width / ratio + youtubeHelper.chromeSize).toString; obj.attr('width', width).attr('height', newHeight); // Set height obj.children('embed').attr('width', width).attr('height', newHeight); // Set height }		}	},	resizeTimer:null, resize:function { if(youtubeHelper.resizeTimer != null) { clearTimeout(youtubeHelper.resizeTimer); }		youtubeHelper.resizeTimer = setTimeout(youtubeHelper.onResize, 100); },	onResize:function { $('.youtubebox').each(youtubeHelper.setSize); },	init:function { $('.youtubebox').each(youtubeHelper.setUrl); $(window).resize(youtubeHelper.resize); youtubeHelper.onResize; } }; $(youtubeHelper.init); // END EmbedVideo fixes