MediaWiki:Common.js
Jump to navigation
Jump to search
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.
/* Any JavaScript here will be loaded for all users on every page load. */
/* === STICKY FUNCTION === */
/**** mobile views ****/
if ($(window).width() < 768) {
/* Wrap Tables for scrolling in mobile views */
$('table').not('.mw-collapsible').not('.pi-horizontal-group').wrap(
'<div style="max-width: 90vw; overflow-x: scroll" class="table-scroll-shadow"></div>'
);
/* Hide heading if infobox exists - shows duplicate information */
var infobox = $('.portable-infobox');
if (infobox.length > 0) {
$('.firstHeading').hide();
}
}
else {
$(window).on("scroll", function() {
var nav = document.getElementById("mw-panel");
if (window.scrollY >= nav.offsetTop) {
nav.classList.add("sticky");
} else {
nav.classList.remove("sticky");
}
});
}
/* === SHORTCUTS === */
/*
* VisualEditor keyboard shortcuts in WikiEditor
* Script aims for feature parity, as long as it improves usability.
* List for comparison:
* https://www.mediawiki.org/wiki/VisualEditor/Portal/Keyboard_shortcuts
*/
$( function() {
// List of shortcuts
var ctrlShortcuts = {
'bold': {
key: 'B',
fn: function( e ) {
return ( e.which === 66 );
}
},
'italic': {
key: 'I',
fn: function( e ) {
return ( e.which === 73 );
}
},
'replace': {
key: 'F',
fn: function( e ) {
return ( e.which === 70 );
}
},
'reference': {
key: 'Shift+K',
fn: function( e ) {
return ( e.shiftKey && e.which === 75 );
}
},
'link': {
key: 'K',
fn: function( e ) {
return ( !e.shiftKey && e.which === 75 );
}
},
'nowiki': {
key: '\\',
fn: function( e ) {
return ( e.key === '\\' );
}
},
'superscript': {
key: '.',
fn: function( e ) {
return ( e.key === '.' );
}
},
'subscript': {
key: ',',
fn: function( e ) {
return ( e.key === ',' );
}
},
'heading-2': {
key: '2',
fn: function( e ) {
return ( e.which === 50 );
}
},
'heading-3': {
key: '3',
fn: function( e ) {
return ( e.which === 51 );
}
},
'heading-4': {
key: '4',
fn: function( e ) {
return ( e.which === 52 );
}
},
'heading-5': {
key: '5',
fn: function( e ) {
return ( e.which === 53 );
}
}
};
// Dependencies
var deps = [
'ext.wikiEditor',
'jquery.client',
'jquery.textSelection'
];
// Current platform
var platform;
// Test if shortcut is triggered
function testShortcut( e ) {
if ( platform === 'mac' && !e.metaKey ) {
return false;
}
if ( platform !== 'mac' && !e.ctrlKey ) {
return false;
}
var result = false;
for ( var tool in ctrlShortcuts ) {
var isCorrect = ctrlShortcuts[ tool ].fn( e );
if ( isCorrect ) {
result = tool;
break;
}
}
return result;
}
// Get correct toolbar button
function getButton( name ) {
var $button = $( '.tool[rel="' + name + '"] a' );
if ( $button.length ) {
return $button;
}
$button = $( '.option[rel="' + name + '"]' );
if ( $button.length ) {
return $button;
}
return false;
}
// Insert on keydown
function insertOnKeydown( e ) {
var shortcut = testShortcut( e, platform );
if ( shortcut ) {
e.preventDefault();
var $button = getButton( shortcut );
if ( $button.length ) {
$button[ 0 ].click();
}
}
}
// Initialise the shortcuts
function init( editor ) {
platform = $.client.profile().platform;
var $textbox = ( editor ? $( editor ) : $( '#wpTextbox1' ) );
if ( !$textbox.length ) {
return;
}
// Support Ctrl shortcuts
$textbox.on( 'keydown', insertOnKeydown );
// Inform about shortcuts
for ( var tool in ctrlShortcuts ) {
var $button = getButton( tool );
if ( !$button.length ) {
return;
}
// Get shortcut text
var shortcut = ctrlShortcuts[ tool ].key;
shortcut = ( platform === 'mac' ? 'Cmd+' : 'Ctrl+' ) + shortcut;
// Set shortcut as tooltip
var title = $button.attr( 'title' );
title = ( title ? title : $button.text() );
$button.attr( 'title', title + ' [' + shortcut + ']' );
}
}
// Wait for dependencies and initialise
$.when(
mw.loader.using( deps ),
$.ready
).then( function() {
// Do not transfer promise to this function
init();
} );
// [[User:JWBTH/CD]] support
mw.hook( 'convenientDiscussions.commentFormCreated' ).add( function ( commentForm ) {
mw.loader.using( deps );
var editor = commentForm.commentInput.$input;
editor.on( 'wikiEditor-toolbar-doneInitialSections', function() {
init( editor );
} );
} );
} );
/* show search dropdown results on mobile */
/* by vGhost on wiki.gg */
if ($(window).width() < 1000) {
var $searchInput = $( 'div#simpleSearch #searchInput,div#simpleSearch input' );
$searchInput.on('input',function(e){
//trigger keydown with e object
$(this).trigger(jQuery.Event('keydown', {
keyCode: e.keyCode,
which: e.which,
}));
$(this).trigger(jQuery.Event('keypress', {
keyCode: e.keyCode,
which: e.which,
}));
})
}