Debian Codegraph @

extensions/ConfirmEdit/resources/libs/ext.confirmEdit.CaptchaInputWidget.js

raw symbols SCIP
1 mw.libs.confirmEdit = {};
2
3 /**
4 * @class
5 * @extends OO.ui.TextInputWidget
6 *
7 * @constructor
8 * @param {Object} [captchaData] Value of 'captcha' property returned from action=edit API
9 * @param {Object} [config] Configuration options
10 */
11 mw.libs.confirmEdit.CaptchaInputWidget = function MwWidgetsCaptchaInputWidget( captchaData, config ) {
12 config = config || {};
13
14 // Parent constructor
15 mw.libs.confirmEdit.CaptchaInputWidget.parent.call( this, config );
16
17 // Properties
18 this.$captchaImg = null;
19 this.captchaId = null;
20
21 // Initialization
22 this.$element.addClass( 'mw-confirmEdit-captchaInputWidget' );
23 this.$element.prepend( this.makeCaptchaInterface( captchaData ) );
24 };
25
26 /* Setup */
27
28 OO.inheritClass( mw.libs.confirmEdit.CaptchaInputWidget, OO.ui.TextInputWidget );
29
30 /* Events */
31
32 /**
33 * @event load
34 *
35 * A load event is emitted when the CAPTCHA image loads.
36 */
37
38 /* Methods */
39
40 mw.libs.confirmEdit.CaptchaInputWidget.prototype.makeCaptchaInterface = function ( captchaData ) {
41 let $captchaImg, msg, question;
42
43 const $captchaParagraph = $( '<div>' ).append(
44 $( '<strong>' ).text( mw.msg( 'captcha-label' ) ),
45 document.createTextNode( mw.msg( 'colon-separator' ) )
46 );
47 const $captchaDiv = $( '<div>' ).append( $captchaParagraph );
48
49 if ( captchaData.url ) {
50 // FancyCaptcha
51 // Based on FancyCaptcha::getFormInformation() and ext.confirmEdit.fancyCaptcha.js
52 this.$input.attr( 'placeholder', mw.msg( 'fancycaptcha-imgcaptcha-ph' ) );
53 mw.loader.load( 'ext.confirmEdit.fancyCaptcha' );
54 $captchaDiv.addClass( 'fancycaptcha-captcha-container' );
55 $captchaParagraph.append( mw.message( 'fancycaptcha-edit' ).parseDom() );
56 $captchaImg = $( '<img>' )
57 .attr( 'src', captchaData.url )
58 .data( 'captchaId', captchaData.id )
59 .addClass( 'fancycaptcha-image' )
60 .on( 'load', this.emit.bind( this, 'load' ) );
61 $captchaDiv.append(
62 $captchaImg,
63 ' ',
64 $( '<a>' ).addClass( 'fancycaptcha-reload' ).text( mw.msg( 'fancycaptcha-reload-text' ) )
65 );
66 } else {
67 if ( captchaData.type === 'simple' ) {
68 msg = 'captcha-edit';
69 } else if ( captchaData.type === 'question' ) {
70 // QuestyCaptcha
71 msg = 'questycaptcha-edit';
72 }
73
74 if ( msg ) {
75 switch ( captchaData.mime ) {
76 case 'text/html':
77 question = $.parseHTML( captchaData.question );
78 break;
79 case 'text/plain':
80 question = document.createTextNode( captchaData.question );
81 break;
82 }
83 // Messages documented above
84 // eslint-disable-next-line mediawiki/msg-doc
85 $captchaParagraph.append( mw.message( msg ).parseDom(), '<br>', question );
86 }
87 }
88
89 if ( $captchaImg ) {
90 this.$captchaImg = $captchaImg;
91 } else {
92 this.captchaId = captchaData.id;
93 }
94
95 return $captchaDiv;
96 };
97
98 mw.libs.confirmEdit.CaptchaInputWidget.prototype.getCaptchaId = function () {
99 // 'ext.confirmEdit.fancyCaptcha' can update this value if the "Refresh" button is used
100 return this.$captchaImg ? this.$captchaImg.data( 'captchaId' ) : this.captchaId;
101 };
102
103 mw.libs.confirmEdit.CaptchaInputWidget.prototype.getCaptchaWord = function () {
104 return this.getValue();
105 };