/**
* cdQuickReply
* Quickly support or just reply to messages in CD.
*
* Requires [[c:user:JWBTH/CD|Convenient Discussions]] to work.
* <nowiki>
*/
( function() {
var scriptMessage = 'Поддерживаю.';
var scriptMention = (
location.hostname === 'ru.wikipedia.org'
? ' ([[user:stjn/cdQuickReply.js|cdQuickReply]])'
: ' (cdQuickReply)'
);
// Handle prompt response
function handlePromptResponse( response, comment, cd ) {
if ( response === null ) return;
response = response.trim().replace( / *~{3,5}$/, '' );
response = response || scriptMessage;
if ( !/[\.,\?!]$/.test( response ) ) {
response = response + '.';
}
// Form edit summary
var summaryText = [
'ответ ',
comment.authorName,
( response.length > 0 && response.length < 30 ? ': ' + response + ' (-)' : '' ),
scriptMention
].join( '' );
summaryText = cd.api.buildEditSummary( {
section: comment.section && comment.section.headline,
text: summaryText
} );
// Get page code and reply
var sourcePage = comment.getSourcePage();
sourcePage.loadCode().then( function() {
comment.locateInCode();
var commentText = [
comment.source.replyIndentation,
response,
'~~~~\n'
].join( ' ' );
var modifiedCode = comment.source.modifyContext( {
action: 'reply',
commentCode: commentText
} ).contextCode;
// Do the edit
try {
sourcePage.edit( {
text: modifiedCode,
summary: summaryText,
minor: false,
baserevid: sourcePage.revisionId,
starttimestamp: sourcePage.queryTimestamp
} ).then( function( res ) {
$( '#cd-navPanel-refreshButton' ).click();
mw.loader.using( 'mediawiki.action.view.postEdit', function() {
mw.hook( 'postEdit' ).fire();
} );
} );
} catch ( e ) {
mw.notify(
cd.api.wrap(
'cdQuickReply: комментарий не удалось отправить. Ошибка CD: '
+ ( e.data && e.data.details && e.data.details.message || e )
),
{ type: 'error' }
);
// Open a CD form with entered comment text
comment.reply( {
comment: response,
originalHeadline: comment.section && comment.section.headline,
originalComment: '',
isSummaryAltered: false
} );
}
} );
}
// Prompt formatter
function showQuickReplyPrompt( comment, cd, e ) {
e.preventDefault();
var promptLabel = [
'Краткий ответ ',
comment.author.getName(),
' (по умолчанию: <i>',
scriptMessage,
'</i>)'
].join( '' );
OO.ui.prompt(
$( '<span>' + promptLabel + '</span>' ),
{
size: 'medium',
textInput: { classes: [ 'cdQuickReply-input' ] }
}
).done( function( response ) {
handlePromptResponse( response, comment, cd );
} );
}
// Button formatter
function addQuickReplyButton( item, cd ) {
if ( item.isActionable === false || item.isOwn === true ) return;
if ( item.replyButton === undefined ) return;
var $replyButton = $( item.replyButton.element );
var $button = $( $replyButton.prop( 'outerHTML' ) );
var $buttonTarget = $button;
var position = 'after';
if ( !cd.settings.get( 'reformatComments' ) ) {
$buttonTarget = $button.find( 'a' );
position = 'before';
$buttonTarget.find( '.oo-ui-labelElement-label' ).text( 'Поддержать' );
} else {
$buttonTarget.text( 'Поддержать' );
}
$buttonTarget.on( 'click', function( e ) {
showQuickReplyPrompt( item, cd, e );
} );
$replyButton[ position ]( $button );
}
// Hook for the new format of CD
mw.hook( 'convenientDiscussions.pageReady' ).add( function( cd ) {
if ( !cd.settings.get( 'reformatComments' ) ) return;
cd.comments.forEach( function( item ) {
addQuickReplyButton( item, cd );
} );
} );
// Hook for the old format of CD
mw.hook( 'convenientDiscussions.commentLayerCreated' ).add( function( item ) {
var cd = convenientDiscussions;
if ( cd.settings.get( 'reformatComments' ) ) return;
addQuickReplyButton( item, cd );
} );
}() );
// </nowiki>