MediaWiki:Gadget-Checkbox.js

De Wikipedia, la enciclopedia libre

Nota: Después de guardar, debes refrescar la caché de tu navegador para ver los cambios. Internet Explorer: mantén presionada Ctrl mientras pulsas Actualizar. Firefox: mientras presionas Mayús pulsas el botón Actualizar, (o presiona Ctrl-Shift-R). Los usuarios de Google Chrome y Safari pueden simplemente pulsar el botón Recargar. Para más detalles e instrucciones acerca de otros exploradores, véase Ayuda:Cómo limpiar la caché.

var TemplateCheckbox = {

	init: function () {
		$( '.template-checkbox' ).click( TemplateCheckbox.toggle );
	},

	toggle: function () {
		var checkbox = $( this );

		// Only autoconfirmed users to prevent vandalism
		var groups = mw.config.get( 'wgUserGroups' );
		if ( !groups.includes( 'autoconfirmed' ) ) {
			return;
		}

		// Toggle the checkbox in the frontend
		var check = checkbox.text();
		if ( check ) {
			checkbox.empty();
		} else {
			checkbox.text( '✓' );
		}

		// Figure out the index of the current checkbox
		var index = 0;
		var current = this;
		$( '.template-checkbox' ).each( function () {
			index++;
			if ( current === this ) {
				return false;
			}
		} );

		// Toggle the checkbox in the backend
		var page = mw.config.get( 'wgPageName' );
		new mw.Api().edit( page, function ( revision ) {

			// Replace the nth occurrence of '{{Checkbox'
			var search = '\\{\\{Checkbox\\|?[^}]*';
			var regexp = RegExp( '^(?:[\\s\\S]*?' + search +  '){' + index + '}', 'gi' );
			var text = revision.content.replace( regexp, function ( match ) {
				var replacement = '|✓';
				if ( check ) {
					replacement = '';
				}
				return match.replace( RegExp( search + '$' ), '{{Checkbox' + replacement );
			} );

			// Return the edit params
			return {
				text: text,
				summary: ( check ? 'Uncheck' : 'Check' ),
				minor: true
			};
		} );
	}
};

$( TemplateCheckbox.init );