MediaWiki:Gadget-botonera.js

De Wikilibros, la colección de libros de texto de contenido libre.

Nota: Después de publicar, quizás necesite actualizar la caché de su navegador para ver los cambios.

  • Firefox/Safari: Mantenga presionada la tecla Shift mientras pulsa el botón Actualizar, o presiona Ctrl+F5 o Ctrl+R (⌘+R en Mac)
  • Google Chrome: presione Ctrl+Shift+R (⌘+Shift+R en Mac)
  • Internet Explorer/Edge: mantenga presionada Ctrl mientras pulsa Actualizar, o presione Ctrl+F5
  • Opera: Presiona Ctrl+F5.
/**
 * Botonera
 * <nowiki>
 */
/*jslint eqeq: true, vars: true, plusplus: true, white: true, browser: true*/
/*global jQuery, mediaWiki*/

// Protege contra doble inclusión, desactiva en páginas que no sean de edición
if ( window.botonera === undefined && $.inArray( mw.config.get( 'wgAction' ), ['edit', 'submit'] ) > -1 ) {

	//Global
	var botonera = {};

	//Enlaza con window
	window.botonera = botonera;

	//Scope local para no contaminar espacio global
	(function ( $, mw ) {
		'use strict';

		/**
		 * Main
		 */
		var queue = [],
			krInsertWikiEditorButton,
			queueButton,
			handleQueue,
			//insertButton,
			ejecutar;

		/**
		 * krInsertWikiEditorButton
		 * Insert WikiEditor Button
		 * @created 2011-06-05
		 * @source meta.wikimedia.org/wiki/User:Krinkle/Scripts/InsertWikiEditorButton
		 * @version 0.3.0 (2012-03-12)
		 * @author Krinkle, 2011 - 2012
		 * @author Locos epraix, 2012
		 * @license Released in the public domain
		 * @param options {Object} An object with options:
		 * - section {String} (optional) The name of the section in the WikiEditor. Defaults to 'main'
		 * - group {String} (optional) The name of the group in the WikiEditor. Defaults to 'insert'
		 * - id {String} (required) Unique id (ie. 'my-button')
		 * - icon {String} (recommended) URL to the icon, should be square about 21 to 22px
		 * - label {String} (required) Tooltip displayed when hovering button
		 * - callBackPrev {Function} (optional) Called when the button is clicked, executed before the string manipulation stuff
		 * - insertBefore {String} (optional) Wikitext to be inserted before the cursor on-click
		 * - sampleText {String} (optional) Text inserted in place of the cursor if no text was selected
		 * - insertAfter {String} (optional) Wikitext to be inserted after the cursor on-click
		 * - ownline {Boolean} (optional) Specifies if the inserted text go in it's own line. Defaults to 'false'
		 * - callback {Function} (optional) Called when the button is clicked
		 * - autoSummary {mixed} (optional) Null or an Object with the following properties:
		 *   - summary {String} (required) Edit summary that should be used
		 *   - position {String} (optional) 'append', 'prepend' or 'replace'
		 *   - delimiter {String} (optional) delimiter between the (possibly) current summary and the to-be-inserted summary
		 */
		krInsertWikiEditorButton = function( options ) {
			//options y chequeo de id y label movido a queueButton() para evitar duplicado
			var cajaTexto1 = $( '#wpTextbox1' ),
				btnObj = {
					'section': options.section,
					'group': options.group,
					'tools': {}
			};
			btnObj.tools[options.id] = {
					label: options.label,
					type: 'button',
					icon: options.icon,
					action: {
						type: 'callback',
						execute: function () {
							// Callback
							if ( $.isFunction( options.callbackPrev ) ) {
								options.callbackPrev();
							}
							// encapsulateSelection
							cajaTexto1.textSelection( 'encapsulateSelection', {
								pre: options.insertBefore,
								peri: options.sampleText,
								post: options.insertAfter,
								ownline: options.ownline
							});
							// Auto summary
							if ( options.autoSummary && options.autoSummary.summary ) {
								var $summary = $('#wpSummary'),
									currentSum = $summary.val();
								if ( $.isEmpty( currentSum ) ) {
									$summary.val( options.autoSummary.summary );
								} else {
									switch ( options.autoSummary.position ) {
									case 'prepend':
										$summary.val(
												options.autoSummary.summary +
												options.autoSummary.delimiter +
												currentSum
										);
										break;
									case 'replace':
										$summary.val( options.autoSummary.summary );
										break;
									default: // 'append'
										$summary.val(
												currentSum +
												options.autoSummary.delimiter +
												options.autoSummary.summary
										);
									}
								}
							}
							// Callback
							if ( $.isFunction( options.callback ) ) {
								options.callback();
							}
						}
					}
			};
			cajaTexto1.wikiEditor( 'addToToolbar', btnObj );
		};

		queueButton = function( options ) {
			// Defaults
			options = $.extend( {
				'section': 'main',
				'group': 'insert',
				'id': null,
				'icon': '//upload.wikimedia.org/wikipedia/commons/thumb/f/f0/Toolbaricon_bold_%21.png/21px-Toolbaricon_bold_%21.png',
				'iconOld': '//upload.wikimedia.org/wikipedia/commons/e/ec/Button_base.png',
				'label': '',
				'callbackPrev': null,
				'insertBefore': '',
				'sampleText': '',
				'insertAfter': '',
				'ownline': false,
				'callback': null,
				'autoSummary': {
					'summary': null,
					'position': 'append',
					'delimiter': '; '
				}
			}, options );
			// Required
			if ( !options.id || !options.label ) {
				return false;
			}
			queue.push( options );
		};

		handleQueue = function() {
			var i;
			for (i = 0; i < queue.length; i += 1) {
				botonera.insertButton( queue[i] );
			}
			queue = [];
		};

		/**
		 * Función expuesta para el que quiera añadir botones adicionales
		 */
		botonera.insertButton = function( btnObj ) {
			if( mw.user.options.get( 'usebetatoolbar' ) ) {
				mw.loader.using( 'ext.wikiEditor', function () {
					krInsertWikiEditorButton( btnObj );
				} );
			} else {
				mw.loader.using( 'mediawiki.action.edit', function() {
					mw.toolbar.addButton( btnObj.iconOld || btnObj.icon, btnObj.label, btnObj.insertBefore,
						btnObj.insertAfter, btnObj.sampleText, btnObj.id);
				} );
			}
		};

		/**
		 * Aux
		 */
		var selectionStart,
			selectionEnd;

		function extraEscucharSeleccion() {
			var cajaTexto1 = $( '#wpTextbox1' );
			cajaTexto1.on( "select", function () {
				selectionStart = cajaTexto1[0].selectionStart;
				selectionEnd = cajaTexto1[0].selectionEnd;
			});
		}

		function extraCambiarCase() {
			var cajaTexto1 = $( '#wpTextbox1' ),
				selr = cajaTexto1.val().length - selectionEnd,
				selt = cajaTexto1.val().substring( selectionStart, selectionEnd ),
				temp;

			if (selectionEnd > selectionStart) {
				if (selt == selt.toUpperCase()) {
					selt = selt.toLowerCase();
				} else if (selt == selt.toLowerCase() && ( selectionEnd - selectionStart ) > 1) {
					selt = selt.substring(0, 1).toUpperCase() + selt.substring(1).toLowerCase();
				} else {
					selt = selt.toUpperCase();
				}

				cajaTexto1.val(
					cajaTexto1.val().substring( 0, selectionStart ) +
					selt +
					cajaTexto1.val().substring( selectionEnd ) );
				cajaTexto1[0].selectionStart = selectionStart;
				if ( selectionEnd > selectionStart ) {
					cajaTexto1[0].selectionEnd = cajaTexto1.val().length - selr;
				} else {
					temp = cajaTexto1[0].selectionEnd;
					cajaTexto1[0].selectionEnd = selectionStart;
					cajaTexto1[0].selectionStart = temp;
				}
			}
		}

		/*
		function extraAvisoUsuario() {
			btnAviso.avisoStr = window.prompt(
				"Tipo de aviso a usuario:\n" + "prueba : Prueba\n" +
				"prueba0a : Wikietiqueta\n" + "prueba0b : Estilo\n" + "prueba0c : Spam\n" +
				"prueba2 : Sin sentido\n" + "prueba2a : Blanqueo\n" + "prueba3 : Detente\n" +
				"prueba4 : Última advertencia\n" + "prueba5 : Bloqueo"
			);
		}*/

		function extraPreview() {
			$( '#wpPreview' ).click();
		}

		function extraGrabar() {
			$( '#editform' ).submit();
		}

		function extraCateg() {
			var cajaTexto1 = $( '#wpTextbox1' ),
			    texto = cajaTexto1.val(),
			    indice = texto.search( /\[\[Categoría:/ ),
			    nombrecat;

			nombrecat = window.prompt( 'Nombre de la categoría:' );
			if (indice == -1) {
				cajaTexto1.val( cajaTexto1.val() + '\n[[Categoría:' + nombrecat + ']]' );
			} else {
				var nuevotexto = texto.substr( 0, indice ) + '\n[[Categoría:' + nombrecat + ']]' +
					'\n' + texto.substr( indice, texto.length );
				cajaTexto1.val( nuevotexto );
			}
		}

		function extraArchivarConsultaBorrado() {
			var cajaTexto1 = $( '#wpTextbox1' );
			cajaTexto1.val( cajaTexto1.val() + '\n{{cierracdb-ab}}' );
			cajaTexto1[0].selectionStart = 0;
			cajaTexto1[0].selectionEnd = 0;
		}

		ejecutar = function () {
			var cajaTexto1 = $( '#wpTextbox1' );
			/**
			 * Añadiendo secciones para el wikiEditor y rutina para el caseChanger
			 * Miscelánea
			 * Mantenimiento
			 */
			if( mw.user.options.get( 'usebetatoolbar' ) ) {
				extraEscucharSeleccion();
				mw.loader.using( 'ext.wikiEditor', function () {
					cajaTexto1.wikiEditor( 'addToToolbar', {
						'section': 'advanced',
						'groups': {
							'mw-botonera-miscelanea': {
								'label': 'Miscelánea'
							}
						}
					});
					cajaTexto1.wikiEditor( 'addToToolbar', {
						'section': 'advanced',
						'groups': {
							'mw-botonera-mantenimiento': {
								'label': 'Mantenimiento'
							}
						}
					});
				} );
			}

                      queueButton({
				'id': 'mw-botonera-tachado',
				'section': 'advanced',
				'group': 'format',
				'icon': '//upload.wikimedia.org/wikipedia/commons/f/f9/Toolbaricon_regular_S_stroke.png',
				'iconOld': '//upload.wikimedia.org/wikipedia/commons/3/30/Btn_toolbar_rayer.png',
				'label': 'Texto tachado',
				'insertBefore': '<s>',
				'sampleText': 'Texto tachado',
				'insertAfter': '</s>'
			});

			queueButton({
				'id': 'mw-botonera-subrayado',
				'section': 'advanced',
				'group': 'format',
				'icon': '//upload.wikimedia.org/wikipedia/commons/1/13/Toolbaricon_regular_U_underline.png',
				'iconOld': '//upload.wikimedia.org/wikipedia/commons/f/fd/Button_underline.png',
				'label': 'Texto subrayado',
				'insertBefore': '<u>',
				'sampleText': 'Texto subrayado',
				'insertAfter': '</u>'
			});

                 if ( mw.config.get( 'wgNamespaceNumber' ) == 0) {
                       queueButton({
					'id': 'mw-botonera-awikipedia',
					'section': 'advanced',
					'group': 'mw-botonera-mantenimiento',
					'icon': '//upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png',
					'label': 'A Wikipedia',
					'insertBefore': '{{A Wikipedia}}',
					'autoSummary': {
					'summary': 'Trasladar a Wikipedia'
					}
				});

                         queueButton({
					'id': 'mw-botonera-renombrar',
					'section': 'advanced',
					'group': 'mw-botonera-mantenimiento',
					'icon': '//upload.wikimedia.org/wikipedia/commons/c/cf/Icono_listas.png',
					'label': 'Renombrar',
					'insertBefore': '{{Corregir nombrado}}',
					'autoSummary': {
					'summary': 'Cambiar el nombre por otro más adecuado'
					}
				});
                          queueButton({
					'id': 'mw-botonera-integrar-wikilibro',
					'section': 'advanced',
					'group': 'mw-botonera-mantenimiento',
					'icon': '//upload.wikimedia.org/wikipedia/commons/f/fb/Book_important.svg',
					'label': 'Renombrar',
					'insertBefore': '{{Integrar al wikilibro}}',
					'autoSummary': {
					'summary': 'Integrar a un wikilibro'
					}
				});
                     queueButton({
					'id': 'mw-botonera-borrar',
					'section': 'advanced',
					'group': 'mw-botonera-mantenimiento',
					'icon': '//upload.wikimedia.org/wikipedia/commons/f/f1/Stop_hand_nuvola.svg',
					'label': 'Renombrar',
					'insertBefore': '{{Borrar|ingrese el motivo}}',
					'autoSummary': {
					'summary': 'Borrar'
					}
				});
                             queueButton({
					'id': 'mw-botonera-desarrollo',
					'section': 'advanced',
					'group': 'mw-botonera-mantenimiento',
					'icon': '//upload.wikimedia.org/wikipedia/commons/d/d1/Toolbar_indevelopment.png',
					'label': 'En Desarrollo',
					'insertBefore': '{{En desarrollo}}',
					'autoSummary': {
					'summary': 'Borrar'
					}
				});

                             queueButton({
					'id': 'mw-botonera-desarrollo',
					'section': 'advanced',
					'group': 'mw-botonera-mantenimiento',
					'icon': '//upload.wikimedia.org/wikipedia/commons/e/ec/Traducci%C3%B3n.png',
					'label': 'En traducción',
					'insertBefore': '{{traduciendo|desde donde|adonde|codigo de idioma}}',
					'autoSummary': {
					'summary': 'En traducción'
					}
				});
                             queueButton({
					'id': 'mw-botonera-desarrollo',
					'section': 'advanced',
					'group': 'mw-botonera-mantenimiento',
					'icon': '//upload.wikimedia.org/wikipedia/commons/f/fb/Book_important.svg',
					'label': 'Esbozo',
					'insertBefore': '{{Esbozo}}',
					'autoSummary': {
					'summary': 'Esbozo'
					}
				});

                           queueButton({
					'id': 'mw-botonera-fuenteprimaria',
					'section': 'advanced',
					'group': 'mw-botonera-mantenimiento',
					'icon': '//upload.wikimedia.org/wikipedia/commons/9/92/Torchlight_help_yellow.png',
					'label': 'Fuente primaria',
					'insertBefore': '{{fuente primaria}}',
					'autoSummary': {
					'summary': 'Fuente primaria'
					}
				});

                           queueButton({
					'id': 'mw-botonera-fusionar',
					'section': 'advanced',
					'group': 'mw-botonera-mantenimiento',
					'icon': '//upload.wikimedia.org/wikipedia/commons/5/52/Merge-arrows.svg',
					'label': 'Fusionar',
					'insertBefore': '{{fusionar}}',
					'autoSummary': {
					'summary': 'Fusionar'
					}
				});
                       }
			handleQueue();
		};

		$( document ).ready( ejecutar );

	})( jQuery, mediaWiki ); // Fin de función anónima
} //fin de chequeo
// </nowiki>