MediaWiki:Gadget-WikiSign.js
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é.
/**
* WikiSign is a tool for easy signing and voting in Wikipedia and other wikis
* Authors: User:Sophivorus & User:Leoncastro
* License: https://creativecommons.org/licenses/by-sa/3.0/
*/
window.WikiSign = {
/**
* Internationalized messages
*
* @type {Object} Map from message key to value
*/
i18n: {
dialogTitle: 'Firma automática',
commentLabel: 'Opcionalmente puedes agregar un comentario',
commentPlaceholder: 'Este mensaje acompañará tu firma, por favor no te extiendas demasiado.',
signatureLabel: 'Firma',
signAction: 'Firmar',
signManuallyAction: 'Firmar manualmente',
cancelAction: 'Cancelar',
previewWarning: 'No se puede firmar durante la edición o previsualización.',
summary: 'Agrego mi firma',
error: 'Hubo un error con la firma automática.'
},
/**
* Configuration options
*
* @type {Object} Map from option key to value
*/
options: {
buttonClassName: '.ui-action-sign',
validNamespaces: [ 4, 10, 102, 2 ],
signaturePrefix: '# ',
signatureSuffix: ''
},
/**
* Initialization script
*/
init: function () {
if ( ! WikiSign.options.validNamespaces.includes( mw.config.get( 'wgNamespaceNumber' ) ) ) {
return;
}
var $buttons = $( WikiSign.options.buttonClassName );
if ( ! $buttons.length ) {
return;
}
if ( mw.config.get( 'wgAction' ) === 'view' ) {
mw.loader.using( [
'mediawiki.api',
'mediawiki.user',
'oojs-ui-core',
'oojs-ui-widgets',
'oojs-ui-windows'
] ).then( function () {
$buttons.click( WikiSign.dialog );
} );
}
if ( mw.config.get( 'wgAction' ) === 'edit' || mw.config.get( 'wgAction' ) === 'submit' ) {
$buttons.click( function ( event ) {
mw.notify( WikiSign.i18n.previewWarning );
event.preventDefault();
return false;
} );
}
},
/**
* Build the dialog
*
* @param {jQuery.Event} Click event
* @return {bool} false
*/
dialog: function ( event ) {
var $button = $( this );
var sectionCount;
var sectionTitle;
var $section = WikiSign.findClosestSection( $button );
if ( $section ) {
sectionCount = 1 + $section.prevAll( ':header, .mw-heading' ).length;
sectionTitle = $section.children( '.mw-headline' ).text();
}
var signaturePrefix = $button.data( 'sign-prefix' ) ? $button.data( 'sign-prefix' ) : WikiSign.options.signaturePrefix;
var signatureSuffix = $button.data( 'sign-suffix' ) ? $button.data( 'sign-suffix' ) : WikiSign.options.signatureSuffix;
var fullSignature = '\n' + signaturePrefix + signatureSuffix + '~~' + '~~';
// Anons cannot edit via JavaScript
// so send them to edit manually
if ( ! mw.config.get( 'wgUserName' ) ) {
window.location.href = mw.util.getUrl( null, {
action: 'edit',
section: sectionCount ? sectionCount : null
} );
event.preventDefault();
return false;
}
// If commenting is disabled, sign immediately
if ( $button.data( 'sign-no-comment' ) ) {
WikiSign.sign( fullSignature, sectionCount, sectionTitle );
event.preventDefault();
return false;
}
// Build the dialog
var windowManager = new OO.ui.WindowManager();
var messageDialog = new OO.ui.MessageDialog();
$( 'body' ).append( windowManager.$element );
windowManager.addWindows( [ messageDialog ] );
var commentInput = new OO.ui.MultilineTextInputWidget( {
placeholder: WikiSign.i18n.commentPlaceholder,
required: $button.data( 'sign-comment-required' ) ? true : false,
rows: 3
} );
var signatureInput = new OO.ui.TextInputWidget( {
value: '~~' + '~~',
disabled: true
} );
var fieldset = new OO.ui.FieldsetLayout( {
classes: [ 'container' ],
} ).addItems( [
new OO.ui.FieldLayout( commentInput, {
label: WikiSign.i18n.commentLabel,
align: 'top'
} ),
new OO.ui.FieldLayout( signatureInput, {
label: WikiSign.i18n.signatureLabel,
} ),
] );
var form = new OO.ui.FormLayout( {
items: [ fieldset ],
} );
var dialog = windowManager.openWindow( messageDialog, {
title: WikiSign.i18n.dialogTitle,
message: form.$element,
actions: [
{ label: WikiSign.i18n.signAction, action: 'sign', flags: 'progressive' },
{ label: WikiSign.i18n.cancelAction },
],
size: 'medium',
} );
// Submit the data
dialog.closed.then( function ( data ) {
if ( data && data.action === 'sign' ) {
var comment = commentInput.getValue();
if ( comment ) {
fullSignature = '\n' + signaturePrefix + ' ' + comment + signatureSuffix + ' ~~' + '~~';
}
WikiSign.sign( fullSignature, sectionCount, sectionTitle );
}
windowManager.destroy();
} );
event.preventDefault();
return false;
},
/**
* Append the signature to the section or page
* @todo What if the button or some text is AFTER the list of signatures
*
* @param {string} Full signature to append
* @param {number} Section number where to append the signature
* @param {string} Section title where to append the signature
*/
sign: function ( fullSignature, sectionCount, sectionTitle ) {
var api = new mw.Api();
api.get( {
format: 'json',
formatversion: 2,
action: 'parse',
prop: 'wikitext',
page: mw.config.get( 'wgPageName' ),
section: sectionCount
} ).then( function ( result ) {
var wikitext = result.parse.wikitext;
var summary = sectionTitle ? '/' + '* ' + sectionTitle + ' *' + '/ ' + WikiSign.i18n.summary : WikiSign.i18n.summary;
api.postWithEditToken( {
action: 'edit',
title: mw.config.get( 'wgPageName' ),
text: wikitext + fullSignature,
summary: summary,
section: sectionCount,
} ).then( function ( data ) {
// If we reach this point, all went well
// so reload the page to show the signature to the user
window.location.reload( true );
} ).fail( function ( errorCode ) {
// If we reach this point, something went wrong
// but rather than show an unhelpful error code
// prompt the user to sign manually
var windowManager = new OO.ui.WindowManager();
var messageDialog = new OO.ui.MessageDialog();
$( 'body' ).append( windowManager.$element );
windowManager.addWindows( [ messageDialog ] );
var errorDialog = windowManager.openWindow( messageDialog, {
message: WikiSign.i18n.error,
actions: [
{ label: WikiSign.i18n.signManuallyAction, action: 'signManually', flags: 'progressive' },
{ label: WikiSign.i18n.cancelAction }
]
} );
errorDialog.closed.then( function ( data ) {
if ( data && data.action === 'signManually' ) {
window.location.href = mw.util.getUrl( null, {
action: 'edit',
section: sectionCount ? sectionCount : null
} );
}
windowManager.destroy();
} );
} );
} );
},
/**
* Helper function to find the closest section
* by traversing back and up the DOM tree
*
* @param {jQuery object} Starting element
* @return {jQuery object} Closest section
*/
findClosestSection: function ( $element ) {
if ( $element.attr( 'id' ) === 'mw-content-text' ) {
return;
}
if ( $element.is( ':header, .mw-heading' ) ) {
return $element;
}
var $previous = $element.prevAll( ':header, .mw-heading' ).first();
if ( $previous.length ) {
return $previous;
}
var $parent = $element.parent();
return WikiSign.findClosestSection( $parent );
}
};
$( WikiSign.init );