MediaWiki:Common.js
Revision as of 09:18, 1 August 2015 by FantasyTeddy (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-28926290-49', 'wiki.aidancbrady.com');
ga('send', 'pageview');
( function() {
'use strict';
/**
* Instead of cluttering up the global scope with
* variables, they should instead be set as a
* property of this global variable
*
* E.g: Instead of
* myVar = 'blah';
* use
* mcw.myVar = 'blah';
*/
var mcw = window.mcw = {};
/* Variables for interface text used throughout the script, for ease of translating */
mcw.i18n = {
// Collapsible tables and page loader
hideText: 'hide',
showText: 'show',
// Page loader
loadErrorTitle: 'An error occurred loading the content',
// File upload
defaultLicense: 'License'
};
/* Keep track of delegated events on dynamic content */
mcw.events = {};
/* Fired whenever wiki content is added. (#mw-content-text, live preview, load page, etc.) */
mw.hook( 'wikipage.content' ).add( function( $content ) {
/**
* Collapsible tables
*
* Based on http://www.mediawiki.org/wiki/Manual:Collapsible_tables#Common.js_script_.28before_1.18.29
*/
( function() {
var $tables = $content.find( 'table.collapsible' );
if ( !$tables || !$tables.length ) {
return false;
}
var buttonText = ' <span class="collapsible-button">[<span class="jslink">' + mcw.i18n.hideText + '</span>]</span> ';
$tables.each( function() {
var $table = $( this ), $header, $collapseButton, firstWidth, secondWidth;
// This table is already collapsible
if ( $table.data( 'collapsible' ) ) {
return true;
}
// Use the collapse-button if specified otherwise the first header cell of the first row
$header = $table.find( 'tr:first .collapse-button' );
if ( !$header.length ) {
$header = $table.find( 'tr:first > th:first' );
}
// No header or the table body is empty
if ( !$header.length || !$table.find( 'tr:not(tr:first)' ).html().trim().length ) {
return true;
}
// For the button to float properly, it has to be /before/ the cell text
if ( $table.hasClass( 'collapse-button-none' ) ) {
$header.append( buttonText );
} else {
$header.prepend( buttonText );
}
// Find max button size, and set its min-width to it
$collapseButton = $table.find( '.collapsible-button' );
firstWidth = $collapseButton.width();
$collapseButton.find( '> .jslink' ).text( mcw.i18n.showText );
secondWidth = $collapseButton.width();
if ( firstWidth != secondWidth ) {
if ( firstWidth < secondWidth ) {
$collapseButton.css( 'min-width', secondWidth );
} else {
$collapseButton.css( 'min-width', firstWidth );
}
}
// Set the text back to hide if it's not collapsed to begin with
if ( !$table.hasClass( 'collapsed' ) ) {
$collapseButton.find( '> .jslink' ).text( mcw.i18n.hideText );
}
$table.data( 'collapsible', true );
} );
// This is bound directly because sortable tables don't play nice
$tables.find( '.collapsible-button .jslink' ).click( function( e ) {
var $table = $( this ).closest( 'table.collapsible' );
// Stop table sorting activating when clicking the link
e.stopPropagation();
if ( $table.hasClass( 'collapsed' ) ) {
$table.removeClass( 'collapsed' ).addClass( 'expanded' );
$( this ).text( mcw.i18n.hideText );
} else {
$table.removeClass( 'expanded' ).addClass( 'collapsed' );
$( this ).text( mcw.i18n.showText );
}
} );
}() );
} );
/* End wiki content hook */
/* Fires when DOM is ready */
$( function() {
/**
* Creates minecraft style tooltips
*
* Replaces normal tooltips. Supports minecraft [[formatting codes]] (except k), and a description with line breaks (/).
* Use mcw.useNativeMinetip = true to use normal tooltips, with the description added
*/
mcw.minetip = {
// Add normal minetip events, removing legacy tooltip
create: function() {
var tooltip;
$( '#mw-content-text' ).on( {
'mouseenter.minetip': function( e ) {
var $elem = $( this ),
title = $elem.data( 'minetip-title' ),
description = $elem.data( 'minetip-text' );
// No title or title only contains formatting codes
if ( title === undefined || title && title.replace( /&([0-9a-fl-o])|\s+/g, '' ) === '' ) {
// Use title attribute of the element or the first link directly under it
var attrTitle = $elem.attr( 'title' ) || $elem.find( '> a:first' ).attr( 'title' );
if ( title === undefined ) {
title = attrTitle;
} else {
title += attrTitle;
}
if ( title ) {
// Set the retrieved title as data for future use
$elem.data( 'minetip-title', title );
} else {
return;
}
}
$elem.add( '*', $elem ).filter( '[title]' ).removeAttr( 'title' );
if ( title === 0 ) {
return;
}
var text = '<span class="title">' + title + '&f</span>';
if ( description ) {
text += '\n<span class="description">' +
description.replace( /\\\//g, '/' ).replace( /\//g, '<br>' ) +
'&f</span>';
}
if ( !$( '#minetip-tooltip' ).length ) {
$( 'body' ).append( '<div id="minetip-tooltip"/>' );
}
tooltip = $( '#minetip-tooltip' );
// Add classes for minecraft formatting codes
while ( text.match( /&[0-9a-el-o]/ ) ) {
text = text.replace( /&([0-9a-el-o])(.*?)(&f|$)/g, '<span class="format-$1">$2</span>&f' );
}
// Remove reset formatting
text = text.replace( /&f/g, '' );
tooltip.html( text );
// Trigger a mouse movement to position the tooltip
$elem.trigger( 'mousemove', e );
},
'mousemove.minetip': function( e, trigger ) {
if ( !$( '#minetip-tooltip' ).length ) {
$( this ).trigger( 'mouseenter' );
return;
}
// Get event data from remote trigger
e = trigger || e;
var top = e.clientY - 34,
left = e.clientX + 14,
width = tooltip.outerWidth( true ),
height = tooltip.outerHeight( true ),
$win = $( window ),
winWidth = $win.width(),
winHeight = $win.height();
// If going off the right of the screen, go to the left of the cursor
if ( left + width > winWidth ) {
left -= width + 36;
}
// If now going off to the left of the screen, resort to going below the cursor
if ( left < 0 ) {
left = 0;
top += 82;
// Go above the cursor if too low
if ( top + height > winHeight ) {
top -= 77 + height;
}
// Don't go off the top of the screen
} else if ( top < 0 ) {
top = 0;
// Don't go off the bottom of the screen
} else if ( top + height > winHeight ) {
top = winHeight - height;
}
// Apply the positions
tooltip.css( {
top: top,
left: left
} );
},
'mouseleave.minetip': function() {
if ( !tooltip ) {
return;
}
tooltip.remove();
}
}, '.minetip, .grid .image, .grid .item, .grid2 .item' ).off( '.minetipNative' );
},
// Remove all events
destroy: function() {
$( '#mw-content-text' ).off( '.minetip .minetipNative' );
$( '#minetip-tooltip' ).remove();
},
// Add native browser tooltip events, removing normal minetip
native: function() {
$( '#mw-content-text' ).on( 'mouseenter.minetipNative', '.minetip, .grid .image, .grid .item, .grid2 .item', function() {
var title = $( this ).data( 'minetip-title' ),
description = $( this ).data( 'minetip-text' ),
existingTitle = $( this ).attr( 'title' ) || $( this ).find( '> a:first' ).attr( 'title' );
if ( title || title === 0 || $( this ).attr( 'title' ) ) {
// Remove titles within so they don't interfere
$( this ).find( '[title]' ).removeAttr( 'title' );
}
if ( title === 0 ) {
$( this ).removeAttr( 'title' );
return;
} else if ( !title && ( !existingTitle || !description ) ) {
return;
} else if ( !title && existingTitle ) {
$( this ).data( 'minetip-title', existingTitle );
}
var text = title || existingTitle;
if ( description ) {
text += '\n' + description;
}
// Remove formatting
text = text.replace( /&([0-9a-fl-o])/g, '' )
.replace( /\\\//g, '/' )
.replace( /\//g, '\n' )
.replace( ///g, '/' );
$( this ).attr( 'title', text );
} ).off( '.minetip' );
}
};
if ( mcw.useNativeMinetip ) {
mcw.minetip.native();
} else {
mcw.minetip.create();
}
} );
/* End DOM ready */
}() );
Cookies help us deliver our services. By using our services, you agree to our use of cookies.