Diferencia entre revisiones de «MediaWiki:Gadget-WikiSign.js»
Apariencia
Contenido eliminado Contenido añadido
mSin resumen de edición Etiquetas: Reversión manual Revertido |
los cambios dejaron al módulo sin funcionar Etiqueta: Reversión manual |
||
Línea 1: | Línea 1: | ||
( function ( mw, $ ) { |
|||
/** |
|||
* 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 = { |
|||
$( function () { |
|||
/** |
|||
var ws$i18n = { |
|||
* Internationalized messages |
|||
dlgTitle: 'Firma automática', |
|||
* |
|||
fldInput: 'Opcionalmente puedes agregar un comentario', |
|||
* @type {Object} Map from message key to value |
|||
comments: 'Este mensaje acompañará tu firma, por favor no te extiendas demasiado.', |
|||
*/ |
|||
fldSign: 'Firma', |
|||
i18n: { |
|||
btnAccept: 'Firmar', |
|||
dialogTitle: 'Firma automática', |
|||
btnCancel: 'Cancelar', |
|||
commentLabel: 'Opcionalmente puedes agregar un comentario', |
|||
previewWarning: 'No se puede firmar durante la edición o previsualización.' |
|||
commentPlaceholder: 'Este mensaje acompañará tu firma, por favor no te extiendas demasiado.', |
|||
}; |
|||
signatureLabel: 'Firma', |
|||
var ws$options = { |
|||
signAction: 'Firmar', |
|||
classNameButton: '.ui-action-sign', |
|||
cancelAction: 'Cancelar', |
|||
validNamespaces: [ 4, 102 ], |
|||
previewWarning: 'No se puede firmar durante la edición o previsualización.' |
|||
prevSignDefault: '# ', |
|||
}, |
|||
textSignDefault: '~~' + '~~', |
|||
postSignDefault: '', |
|||
}; |
|||
if ( !( ws$options.validNamespaces.includes( mw.config.get('wgNamespaceNumber') ) ) |
|||
/** |
|||
|| !( mw.config.get( 'wgUserName' ) ) |
|||
* Configuration options |
|||
|| !( $( ws$options.classNameButton ).length ) |
|||
* |
|||
) { |
|||
* @type {Object} Map from option key to value |
|||
return; |
|||
*/ |
|||
} |
|||
options: { |
|||
buttonClassName: '.ui-action-sign', |
|||
validNamespaces: [ 4, 10, 102 ], |
|||
signaturePrefix: '# ', |
|||
signatureSuffix: '' |
|||
}, |
|||
if ( $.inArray( mw.config.get('wgAction'), [ 'view' ] ) !== -1 ) |
|||
/** |
|||
{ |
|||
* Initialization script |
|||
mw.loader.using( [ |
|||
*/ |
|||
'mediawiki.api', |
|||
init: function () { |
|||
'oojs-ui-core', |
|||
if ( ! Wikisign.options.validNamespaces.includes( mw.config.get( 'wgNamespaceNumber' ) ) ) { |
|||
'oojs-ui-widgets', |
|||
return; |
|||
'oojs-ui-windows' |
|||
} |
|||
] ) |
|||
let $buttons = $( Wikisign.options.buttonClassName ); |
|||
.then( function() |
|||
if ( ! $buttons.length ) { |
|||
{ |
|||
return; |
|||
$( ws$options.classNameButton ).on( 'click', function( e ) |
|||
} |
|||
{ |
|||
if ( mw.config.get( 'wgAction' ) === 'view' ) { |
|||
var button = $( this ); |
|||
mw.loader.using( [ |
|||
var sectionTitle; |
|||
'mediawiki.api', |
|||
while( button.attr( 'id' ) != 'mw-content-text' ) |
|||
'oojs-ui-core', |
|||
{ |
|||
'oojs-ui-widgets', |
|||
if ( $.inArray( button.prop( 'tagName' ), [ 'H1','H2','H3','H4','H5','H6' ] ) === -1 ) |
|||
'oojs-ui-windows' |
|||
{ |
|||
] ).then( function () { |
|||
button = button.prev().length ? button.prev() : button.parent(); |
|||
$buttons.click( Wikisign.dialog ); |
|||
} |
|||
else |
|||
} |
|||
{ |
|||
if ( mw.config.get( 'wgAction' ) === 'edit' || mw.config.get( 'wgAction' ) === 'submit' ) { |
|||
sectionTitle = button.children( '.mw-headline' ).attr( 'id' ); |
|||
$buttons.click( function ( event ) { |
|||
break; |
|||
mw.notify( Wikisign.i18n.previewWarning ); |
|||
} |
|||
event.preventDefault(); |
|||
} |
|||
return false; |
|||
} ); |
|||
} |
|||
}, |
|||
var windowManager = new OO.ui.WindowManager(); |
|||
/** |
|||
var messageDialog = new OO.ui.MessageDialog(); |
|||
* Build the dialog |
|||
$( 'body' ).append( windowManager.$element ); |
|||
* |
|||
windowManager.addWindows( [ messageDialog ] ); |
|||
* @param {jQuery.Event} Click event |
|||
* @return {bool} false |
|||
*/ |
|||
dialog: function ( event ) { |
|||
let $button = $( this ); |
|||
let sectionCount; |
|||
let sectionTitle; |
|||
let $section = Wikisign.findClosestSection( $button ); |
|||
if ( $section.length ) { |
|||
sectionCount = 1 + $section.prevAll( ':header' ).length; |
|||
sectionTitle = $section.children( '.mw-headline' ).text(); |
|||
} |
|||
let signaturePrefix = $button.data( 'sign-prefix' ) ? $button.data( 'sign-prefix' ) : Wikisign.options.signaturePrefix; |
|||
let signatureSuffix = $button.data( 'sign-suffix' ) ? $button.data( 'sign-suffix' ) : Wikisign.options.signatureSuffix; |
|||
let fullSignature = '\n' + signaturePrefix + signatureSuffix + ' ~~' + '~~'; |
|||
var inputComm = new OO.ui.MultilineTextInputWidget( { |
|||
// If anons are not allowed to sign, exit |
|||
placeholder: ws$i18n.comments, |
|||
if ( ! mw.config.get( 'wgUserName' ) && ! $button.data( 'sign-allow-anons' ) ) { |
|||
rows: 3 |
|||
event.preventDefault(); |
|||
} ); |
|||
var inputSign = new OO.ui.TextInputWidget( { |
|||
} |
|||
value: ws$options.textSignDefault, |
|||
disabled: true |
|||
} ); |
|||
var fieldset = new OO.ui.FieldsetLayout( { |
|||
classes: [ 'container' ], |
|||
} ); |
|||
fieldset.addItems( [ |
|||
new OO.ui.FieldLayout( inputComm, { |
|||
label: ws$i18n.fldInput, |
|||
align: 'top' |
|||
} ), |
|||
new OO.ui.FieldLayout( inputSign, { |
|||
label: ws$i18n.fldSign, |
|||
} ), |
|||
] ); |
|||
var form = new OO.ui.FormLayout( { |
|||
items: [ fieldset ], |
|||
} ); |
|||
var dlg = windowManager.openWindow( messageDialog, { |
|||
// If commenting is disabled, sign immediately |
|||
title: ws$i18n.dlgTitle, |
|||
if ( $button.data( 'sign-no-comment' ) ) { |
|||
message: form.$element, |
|||
Wikisign.sign( fullSignature, sectionCount, sectionTitle ); |
|||
actions: [ |
|||
event.preventDefault(); |
|||
{ label: ws$i18n.btnAccept, action: 'sign', flags: 'progressive' }, |
|||
return false; |
|||
{ label: ws$i18n.btnCancel }, |
|||
} |
|||
], |
|||
size: 'medium', |
|||
} ); |
|||
dlg.closed.then( function ( data ) |
|||
// Build the dialog |
|||
{ |
|||
let windowManager = new OO.ui.WindowManager(); |
|||
if ( data && data.action === 'sign' ) |
|||
let messageDialog = new OO.ui.MessageDialog(); |
|||
{ |
|||
$( 'body' ).append( windowManager.$element ); |
|||
var comments = inputComm.getValue(); |
|||
windowManager.addWindows( [ messageDialog ] ); |
|||
var prevSign; |
|||
let commentInput = new OO.ui.MultilineTextInputWidget( { |
|||
switch ( button.data('prev-sign') ) |
|||
placeholder: Wikisign.i18n.commentPlaceholder, |
|||
{ |
|||
required: $button.data( 'sign-comment-required' ) ? true : false, |
|||
case 'ul': |
|||
case 'bullet': |
|||
} ); |
|||
prevSign = '* '; |
|||
let signatureInput = new OO.ui.TextInputWidget( { |
|||
break; |
|||
value: '~~' + '~~', |
|||
case 'ol': |
|||
case 'number': |
|||
} ); |
|||
prevSign = '# '; |
|||
let fieldset = new OO.ui.FieldsetLayout( { |
|||
break; |
|||
classes: [ 'container' ], |
|||
case 'custom': |
|||
} ).addItems( [ |
|||
prevSign = button.data('prev-sign-custom') || ''; |
|||
new OO.ui.FieldLayout( commentInput, { |
|||
break; |
|||
label: Wikisign.i18n.commentLabel, |
|||
default: |
|||
prevSign = ws$options.prevSignDefault; |
|||
} ), |
|||
} |
|||
new OO.ui.FieldLayout( signatureInput, { |
|||
var textSign; |
|||
label: Wikisign.i18n.signatureLabel, |
|||
switch ( button.data('post-sign') ) |
|||
} ), |
|||
{ |
|||
case '3': |
|||
let form = new OO.ui.FormLayout( { |
|||
case 'name': |
|||
items: [ fieldset ], |
|||
case 'username': |
|||
} ); |
|||
textSign = '~~' + '~'; |
|||
let dialog = windowManager.openWindow( messageDialog, { |
|||
break; |
|||
title: Wikisign.i18n.dialogTitle, |
|||
case '4': |
|||
message: form.$element, |
|||
case 'sign': |
|||
actions: [ |
|||
case 'usersign': |
|||
{ label: Wikisign.i18n.signAction, action: 'sign', flags: 'progressive' }, |
|||
textSign = '~~' + '~~'; |
|||
{ label: Wikisign.i18n.cancelAction }, |
|||
break; |
|||
], |
|||
case 'custom': |
|||
textSign = button.data('post-sign-custom') || '~~' + '~~'; // cannot be empty |
|||
} ); |
|||
break; |
|||
default: |
|||
textSign = ws$options.postSignDefault || '~~' + '~~'; // cannot be empty; |
|||
} |
|||
var postSign; |
|||
switch ( button.data('post-sign') ) |
|||
{ |
|||
case 'custom': |
|||
postSign = button.data('post-sign-custom') || ''; |
|||
break; |
|||
default: |
|||
postSign = ws$options.postSignDefault; |
|||
} |
|||
var fullSign = '\n'; |
|||
if ( prevSign ) |
|||
{ |
|||
fullSign = fullSign + prevSign; |
|||
} |
|||
if ( comments ) |
|||
{ |
|||
fullSign = fullSign + comments + ' ' |
|||
} |
|||
if ( textSign ) |
|||
{ |
|||
fullSign = fullSign + textSign; |
|||
} |
|||
if ( postSign ) |
|||
{ |
|||
fullSign = fullSign + postSign; |
|||
} |
|||
var api = new mw.Api(); |
|||
// Submit the data |
|||
dialog.closed.then( function ( data ) { |
|||
if ( data && data.action === 'sign' ) { |
|||
let comment = commentInput.getValue(); |
|||
if ( comment ) { |
|||
fullSignature = '\n' + signaturePrefix + ' ' + comment + signatureSuffix + ' ~~' + '~~'; |
|||
} |
|||
Wikisign.sign( fullSignature, sectionCount, sectionTitle ); |
|||
} |
|||
windowManager.destroy(); |
|||
} ); |
|||
if( sectionTitle ) |
|||
event.preventDefault(); |
|||
{ |
|||
return false; |
|||
api.get( { |
|||
}, |
|||
format: 'json', |
|||
action: 'parse', |
|||
prop: 'sections', |
|||
page: mw.config.get( 'wgPageName' ) |
|||
} ) |
|||
.then( function ( result ) |
|||
{ |
|||
var sectionParse = result.parse.sections; |
|||
var sectionCount = 1; |
|||
for ( var s in sectionParse ) |
|||
{ |
|||
if ( $.trim( sectionParse[ s ].anchor ) === sectionTitle ) |
|||
{ |
|||
api.get( { |
|||
format: 'json', |
|||
action: 'parse', |
|||
prop: 'wikitext', |
|||
page: mw.config.get( 'wgPageName' ), |
|||
section: sectionCount |
|||
} ) |
|||
.then( function ( result ) |
|||
{ |
|||
var wikitext = result.parse.wikitext[ '*' ]; |
|||
api.post( { |
|||
action: 'edit', |
|||
title: mw.config.get( 'wgPageName' ), |
|||
text: wikitext + fullSign, |
|||
summary: '/' + '* ' + sectionTitle + ' *' + '/ ', |
|||
section: sectionCount, |
|||
token: mw.user.tokens.get( 'csrfToken' ) |
|||
} ).then( function () { |
|||
window.location.reload( true ); |
|||
} ); |
|||
} ); |
|||
break; |
|||
} |
|||
sectionCount++; |
|||
} |
|||
} ); |
|||
} |
|||
else |
|||
{ |
|||
api.get( { |
|||
format: 'json', |
|||
action: 'parse', |
|||
prop: 'wikitext', |
|||
page: mw.config.get( 'wgPageName' ) |
|||
} ) |
|||
.then( function ( result ) |
|||
{ |
|||
var wikitext = result.parse.wikitext[ '*' ]; |
|||
api.post( { |
|||
action: 'edit', |
|||
title: mw.config.get( 'wgPageName' ), |
|||
text: wikitext + fullSign, |
|||
token: mw.user.tokens.get( 'csrfToken' ) |
|||
} ).then( function () { |
|||
window.location.reload( true ); |
|||
} ); |
|||
} ); |
|||
} |
|||
} |
|||
windowManager.destroy(); |
|||
} ); |
|||
e.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 ) { |
|||
let api = new mw.Api(); |
|||
api.get( { |
|||
format: 'json', |
|||
action: 'parse', |
|||
prop: 'wikitext', |
|||
page: mw.config.get( 'wgPageName' ), |
|||
section: sectionCount |
|||
} ).then( function ( result ) { |
|||
let wikitext = result.parse.wikitext['*']; |
|||
api.post( { |
|||
action: 'edit', |
|||
title: mw.config.get( 'wgPageName' ), |
|||
text: wikitext + fullSignature, |
|||
summary: ( sectionTitle ? '/' + '* ' + sectionTitle + ' *' + '/ ' : null ), |
|||
section: sectionCount, |
|||
token: mw.user.tokens.get( 'csrfToken' ) |
|||
} ).then( function () { |
|||
window.location.reload( true ); |
|||
} ); |
} ); |
||
} |
} |
||
else |
|||
}, |
|||
if ( $.inArray( mw.config.get('wgAction'), [ 'edit', 'submit' ] ) !== -1 ) |
|||
{ |
|||
/** |
|||
$( ws$options.classNameButton ).on('click', function( e ) { |
|||
* Helper function to find the closest section |
|||
mw.notify( ws$i18n.previewWarning ); |
|||
* by traversing back and up the DOM tree |
|||
e.preventDefault(); |
|||
* |
|||
return false; |
|||
* @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' ) ) { |
|||
return element; |
|||
} |
|||
var prevSection = $element.prevAll( ':header' ).first(); |
|||
if ( prevSection.length ) { |
|||
return prevSection; |
|||
} |
|||
var parent = $element.parent(); |
|||
return Wikisign.findClosestSection( parent ); |
|||
} |
|||
}; |
|||
}( mediaWiki, jQuery ) ); |
|||
$( Wikisign.init ); |
Revisión del 14:04 2 mar 2022
( function ( mw, $ ) {
$( function () {
var ws$i18n = {
dlgTitle: 'Firma automática',
fldInput: 'Opcionalmente puedes agregar un comentario',
comments: 'Este mensaje acompañará tu firma, por favor no te extiendas demasiado.',
fldSign: 'Firma',
btnAccept: 'Firmar',
btnCancel: 'Cancelar',
previewWarning: 'No se puede firmar durante la edición o previsualización.'
};
var ws$options = {
classNameButton: '.ui-action-sign',
validNamespaces: [ 4, 102 ],
prevSignDefault: '# ',
textSignDefault: '~~' + '~~',
postSignDefault: '',
};
if ( !( ws$options.validNamespaces.includes( mw.config.get('wgNamespaceNumber') ) )
|| !( mw.config.get( 'wgUserName' ) )
|| !( $( ws$options.classNameButton ).length )
) {
return;
}
if ( $.inArray( mw.config.get('wgAction'), [ 'view' ] ) !== -1 )
{
mw.loader.using( [
'mediawiki.api',
'oojs-ui-core',
'oojs-ui-widgets',
'oojs-ui-windows'
] )
.then( function()
{
$( ws$options.classNameButton ).on( 'click', function( e )
{
var button = $( this );
var sectionTitle;
while( button.attr( 'id' ) != 'mw-content-text' )
{
if ( $.inArray( button.prop( 'tagName' ), [ 'H1','H2','H3','H4','H5','H6' ] ) === -1 )
{
button = button.prev().length ? button.prev() : button.parent();
}
else
{
sectionTitle = button.children( '.mw-headline' ).attr( 'id' );
break;
}
}
var windowManager = new OO.ui.WindowManager();
var messageDialog = new OO.ui.MessageDialog();
$( 'body' ).append( windowManager.$element );
windowManager.addWindows( [ messageDialog ] );
var inputComm = new OO.ui.MultilineTextInputWidget( {
placeholder: ws$i18n.comments,
rows: 3
} );
var inputSign = new OO.ui.TextInputWidget( {
value: ws$options.textSignDefault,
disabled: true
} );
var fieldset = new OO.ui.FieldsetLayout( {
classes: [ 'container' ],
} );
fieldset.addItems( [
new OO.ui.FieldLayout( inputComm, {
label: ws$i18n.fldInput,
align: 'top'
} ),
new OO.ui.FieldLayout( inputSign, {
label: ws$i18n.fldSign,
} ),
] );
var form = new OO.ui.FormLayout( {
items: [ fieldset ],
} );
var dlg = windowManager.openWindow( messageDialog, {
title: ws$i18n.dlgTitle,
message: form.$element,
actions: [
{ label: ws$i18n.btnAccept, action: 'sign', flags: 'progressive' },
{ label: ws$i18n.btnCancel },
],
size: 'medium',
} );
dlg.closed.then( function ( data )
{
if ( data && data.action === 'sign' )
{
var comments = inputComm.getValue();
var prevSign;
switch ( button.data('prev-sign') )
{
case 'ul':
case 'bullet':
prevSign = '* ';
break;
case 'ol':
case 'number':
prevSign = '# ';
break;
case 'custom':
prevSign = button.data('prev-sign-custom') || '';
break;
default:
prevSign = ws$options.prevSignDefault;
}
var textSign;
switch ( button.data('post-sign') )
{
case '3':
case 'name':
case 'username':
textSign = '~~' + '~';
break;
case '4':
case 'sign':
case 'usersign':
textSign = '~~' + '~~';
break;
case 'custom':
textSign = button.data('post-sign-custom') || '~~' + '~~'; // cannot be empty
break;
default:
textSign = ws$options.postSignDefault || '~~' + '~~'; // cannot be empty;
}
var postSign;
switch ( button.data('post-sign') )
{
case 'custom':
postSign = button.data('post-sign-custom') || '';
break;
default:
postSign = ws$options.postSignDefault;
}
var fullSign = '\n';
if ( prevSign )
{
fullSign = fullSign + prevSign;
}
if ( comments )
{
fullSign = fullSign + comments + ' '
}
if ( textSign )
{
fullSign = fullSign + textSign;
}
if ( postSign )
{
fullSign = fullSign + postSign;
}
var api = new mw.Api();
if( sectionTitle )
{
api.get( {
format: 'json',
action: 'parse',
prop: 'sections',
page: mw.config.get( 'wgPageName' )
} )
.then( function ( result )
{
var sectionParse = result.parse.sections;
var sectionCount = 1;
for ( var s in sectionParse )
{
if ( $.trim( sectionParse[ s ].anchor ) === sectionTitle )
{
api.get( {
format: 'json',
action: 'parse',
prop: 'wikitext',
page: mw.config.get( 'wgPageName' ),
section: sectionCount
} )
.then( function ( result )
{
var wikitext = result.parse.wikitext[ '*' ];
api.post( {
action: 'edit',
title: mw.config.get( 'wgPageName' ),
text: wikitext + fullSign,
summary: '/' + '* ' + sectionTitle + ' *' + '/ ',
section: sectionCount,
token: mw.user.tokens.get( 'csrfToken' )
} ).then( function () {
window.location.reload( true );
} );
} );
break;
}
sectionCount++;
}
} );
}
else
{
api.get( {
format: 'json',
action: 'parse',
prop: 'wikitext',
page: mw.config.get( 'wgPageName' )
} )
.then( function ( result )
{
var wikitext = result.parse.wikitext[ '*' ];
api.post( {
action: 'edit',
title: mw.config.get( 'wgPageName' ),
text: wikitext + fullSign,
token: mw.user.tokens.get( 'csrfToken' )
} ).then( function () {
window.location.reload( true );
} );
} );
}
}
windowManager.destroy();
} );
e.preventDefault();
return false;
} );
} );
}
else
if ( $.inArray( mw.config.get('wgAction'), [ 'edit', 'submit' ] ) !== -1 )
{
$( ws$options.classNameButton ).on('click', function( e ) {
mw.notify( ws$i18n.previewWarning );
e.preventDefault();
return false;
} );
}
} );
}( mediaWiki, jQuery ) );