MediaWiki:Gadget-Edittools.js

Origem: Wikilivros, livros abertos por um mundo aberto.

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.
/*
  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:Edittools2]].
*/
window.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: 'Edittools',
					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:Edittools2'
		}, ImportEditTools
	);
}

// [[Categoria:Gadgets|Edittools.js]]
// [[Categoria:Scripts do Wikilivros|Edittools.js]]