MediaWiki:Edittools.js

From radlines.org
Revision as of 20:04, 27 May 2018 by Admin (talk | contribs) (Created page with "/*! * 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 WikiEd...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Note: After saving, 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: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/*!
* 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]].
*/
// <nowiki>
/* global jQuery, mediaWiki */
( function ( $, mw ) {
'use strict';

var oldEdittools,
	$currentFocused,
	$spec,
	EditTools = window.EditTools = {

		insertTags: function ( start, peri, end ) {
			if ( $currentFocused.length ) {
				$currentFocused.textSelection(
					'encapsulateSelection', {
						pre: start,
						peri: peri,
						post: end
					}
				);
			}
		},

		createSelector: function () {
			var $sel,
				$sb = $spec.find( 'p.specialbasic' );

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

			$sel = $( '<select>' ).on( 'change', this.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 + '">' ).text( decodeURIComponent( id ) )
				);
			} );

			$spec.prepend( $sel );
			this.chooseCharSubset();
		},

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

			EditTools.makeButtons( $wanted );
			$sb.hide();
			$wanted.css( 'display', 'inline' );
		},

		bindOnClick: function ( $button, self ) {
			var onclick = self.getAttribute( 'onclick' ), // TODO: outdated? For FF, IE8, Chrome
				$self = $( self ),
				start = $self.data( 'mw-charinsert-start' ),
				end = $self.data( 'mw-charinsert-end' );
			onclick = onclick || $._data( self, 'events' ).click;

			if ( !$.isFunction( onclick ) ) {
				if ( $.isArray( onclick ) && onclick.length ) {
				// Copy event
					onclick = onclick[ 0 ].handler;
				} else if ( start || end ) {
				// Create new event
					onclick = function ( e ) {
						e.preventDefault();
						EditTools.insertTags( start, '', end );
					};
				}
			}
			$button.on( 'click', onclick );
		},

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

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

				self.bindOnClick( $button, this );

				$( this ).replaceWith( $button ).blur();
			} );
			$wanted.contents().not( 'button' ).remove();
		},

		makeToolbarButtons: function () {
			var section = [],
				self = this;

			// Add Edittool section
			$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
				sections: {
					Edittools: {
						type: 'booklet',
						label: 'Edittools',
						pages: {
							Edittools1: {
								layout: 'characters',
								label: 'Edittools2'
							}
						}
					}
				}
			} );

			$spec.find( 'p.specialbasic' ).eq( 0 ).find( 'a' )
				.each( function () {
					var $button = $( '<span>' )
						.text( $( this ).text() );
					self.bindOnClick( $button, this );
					section.push( $button );
				} );
			$( '.page-Edittools1 div' )
				.append( section )
				.addClass( 'com-editbuttons' );
			// $( '.mw-editTools' ).remove(); // The full remove is not implicit and there is more as only the standard buttons
		},

		enableForAllFields: function () {
			$( 'textarea, input:text' ).on( 'focus', function () {
				$currentFocused = $( this );
			} );
		},

		setup: function () {
			mw.loader.load( '//commons.wikimedia.org/?title=MediaWiki:Edittools.css&action=raw&ctype=text/css', 'text/css' );
			// Decide whether to use the toolbar
			if ( ( !window.oldEdittools || !oldEdittools ) && !$( '#wpUploadDescription' ).length && $.wikiEditor ) {
				this.makeToolbarButtons();
			}
			this.createSelector();
			this.enableForAllFields();

		}
	};
$( function () {
	// Don't do anything if no edittools present.
	$spec = $( '#specialchars' );
	if ( !$spec.length ) { return; }
	mw.loader.using( 'user.options', function () {
		// Check user preferences
		oldEdittools = mw.user.options.get( 'gadget-OldEdittools' );
		if ( ( mw.user.options.get( 'usebetatoolbar' ) ||
			$.inArray( mw.loader.getState( 'ext.wikiEditor' ), [ 'loading', 'ready', 'loaded' ] ) !== -1 ) &&
			!oldEdittools ) {
			mw.hook( 'resourceloader.loadEnd' ).add( function () { EditTools.setup(); } );
		} else {
			EditTools.setup();
		}
	} );
} );
}( jQuery, mediaWiki ) );
// </nowiki>