پرش به محتوا

مدیاویکی:Gadget-Edittools.js: تفاوت میان نسخه‌ها

از دانشنامه علوم انسانی
imported>Dalba
درست شد
imported>Yamaha5
from commons
خط ۳: خط ۳:
  * The special characters to insert are defined at [[MediaWiki:Edittools]].
  * The special characters to insert are defined at [[MediaWiki:Edittools]].
  *
  *
*  2018 (on the commons.wikimedia.org/wiki/MediaWiki:Edittools.js)
  * @author Arnomane, 2006 (on the commons.wikimedia.org/wiki/MediaWiki:Edittools.js)
  * @author Arnomane, 2006 (on the commons.wikimedia.org/wiki/MediaWiki:Edittools.js)
  * @author Kaganer, 2007 (adapting to www.mediawiki.org)
  * @author Kaganer, 2007 (adapting to www.mediawiki.org)
خط ۱۱: خط ۱۲:
/*jslint browser: true*/
/*jslint browser: true*/
/*global jQuery, mediaWiki*/
/*global jQuery, mediaWiki*/
(function ($, mw) {
/*!
"use strict";
* 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 */
/* eslint indent:["error","tab",{"outerIIFEBody":0}] */
( function ( $, mw ) {
'use strict';


var conf, editTools, $sections;
var oldEdittools,
$currentFocused,
$spec,
$sb,
$toolbar,
EditTools = window.EditTools = {


conf = {
insertTags: function ( start, peri, end ) {
initialSubset: window.EditTools_initial_subset === undefined ? window.EditTools_initial_subset : 0
if ( $currentFocused.length ) {
};
$currentFocused.textSelection(
'encapsulateSelection', {
pre: start,
peri: peri,
post: end
}
);
}
},
 
createSelector: function () {
var $sel;
// Only care if there is more than one
if ( $sb.length <= 1 ) { return; }


editTools = {
$sel = $( '<select>' ).on( 'change', this.chooseCharSubset );


/**
$sb.each( function ( i ) {
* Creates the selector
var id = $( this ).attr( 'id' ).replace( /.([0-9A-F][0-9A-F])/g, '%$1' ).replace( /_/g, ' ' );
*/
$sel.append(
setup: function () {
$( '<option value="' + i + '">' ).text( decodeURIComponent( id ) )
var $container, $select, initial;
);
} );


$container = $('#mw-edittools-charinsert');
$spec.prepend( $sel );
if (!$container.length) {
this.chooseCharSubset();
return;
// Move old edittools just below
}
$( '#editpage-copywarn' ).parent().before( $spec.parent() );
$sections = $container.find('.mw-edittools-section');
},
if ($sections.length <= 1) {
// Only care if there is more than one
return;
}


$select = $('<select>').css('display', 'inline');
chooseCharSubset: function () {
var id = $spec.find( 'select' ).val(),
$wanted = $sb.eq( id );


initial = conf.initialSubset;
$sb.hide();
if (isNaN(initial) || initial < 0 || initial >= $select.length) {
EditTools.makeButtons( $wanted );
initial = 0;
$wanted.css( 'display', 'inline' );
}
},


$sections.each(function (i, el) {
bindOnClick: function ( $button, self ) {
var $section, sectionTitle, $option;
// Copy event
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' );


$section = $(el);
if ( !$.isFunction( onclick ) ) {
sectionTitle = $section.data('sectionTitle');
if ( start || end ) {
// Create new event
onclick = function ( e ) {
e.preventDefault();
EditTools.insertTags( start, '', end );
};
// Shorten button text
if ( start && end ) { $button.text( $self.text().replace( end, '' ) ); }
} else if ( !onclick && $.isFunction( $._data ) ) {
// Fallback hack for backward compatibility
onclick = $._data( self, 'events' ).click;
if ( $.isArray( onclick ) && onclick.length ) {
onclick = onclick[ 0 ].handler;
}
}
}
$button.on( 'click', onclick );
},


$option = $('<option>')
makeButtons: function ( $wanted ) {
.text(sectionTitle)
var $links = $wanted.find( 'a' ),
.prop('value', i)
self = this;
.prop('selected', i === initial);


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


$select.change(editTools.handleOnchange);
self.bindOnClick( $button, this );
$container.prepend($select);


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


/**
makeToolbarButtons: function () {
* Handle onchange event of the <select>
var section = [],
*
self = this;
* @context {Element}
* @param e {jQuery.Event}
*/
handleOnchange: function () {
editTools.chooseSection(Number($(this).val()));


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


/**
$sb.eq( 0 ).find( 'a' )
* Toggle the currently visible section
.each( function () {
*
var $button = $( '<span>' )
* @param sectionNr {Number}
.text( $( this ).text() );
* @param setFocus {Boolean}
self.bindOnClick( $button, this );
*/
section.push( $button );
chooseSection: function (sectionNr) {
} );
var $choise = $sections.eq(sectionNr);
$( '.page-Edittools1 div' )
if ($choise.length !== 1) {
.append( section )
return;
.addClass( 'com-editbuttons' );
}


// Making these buttons is a little slow,
// Must start after toolbar creation
// If we made them all at once the browser would hang
this.createSelector();
// for over 2 seconds, so instead we're doing it on-demand
// $( '.mw-editTools' ).remove(); // The full remove is not implicit and there is more as only the standard buttons
// for each section. No need to do it twice thoguh, so remember
},
// in data whether it was done already
if (!$choise.data('charInsert.buttonsMade')) {
$choise.data('charInsert.buttonsMade', true);
editTools.makeButtons($choise);
}


$choise.show();
enableForAllFields: function () {
$sections.not($choise).hide();
$currentFocused = $toolbar;
// Apply to dynamically created textboxes as well as normal ones
$( document ).on( 'focus', 'textarea, input:text, .CodeMirror', function () {
// CodeMirror hooks into #wpTextbox1 for textSelection changes
$currentFocused = $( this );
if ( $currentFocused.hasClass( 'CodeMirror' ) ) { $currentFocused = $( '#wpTextbox1' ); }
} );
},
},


/**
// As elements from ext.wikiEditor are not immediately ready on load.
* Convert the <a onclick> links to buttons in a given section.
handleToolbarQueue: function () {
*
// if ( $.fn.wikiEditor && mw.loader.getState( 'ext.wikiEditor' ) === 'ready' )
* @param $section {jQuery}
// FIXME: the current sync load is a bit hackish (double try),
*/
// as we don't have a HOOK, only a ready event,
makeButtons: function ($section) {
// which we don't know has already triggered. [[phab:T30563]]
var $links;
// Try early as possible to put the event
 
$toolbar.on( 'wikiEditor-toolbar-doneInitialSections', function () {
if (!$section.length) {
EditTools.done = 1;
return;
EditTools.makeToolbarButtons();
}
} );
 
// Try again if the event was to late, on the end of load
$links = $section.find('a');
// (as document.ready is also too early)
$links.each(function (i, a) {
$( window ).on( 'load', function () {
var $a, $button, start, end;
if ( !EditTools.done ) { EditTools.makeToolbarButtons(); }
$a = $(a);
} );
$button = $( '<input>' ).attr( 'type', 'button' );
start = $a.data( 'mw-charinsert-start' );
end = $a.data( 'mw-charinsert-end' );
if ( start !== undefined && end !== undefined ) {
$button.click( function( e ) {
e.preventDefault();
editTools.insertTags( start, '', end )
} );
} else {
$button[0].onclick = a.onclick;
                                }
$button[0].value = $a.text();
$a.replaceWith($button);
});
},
},


insertTags: function ( pre, peri, post ) {
setup: function ( $toolbar ) {
var current = $( '#wpTextbox1' );
mw.loader.load( '//commons.wikimedia.org/?title=MediaWiki:Edittools.css&action=raw&ctype=text/css', 'text/css' );
if ( current && current.length ) {
$sb = $spec.find( 'p.specialbasic' );
current.textSelection(
// Decide whether to use the toolbar
'encapsulateSelection', { pre: pre, peri: peri, post: post }
if ( $toolbar && $toolbar[ 0 ] && !( window.oldEdittools || oldEdittools ) && !$( '#wpUploadDescription' ).length ) {
);
this.handleToolbarQueue( /* $toolbar*/ );
} else {
this.createSelector();
}
}
mw.hook( 'wikipage.content' ).add( this.enableForAllFields );
}
}
};
};
 
$( function () {
$(document).ready(editTools.setup);
$spec = $( '#specialchars' );
 
// Don't do anything if no edittools present.
}(jQuery, mediaWiki));
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' ) || mw.loader.getState( 'ext.wikiEditor' ) !== 'registered' ) && !oldEdittools ) {
$toolbar = $( '#wpTextbox1' );
}
EditTools.setup( $toolbar );
} );
} );
}( jQuery, mediaWiki ) );
// </nowiki>

