پرش به محتوا

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

از دانشنامه علوم انسانی
imported>Yamaha5
from commons
imported>Yamaha5
خنثی‌سازی ویرایش 24787796 توسط Yamaha5 (بحث) گویا درست شده
 
خط ۳: خط ۳:
  * 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) {
* EditTools support
"use strict";
*
* 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,
var conf, editTools, $sections;
$currentFocused,
$spec,
$sb,
$toolbar,
EditTools = window.EditTools = {


insertTags: function ( start, peri, end ) {
conf = {
if ( $currentFocused.length ) {
initialSubset: window.EditTools_initial_subset === undefined ? window.EditTools_initial_subset : 0
$currentFocused.textSelection(
};
'encapsulateSelection', {
 
pre: start,
editTools = {
peri: peri,
 
post: end
/**
}
* Creates the selector
);
*/
setup: function () {
var $container, $select, initial;
 
$container = $('#mw-edittools-charinsert');
if (!$container.length) {
return;
}
$sections = $container.find('.mw-edittools-section');
if ($sections.length <= 1) {
// Only care if there is more than one
return;
}
}
},


createSelector: function () {
$select = $('<select>').css('display', 'inline');
var $sel;
// Only care if there is more than one
if ( $sb.length <= 1 ) { return; }


$sel = $( '<select>' ).on( 'change', this.chooseCharSubset );
initial = conf.initialSubset;
if (isNaN(initial) || initial < 0 || initial >= $select.length) {
initial = 0;
}


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


$spec.prepend( $sel );
$section = $(el);
this.chooseCharSubset();
sectionTitle = $section.data('sectionTitle');
// Move old edittools just below
$( '#editpage-copywarn' ).parent().before( $spec.parent() );
},


chooseCharSubset: function () {
$option = $('<option>')
var id = $spec.find( 'select' ).val(),
.text(sectionTitle)
$wanted = $sb.eq( id );
.prop('value', i)
.prop('selected', i === initial);


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


bindOnClick: function ( $button, self ) {
$select.change(editTools.handleOnchange);
// Copy event
$container.prepend($select);
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 ) ) {
editTools.chooseSection(initial);
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' ),
* Handle onchange event of the <select>
self = this;
*
* @context {Element}
* @param e {jQuery.Event}
*/
handleOnchange: function () {
editTools.chooseSection(Number($(this).val()));


$links.each( function () {
return true;
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 = [],
* Toggle the currently visible section
self = this;
*
* @param sectionNr {Number}
* @param setFocus {Boolean}
*/
chooseSection: function (sectionNr) {
var $choise = $sections.eq(sectionNr);
if ($choise.length !== 1) {
return;
}


// Add Edittool section
// Making these buttons is a little slow,
$toolbar.wikiEditor( 'addToToolbar', {
// If we made them all at once the browser would hang
sections: {
// for over 2 seconds, so instead we're doing it on-demand
Edittools: {
// for each section. No need to do it twice thoguh, so remember
type: 'booklet',
// in data whether it was done already
label: 'Edittools',
if (!$choise.data('charInsert.buttonsMade')) {
pages: {
$choise.data('charInsert.buttonsMade', true);
Edittools1: {
editTools.makeButtons($choise);
layout: 'characters',
}
label: 'Edittools2'
}
}
}
}
} );


$sb.eq( 0 ).find( 'a' )
$choise.show();
.each( function () {
$sections.not($choise).hide();
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();
* Convert the <a onclick> links to buttons in a given section.
// $( '.mw-editTools' ).remove(); // The full remove is not implicit and there is more as only the standard buttons
*
},
* @param $section {jQuery}
*/
makeButtons: function ($section) {
var $links;


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


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

	var conf, editTools, $sections;

	conf = {
		initialSubset: window.EditTools_initial_subset === undefined ? window.EditTools_initial_subset : 0
	};

	editTools = {

		/**
		 * Creates the selector
		 */
		setup: function () {
			var $container, $select, initial;

			$container = $('#mw-edittools-charinsert');
			if (!$container.length) {
				return;
			}
			$sections = $container.find('.mw-edittools-section');
			if ($sections.length <= 1) {
				// Only care if there is more than one
				return;
			}

			$select = $('<select>').css('display', 'inline');

			initial = conf.initialSubset;
			if (isNaN(initial) || initial < 0 || initial >= $select.length) {
				initial = 0;
			}

			$sections.each(function (i, el) {
				var $section, sectionTitle, $option;

				$section = $(el);
				sectionTitle = $section.data('sectionTitle');

				$option = $('<option>')
					.text(sectionTitle)
					.prop('value', i)
					.prop('selected', i === initial);

				$select.append($option);
			});

			$select.change(editTools.handleOnchange);
			$container.prepend($select);

			editTools.chooseSection(initial);
		},

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

			return true;
		},

		/**
		 * Toggle the currently visible section
		 *
		 * @param sectionNr {Number}
		 * @param setFocus {Boolean}
		 */
		chooseSection: function (sectionNr) {
			var $choise = $sections.eq(sectionNr);
			if ($choise.length !== 1) {
				return;
			}

			// Making these buttons is a little slow,
			// If we made them all at once the browser would hang
			// for over 2 seconds, so instead we're doing it on-demand
			// 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();
			$sections.not($choise).hide();
		},

		/**
		 * Convert the <a onclick> links to buttons in a given section.
		 *
		 * @param $section {jQuery}
		 */
		makeButtons: function ($section) {
			var $links;

			if (!$section.length) {
				return;
			}

			$links = $section.find('a');
			$links.each(function (i, a) {
				var $a, $button, start, end;
				$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 ) {
			var current = $( '#wpTextbox1' );
			if ( current && current.length ) {
				current.textSelection(
					'encapsulateSelection', { pre: pre, peri: peri, post: post }
				);
			}
		}

	};

	$(document).ready(editTools.setup);

}(jQuery, mediaWiki));