MediaWiki:Gadget-Edittools.js

Origem: Wikcionário, o dicionário livre.
Nota: Após salvar, você terá de limpar a cache do seu navegador para ver as alterações:
  • Internet Explorer: Mantenha pressionada a tecla Ctrl e aperte F5; ou mantenha pressionada a tecla Ctrl e clique no botão "Recarregar" (Reload)
  • Firefox e SeaMonkey: Mantenha pressionada a tecla Shift ⇧ e clique no botão "Recarregar" (Reload ou Atualizar) ou mantenha pressionada a tecla Ctrl e aperte F5; ou mantenha pressionada as teclas Ctrl, Shift ⇧ e R (Command ⌘, Shift ⌥ e R em Macintosh)
  • Safari e Konqueror: Clique no botão "Recarregar" (Reload)
  • Opera: É necessário limpar manualmente a cache no menu "Ferramentas"→"Preferências"→"Avançadas"→"Histórico" e clicar no botão "Esvaziar Já"
  • Chrome: É necessário limpar manualmente a cache no botão "Ferramentas"→"Limpar dados de navegação", selecione "Esvaziar o cache" e clicar no botão "Limpar dados de navegação"
/*
  EditTools support: add a selector, change into true buttons, enable for all text input fields
  If enabled in preferences, the script puts the buttons into the WikiEditor Toolbar
  The special characters to insert are defined at [[MediaWiki:Edittools]].
*/
window.mw.toolbar.insertTags = function ( tagOpen, tagClose, sampleText ) {
	var $txtarea = EditTools.getTextArea();
	if ( $txtarea.length !== 1 ) {
		return;
	}

	/* Usability initiative compatibility */
	if ( typeof $.fn.textSelection !== 'undefined' ) {
		$txtarea.textSelection( 'encapsulateSelection', {
			pre: tagOpen,
			peri: sampleText,
			post: tagClose
		} );
		return;
	}
};
var EditTools = {
	createSelector: function () {
		var $sel,
			$spec = $( '#specialchars' ),
			$sb = $( '#specialchars p.specialbasic' );

		// Only care if there is more than one
		if ( !$spec.length || $sb.length <= 1 ) {
			return;
		}

		$sel = $( '<select>' );

		$sel.change( function () {
			EditTools.chooseCharSubset();
		} );

		$sb.each( function ( i ) {
			var id = $( this ).attr( 'id' ).replace( /.([0-9A-F][0-9A-F])/g, '%$1' ).replace( /_/g, ' ' );
			$sel.append( '<option value=' + i + '>' + decodeURIComponent( id ) + '</option>' );
		} );

		$spec.prepend( $sel );

		this.chooseCharSubset();
	},

	chooseCharSubset: function () {
		var $sb = $( '#specialchars p.specialbasic' ),
			id = $( '#specialchars select' ).val(),
			$wanted = $sb.eq( id );

		this.makeButtons( $wanted );

		$sb.hide();

		$wanted.css( 'display', 'inline' );

	},

	makeButtons: function ( $wanted ) {
		var $links = $wanted.find( 'a' );

		$links.each( function () {
			var $button = $( '<button type="button">' );
			$button.text( $( this ).text() );

			$button.attr( 'onclick', $( this ).attr( 'onclick' ) );

			$( this ).replaceWith( $button );
			$( this ).blur();
		} );
		$wanted.contents().not( 'button' ).remove();
	},
	makeToolbarButtons: function () {
		var $sb = $( '#specialchars p.specialbasic' ),
			pages = {};

		$sb.each( function ( i ) {
			var label = decodeURIComponent( $( this ).attr( 'id' ).replace( /.([0-9A-F][0-9A-F])/g, '%$1' ).replace( /_/g, ' ' ) );
			pages[ 'tools-' + i ] = {
				layout: 'characters',
				label: label
			};
		} );

		// Add  Edittool section
		$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
			sections: {
				edittools: {
					type: 'booklet',
					label: 'Outras ferramentas',
					pages: pages
				}
			}
		} );
		$sb.each( function ( i ) {
			var $section = $( '.page-tools-' + i + ' div' ),
				$links = $( this ).find( 'a' );
			$links.each( function () {
				var $button = $( '<span>' );
				$button.text( $( this ).text() );

				$button.attr( 'onclick', $( this ).attr( 'onclick' ) );
				$section.append( $button );
			} );
		} );
		$( '.mw-editTools' ).remove();
	},

	last_active_textfield: null,

	enableForAllFields: function () {
		$( 'textarea, input' ).focus( function () {
			EditTools.last_active_textfield = this.id;
		} );
	},

	getTextArea: function () {
		var $txtarea = {};
		if ( EditTools.last_active_textfield !== null ) {
			$txtarea = $( '#' + EditTools.last_active_textfield ).eq( 0 );
		}
		if ( $txtarea.length !== 1 ) {
			$txtarea = $( '#bodyContent textarea' ).eq( 0 );
		}
		return $txtarea;
	},

	registerTextField: function ( evt ) {
		var e = evt || window.event,
			node = e.target || e.srcElement;
		if ( !node ) {
			return;
		}
		EditTools.last_active_textfield = node.id;
		return true;
	},

	setup: function () {
		// Decide whether to use the toolbar or the bottom div
		if (
			$( '#toolbar' ).length ||
			( typeof oldEdittools !== 'undefined' && oldEdittools === true ) ||
			$( '#wpUploadDescription' ).length ||
			!$.wikiEditor
		) {
			EditTools.createSelector();
			EditTools.enableForAllFields();
		} else {
			EditTools.makeToolbarButtons();
			EditTools.enableForAllFields();
		}
	}
};

/**
 * Edittools
 * Botões para caracteres especiais na barra de ferramentas ou abaixo da janela de edição
 * Também habilita estes botões em qualquer área de texto na página.
 * @author [[commons:User:Lupo]]
 * @author [[commons:User:DieBuche]]
 */

function ImportEditTools( res ) {
	$( function () {
		var html = res && res.parse && res.parse.text && res.parse.text[ '*' ];
		if ( !html ) {
			return;
		}
		$( '.mw-editTools' ).prepend( html );
		if ( $( '#specialchars' ).length !== 1 ) {
			// Don't do anything if no edittools present.
			return;
		}
		EditTools.setup();
	} );
}

if (
	$.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ||
	mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Upload'
) {
	$.getJSON(
		mw.util.wikiScript( 'api' ), {
			format: 'json',
			action: 'parse',
			prop: 'text',
			page: 'MediaWiki:Edittools'
		}, ImportEditTools
	);
}