MediaWiki:Common.js: Difference between revisions
MediaWiki interface page
More actions
No I'm not making fun of their capitalization |
m Fix wgPageName reference error, remove flattrButton |
||
| (80 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
/ | /* jQuery plugins */ | ||
/** | |||
* | * Cookie plugin | ||
* Copyright | * Copyright (c) 2006 Klaus Hartl (stilbuero.de) | ||
* Dual licensed under the MIT | * Dual licensed under the MIT and GPL licenses: | ||
* | * https://opensource.org/licenses/mit-license.php | ||
* https://www.gnu.org/licenses/gpl.html | |||
* | |||
*/ | */ | ||
jQuery.cookie=function(name | |||
/*BEGIN | /* BEGIN SPOILER TOGGLER */ | ||
var spoilers = { | |||
enabled: true, | |||
text: null, | |||
nodes: null, | |||
imgOn: null, | |||
imgOff: null, | |||
toggle: function() { | |||
spoilers.set(!spoilers.enabled); | |||
}, | |||
set: function(enabled) { | |||
spoilers.enabled = enabled; | |||
if(enabled) { | |||
spoilers.nodes.removeClass('spoiler-inline'); | |||
$(".spoiler-thumb").remove(); | |||
spoilers.nodes.find("img").show(); | |||
$.cookie('spoilers', 'true', {expires: 31, path: '/'}); | |||
spoilers.text.text(' Spoilers on'); | |||
spoilers.imgOff.hide(0); | |||
spoilers.imgOn.show(0); | |||
} else { | |||
spoilers.nodes.addClass('spoiler-inline'); | |||
spoilers.nodes.find("img").hide(); | |||
spoilers.thumbNodes.each(function() { if($(this).find(".spoiler-text").length > 0) { $(this).find(".thumb, .thumbinner").append('<div class="spoiler-thumb"/>');} }); | |||
$.cookie('spoilers', 'false', {expires: 31, path: '/'}); | |||
spoilers.text.text(' Spoilers off'); | |||
spoilers.imgOn.hide(0); | |||
spoilers.imgOff.show(0); | |||
} | |||
}, | |||
init: function() { | |||
spoilers.nodes = $('.spoiler-text'); | |||
spoilers.thumbNodes = $(".gallerybox, .thumb"); | |||
spoilers.text = $('<div style="display:inline;"></div>'); | |||
spoilers.imgOn = $('<img alt="" style="display:none;"/>').attr('src', 'https://i2.theportalwiki.net/img/2/2b/Wheatley_spoils_you.png'); | |||
spoilers.imgOff = $('<img alt="" style="display:none;"/>').attr('src', 'https://i2.theportalwiki.net/img/e/ef/Wheatley_spoils_nothing.png'); | |||
var wrapped = $('<li/>').append($('<span/>').append($('<a/>').append(spoilers.imgOn, spoilers.imgOff, spoilers.text))); | |||
wrapped.click(spoilers.toggle); | |||
$('#p-namespaces ul').append(wrapped); | |||
spoilers.set(!$.cookie('spoilers') || $.cookie('spoilers') == 'true'); | |||
} | |||
}; | |||
$(spoilers.init); | |||
/* END SPOILER TOGGLER */ | |||
/* | // 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.length) return; | |||
obj.append($('<param name="allowscriptaccess" value="true"></param>')); | |||
obj.append($('<param name="allowfullscreen" value="true"></param>')); | |||
var titleParts = mw.config.get('wgPageName').split(/\//g); | |||
var lang = 'en'; | |||
if(titleParts.length == 2 && !mw.config.get('wgCanonicalSpecialPageName')) { | |||
lang = titleParts[titleParts.length-1]; | |||
} | |||
var playerUrl = obj.children('param[name="movie"]').attr('value') + '&version=2&fs=1&theme=dark&color=white' + ($(this).hasClass('hd-on') ? '&hd=1' : '') + '&cc_load_policy=1&modestbranding=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 <object> height | |||
obj.children('embed').attr('width', width).attr('height', youtubeHelper.chromeSize); // Set <embed> 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 <object> height | |||
obj.children('embed').attr('width', width).attr('height', newHeight); // Set <embed> 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); | |||
/**** function | // Start overly-complicated collapsible tables | ||
* | // Todo: jQuery this thing | ||
/** Collapsible tables ********************************************************* | |||
* | |||
* Description: Allows tables to be collapsed, showing only the header. See | |||
* [[Wikipedia: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 | |||
/* | |||
* Add language-dependent class to <body> | |||
* Adds .lang-something to the body, where "something" is the language of the page. | |||
*/ | */ | ||
function | function langClass() { | ||
{ | // Supported list of languages (not including the default one): | ||
var langList = ['ar', 'cs', 'da', 'de', 'es', 'fi', 'fr', 'hu', 'it', 'ja', 'ko', 'nl', 'no', 'pl', 'pt', 'pt-br', 'ro', 'ru', 'sv', 'zh-hans', 'zh-hant']; | |||
// Assumed language if the page is in none of the languages above: | |||
var defaultLang = 'en'; | |||
try { | |||
var body = document.getElementsByTagName('body')[0]; | |||
} catch(e) { | |||
return; | return; | ||
} | } | ||
var lang = defaultLang; | |||
for(var i in langList) { | |||
if(mw.config.get('wgPageName').substr(mw.config.get('wgPageName').length - 1 - langList[i].length).toLowerCase() == '/' + langList[i].toLowerCase()) { | |||
lang = langList[i]; | |||
break; | |||
} | |||
} | } | ||
if(body.getAttribute('class')) { | |||
body.setAttribute('class', body.getAttribute('class') + ' lang-' + lang); | |||
if ( | } else { | ||
body.setAttribute('class', 'lang-' + lang); | |||
} | |||
} | } | ||
} | } | ||
addOnloadHook(langClass); | |||
// PootTabs by User:WindPower~ | |||
// It puts tabs on pages. | |||
var pootTabsHere = { | |||
animationsEnabled: $.support.opacity, | |||
getTab:function(poot, index) { | |||
return $(poot.children('.poot-tabs').children('ul').children('li')[parseInt(index)]); | |||
}, | |||
changeTab:function(poot, index, duration, force) { | |||
if(index == parseInt(poot.attr('pootSelected')) && !force && duration) return; | |||
if(!pootTabsHere.animationsEnabled) { | |||
duration = 0; | |||
} | |||
poot.attr('pootSelected', index.toString()); | |||
var babies = poot.children('.poot-tabs-content').children(); | |||
babies.each(function() { | |||
$(this).fadeOut(duration, function(){ | |||
$(this).removeClass('poot-tabs-selected'); | |||
}); | |||
}); | |||
$(babies[index]).each(function() { | |||
$(this).fadeIn(duration, function(){ | |||
$(this).addClass('poot-tabs-selected'); | |||
}); | |||
}); | |||
var cowtabs = poot.children('.poot-tabs').children('ul').children('li'); | |||
cowtabs.removeClass('poot-tabs-selected'); | |||
$(cowtabs[index]).addClass('poot-tabs-selected'); | |||
pootTabsHere.updatePoot(poot, $(babies[index]).height()); | |||
}, | |||
updatePoot:function(poot, babysize) { | |||
poot.find('.poot-tabs-titletext').html(poot.attr('originalTitle') + ' — ' + pootTabsHere.getTab(poot, poot.attr('pootSelected')).html()); | |||
var bestHeight = Math.max(poot.children('.poot-tabs-content').height(), Math.max(poot.children('.poot-tabs').height(), babysize)).toString() + 'px'; | |||
poot.children('.poot-tabs-content').css('height', bestHeight); | |||
if(poot.attr('vertical')) { | |||
poot.children('.poot-tabs').css('height', bestHeight); | |||
} | |||
}, | |||
toggleCollapse:function(poot) { | |||
var pootLinkText = poot.children('.poot-tabs-showhide').text().split(';'); | |||
var duration = pootTabsHere.animationsEnabled ? parseInt(poot.attr('pootslideduration')) : 0; | |||
if(poot.attr('pootcollapse') != 'true') { | |||
poot.attr('pootcollapse', 'true'); | |||
poot.find('.poot-tabs-hidelink a').text(pootLinkText[0]); | |||
poot.children('.poot-tabs, .poot-tabs-content').slideUp(duration); | |||
} | |||
else { | |||
poot.attr('pootcollapse', ''); | |||
poot.find('.poot-tabs-hidelink a').text(pootLinkText[1]); | |||
poot.children('.poot-tabs, .poot-tabs-content').slideDown(duration); | |||
} | |||
}, | |||
delayHeight:function(poot, selected) { | |||
setTimeout(function() { | |||
poot.attr('pootselected', selected.toString()); | |||
pootTabsHere.changeTab(poot, selected, 0, true); | |||
if(poot.hasClass('poot-tabs-collapsed')) { | |||
pootTabsHere.toggleCollapse(poot); | |||
} | |||
}, 100); | |||
}, | |||
poot:function() { | |||
var dis = $(this); | |||
var ind = 0; | |||
dis.attr('originalTitle', dis.find('.poot-tabs-titletext').html()); | |||
var selected = /poot-tabs-selected-(\d+)/i.exec(dis.attr('class')); | |||
if(selected) { | |||
pootTabsHere.delayHeight(dis, parseInt(selected[1])-1); | |||
} | |||
else { | |||
pootTabsHere.delayHeight(dis, 0); | |||
} | |||
var duration = dis.hasClass('poot-tabs-noanimations') ? 0 : 200; | |||
dis.attr('pootslideduration', dis.hasClass('poot-tabs-noanimations') ? '0' : '75'); | |||
dis.children('.poot-tabs').children('ul').children('li').each(function(){ | |||
var thisInd = ind; | |||
$(this).click(function(){ | |||
pootTabsHere.changeTab(dis, thisInd, duration, false); | |||
$(this).blur(); | |||
$(this).find('*').blur(); | |||
return false; | |||
}); | |||
ind++; | |||
}); | |||
var isVertical = dis.hasClass('poot-tabs-vertical'); | |||
dis.attr('pootvertical', isVertical ? 'true' : ''); | |||
if(isVertical) { | |||
var teenie = dis.children('.poot-tabs').width().toString() + 'px'; | |||
dis.children('.poot-tabs-content').css('margin-left', teenie); | |||
} | |||
dis.attr('pootcollapse', ''); // False | |||
dis.find('.poot-tabs-hidelink a').click(function(){ | |||
pootTabsHere.toggleCollapse(dis); | |||
return false; | |||
}); | |||
}, | |||
init:function() { | |||
$('.poot-tabs-container').each(pootTabsHere.poot); | |||
} | |||
}; | |||
$(pootTabsHere.init); | |||
// End of PootTabs | |||
// Start test chamber infobox flicker | |||
infoboxFlicker = { | |||
setOn:function(infobox, isOn) { | |||
if(isOn) { | |||
infobox.removeClass('testchamber-off'); | |||
} else { | |||
infobox.addClass('testchamber-off'); | |||
} | |||
}, | |||
setDisplay1:function(infobox, isOn) { | |||
if(isOn) { | |||
infobox.removeClass('testchamber-display1-off'); | |||
} else { | |||
infobox.addClass('testchamber-display1-off'); | |||
} | |||
}, | |||
setDisplay2:function(infobox, isOn) { | |||
if(isOn) { | |||
infobox.removeClass('testchamber-display2-off'); | |||
} else { | |||
infobox.addClass('testchamber-display2-off'); | |||
} | |||
}, | |||
flicker:function() { // Step 0: Start off | |||
var infobox = $(this); | |||
infoboxFlicker.setOn(infobox, false); | |||
infoboxFlicker.setDisplay1(infobox, false); | |||
infoboxFlicker.setDisplay2(infobox, false); | |||
setTimeout(function() { // Step 1: Switch on, no details | |||
infoboxFlicker.setOn(infobox, true); | |||
setTimeout(function() { // Step 2: Switch back off | |||
infoboxFlicker.setOn(infobox, false); | |||
setTimeout(function() { // Step 3: Switch back on, still no details | |||
infoboxFlicker.setOn(infobox, true); | |||
setTimeout(function() { // Step 4: Display top half | |||
infoboxFlicker.setDisplay1(infobox, true); | |||
setTimeout(function() { // Step 5: Display bottom half | |||
infoboxFlicker.setDisplay2(infobox, true); | |||
}, 200); | |||
}, 200); | |||
}, 340); | |||
}, 66); | |||
}, 1000); | |||
}, | |||
init:function() { | |||
$('.testchamber').each(infoboxFlicker.flicker); | |||
} | |||
}; | |||
$(infoboxFlicker.init); | |||
// End test chamber infobox flicker | |||
// Start OldAperture infobox hover code | |||
oldApertureInfobox = { | |||
mouseIn: function() { | |||
$(this).children('.old-aperture-active').css('display', 'block'); | |||
$(this).children('.old-aperture-inactive').css('display', 'none'); | |||
}, | |||
mouseOut: function() { | |||
if(!$(this).hasClass('old-aperture-location-active')) { | |||
$(this).children('.old-aperture-active').css('display', 'none'); | |||
$(this).children('.old-aperture-inactive').css('display', 'block'); | |||
} | |||
}, | |||
init: function() { | |||
$('.old-aperture-location').hover(oldApertureInfobox.mouseIn, oldApertureInfobox.mouseOut); | |||
} | |||
}; | |||
$(oldApertureInfobox.init); | |||
// End OldAperture infobox hover code | |||
// 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 = mw.config.get('wgPageName'); | |||
if(patchName && patchName.length && patchName.attr('class')) { | |||
diffName = patchName.attr('class').substr(9); | |||
} | |||
$.get('/w/?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($('<img/>').attr('src', 'https://i2.theportalwiki.net/img/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); | |||
// Lightbox stuff | |||
var lightboxStuff = { | |||
settings: { | |||
imageLoading: 'https://i2.theportalwiki.net/img/5/56/Lightbox_loading.gif', | |||
imageBtnClose: 'https://i2.theportalwiki.net/img/e/e2/Lightbox_button_close.gif', | |||
imageBtnPrev: 'https://i2.theportalwiki.net/img/c/c8/Lightbox_button_previous.gif', | |||
imageBtnNext: 'https://i2.theportalwiki.net/img/9/9e/Lightbox_button_next.gif', | |||
imageBlank: 'https://i2.theportalwiki.net/img/f/f6/Lightbox_blank.gif', | |||
}, | |||
init: function() { | |||
var galleryIndex = 0; | |||
var galleryBuckets = {}; | |||
$('.pwiki-gallery').each(function() { | |||
var gID = 'pwiki-gallery-' + galleryIndex.toString(); | |||
$(this).attr('id', gID); | |||
galleryBuckets[gID] = null; | |||
galleryIndex++; | |||
}); | |||
$('.lightboxed').each(function() { | |||
var gallery = $(this).closest('.pwiki-gallery'); | |||
if(gallery.length) { | |||
// In gallery | |||
$(this).attr('data-title', '<p>' + $(this).attr('title') + '</p>' + $(this).attr('data-title')); | |||
var gID = gallery.attr('id'); | |||
if(galleryBuckets[gID] == null) { | |||
galleryBuckets[gID] = $(this); | |||
} else { | |||
galleryBuckets[gID] = galleryBuckets[gID].add($(this)); | |||
} | |||
} else { | |||
// Not in gallery (separate Lightbox) | |||
var thumb = $(this).closest('.thumbinner'); | |||
if(thumb.length) { | |||
var caption = thumb.children('.thumbcaption'); | |||
if(caption.length) { | |||
$(this).attr('data-title', '<p>' + caption.html().replace(/<div[^<>]*class="magnify"[^<>]*>[\s\S]*<\/div>/i, '').replace(/^\s+|\s+$/g, '') + '</p>' + $(this).attr('data-title')); | |||
} | |||
} | |||
$(this).lightBox(lightboxStuff.settings); | |||
} | |||
}); | |||
for(var gID in galleryBuckets) { | |||
galleryBuckets[gID].lightBox(lightboxStuff.settings); | |||
} | |||
} | |||
}; | |||
$(lightboxStuff.init); | |||
// Audio player thing | |||
var audioPlayer = { | |||
idCount: 0, | |||
init: function() { | |||
$('.audio-player').each(function() { | |||
var theDiv = $(this); | |||
var newID = 'audio-id-' + audioPlayer.idCount.toString(); | |||
audioPlayer.idCount++; | |||
theDiv.attr('id', newID); | |||
theDiv.children('a').click(function() { | |||
theDiv.html(''); | |||
jwplayer(newID).setup({ | |||
flashplayer: '/w/extensions/MediawikiPlayer/player.swf', | |||
height: '24', | |||
controlbar: 'bottom', | |||
width: '400', | |||
allowfullscreen: 'true', | |||
backcolor: 'ffffff', | |||
file: theDiv.attr('data-url'), | |||
autostart: true | |||
}); | |||
return false; | |||
}); | |||
}); | |||
} | |||
}; | |||
$(audioPlayer.init); | |||
// Page-specific JavaScript/CSS | |||
var pageScripts = { | |||
pagesJS: [], | |||
pagesCSS: ['User:MogDog66', 'Portal_Wiki:Donate', 'User:Headquarter8302'], | |||
suffixJS: '/Page.js', | |||
suffixCSS: '/Page.css', | |||
init: function() { | |||
for(var i in pageScripts.pagesJS) { | |||
if(mw.config.get('wgPageName') == pageScripts.pagesJS[i]) { | |||
mw.loader.load(mw.config.get('wgScript') + '?title=' + encodeURIComponent(mw.config.get('wgPageName') + pageScripts.suffixJS) + '&ctype=text/javascript&action=raw'); | |||
} | |||
} | |||
for(var i in pageScripts.pagesCSS) { | |||
if(mw.config.get('wgPageName') == pageScripts.pagesCSS[i]) { | |||
mw.loader.load(mw.config.get('wgScript') + '?title=' + encodeURIComponent(mw.config.get('wgPageName') + pageScripts.suffixCSS) + '&ctype=text/css&action=raw', 'text/css'); | |||
} | |||
} | |||
} | |||
}; | |||
$(pageScripts.init); | |||
// 3D model viewer | |||
var viewer3d = { | |||
dragging: null, | |||
draggingFrameX: 0, | |||
draggingFrameY: 0, | |||
} | viewers: [], | ||
frameThresholdX: 10, | |||
frameThresholdY: 128, | |||
realMod: function(x, y) { | |||
return ((x % y) + y) % y; | |||
}, | |||
init: function() { | |||
$('.viewer-3d').each(viewer3d.bind); | |||
$(document).mouseup(viewer3d.release); | |||
$(document).mousemove(viewer3d.move); | |||
}, | |||
bind: function() { | |||
var v = $(this); | |||
var num = viewer3d.viewers.length; | |||
var allModels = []; | |||
var modelID = 0; | |||
var viewerSize = 0; | |||
while(true) { | |||
var modelMap = v.find('.viewer-3d-map-' + modelID); | |||
var urlNode = v.find('.viewer-3d-url-' + modelID); | |||
if(!modelMap.length || !urlNode.length) break; | |||
var url = $('<div/>').html(urlNode.text()).text(); | |||
var framesS = $('<div/>').html(modelMap.text()).text().replace(/^\s+|\s+$/g).split(/,/g); | |||
var frameMap = []; | |||
var heightMap = []; | |||
var leftCropMap = []; | |||
var totalW = parseInt(framesS[0]); | |||
var maxFrameW = parseInt(framesS[1]); | |||
var totalH = parseInt(framesS[2]); | |||
var verticalSteps = parseInt(framesS[3]); | |||
var midVertical = Math.floor(verticalSteps / 2); | |||
for(var f = 4; f < framesS.length; f += 3) { | |||
frameMap.push(parseInt(framesS[f])); | |||
heightMap.push(parseInt(framesS[f + 1])); | |||
leftCropMap.push(parseInt(framesS[f + 2])); | |||
} | |||
allModels.push({ | |||
imageURL: url, | |||
map: frameMap, | |||
cropMap: leftCropMap, | |||
totalWidth: totalW, | |||
totalHeight: totalH, | |||
maxFrameWidth: maxFrameW, | |||
xStep: verticalSteps | |||
}); | |||
viewerSize = Math.max(viewerSize, totalH, maxFrameW); | |||
modelID++; | |||
} | |||
if(!modelID) return; | |||
var overlayNode = $('<div class="viewer-3d-overlay"></div>'); | |||
var frameN = v.find('.viewer-3d-frame'); | |||
v.find('img').detach(); | |||
var klasses = v.attr('class').split(/ /g); | |||
var startFrame = 0; | |||
for(var k in klasses) { | |||
if(klasses[k].substr(0, 11) == 'startframe-') { | |||
startFrame = Math.max(0, parseInt(klasses[k].substr(11))); | |||
} | |||
} | |||
var viewer = { | |||
node: v, | |||
frameX: startFrame, | |||
frameY: midVertical, | |||
models: allModels, | |||
currentModel: -1, | |||
frameNode: frameN, | |||
width: viewerSize, | |||
height: viewerSize, | |||
mouseX: 0, | |||
mouseY: 0, | |||
overlay: overlayNode | |||
}; | |||
viewer3d.viewers.push(viewer); | |||
v.hover(viewer3d.hover, viewer3d.unhover).mousedown(viewer3d.drag).append(overlayNode).attr('data-id', num).css({ | |||
width: viewer.width + 'px', | |||
height: viewer.height + 'px' | |||
}); | |||
frameN.mousedown(viewer3d.drag).attr('data-id', num).css('height', viewer.height + 'px'); | |||
viewer3d.changeVersion(viewer, 0); | |||
}, | |||
getCurrentModel: function(v) { | |||
return v.models[v.currentModel]; | |||
}, | |||
changeVersion: function(v, version) { | |||
version = Math.max(0, Math.min(v.models.length - 1, version)); | |||
if(v.currentModel == version) return; | |||
v.currentModel = version; | |||
v.frameNode.css('background', 'url(' + viewer3d.getCurrentModel(v).imageURL + ') top left no-repeat'); | |||
viewer3d.display(v, v.frameX, v.frameY); | |||
}, | |||
hover: function(e) { | |||
var v = viewer3d.getViewer(this); | |||
if(viewer3d.dragging != v) { | |||
v.overlay.animate({'opacity': '1'}, 'fast'); | |||
} | |||
}, | |||
unhover: function(e) { | |||
var v = viewer3d.getViewer(this); | |||
if(viewer3d.dragging != v) { | |||
v.overlay.animate({'opacity': '0.5'}, 'fast'); | |||
} | |||
}, | |||
drag: function(e) { | |||
var v = viewer3d.getViewer(this); | |||
v.mouseX = e.pageX; | |||
v.mouseY = e.pageY; | |||
viewer3d.dragging = v; | |||
draggingFrameX = v.frameX; | |||
draggingFrameY = v.frameY; | |||
return false; | |||
}, | |||
release: function() { | |||
var v = viewer3d.dragging; | |||
viewer3d.dragging = null; | |||
if(v != null) { | |||
v.frameX = viewer3d.draggingFrameX; | |||
v.frameY = viewer3d.draggingFrameY; | |||
v.overlay.animate({'opacity': '0.5'}, 'fast'); | |||
} | |||
viewer3d.draggingFrameX = 0; | |||
viewer3d.draggingFrameY = 0; | |||
}, | |||
getViewer: function(node) { | |||
return viewer3d.viewers[parseInt($(node).attr('data-id'))]; | |||
}, | |||
display: function(v, frameX, frameY) { | |||
var model = viewer3d.getCurrentModel(v); | |||
var frameID = viewer3d.realMod(frameX * model.xStep + frameY, model.map.length); | |||
var frameOffset = model.map[frameID]; | |||
var frameWidth = 0; | |||
if(frameID == model.map.length - 1) { | |||
frameWidth = model.totalWidth - frameOffset; | |||
} else { | |||
frameWidth = model.map[frameID + 1] - frameOffset; | |||
} | |||
v.frameNode.css({ | |||
backgroundPosition: (-frameOffset - frameID) + 'px 0px', | |||
left: Math.round((v.width - model.maxFrameWidth) / 2.0 + model.cropMap[frameID]) + 'px', | |||
top: Math.round((v.height - model.totalHeight) / 2) + 'px', | |||
width: frameWidth + 'px', | |||
height: model.totalHeight + 'px' | |||
}); | |||
}, | |||
move: function(e) { | |||
if(viewer3d.dragging == null) { | |||
return; | |||
} | |||
var v = viewer3d.dragging; | |||
var model = viewer3d.getCurrentModel(v); | |||
var mouseDeltaX = e.pageX - v.mouseX; | |||
var mouseDeltaY = e.pageY - v.mouseY; | |||
var frameDeltaX = Math.round(mouseDeltaX / viewer3d.frameThresholdX); | |||
var frameDeltaY = -Math.round(mouseDeltaY / viewer3d.frameThresholdY); | |||
viewer3d.draggingFrameX = v.frameX + frameDeltaX; | |||
viewer3d.draggingFrameY = Math.max(0, Math.min(model.xStep - 1, v.frameY + frameDeltaY)); | |||
viewer3d.display(v, viewer3d.draggingFrameX, viewer3d.draggingFrameY); | |||
} | |||
}; | |||
$(viewer3d.init); | |||
var selector3d = { | |||
bind: function() { | |||
var viewer = viewer3d.getViewer($(this).find('.viewer-3d')); | |||
var keepGoing = true; | |||
var modelVariant = 0; | |||
var selector; | |||
while(keepGoing) { | |||
selector = $(this).find('.selector-' + modelVariant); | |||
if(selector.length) { | |||
selector.attr('data-variant', modelVariant).click(function() { | |||
viewer3d.changeVersion(viewer, parseInt($(this).attr('data-variant'))); | |||
return false; | |||
}); | |||
} | |||
modelVariant++; | |||
keepGoing = selector.length; | |||
} | |||
}, | |||
init: function() { | |||
$('.viewer-3d-multi').each(selector3d.bind); | |||
} | |||
}; | |||
$(selector3d.init); | |||
// Google Analytics/Flattr; do not track if user is on Tor. | |||
if(location.host.indexOf('.onion') == -1) { | |||
var _gaq = _gaq || []; | |||
_gaq.push(['_setAccount', 'UA-22928841-2']); | |||
_gaq.push(['_trackPageview']); | |||
_gaq.push(['_trackPageLoadTime']); | |||
(function() { | |||
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; | |||
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; | |||
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); | |||
})(); | |||
} | } | ||