1 /* eslint-disable no-use-before-define */
2 ( function ( $, mw
, OO
) {
4 var ApiSandbox
, Util
, WidgetMethods
, Validators
,
5 $content
, panel
, booklet
, oldhash
, windowManager
, fullscreenButton
,
10 suppressErrors
= true,
11 updatingBooklet
= false,
18 getApiValue: function () {
19 return this.getValue();
21 setApiValue: function ( v
) {
22 if ( v
=== undefined ) {
23 v
= this.paramInfo
[ 'default' ];
27 apiCheckValid: function () {
29 return this.getValidity().then( function () {
30 return $.Deferred().resolve( true ).promise();
32 return $.Deferred().resolve( false ).promise();
33 } ).done( function ( ok
) {
34 ok
= ok
|| suppressErrors
;
35 that
.setIcon( ok
? null : 'alert' );
36 that
.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
41 dateTimeInputWidget
: {
42 getValidity: function () {
43 if ( !Util
.apiBool( this.paramInfo
.required
) || this.getApiValue() !== '' ) {
44 return $.Deferred().resolve().promise();
46 return $.Deferred().reject().promise();
52 alertTokenError: function ( code
, error
) {
53 windowManager
.openWindow( 'errorAlert', {
55 'apisandbox-results-fixtoken-fail', this.paramInfo
.tokentype
61 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
67 fetchToken: function () {
69 return api
.getToken( this.paramInfo
.tokentype
)
70 .done( this.setApiValue
.bind( this ) )
71 .fail( this.alertTokenError
.bind( this ) )
72 .always( this.popPending
.bind( this ) );
74 setApiValue: function ( v
) {
75 WidgetMethods
.textInputWidget
.setApiValue
.call( this, v
);
76 if ( v
=== '123ABC' ) {
83 getApiValueForDisplay: function () {
89 getApiValue: function () {
90 return this.getValue() ? 1 : undefined;
92 setApiValue: function ( v
) {
93 this.setValue( Util
.apiBool( v
) );
95 apiCheckValid: function () {
96 return $.Deferred().resolve( true ).promise();
101 getApiValue: function () {
102 var item
= this.getMenu().getSelectedItem();
103 return item
=== null ? undefined : item
.getData();
105 setApiValue: function ( v
) {
106 var menu
= this.getMenu();
108 if ( v
=== undefined ) {
109 v
= this.paramInfo
[ 'default' ];
111 if ( v
=== undefined ) {
114 menu
.selectItemByData( String( v
) );
117 apiCheckValid: function () {
118 var ok
= this.getApiValue() !== undefined || suppressErrors
;
119 this.setIcon( ok
? null : 'alert' );
120 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
121 return $.Deferred().resolve( ok
).promise();
126 getApiValue: function () {
127 var items
= this.getItemsData();
128 if ( items
.join( '' ).indexOf( '|' ) === -1 ) {
129 return items
.join( '|' );
131 return '\x1f' + items
.join( '\x1f' );
134 setApiValue: function ( v
) {
135 if ( v
=== undefined || v
=== '' || v
=== '\x1f' ) {
136 this.setItemsFromData( [] );
139 if ( v
.indexOf( '\x1f' ) !== 0 ) {
140 this.setItemsFromData( v
.split( '|' ) );
142 this.setItemsFromData( v
.substr( 1 ).split( '\x1f' ) );
146 apiCheckValid: function () {
147 var ok
= this.getApiValue() !== undefined || suppressErrors
;
148 this.setIcon( ok
? null : 'alert' );
149 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
150 return $.Deferred().resolve( ok
).promise();
155 getApiValue: function () {
156 return this.isDisabled() ? undefined : this.widget
.getApiValue();
158 setApiValue: function ( v
) {
159 this.setDisabled( v
=== undefined );
160 this.widget
.setApiValue( v
);
162 apiCheckValid: function () {
163 if ( this.isDisabled() ) {
164 return $.Deferred().resolve( true ).promise();
166 return this.widget
.apiCheckValid();
172 single: function () {
173 var v
= this.isDisabled() ? this.paramInfo
[ 'default' ] : this.getApiValue();
174 return v
=== undefined ? [] : [ { value
: v
, path
: this.paramInfo
.submodules
[ v
] } ];
177 var map
= this.paramInfo
.submodules
,
178 v
= this.isDisabled() ? this.paramInfo
[ 'default' ] : this.getApiValue();
179 return v
=== undefined || v
=== '' ? [] : $.map( String( v
).split( '|' ), function ( v
) {
180 return { value
: v
, path
: map
[ v
] };
186 getApiValueForDisplay: function () {
189 getApiValue: function () {
190 return this.getValue();
192 setApiValue: function () {
195 apiCheckValid: function () {
196 var ok
= this.getValue() !== null || suppressErrors
;
197 this.setIcon( ok
? null : 'alert' );
198 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
199 return $.Deferred().resolve( ok
).promise();
205 generic: function () {
206 return !Util
.apiBool( this.paramInfo
.required
) || this.getApiValue() !== '';
211 * @class mw.special.ApiSandbox.Utils
216 * Fetch API module info
218 * @param {string} module Module to fetch data for
219 * @return {jQuery.Promise}
221 fetchModuleInfo: function ( module
) {
223 deferred
= $.Deferred();
225 if ( moduleInfoCache
.hasOwnProperty( module
) ) {
227 .resolve( moduleInfoCache
[ module
] )
228 .promise( { abort: function () {} } );
230 apiPromise
= api
.post( {
234 uselang
: mw
.config
.get( 'wgUserLanguage' )
235 } ).done( function ( data
) {
238 if ( data
.warnings
&& data
.warnings
.paraminfo
) {
239 deferred
.reject( '???', data
.warnings
.paraminfo
[ '*' ] );
243 info
= data
.paraminfo
.modules
;
244 if ( !info
|| info
.length
!== 1 || info
[ 0 ].path
!== module
) {
245 deferred
.reject( '???', 'No module data returned' );
249 moduleInfoCache
[ module
] = info
[ 0 ];
250 deferred
.resolve( info
[ 0 ] );
251 } ).fail( function ( code
, details
) {
252 if ( code
=== 'http' ) {
253 details
= 'HTTP error: ' + details
.exception
;
254 } else if ( details
.error
) {
255 details
= details
.error
.info
;
257 deferred
.reject( code
, details
);
260 .promise( { abort
: apiPromise
.abort
} );
265 * Mark all currently-in-use tokens as bad
267 markTokensBad: function () {
268 var page
, subpages
, i
,
269 checkPages
= [ pages
.main
];
271 while ( checkPages
.length
) {
272 page
= checkPages
.shift();
274 if ( page
.tokenWidget
) {
275 api
.badToken( page
.tokenWidget
.paramInfo
.tokentype
);
278 subpages
= page
.getSubpages();
279 for ( i
= 0; i
< subpages
.length
; i
++ ) {
280 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
281 checkPages
.push( pages
[ subpages
[ i
].key
] );
288 * Test an API boolean
290 * @param {Mixed} value
293 apiBool: function ( value
) {
294 return value
!== undefined && value
!== false;
298 * Create a widget for a parameter.
300 * @param {Object} pi Parameter info from API
301 * @param {Object} opts Additional options
302 * @return {OO.ui.Widget}
304 createWidgetForParameter: function ( pi
, opts
) {
305 var widget
, innerWidget
, finalWidget
, items
, $button
, $content
, func
,
312 widget
= new OO
.ui
.ToggleSwitchWidget();
313 widget
.paramInfo
= pi
;
314 $.extend( widget
, WidgetMethods
.toggleSwitchWidget
);
315 pi
.required
= true; // Avoid wrapping in the non-required widget
320 if ( pi
.tokentype
) {
321 widget
= new TextInputWithIndicatorWidget( {
323 indicator
: 'previous',
324 indicatorTitle
: mw
.message( 'apisandbox-fetch-token' ).text(),
325 required
: Util
.apiBool( pi
.required
)
328 } else if ( Util
.apiBool( pi
.multi
) ) {
329 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
332 widget
.paramInfo
= pi
;
333 $.extend( widget
, WidgetMethods
.capsuleWidget
);
335 widget
= new OO
.ui
.TextInputWidget( {
336 required
: Util
.apiBool( pi
.required
)
339 if ( !Util
.apiBool( pi
.multi
) ) {
340 widget
.paramInfo
= pi
;
341 $.extend( widget
, WidgetMethods
.textInputWidget
);
342 widget
.setValidation( Validators
.generic
);
344 if ( pi
.tokentype
) {
345 $.extend( widget
, WidgetMethods
.tokenWidget
);
346 widget
.input
.paramInfo
= pi
;
347 $.extend( widget
.input
, WidgetMethods
.textInputWidget
);
348 $.extend( widget
.input
, WidgetMethods
.tokenWidget
);
349 widget
.on( 'indicator', widget
.fetchToken
, [], widget
);
354 widget
= new OO
.ui
.TextInputWidget( {
356 required
: Util
.apiBool( pi
.required
)
358 widget
.paramInfo
= pi
;
359 $.extend( widget
, WidgetMethods
.textInputWidget
);
360 widget
.setValidation( Validators
.generic
);
364 widget
= new OO
.ui
.TextInputWidget( {
366 required
: Util
.apiBool( pi
.required
)
368 widget
.paramInfo
= pi
;
369 $.extend( widget
, WidgetMethods
.textInputWidget
);
370 $.extend( widget
, WidgetMethods
.passwordWidget
);
371 widget
.setValidation( Validators
.generic
);
376 widget
= new OO
.ui
.NumberInputWidget( {
377 required
: Util
.apiBool( pi
.required
),
380 widget
.setIcon
= widget
.input
.setIcon
.bind( widget
.input
);
381 widget
.setIconTitle
= widget
.input
.setIconTitle
.bind( widget
.input
);
382 widget
.getValidity
= widget
.input
.getValidity
.bind( widget
.input
);
383 widget
.paramInfo
= pi
;
384 $.extend( widget
, WidgetMethods
.textInputWidget
);
385 if ( Util
.apiBool( pi
.enforcerange
) ) {
386 widget
.setRange( pi
.min
|| -Infinity
, pi
.max
|| Infinity
);
392 widget
= new OO
.ui
.TextInputWidget( {
393 required
: Util
.apiBool( pi
.required
)
395 widget
.setValidation( function ( value
) {
396 var n
, pi
= this.paramInfo
;
398 if ( value
=== 'max' ) {
402 return !isNaN( n
) && isFinite( n
) &&
403 // eslint-disable-next-line no-bitwise
405 n
>= pi
.min
&& n
<= pi
.apiSandboxMax
;
408 pi
.min
= pi
.min
|| 0;
409 pi
.apiSandboxMax
= mw
.config
.get( 'apihighlimits' ) ? pi
.highmax
: pi
.max
;
410 widget
.paramInfo
= pi
;
411 $.extend( widget
, WidgetMethods
.textInputWidget
);
416 widget
= new mw
.widgets
.datetime
.DateTimeInputWidget( {
418 format
: '${year|0}-${month|0}-${day|0}T${hour|0}:${minute|0}:${second|0}${zone|short}'
420 required
: Util
.apiBool( pi
.required
),
423 widget
.paramInfo
= pi
;
424 $.extend( widget
, WidgetMethods
.textInputWidget
);
425 $.extend( widget
, WidgetMethods
.dateTimeInputWidget
);
426 multiMode
= 'indicator';
430 widget
= new OO
.ui
.SelectFileWidget();
431 widget
.paramInfo
= pi
;
432 $.extend( widget
, WidgetMethods
.uploadWidget
);
436 items
= $.map( mw
.config
.get( 'wgFormattedNamespaces' ), function ( name
, ns
) {
438 name
= mw
.message( 'blanknamespace' ).text();
440 return new OO
.ui
.MenuOptionWidget( { data
: ns
, label
: name
} );
441 } ).sort( function ( a
, b
) {
442 return a
.data
- b
.data
;
444 if ( Util
.apiBool( pi
.multi
) ) {
445 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
446 menu
: { items
: items
}
448 widget
.paramInfo
= pi
;
449 $.extend( widget
, WidgetMethods
.capsuleWidget
);
451 widget
= new OO
.ui
.DropdownWidget( {
452 menu
: { items
: items
}
454 widget
.paramInfo
= pi
;
455 $.extend( widget
, WidgetMethods
.dropdownWidget
);
460 if ( !$.isArray( pi
.type
) ) {
461 throw new Error( 'Unknown parameter type ' + pi
.type
);
464 items
= $.map( pi
.type
, function ( v
) {
465 return new OO
.ui
.MenuOptionWidget( { data
: String( v
), label
: String( v
) } );
467 if ( Util
.apiBool( pi
.multi
) ) {
468 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
469 menu
: { items
: items
}
471 widget
.paramInfo
= pi
;
472 $.extend( widget
, WidgetMethods
.capsuleWidget
);
473 if ( Util
.apiBool( pi
.submodules
) ) {
474 widget
.getSubmodules
= WidgetMethods
.submoduleWidget
.multi
;
475 widget
.on( 'change', ApiSandbox
.updateUI
);
478 widget
= new OO
.ui
.DropdownWidget( {
479 menu
: { items
: items
}
481 widget
.paramInfo
= pi
;
482 $.extend( widget
, WidgetMethods
.dropdownWidget
);
483 if ( Util
.apiBool( pi
.submodules
) ) {
484 widget
.getSubmodules
= WidgetMethods
.submoduleWidget
.single
;
485 widget
.getMenu().on( 'choose', ApiSandbox
.updateUI
);
492 if ( Util
.apiBool( pi
.multi
) && multiMode
!== 'none' ) {
493 innerWidget
= widget
;
494 switch ( multiMode
) {
496 $content
= innerWidget
.$element
;
500 $button
= innerWidget
.$indicator
;
501 $button
.css( 'cursor', 'pointer' );
502 $button
.attr( 'tabindex', 0 );
503 $button
.parent().append( $button
);
504 innerWidget
.setIndicator( 'next' );
505 $content
= innerWidget
.$element
;
509 throw new Error( 'Unknown multiMode "' + multiMode
+ '"' );
512 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
513 allowArbitrary
: true,
515 classes
: [ 'mw-apisandbox-popup' ],
519 widget
.paramInfo
= pi
;
520 $.extend( widget
, WidgetMethods
.capsuleWidget
);
523 if ( !innerWidget
.isDisabled() ) {
524 innerWidget
.apiCheckValid().done( function ( ok
) {
526 widget
.addItemsFromData( [ innerWidget
.getApiValue() ] );
527 innerWidget
.setApiValue( undefined );
533 switch ( multiMode
) {
535 innerWidget
.connect( null, { enter
: func
} );
541 keypress: function ( e
) {
542 if ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) {
551 if ( Util
.apiBool( pi
.required
) || opts
.nooptional
) {
552 finalWidget
= widget
;
554 finalWidget
= new OptionalWidget( widget
);
555 finalWidget
.paramInfo
= pi
;
556 $.extend( finalWidget
, WidgetMethods
.optionalWidget
);
557 if ( widget
.getSubmodules
) {
558 finalWidget
.getSubmodules
= widget
.getSubmodules
.bind( widget
);
559 finalWidget
.on( 'disable', function () { setTimeout( ApiSandbox
.updateUI
); } );
561 finalWidget
.setDisabled( true );
564 widget
.setApiValue( pi
[ 'default' ] );
570 * Parse an HTML string, adding target="_blank" to any links
572 * @param {string} html HTML to parse
575 parseHTML: function ( html
) {
576 var $ret
= $( $.parseHTML( html
) );
577 $ret
.filter( 'a' ).add( $ret
.find( 'a' ) )
578 .filter( '[href]:not([target])' )
579 .attr( 'target', '_blank' );
585 * Interface to ApiSandbox UI
587 * @class mw.special.ApiSandbox
593 * Automatically called on $.ready()
598 ApiSandbox
.isFullscreen
= false;
600 $content
= $( '#mw-apisandbox' );
602 windowManager
= new OO
.ui
.WindowManager();
603 $( 'body' ).append( windowManager
.$element
);
604 windowManager
.addWindows( {
605 errorAlert
: new OO
.ui
.MessageDialog()
608 fullscreenButton
= new OO
.ui
.ButtonWidget( {
609 label
: mw
.message( 'apisandbox-fullscreen' ).text(),
610 title
: mw
.message( 'apisandbox-fullscreen-tooltip' ).text()
611 } ).on( 'click', ApiSandbox
.toggleFullscreen
);
613 $toolbar
= $( '<div>' )
614 .addClass( 'mw-apisandbox-toolbar' )
616 fullscreenButton
.$element
,
617 new OO
.ui
.ButtonWidget( {
618 label
: mw
.message( 'apisandbox-submit' ).text(),
619 flags
: [ 'primary', 'progressive' ]
620 } ).on( 'click', ApiSandbox
.sendRequest
).$element
,
621 new OO
.ui
.ButtonWidget( {
622 label
: mw
.message( 'apisandbox-reset' ).text(),
624 } ).on( 'click', ApiSandbox
.resetUI
).$element
627 booklet
= new OO
.ui
.BookletLayout( {
632 panel
= new OO
.ui
.PanelLayout( {
633 classes
: [ 'mw-apisandbox-container' ],
634 content
: [ booklet
],
639 pages
.main
= new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } );
641 // Parse the current hash string
642 if ( !ApiSandbox
.loadFromHash() ) {
643 ApiSandbox
.updateUI();
646 // If the hashchange event exists, use it. Otherwise, fake it.
647 // And, of course, IE has to be dumb.
648 if ( 'onhashchange' in window
&&
649 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
651 $( window
).on( 'hashchange', ApiSandbox
.loadFromHash
);
653 setInterval( function () {
654 if ( oldhash
!== location
.hash
) {
655 ApiSandbox
.loadFromHash();
662 .append( $( '<p>' ).append( mw
.message( 'apisandbox-intro' ).parse() ) )
664 $( '<div>', { id
: 'mw-apisandbox-ui' } )
666 .append( panel
.$element
)
669 $( window
).on( 'resize', ApiSandbox
.resizePanel
);
671 ApiSandbox
.resizePanel();
675 * Toggle "fullscreen" mode
677 toggleFullscreen: function () {
678 var $body
= $( document
.body
),
679 $ui
= $( '#mw-apisandbox-ui' );
681 ApiSandbox
.isFullscreen
= !ApiSandbox
.isFullscreen
;
683 $body
.toggleClass( 'mw-apisandbox-fullscreen', ApiSandbox
.isFullscreen
);
684 $ui
.toggleClass( 'mw-body-content', ApiSandbox
.isFullscreen
);
685 if ( ApiSandbox
.isFullscreen
) {
686 fullscreenButton
.setLabel( mw
.message( 'apisandbox-unfullscreen' ).text() );
687 fullscreenButton
.setTitle( mw
.message( 'apisandbox-unfullscreen-tooltip' ).text() );
690 fullscreenButton
.setLabel( mw
.message( 'apisandbox-fullscreen' ).text() );
691 fullscreenButton
.setTitle( mw
.message( 'apisandbox-fullscreen-tooltip' ).text() );
692 $content
.append( $ui
);
694 ApiSandbox
.resizePanel();
698 * Set the height of the panel based on the current viewport.
700 resizePanel: function () {
701 var height
= $( window
).height(),
702 contentTop
= $content
.offset().top
;
704 if ( ApiSandbox
.isFullscreen
) {
705 height
-= panel
.$element
.offset().top
- $( '#mw-apisandbox-ui' ).offset().top
;
706 panel
.$element
.height( height
- 1 );
708 // Subtract the height of the intro text
709 height
-= panel
.$element
.offset().top
- contentTop
;
711 panel
.$element
.height( height
- 10 );
712 $( window
).scrollTop( contentTop
- 5 );
717 * Update the current query when the page hash changes
719 loadFromHash: function () {
721 hash
= location
.hash
;
723 if ( oldhash
=== hash
) {
731 // I'm surprised this doesn't seem to exist in jQuery or mw.util.
733 hash
= hash
.replace( /\+/g, '%20' );
734 re
= /([^&=#]+)=?([^&#]*)/g;
735 while ( ( m
= re
.exec( hash
) ) ) {
736 params
[ decodeURIComponent( m
[ 1 ] ) ] = decodeURIComponent( m
[ 2 ] );
739 ApiSandbox
.updateUI( params
);
744 * Update the pages in the booklet
746 * @param {Object} [params] Optional query parameters to load
748 updateUI: function ( params
) {
749 var i
, page
, subpages
, j
, removePages
,
752 if ( !$.isPlainObject( params
) ) {
756 if ( updatingBooklet
) {
759 updatingBooklet
= true;
761 if ( params
!== undefined ) {
762 pages
.main
.loadQueryParams( params
);
764 addPages
.push( pages
.main
);
765 if ( resultPage
!== null ) {
766 addPages
.push( resultPage
);
768 pages
.main
.apiCheckValid();
771 while ( addPages
.length
) {
772 page
= addPages
.shift();
773 if ( bookletPages
[ i
] !== page
) {
774 for ( j
= i
; j
< bookletPages
.length
; j
++ ) {
775 if ( bookletPages
[ j
].getName() === page
.getName() ) {
776 bookletPages
.splice( j
, 1 );
779 bookletPages
.splice( i
, 0, page
);
780 booklet
.addPages( [ page
], i
);
784 if ( page
.getSubpages
) {
785 subpages
= page
.getSubpages();
786 for ( j
= 0; j
< subpages
.length
; j
++ ) {
787 if ( !pages
.hasOwnProperty( subpages
[ j
].key
) ) {
788 subpages
[ j
].indentLevel
= page
.indentLevel
+ 1;
789 pages
[ subpages
[ j
].key
] = new ApiSandbox
.PageLayout( subpages
[ j
] );
791 if ( params
!== undefined ) {
792 pages
[ subpages
[ j
].key
].loadQueryParams( params
);
794 addPages
.splice( j
, 0, pages
[ subpages
[ j
].key
] );
795 pages
[ subpages
[ j
].key
].apiCheckValid();
800 if ( bookletPages
.length
> i
) {
801 removePages
= bookletPages
.splice( i
, bookletPages
.length
- i
);
802 booklet
.removePages( removePages
);
805 if ( !booklet
.getCurrentPageName() ) {
806 booklet
.selectFirstSelectablePage();
809 updatingBooklet
= false;
814 * Reset button handler
816 resetUI: function () {
817 suppressErrors
= true;
819 main
: new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } )
822 ApiSandbox
.updateUI();
826 * Submit button handler
828 * @param {Object} [params] Use this set of params instead of those in the form fields.
829 * The form fields will be updated to match.
831 sendRequest: function ( params
) {
832 var page
, subpages
, i
, query
, $result
, $focus
,
833 progress
, $progressText
, progressLoading
,
835 paramsAreForced
= !!params
,
837 checkPages
= [ pages
.main
];
839 // Blur any focused widget before submit, because
840 // OO.ui.ButtonWidget doesn't take focus itself (T128054)
841 $focus
= $( '#mw-apisandbox-ui' ).find( document
.activeElement
);
842 if ( $focus
.length
) {
846 suppressErrors
= false;
848 // save widget state in params (or load from it if we are forced)
849 if ( paramsAreForced
) {
850 ApiSandbox
.updateUI( params
);
853 while ( checkPages
.length
) {
854 page
= checkPages
.shift();
855 deferreds
.push( page
.apiCheckValid() );
856 page
.getQueryParams( params
, displayParams
);
857 subpages
= page
.getSubpages();
858 for ( i
= 0; i
< subpages
.length
; i
++ ) {
859 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
860 checkPages
.push( pages
[ subpages
[ i
].key
] );
865 if ( !paramsAreForced
) {
866 // forced params means we are continuing a query; the base query should be preserved
867 baseRequestParams
= $.extend( {}, params
);
870 $.when
.apply( $, deferreds
).done( function () {
871 if ( $.inArray( false, arguments
) !== -1 ) {
872 windowManager
.openWindow( 'errorAlert', {
873 title
: mw
.message( 'apisandbox-submit-invalid-fields-title' ).parse(),
874 message
: mw
.message( 'apisandbox-submit-invalid-fields-message' ).parse(),
878 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
886 query
= $.param( displayParams
);
888 // Force a 'fm' format with wrappedhtml=1, if available
889 if ( params
.format
!== undefined ) {
890 if ( availableFormats
.hasOwnProperty( params
.format
+ 'fm' ) ) {
891 params
.format
= params
.format
+ 'fm';
893 if ( params
.format
.substr( -2 ) === 'fm' ) {
894 params
.wrappedhtml
= 1;
898 progressLoading
= false;
899 $progressText
= $( '<span>' ).text( mw
.message( 'apisandbox-sending-request' ).text() );
900 progress
= new OO
.ui
.ProgressBarWidget( {
902 $content
: $progressText
905 $result
= $( '<div>' )
906 .append( progress
.$element
);
908 resultPage
= page
= new OO
.ui
.PageLayout( '|results|' );
909 page
.setupOutlineItem = function () {
910 this.outlineItem
.setLabel( mw
.message( 'apisandbox-results' ).text() );
912 page
.$element
.empty()
914 new OO
.ui
.FieldLayout(
915 new OO
.ui
.TextInputWidget( {
917 value
: mw
.util
.wikiScript( 'api' ) + '?' + query
919 label
: mw
.message( 'apisandbox-request-url-label' ).parse()
924 ApiSandbox
.updateUI();
925 booklet
.setPage( '|results|' );
927 location
.href
= oldhash
= '#' + query
;
930 contentType
: 'multipart/form-data',
933 var xhr
= new window
.XMLHttpRequest();
934 xhr
.upload
.addEventListener( 'progress', function ( e
) {
935 if ( !progressLoading
) {
936 if ( e
.lengthComputable
) {
937 progress
.setProgress( e
.loaded
* 100 / e
.total
);
939 progress
.setProgress( false );
943 xhr
.addEventListener( 'progress', function ( e
) {
944 if ( !progressLoading
) {
945 progressLoading
= true;
946 $progressText
.text( mw
.message( 'apisandbox-loading-results' ).text() );
948 if ( e
.lengthComputable
) {
949 progress
.setProgress( e
.loaded
* 100 / e
.total
);
951 progress
.setProgress( false );
957 .then( null, function ( code
, data
, result
, jqXHR
) {
958 if ( code
!== 'http' ) {
959 // Not really an error, work around mw.Api thinking it is.
961 .resolve( result
, jqXHR
)
966 .fail( function ( code
, data
) {
967 var details
= 'HTTP error: ' + data
.exception
;
970 new OO
.ui
.LabelWidget( {
971 label
: mw
.message( 'apisandbox-results-error', details
).text(),
976 .done( function ( data
, jqXHR
) {
977 var m
, loadTime
, button
, clear
,
978 ct
= jqXHR
.getResponseHeader( 'Content-Type' );
981 if ( /^text\/mediawiki-api-prettyprint-wrapped(?:;|$)/.test( ct
) ) {
982 data
= $.parseJSON( data
);
983 if ( data
.modules
.length
) {
984 mw
.loader
.load( data
.modules
);
986 if ( data
.status
&& data
.status
!== 200 ) {
988 .addClass( 'api-pretty-header api-pretty-status' )
990 mw
.message( 'api-format-prettyprint-status', data
.status
, data
.statustext
).parse()
992 .appendTo( $result
);
994 $result
.append( Util
.parseHTML( data
.html
) );
995 loadTime
= data
.time
;
996 } else if ( ( m
= data
.match( /<pre[ >][\s\S]*<\/pre>/ ) ) ) {
997 $result
.append( Util
.parseHTML( m
[ 0 ] ) );
998 if ( ( m
= data
.match( /"wgBackendResponseTime":\s*(\d+)/ ) ) ) {
999 loadTime
= parseInt( m
[ 1 ], 10 );
1003 .addClass( 'api-pretty-content' )
1005 .appendTo( $result
);
1007 if ( paramsAreForced
|| data
[ 'continue' ] ) {
1009 $( '<div>' ).append(
1010 new OO
.ui
.ButtonWidget( {
1011 label
: mw
.message( 'apisandbox-continue' ).text()
1012 } ).on( 'click', function () {
1013 ApiSandbox
.sendRequest( $.extend( {}, baseRequestParams
, data
[ 'continue' ] ) );
1014 } ).setDisabled( !data
[ 'continue' ] ).$element
,
1015 ( clear
= new OO
.ui
.ButtonWidget( {
1016 label
: mw
.message( 'apisandbox-continue-clear' ).text()
1017 } ).on( 'click', function () {
1018 ApiSandbox
.updateUI( baseRequestParams
);
1019 clear
.setDisabled( true );
1020 booklet
.setPage( '|results|' );
1021 } ).setDisabled( !paramsAreForced
) ).$element
,
1022 new OO
.ui
.PopupButtonWidget( {
1026 $content
: $( '<div>' ).append( mw
.message( 'apisandbox-continue-help' ).parse() ),
1033 if ( typeof loadTime
=== 'number' ) {
1035 $( '<div>' ).append(
1036 new OO
.ui
.LabelWidget( {
1037 label
: mw
.message( 'apisandbox-request-time', loadTime
).text()
1043 if ( jqXHR
.getResponseHeader( 'MediaWiki-API-Error' ) === 'badtoken' ) {
1044 // Flush all saved tokens in case one of them is the bad one.
1045 Util
.markTokensBad();
1046 button
= new OO
.ui
.ButtonWidget( {
1047 label
: mw
.message( 'apisandbox-results-fixtoken' ).text()
1049 button
.on( 'click', ApiSandbox
.fixTokenAndResend
)
1050 .on( 'click', button
.setDisabled
, [ true ], button
)
1051 .$element
.appendTo( $result
);
1058 * Handler for the "Correct token and resubmit" button
1060 * Used on a 'badtoken' error, it re-fetches token parameters for all
1061 * pages and then re-submits the query.
1063 fixTokenAndResend: function () {
1064 var page
, subpages
, i
, k
,
1066 tokenWait
= { dummy
: true },
1067 checkPages
= [ pages
.main
],
1068 success = function ( k
) {
1069 delete tokenWait
[ k
];
1070 if ( ok
&& $.isEmptyObject( tokenWait
) ) {
1071 ApiSandbox
.sendRequest();
1074 failure = function ( k
) {
1075 delete tokenWait
[ k
];
1079 while ( checkPages
.length
) {
1080 page
= checkPages
.shift();
1082 if ( page
.tokenWidget
) {
1083 k
= page
.apiModule
+ page
.tokenWidget
.paramInfo
.name
;
1084 tokenWait
[ k
] = page
.tokenWidget
.fetchToken()
1085 .done( success
.bind( page
.tokenWidget
, k
) )
1086 .fail( failure
.bind( page
.tokenWidget
, k
) );
1089 subpages
= page
.getSubpages();
1090 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1091 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1092 checkPages
.push( pages
[ subpages
[ i
].key
] );
1097 success( 'dummy', '' );
1101 * Reset validity indicators for all widgets
1103 updateValidityIndicators: function () {
1104 var page
, subpages
, i
,
1105 checkPages
= [ pages
.main
];
1107 while ( checkPages
.length
) {
1108 page
= checkPages
.shift();
1109 page
.apiCheckValid();
1110 subpages
= page
.getSubpages();
1111 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1112 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1113 checkPages
.push( pages
[ subpages
[ i
].key
] );
1121 * PageLayout for API modules
1125 * @extends OO.ui.PageLayout
1127 * @param {Object} [config] Configuration options
1129 ApiSandbox
.PageLayout = function ( config
) {
1130 config
= $.extend( { prefix
: '' }, config
);
1131 this.displayText
= config
.key
;
1132 this.apiModule
= config
.path
;
1133 this.prefix
= config
.prefix
;
1134 this.paramInfo
= null;
1135 this.apiIsValid
= true;
1136 this.loadFromQueryParams
= null;
1138 this.tokenWidget
= null;
1139 this.indentLevel
= config
.indentLevel
? config
.indentLevel
: 0;
1140 ApiSandbox
.PageLayout
[ 'super' ].call( this, config
.key
, config
);
1141 this.loadParamInfo();
1143 OO
.inheritClass( ApiSandbox
.PageLayout
, OO
.ui
.PageLayout
);
1144 ApiSandbox
.PageLayout
.prototype.setupOutlineItem = function () {
1145 this.outlineItem
.setLevel( this.indentLevel
);
1146 this.outlineItem
.setLabel( this.displayText
);
1147 this.outlineItem
.setIcon( this.apiIsValid
|| suppressErrors
? null : 'alert' );
1148 this.outlineItem
.setIconTitle(
1149 this.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1154 * Fetch module information for this page's module, then create UI
1156 ApiSandbox
.PageLayout
.prototype.loadParamInfo = function () {
1157 var dynamicFieldset
, dynamicParamNameWidget
,
1159 removeDynamicParamWidget = function ( name
, layout
) {
1160 dynamicFieldset
.removeItems( [ layout
] );
1161 delete that
.widgets
[ name
];
1163 addDynamicParamWidget = function () {
1164 var name
, layout
, widget
, button
;
1166 // Check name is filled in
1167 name
= dynamicParamNameWidget
.getValue().trim();
1168 if ( name
=== '' ) {
1169 dynamicParamNameWidget
.focus();
1173 if ( that
.widgets
[ name
] !== undefined ) {
1174 windowManager
.openWindow( 'errorAlert', {
1176 'apisandbox-dynamic-error-exists', name
1181 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
1189 widget
= Util
.createWidgetForParameter( {
1196 button
= new OO
.ui
.ButtonWidget( {
1198 flags
: 'destructive'
1200 layout
= new OO
.ui
.ActionFieldLayout(
1208 button
.on( 'click', removeDynamicParamWidget
, [ name
, layout
] );
1209 that
.widgets
[ name
] = widget
;
1210 dynamicFieldset
.addItems( [ layout
], dynamicFieldset
.getItems().length
- 1 );
1213 dynamicParamNameWidget
.setValue( '' );
1216 this.$element
.empty()
1217 .append( new OO
.ui
.ProgressBarWidget( {
1219 text
: mw
.message( 'apisandbox-loading', this.displayText
).text()
1222 Util
.fetchModuleInfo( this.apiModule
)
1223 .done( function ( pi
) {
1224 var prefix
, i
, j
, dl
, widget
, $widgetLabel
, widgetField
, helpField
, tmp
, flag
, count
,
1226 deprecatedItems
= [],
1228 filterFmModules = function ( v
) {
1229 return v
.substr( -2 ) !== 'fm' ||
1230 !availableFormats
.hasOwnProperty( v
.substr( 0, v
.length
- 2 ) );
1232 widgetLabelOnClick = function () {
1233 var f
= this.getField();
1234 if ( $.isFunction( f
.setDisabled
) ) {
1235 f
.setDisabled( false );
1237 if ( $.isFunction( f
.focus
) ) {
1241 doNothing = function () {};
1243 // This is something of a hack. We always want the 'format' and
1244 // 'action' parameters from the main module to be specified,
1245 // and for 'format' we also want to simplify the dropdown since
1246 // we always send the 'fm' variant.
1247 if ( that
.apiModule
=== 'main' ) {
1248 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1249 if ( pi
.parameters
[ i
].name
=== 'action' ) {
1250 pi
.parameters
[ i
].required
= true;
1251 delete pi
.parameters
[ i
][ 'default' ];
1253 if ( pi
.parameters
[ i
].name
=== 'format' ) {
1254 tmp
= pi
.parameters
[ i
].type
;
1255 for ( j
= 0; j
< tmp
.length
; j
++ ) {
1256 availableFormats
[ tmp
[ j
] ] = true;
1258 pi
.parameters
[ i
].type
= $.grep( tmp
, filterFmModules
);
1259 pi
.parameters
[ i
][ 'default' ] = 'json';
1260 pi
.parameters
[ i
].required
= true;
1265 // Hide the 'wrappedhtml' parameter on format modules
1266 if ( pi
.group
=== 'format' ) {
1267 pi
.parameters
= $.grep( pi
.parameters
, function ( p
) {
1268 return p
.name
!== 'wrappedhtml';
1272 that
.paramInfo
= pi
;
1274 items
.push( new OO
.ui
.FieldLayout(
1275 new OO
.ui
.Widget( {} ).toggle( false ), {
1277 label
: Util
.parseHTML( pi
.description
)
1281 if ( pi
.helpurls
.length
) {
1282 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1283 label
: mw
.message( 'apisandbox-helpurls' ).text(),
1286 $content
: $( '<ul>' ).append( $.map( pi
.helpurls
, function ( link
) {
1287 return $( '<li>' ).append( $( '<a>', {
1297 if ( pi
.examples
.length
) {
1298 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1299 label
: mw
.message( 'apisandbox-examples' ).text(),
1302 $content
: $( '<ul>' ).append( $.map( pi
.examples
, function ( example
) {
1304 href
: '#' + example
.query
,
1305 html
: example
.description
1307 a
.find( 'a' ).contents().unwrap(); // Can't nest links
1308 return $( '<li>' ).append( a
);
1314 if ( buttons
.length
) {
1315 items
.push( new OO
.ui
.FieldLayout(
1316 new OO
.ui
.ButtonGroupWidget( {
1318 } ), { align
: 'top' }
1322 if ( pi
.parameters
.length
) {
1323 prefix
= that
.prefix
+ pi
.prefix
;
1324 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1325 widget
= Util
.createWidgetForParameter( pi
.parameters
[ i
] );
1326 that
.widgets
[ prefix
+ pi
.parameters
[ i
].name
] = widget
;
1327 if ( pi
.parameters
[ i
].tokentype
) {
1328 that
.tokenWidget
= widget
;
1332 dl
.append( $( '<dd>', {
1333 addClass
: 'description',
1334 append
: Util
.parseHTML( pi
.parameters
[ i
].description
)
1336 if ( pi
.parameters
[ i
].info
&& pi
.parameters
[ i
].info
.length
) {
1337 for ( j
= 0; j
< pi
.parameters
[ i
].info
.length
; j
++ ) {
1338 dl
.append( $( '<dd>', {
1340 append
: Util
.parseHTML( pi
.parameters
[ i
].info
[ j
] )
1346 switch ( pi
.parameters
[ i
].type
) {
1349 count
= mw
.config
.get( 'wgFormattedNamespaces' ).length
;
1353 if ( pi
.parameters
[ i
].highmax
!== undefined ) {
1354 dl
.append( $( '<dd>', {
1357 Util
.parseHTML( mw
.message(
1358 'api-help-param-limit2', pi
.parameters
[ i
].max
, pi
.parameters
[ i
].highmax
1361 Util
.parseHTML( mw
.message( 'apisandbox-param-limit' ).parse() )
1365 dl
.append( $( '<dd>', {
1368 Util
.parseHTML( mw
.message(
1369 'api-help-param-limit', pi
.parameters
[ i
].max
1372 Util
.parseHTML( mw
.message( 'apisandbox-param-limit' ).parse() )
1380 if ( pi
.parameters
[ i
].min
!== undefined ) {
1383 if ( pi
.parameters
[ i
].max
!== undefined ) {
1387 dl
.append( $( '<dd>', {
1389 append
: Util
.parseHTML( mw
.message(
1390 'api-help-param-integer-' + tmp
,
1391 Util
.apiBool( pi
.parameters
[ i
].multi
) ? 2 : 1,
1392 pi
.parameters
[ i
].min
, pi
.parameters
[ i
].max
1399 if ( $.isArray( pi
.parameters
[ i
].type
) ) {
1401 count
= pi
.parameters
[ i
].type
.length
;
1405 if ( Util
.apiBool( pi
.parameters
[ i
].multi
) ) {
1407 if ( flag
&& !( widget
instanceof OO
.ui
.CapsuleMultiselectWidget
) &&
1409 widget
instanceof OptionalWidget
&&
1410 widget
.widget
instanceof OO
.ui
.CapsuleMultiselectWidget
1413 tmp
.push( mw
.message( 'api-help-param-multi-separate' ).parse() );
1415 if ( count
> pi
.parameters
[ i
].lowlimit
) {
1417 mw
.message( 'api-help-param-multi-max',
1418 pi
.parameters
[ i
].lowlimit
, pi
.parameters
[ i
].highlimit
1423 dl
.append( $( '<dd>', {
1425 append
: Util
.parseHTML( tmp
.join( ' ' ) )
1429 helpField
= new OO
.ui
.FieldLayout(
1432 classes
: [ 'mw-apisandbox-spacer' ]
1435 classes
: [ 'mw-apisandbox-help-field' ],
1440 $widgetLabel
= $( '<span>' );
1441 widgetField
= new OO
.ui
.FieldLayout(
1445 classes
: [ 'mw-apisandbox-widget-field' ],
1446 label
: prefix
+ pi
.parameters
[ i
].name
,
1447 $label
: $widgetLabel
1451 // FieldLayout only does click for InputElement
1452 // widgets. So supply our own click handler.
1453 $widgetLabel
.on( 'click', widgetLabelOnClick
.bind( widgetField
) );
1455 // Don't grey out the label when the field is disabled,
1456 // it makes it too hard to read and our "disabled"
1457 // isn't really disabled.
1458 widgetField
.onFieldDisable( false );
1459 widgetField
.onFieldDisable
= doNothing
;
1461 if ( Util
.apiBool( pi
.parameters
[ i
].deprecated
) ) {
1462 deprecatedItems
.push( widgetField
, helpField
);
1464 items
.push( widgetField
, helpField
);
1469 if ( !pi
.parameters
.length
&& !Util
.apiBool( pi
.dynamicparameters
) ) {
1470 items
.push( new OO
.ui
.FieldLayout(
1471 new OO
.ui
.Widget( {} ).toggle( false ), {
1473 label
: Util
.parseHTML( mw
.message( 'apisandbox-no-parameters' ).parse() )
1478 that
.$element
.empty();
1480 new OO
.ui
.FieldsetLayout( {
1481 label
: that
.displayText
1482 } ).addItems( items
)
1483 .$element
.appendTo( that
.$element
);
1485 if ( Util
.apiBool( pi
.dynamicparameters
) ) {
1486 dynamicFieldset
= new OO
.ui
.FieldsetLayout();
1487 dynamicParamNameWidget
= new OO
.ui
.TextInputWidget( {
1488 placeholder
: mw
.message( 'apisandbox-dynamic-parameters-add-placeholder' ).text()
1489 } ).on( 'enter', addDynamicParamWidget
);
1490 dynamicFieldset
.addItems( [
1491 new OO
.ui
.FieldLayout(
1492 new OO
.ui
.Widget( {} ).toggle( false ), {
1494 label
: Util
.parseHTML( pi
.dynamicparameters
)
1497 new OO
.ui
.ActionFieldLayout(
1498 dynamicParamNameWidget
,
1499 new OO
.ui
.ButtonWidget( {
1501 flags
: 'progressive'
1502 } ).on( 'click', addDynamicParamWidget
),
1504 label
: mw
.message( 'apisandbox-dynamic-parameters-add-label' ).text(),
1511 $( '<legend>' ).text( mw
.message( 'apisandbox-dynamic-parameters' ).text() ),
1512 dynamicFieldset
.$element
1514 .appendTo( that
.$element
);
1517 if ( deprecatedItems
.length
) {
1518 tmp
= new OO
.ui
.FieldsetLayout().addItems( deprecatedItems
).toggle( false );
1521 $( '<legend>' ).append(
1522 new OO
.ui
.ToggleButtonWidget( {
1523 label
: mw
.message( 'apisandbox-deprecated-parameters' ).text()
1524 } ).on( 'change', tmp
.toggle
, [], tmp
).$element
1528 .appendTo( that
.$element
);
1531 // Load stored params, if any, then update the booklet if we
1532 // have subpages (or else just update our valid-indicator).
1533 tmp
= that
.loadFromQueryParams
;
1534 that
.loadFromQueryParams
= null;
1535 if ( $.isPlainObject( tmp
) ) {
1536 that
.loadQueryParams( tmp
);
1538 if ( that
.getSubpages().length
> 0 ) {
1539 ApiSandbox
.updateUI( tmp
);
1541 that
.apiCheckValid();
1543 } ).fail( function ( code
, detail
) {
1544 that
.$element
.empty()
1546 new OO
.ui
.LabelWidget( {
1547 label
: mw
.message( 'apisandbox-load-error', that
.apiModule
, detail
).text(),
1548 classes
: [ 'error' ]
1550 new OO
.ui
.ButtonWidget( {
1551 label
: mw
.message( 'apisandbox-retry' ).text()
1552 } ).on( 'click', that
.loadParamInfo
, [], that
).$element
1558 * Check that all widgets on the page are in a valid state.
1562 ApiSandbox
.PageLayout
.prototype.apiCheckValid = function () {
1565 if ( this.paramInfo
=== null ) {
1566 return $.Deferred().resolve( false ).promise();
1568 return $.when
.apply( $, $.map( this.widgets
, function ( widget
) {
1569 return widget
.apiCheckValid();
1570 } ) ).then( function () {
1571 that
.apiIsValid
= $.inArray( false, arguments
) === -1;
1572 if ( that
.getOutlineItem() ) {
1573 that
.getOutlineItem().setIcon( that
.apiIsValid
|| suppressErrors
? null : 'alert' );
1574 that
.getOutlineItem().setIconTitle(
1575 that
.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1578 return $.Deferred().resolve( that
.apiIsValid
).promise();
1584 * Load form fields from query parameters
1586 * @param {Object} params
1588 ApiSandbox
.PageLayout
.prototype.loadQueryParams = function ( params
) {
1589 if ( this.paramInfo
=== null ) {
1590 this.loadFromQueryParams
= params
;
1592 $.each( this.widgets
, function ( name
, widget
) {
1593 var v
= params
.hasOwnProperty( name
) ? params
[ name
] : undefined;
1594 widget
.setApiValue( v
);
1600 * Load query params from form fields
1602 * @param {Object} params Write query parameters into this object
1603 * @param {Object} displayParams Write query parameters for display into this object
1605 ApiSandbox
.PageLayout
.prototype.getQueryParams = function ( params
, displayParams
) {
1606 $.each( this.widgets
, function ( name
, widget
) {
1607 var value
= widget
.getApiValue();
1608 if ( value
!== undefined ) {
1609 params
[ name
] = value
;
1610 if ( $.isFunction( widget
.getApiValueForDisplay
) ) {
1611 value
= widget
.getApiValueForDisplay();
1613 displayParams
[ name
] = value
;
1619 * Fetch a list of subpage names loaded by this page
1623 ApiSandbox
.PageLayout
.prototype.getSubpages = function () {
1625 $.each( this.widgets
, function ( name
, widget
) {
1627 if ( $.isFunction( widget
.getSubmodules
) ) {
1628 submodules
= widget
.getSubmodules();
1629 for ( i
= 0; i
< submodules
.length
; i
++ ) {
1631 key
: name
+ '=' + submodules
[ i
].value
,
1632 path
: submodules
[ i
].path
,
1633 prefix
: widget
.paramInfo
.submoduleparamprefix
|| ''
1642 * A text input with a clickable indicator
1647 * @param {Object} [config] Configuration options
1649 function TextInputWithIndicatorWidget( config
) {
1652 config
= config
|| {};
1653 TextInputWithIndicatorWidget
[ 'super' ].call( this, config
);
1655 this.$indicator
= $( '<span>' ).addClass( 'mw-apisandbox-clickable-indicator' );
1656 OO
.ui
.mixin
.TabIndexedElement
.call(
1657 this, $.extend( {}, config
, { $tabIndexed
: this.$indicator
} )
1660 this.input
= new OO
.ui
.TextInputWidget( $.extend( {
1661 $indicator
: this.$indicator
,
1662 disabled
: this.isDisabled()
1663 }, config
.input
) );
1665 // Forward most methods for convenience
1666 for ( k
in this.input
) {
1667 if ( $.isFunction( this.input
[ k
] ) && !this[ k
] ) {
1668 this[ k
] = this.input
[ k
].bind( this.input
);
1672 this.$indicator
.on( {
1673 click
: this.onIndicatorClick
.bind( this ),
1674 keypress
: this.onIndicatorKeyPress
.bind( this )
1677 this.$element
.append( this.input
.$element
);
1679 OO
.inheritClass( TextInputWithIndicatorWidget
, OO
.ui
.Widget
);
1680 OO
.mixinClass( TextInputWithIndicatorWidget
, OO
.ui
.mixin
.TabIndexedElement
);
1681 TextInputWithIndicatorWidget
.prototype.onIndicatorClick = function ( e
) {
1682 if ( !this.isDisabled() && e
.which
=== 1 ) {
1683 this.emit( 'indicator' );
1687 TextInputWithIndicatorWidget
.prototype.onIndicatorKeyPress = function ( e
) {
1688 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
1689 this.emit( 'indicator' );
1693 TextInputWithIndicatorWidget
.prototype.setDisabled = function ( disabled
) {
1694 TextInputWithIndicatorWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1696 this.input
.setDisabled( this.isDisabled() );
1702 * A wrapper for a widget that provides an enable/disable button
1707 * @param {OO.ui.Widget} widget
1708 * @param {Object} [config] Configuration options
1710 function OptionalWidget( widget
, config
) {
1713 config
= config
|| {};
1715 this.widget
= widget
;
1716 this.$overlay
= config
.$overlay
||
1717 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-overlay' );
1718 this.checkbox
= new OO
.ui
.CheckboxInputWidget( config
.checkbox
)
1719 .on( 'change', this.onCheckboxChange
, [], this );
1721 OptionalWidget
[ 'super' ].call( this, config
);
1723 // Forward most methods for convenience
1724 for ( k
in this.widget
) {
1725 if ( $.isFunction( this.widget
[ k
] ) && !this[ k
] ) {
1726 this[ k
] = this.widget
[ k
].bind( this.widget
);
1730 this.$overlay
.on( 'click', this.onOverlayClick
.bind( this ) );
1733 .addClass( 'mw-apisandbox-optionalWidget' )
1736 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-fields' ).append(
1737 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-widget' ).append(
1740 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-checkbox' ).append(
1741 this.checkbox
.$element
1746 this.setDisabled( widget
.isDisabled() );
1748 OO
.inheritClass( OptionalWidget
, OO
.ui
.Widget
);
1749 OptionalWidget
.prototype.onCheckboxChange = function ( checked
) {
1750 this.setDisabled( !checked
);
1752 OptionalWidget
.prototype.onOverlayClick = function () {
1753 this.setDisabled( false );
1754 if ( $.isFunction( this.widget
.focus
) ) {
1755 this.widget
.focus();
1758 OptionalWidget
.prototype.setDisabled = function ( disabled
) {
1759 OptionalWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1760 this.widget
.setDisabled( this.isDisabled() );
1761 this.checkbox
.setSelected( !this.isDisabled() );
1762 this.$overlay
.toggle( this.isDisabled() );
1766 $( ApiSandbox
.init
);
1768 module
.exports
= ApiSandbox
;
1770 }( jQuery
, mediaWiki
, OO
) );