MediaWiki:Gadget-AutoNav.js
Nota: Depois de publicar, poderá ter de contornar a cache do seu navegador para ver as alterações.
- Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
- Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
- Internet Explorer / Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5
- Opera: Pressione Ctrl-F5.
/**
* Exibe automaticamente uma barra de navegação para os livros que possuem uma
* Predefinição:Lista de capítulos/<nome do livro>
* @author: Helder (https://github.com/he7d3r)
* @author [[User:Raylton P. Sousa]]
* TODO:
* 1) Adaptar o script do Wikisource que insere um menu lateral para definir o tema (layout) usado ao visualizar as páginas, para poder oferecer pelo menos umas duas opções de estilos diferentes (já que o consenso quanto ao estilo oficial pode demorar um pouco). Ver [[oldwikisource:MediaWiki:PageNumbers.js]]
*/
/*jshint laxbreak:true */
/*global jQuery, mediaWiki */
( function ( mw, $ ) {
'use strict';
var AutoNav = {},
options = {};
/**
* Adaptado da função pr_init_tabs() do arquivo proofread.js (de ThomasV, sob licença GPL)
* @source https://gerrit.wikimedia.org/r/gitweb?p=mediawiki/extensions/ProofreadPage.git;a=blob;f=proofread.js;h=965070b3da9115c9a1a8e65d7f109207b5ecbab2;hb=85861896f0f3d7667ef5f561fa784c073f3ed03d#l1
*/
AutoNav.insert = function () {
var path = mw.config.get('wgScriptPath') + '/extensions/ProofreadPage/',
$inf = $('#auto-nav-inf'),
$prev = $inf.find('.auto-nav-prev a'),
$next = $inf.find('.auto-nav-next a'),
$sup,
$talk,
$main,
$index;
if (!$inf.length) {
//Se não existe a barra de navegação na parte inferior, não há nada a ser feito
return;
}
if ( options.AutoNavOnTop ) {
$sup = $inf.clone().attr('id', 'auto-nav-sup');
if ( mw.util.getParamValue('diff') !== null ) {
$sup.insertAfter('h2:first');
} else {
$sup.insertAfter('#jump-to-nav');
}
}
if ('submit' === mw.config.get('wgAction')) {
$inf.insertBefore('#editform');
}
if( options.AutoNavTopArrows ){
$index = $inf.find('.auto-nav-index a');
if (!$index.length) {
return;
}
$talk = $('#ca-talk');
if (mw.config.get('wgPageName') !== mw.config.get('wgBookName')) {
$talk.after('<li id="ca-index">'
+ '<span>'
+ '<a href="' + $index[0].href + '" title="' + $index[0].title + '">'
+ '<img src="' + path + 'uparrow.png" alt="Índice" width="15" height="15" />'
+ '</a>'
+ '</span>'
+ '</li>');
}
$main = $talk.prev();
if ($prev.length) {
$main.before('<li id="ca-prev">'
+ '<span>'
+ '<a href="' + $prev[0].href + '" title="' + $prev[0].title + '">'
+ '<img src="' + path + 'leftarrow.png" alt="Anterior" width="15" height="15" />'
+ '</a>'
+ '</span>'
+ '</li>');
}
if ($next.length) {
$main.before('<li id="ca-next">'
+ '<span>'
+ '<a href="' + $next[0].href + '" title="' + $next[0].title + '">'
+ '<img src="' + path + 'rightarrow.png" alt="Posterior" width="15" height="15" />'
+ '</a>'
+ '</span>'
+ '</li>');
}
}
/* Navegação pelas setas do teclado */
if ( !options.AutoNavWithKeys ) {
return;
}
$(document).on('keydown', function(e) {
if ( e.altKey || e.ctrlKey || e.shiftKey || !$( document.activeElement ).is('body') || $(document).width() > $(window).width() ) {
return;
}
if( e.which === 37 /* left arrow */ && $prev.length ){
location.href = $prev[0].href;
}
if( e.which === 39 /* right arrow */ && $next.length ){
location.href = $next[0].href;
}
});
};
/* Oculta a navegação em determinadas circunstanciadas (por exemplo, quando visualizamos o histórico ou quando editamos a página)*/
AutoNav.hide = function () {
$('#auto-nav-sup, #auto-nav-inf').hide();
};
AutoNav.hideTemplates = function () {
$( '#bodyContent .b-nav' ).each( function() {
var parentId = $(this).parent().attr('id');
if ('auto-nav-inf' !== parentId && 'auto-nav-sup' !== parentId) {
$(this).hide();
}
});
};
if (0 === mw.config.get('wgNamespaceNumber') && !/\/Imprimir$/.test(mw.config.get('wgTitle'))) {
options.AutoNavOnTop = mw.config.get( 'AutoNavOnTop', true );
options.AutoNavWithKeys = mw.config.get( 'AutoNavWithKeys', true );
options.AutoNavTopArrows = mw.config.get( 'AutoNavTopArrows', false );
$( AutoNav.hideTemplates );
switch (mw.config.get('wgAction')) {
case 'view':
case 'purge':
$( AutoNav.insert );
break;
case 'submit':
// Evita conflito durante a previsão de edições
options.AutoNavWithKeys = false;
$( AutoNav.insert );
break;
default:
$(AutoNav.hide);
break;
}
} else {
$(AutoNav.hide);
}
}( mediaWiki, jQuery ) );
// [[Categoria:Gadgets|AutoNav.js]]
// [[Categoria:Scripts do Wikilivros|AutoNav.js]]