Debian Codegraph @

extensions/Thanks/modules/ext.thanks.corethank.js

raw symbols SCIP
1 ( function () {
2 'use strict';
3
4 const attrName = 'data-revision-id';
5
6 function reloadThankedState() {
7 $( 'a.mw-thanks-thank-link' ).each( ( idx, el ) => {
8 const $thankLink = $( el );
9 if ( mw.thanks.thanked.contains( $thankLink.attr( attrName ) ) ) {
10 $thankLink.before(
11 $( '<span>' ).addClass( 'mw-thanks-thank-confirmation' ).text(
12 mw.msg( 'thanks-thanked', mw.user, $thankLink.data( 'recipient-gender' ) ) )
13 );
14 $thankLink.remove();
15 }
16 } );
17 }
18
19 /**
20 * Send thanks
21 *
22 * @param {jQuery} $thankLink The element with the data-revision-id attribute
23 * @param {jQuery} $thankElement The element to be removed on success
24 */
25 function sendThanks( $thankLink, $thankElement ) {
26 if ( $thankLink.data( 'clickDisabled' ) ) {
27 // Prevent double clicks while we haven't received a response from API request
28 return;
29 }
30 $thankLink.data( 'clickDisabled', true );
31
32 let source;
33 // Determine the thank source (history, diff, or log).
34 if ( mw.config.get( 'wgAction' ) === 'history' ) {
35 source = 'history';
36 } else if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Log' ) {
37 source = 'log';
38 } else {
39 source = 'diff';
40 }
41
42 // Construct the API parameters.
43 const apiParams = {
44 action: 'thank',
45 source: source
46 };
47 if ( $thankLink.data( 'log-id' ) ) {
48 apiParams.log = $thankLink.data( 'log-id' );
49 } else {
50 apiParams.rev = $thankLink.data( 'revision-id' );
51 }
52
53 // Send the API request.
54 ( new mw.Api() ).postWithToken( 'csrf', apiParams )
55 .then(
56 // Success
57 () => {
58 $thankElement.before( mw.message( 'thanks-thanked', mw.user, $thankLink.data( 'recipient-gender' ) ).escaped() );
59 $thankElement.remove();
60 mw.thanks.thanked.push( $thankLink.attr( attrName ) );
61 },
62 // Fail
63 ( errorCode ) => {
64 // If error occurred, enable attempting to thank again
65 $thankLink.data( 'clickDisabled', false );
66 let msg;
67 switch ( errorCode ) {
68 case 'invalidrevision':
69 msg = mw.msg( 'thanks-error-invalidrevision' );
70 break;
71 case 'ratelimited':
72 msg = mw.msg( 'thanks-error-ratelimited', mw.user );
73 break;
74 case 'revdeleted':
75 msg = mw.msg( 'thanks-error-revdeleted' );
76 break;
77 default:
78 msg = mw.msg( 'thanks-error-undefined', errorCode );
79 }
80 OO.ui.alert( msg );
81 }
82 );
83 }
84
85 /**
86 * Add interactive handlers to all 'thank' links in $content
87 *
88 * @param {jQuery} $content
89 */
90 function addActionToLinks( $content ) {
91 const $thankLinks = $content.find( 'a.mw-thanks-thank-link' );
92 if ( mw.config.get( 'thanks-confirmation-required' ) ) {
93 $thankLinks.each( function () {
94 const $thankLink = $( this );
95 $thankLink.confirmable( {
96 i18n: {
97 confirm: mw.msg( 'thanks-confirmation2', mw.user ),
98 no: mw.msg( 'cancel' ),
99 noTitle: mw.msg( 'thanks-thank-tooltip-no', mw.user ),
100 yes: mw.msg( 'thanks-button-thank', mw.user, $thankLink.data( 'recipient-gender' ) ),
101 yesTitle: mw.msg( 'thanks-thank-tooltip-yes', mw.user )
102 },
103 handler: function ( e ) {
104 e.preventDefault();
105 sendThanks( $thankLink, $thankLink.closest( '.jquery-confirmable-wrapper' ) );
106 }
107 } );
108 } );
109 } else {
110 $thankLinks.on( 'click', function ( e ) {
111 const $thankLink = $( this );
112 e.preventDefault();
113 sendThanks( $thankLink, $thankLink );
114 } );
115 }
116 }
117
118 if ( $.isReady ) {
119 // This condition is required for soft-reloads
120 // to also trigger the reloadThankedState
121 reloadThankedState();
122 } else {
123 $( reloadThankedState );
124 }
125
126 $( () => {
127 addActionToLinks( $( 'body' ) );
128 } );
129
130 mw.hook( 'wikipage.diff' ).add( ( $content ) => {
131 addActionToLinks( $content );
132 } );
133
134 mw.hook( 'wikipage.content' ).add( ( $content ) => {
135 addActionToLinks( $content );
136 } );
137 }() );