MediaWiki:Gadget-Edittools.js: differenze tra le versioni

Da WikiFoligno.
Vai alla navigazione Vai alla ricerca
Creata pagina con "/** * EditTools support: add a selector, change <a> into buttons. * The special characters to insert are defined at MediaWiki:Edittools. * * @author Arnomane, 2006 (on..."
 
Nessun oggetto della modifica
 
Riga 7: Riga 7:
  * @author Krinkle, 2012
  * @author Krinkle, 2012
  * @source www.mediawiki.org/wiki/MediaWiki:Gadget-Edittools.js
  * @source www.mediawiki.org/wiki/MediaWiki:Gadget-Edittools.js
  * @revision 2012-02-29
  * @revision 2019-12-17
  */
  */
/*jslint browser: true*/
/*jslint browser: true*/
Riga 13: Riga 13:
(function ($, mw) {
(function ($, mw) {
"use strict";
"use strict";
 
var conf, editTools, $sections;
var conf, editTools, $sections;
 
conf = {
conf = {
initialSubset: window.EditTools_initial_subset === undefined ? window.EditTools_initial_subset : 0
initialSubset: window.EditTools_initial_subset === undefined ? window.EditTools_initial_subset : 0
};
};
 
editTools = {
editTools = {
 
/**
/**
* Creates the selector
* Creates the selector
Riga 27: Riga 27:
setup: function () {
setup: function () {
var $container, $select, initial;
var $container, $select, initial;
 
$container = $('#mw-edittools-charinsert');
$container = $('#mw-edittools-charinsert');
if (!$container.length) {
if (!$container.length) {
Riga 37: Riga 37:
return;
return;
}
}
 
$select = $('<select>').css('display', 'inline');
$select = $('<select>').css('display', 'inline');
 
initial = conf.initialSubset;
initial = conf.initialSubset;
if (isNaN(initial) || initial < 0 || initial >= $select.length) {
if (isNaN(initial) || initial < 0 || initial >= $select.length) {
initial = 0;
initial = 0;
}
}
 
$sections.each(function (i, el) {
$sections.each(function (i, el) {
var $section, sectionTitle, $option;
var $section, sectionTitle, $option;
 
$section = $(el);
$section = $(el);
sectionTitle = $section.data('sectionTitle');
sectionTitle = $section.data('sectionTitle');
 
$option = $('<option>')
$option = $('<option>')
.text(sectionTitle)
.text(sectionTitle)
.prop('value', i)
.prop('value', i)
.prop('selected', i === initial);
.prop('selected', i === initial);
 
$select.append($option);
$select.append($option);
});
});
 
$select.change(editTools.handleOnchange);
$select.change(editTools.handleOnchange);
$container.prepend($select);
$container.prepend($select);
 
editTools.chooseSection(initial);
editTools.chooseSection(initial);
},
},
 
/**
/**
* Handle onchange event of the <select>
* Handle onchange event of the <select>
Riga 73: Riga 73:
handleOnchange: function () {
handleOnchange: function () {
editTools.chooseSection(Number($(this).val()));
editTools.chooseSection(Number($(this).val()));
 
return true;
return true;
},
},
 
/**
/**
* Toggle the currently visible section
* Toggle the currently visible section
Riga 88: Riga 88:
return;
return;
}
}
 
// Making these buttons is a little slow,
// Making these buttons is a little slow,
// If we made them all at once the browser would hang
// If we made them all at once the browser would hang
Riga 98: Riga 98:
editTools.makeButtons($choise);
editTools.makeButtons($choise);
}
}
 
$choise.show();
$choise.show();
$sections.not($choise).hide();
$sections.not($choise).hide();
},
},
 
/**
/**
* Convert the <a onclick> links to buttons in a given section.
* Convert the <a onclick> links to buttons in a given section.
Riga 110: Riga 110:
makeButtons: function ($section) {
makeButtons: function ($section) {
var $links;
var $links;
 
if (!$section.length) {
if (!$section.length) {
return;
return;
}
}
 
$links = $section.find('a');
$links = $section.find('a');
$links.each(function (i, a) {
$links.each(function (i, a) {
var $a, button;
var $a, $button, start, end;
$a = $(a);
$a = $(a);
button = document.createElement('input');
$button = $( '<input>' ).attr( 'type', 'button' );
button.type = 'button';
start = $a.data( 'mw-charinsert-start' );
button.onclick = a.onclick;
end = $a.data( 'mw-charinsert-end' );
button.value = $a.text();
if ( start !== undefined && end !== undefined ) {
$a.replaceWith(button);
$button.click( function( e ) {
e.preventDefault();
$('#wpTextbox1').textSelection('encapsulateSelection', {pre: start, post: end});
} );
} else {
$button[0].onclick = a.onclick;
                                }
$button[0].value = $a.text();
$a.replaceWith($button);
});
});
}
}
 
};
};
 
$(document).ready(editTools.setup);
$(document).ready(editTools.setup);
 
}(jQuery, mediaWiki));
}(jQuery, mediaWiki));

Versione attuale delle 09:48, 30 lug 2021

/**
 * 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 2019-12-17
 */
/*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();
						$('#wpTextbox1').textSelection('encapsulateSelection', {pre: start, post: end});
					} );
				} else {
					$button[0].onclick = a.onclick;
                                }
				$button[0].value = $a.text();
				$a.replaceWith($button);
			});
		}

	};

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

}(jQuery, mediaWiki));