نسخهٔ ۶ نوامبر ۲۰۱۸، ساعت ۱۵:۱۰

/**
 * EditTools support: add a selector, change <a> into buttons.
 * The special characters to insert are defined at [[MediaWiki:Edittools]].
 *
 *  2018 (on the commons.wikimedia.org/wiki/MediaWiki:Edittools.js)
 * @author Arnomane, 2006 (on the commons.wikimedia.org/wiki/MediaWiki:Edittools.js)
 * @author Kaganer, 2007 (adapting to www.mediawiki.org)
 * @author Krinkle, 2012
 * @source www.mediawiki.org/wiki/MediaWiki:Gadget-Edittools.js
 * @revision 2012-02-29
 */
/*jslint browser: true*/
/*global jQuery, mediaWiki*/
/*!
* 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 */
/* eslint indent:["error","tab",{"outerIIFEBody":0}] */
( function ( $, mw ) {
'use strict';

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

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

		createSelector: function () {
			var $sel;
			// 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();
			// Move old edittools just below
			$( '#editpage-copywarn' ).parent().before( $spec.parent() );
		},

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

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

		bindOnClick: function ( $button, self ) {
		// Copy event
			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' );

			if ( !$.isFunction( onclick ) ) {
				if ( start || end ) {
				// Create new event
					onclick = function ( e ) {
						e.preventDefault();
						EditTools.insertTags( start, '', end );
					};
					// Shorten button text
					if ( start && end ) { $button.text( $self.text().replace( end, '' ) ); }
				} else if ( !onclick && $.isFunction( $._data ) ) {
				// Fallback hack for backward compatibility
					onclick = $._data( self, 'events' ).click;
					if ( $.isArray( onclick ) && onclick.length ) {
						onclick = onclick[ 0 ].handler;
					}
				}
			}
			$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
			$toolbar.wikiEditor( 'addToToolbar', {
				sections: {
					Edittools: {
						type: 'booklet',
						label: 'Edittools',
						pages: {
							Edittools1: {
								layout: 'characters',
								label: 'Edittools2'
							}
						}
					}
				}
			} );

			$sb.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' );

			// Must start after toolbar creation
			this.createSelector();
			// $( '.mw-editTools' ).remove(); // The full remove is not implicit and there is more as only the standard buttons
		},

		enableForAllFields: function () {
			$currentFocused = $toolbar;
			// Apply to dynamically created textboxes as well as normal ones
			$( document ).on( 'focus', 'textarea, input:text, .CodeMirror', function () {
				// CodeMirror hooks into #wpTextbox1 for textSelection changes
				$currentFocused = $( this );
				if ( $currentFocused.hasClass( 'CodeMirror' ) ) { $currentFocused = $( '#wpTextbox1' ); }
			} );
		},

		// As elements from ext.wikiEditor are not immediately ready on load.
		handleToolbarQueue: function () {
			// if ( $.fn.wikiEditor && mw.loader.getState( 'ext.wikiEditor' ) === 'ready' )
			// FIXME: the current sync load is a bit hackish (double try),
			// as we don't have a HOOK, only a ready event,
			// which we don't know has already triggered. [[phab:T30563]]
			// Try early as possible to put the event
			$toolbar.on( 'wikiEditor-toolbar-doneInitialSections', function () {
				EditTools.done = 1;
				EditTools.makeToolbarButtons();
			} );
			// Try again if the event was to late, on the end of load
			// (as document.ready is also too early)
			$( window ).on( 'load', function () {
				if ( !EditTools.done ) { EditTools.makeToolbarButtons(); }
			} );
		},

		setup: function ( $toolbar ) {
			mw.loader.load( '//commons.wikimedia.org/?title=MediaWiki:Edittools.css&action=raw&ctype=text/css', 'text/css' );
			$sb = $spec.find( 'p.specialbasic' );
			// Decide whether to use the toolbar
			if ( $toolbar && $toolbar[ 0 ] && !( window.oldEdittools || oldEdittools ) && !$( '#wpUploadDescription' ).length ) {
				this.handleToolbarQueue( /* $toolbar*/ );
			} else {
				this.createSelector();
			}
			mw.hook( 'wikipage.content' ).add( this.enableForAllFields );
		}
	};
$( function () {
	$spec = $( '#specialchars' );
	// Don't do anything if no edittools present.
	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' ) || mw.loader.getState( 'ext.wikiEditor' ) !== 'registered' ) && !oldEdittools ) {
			$toolbar = $( '#wpTextbox1' );
		}
		EditTools.setup( $toolbar );
	} );
} );
}( jQuery, mediaWiki ) );
// </nowiki>