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 * @return {boolean} Successful
721 loadFromHash: function () {
723 hash
= location
.hash
;
725 if ( oldhash
=== hash
) {
733 // I'm surprised this doesn't seem to exist in jQuery or mw.util.
735 hash
= hash
.replace( /\+/g, '%20' );
736 re
= /([^&=#]+)=?([^&#]*)/g;
737 while ( ( m
= re
.exec( hash
) ) ) {
738 params
[ decodeURIComponent( m
[ 1 ] ) ] = decodeURIComponent( m
[ 2 ] );
741 ApiSandbox
.updateUI( params
);
746 * Update the pages in the booklet
748 * @param {Object} [params] Optional query parameters to load
750 updateUI: function ( params
) {
751 var i
, page
, subpages
, j
, removePages
,
754 if ( !$.isPlainObject( params
) ) {
758 if ( updatingBooklet
) {
761 updatingBooklet
= true;
763 if ( params
!== undefined ) {
764 pages
.main
.loadQueryParams( params
);
766 addPages
.push( pages
.main
);
767 if ( resultPage
!== null ) {
768 addPages
.push( resultPage
);
770 pages
.main
.apiCheckValid();
773 while ( addPages
.length
) {
774 page
= addPages
.shift();
775 if ( bookletPages
[ i
] !== page
) {
776 for ( j
= i
; j
< bookletPages
.length
; j
++ ) {
777 if ( bookletPages
[ j
].getName() === page
.getName() ) {
778 bookletPages
.splice( j
, 1 );
781 bookletPages
.splice( i
, 0, page
);
782 booklet
.addPages( [ page
], i
);
786 if ( page
.getSubpages
) {
787 subpages
= page
.getSubpages();
788 for ( j
= 0; j
< subpages
.length
; j
++ ) {
789 if ( !pages
.hasOwnProperty( subpages
[ j
].key
) ) {
790 subpages
[ j
].indentLevel
= page
.indentLevel
+ 1;
791 pages
[ subpages
[ j
].key
] = new ApiSandbox
.PageLayout( subpages
[ j
] );
793 if ( params
!== undefined ) {
794 pages
[ subpages
[ j
].key
].loadQueryParams( params
);
796 addPages
.splice( j
, 0, pages
[ subpages
[ j
].key
] );
797 pages
[ subpages
[ j
].key
].apiCheckValid();
802 if ( bookletPages
.length
> i
) {
803 removePages
= bookletPages
.splice( i
, bookletPages
.length
- i
);
804 booklet
.removePages( removePages
);
807 if ( !booklet
.getCurrentPageName() ) {
808 booklet
.selectFirstSelectablePage();
811 updatingBooklet
= false;
816 * Reset button handler
818 resetUI: function () {
819 suppressErrors
= true;
821 main
: new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } )
824 ApiSandbox
.updateUI();
828 * Submit button handler
830 * @param {Object} [params] Use this set of params instead of those in the form fields.
831 * The form fields will be updated to match.
833 sendRequest: function ( params
) {
834 var page
, subpages
, i
, query
, $result
, $focus
,
835 progress
, $progressText
, progressLoading
,
837 paramsAreForced
= !!params
,
839 checkPages
= [ pages
.main
];
841 // Blur any focused widget before submit, because
842 // OO.ui.ButtonWidget doesn't take focus itself (T128054)
843 $focus
= $( '#mw-apisandbox-ui' ).find( document
.activeElement
);
844 if ( $focus
.length
) {
848 suppressErrors
= false;
850 // save widget state in params (or load from it if we are forced)
851 if ( paramsAreForced
) {
852 ApiSandbox
.updateUI( params
);
855 while ( checkPages
.length
) {
856 page
= checkPages
.shift();
857 deferreds
.push( page
.apiCheckValid() );
858 page
.getQueryParams( params
, displayParams
);
859 subpages
= page
.getSubpages();
860 for ( i
= 0; i
< subpages
.length
; i
++ ) {
861 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
862 checkPages
.push( pages
[ subpages
[ i
].key
] );
867 if ( !paramsAreForced
) {
868 // forced params means we are continuing a query; the base query should be preserved
869 baseRequestParams
= $.extend( {}, params
);
872 $.when
.apply( $, deferreds
).done( function () {
873 if ( $.inArray( false, arguments
) !== -1 ) {
874 windowManager
.openWindow( 'errorAlert', {
875 title
: mw
.message( 'apisandbox-submit-invalid-fields-title' ).parse(),
876 message
: mw
.message( 'apisandbox-submit-invalid-fields-message' ).parse(),
880 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
888 query
= $.param( displayParams
);
890 // Force a 'fm' format with wrappedhtml=1, if available
891 if ( params
.format
!== undefined ) {
892 if ( availableFormats
.hasOwnProperty( params
.format
+ 'fm' ) ) {
893 params
.format
= params
.format
+ 'fm';
895 if ( params
.format
.substr( -2 ) === 'fm' ) {
896 params
.wrappedhtml
= 1;
900 progressLoading
= false;
901 $progressText
= $( '<span>' ).text( mw
.message( 'apisandbox-sending-request' ).text() );
902 progress
= new OO
.ui
.ProgressBarWidget( {
904 $content
: $progressText
907 $result
= $( '<div>' )
908 .append( progress
.$element
);
910 resultPage
= page
= new OO
.ui
.PageLayout( '|results|' );
911 page
.setupOutlineItem = function () {
912 this.outlineItem
.setLabel( mw
.message( 'apisandbox-results' ).text() );
914 page
.$element
.empty()
916 new OO
.ui
.FieldLayout(
917 new OO
.ui
.TextInputWidget( {
919 value
: mw
.util
.wikiScript( 'api' ) + '?' + query
921 label
: mw
.message( 'apisandbox-request-url-label' ).parse()
926 ApiSandbox
.updateUI();
927 booklet
.setPage( '|results|' );
929 location
.href
= oldhash
= '#' + query
;
932 contentType
: 'multipart/form-data',
935 var xhr
= new window
.XMLHttpRequest();
936 xhr
.upload
.addEventListener( 'progress', function ( e
) {
937 if ( !progressLoading
) {
938 if ( e
.lengthComputable
) {
939 progress
.setProgress( e
.loaded
* 100 / e
.total
);
941 progress
.setProgress( false );
945 xhr
.addEventListener( 'progress', function ( e
) {
946 if ( !progressLoading
) {
947 progressLoading
= true;
948 $progressText
.text( mw
.message( 'apisandbox-loading-results' ).text() );
950 if ( e
.lengthComputable
) {
951 progress
.setProgress( e
.loaded
* 100 / e
.total
);
953 progress
.setProgress( false );
959 .then( null, function ( code
, data
, result
, jqXHR
) {
960 if ( code
!== 'http' ) {
961 // Not really an error, work around mw.Api thinking it is.
963 .resolve( result
, jqXHR
)
968 .fail( function ( code
, data
) {
969 var details
= 'HTTP error: ' + data
.exception
;
972 new OO
.ui
.LabelWidget( {
973 label
: mw
.message( 'apisandbox-results-error', details
).text(),
978 .done( function ( data
, jqXHR
) {
979 var m
, loadTime
, button
, clear
,
980 ct
= jqXHR
.getResponseHeader( 'Content-Type' );
983 if ( /^text\/mediawiki-api-prettyprint-wrapped(?:;|$)/.test( ct
) ) {
984 data
= JSON
.parse( data
);
985 if ( data
.modules
.length
) {
986 mw
.loader
.load( data
.modules
);
988 if ( data
.status
&& data
.status
!== 200 ) {
990 .addClass( 'api-pretty-header api-pretty-status' )
992 mw
.message( 'api-format-prettyprint-status', data
.status
, data
.statustext
).parse()
994 .appendTo( $result
);
996 $result
.append( Util
.parseHTML( data
.html
) );
997 loadTime
= data
.time
;
998 } else if ( ( m
= data
.match( /<pre[ >][\s\S]*<\/pre>/ ) ) ) {
999 $result
.append( Util
.parseHTML( m
[ 0 ] ) );
1000 if ( ( m
= data
.match( /"wgBackendResponseTime":\s*(\d+)/ ) ) ) {
1001 loadTime
= parseInt( m
[ 1 ], 10 );
1005 .addClass( 'api-pretty-content' )
1007 .appendTo( $result
);
1009 if ( paramsAreForced
|| data
[ 'continue' ] ) {
1011 $( '<div>' ).append(
1012 new OO
.ui
.ButtonWidget( {
1013 label
: mw
.message( 'apisandbox-continue' ).text()
1014 } ).on( 'click', function () {
1015 ApiSandbox
.sendRequest( $.extend( {}, baseRequestParams
, data
[ 'continue' ] ) );
1016 } ).setDisabled( !data
[ 'continue' ] ).$element
,
1017 ( clear
= new OO
.ui
.ButtonWidget( {
1018 label
: mw
.message( 'apisandbox-continue-clear' ).text()
1019 } ).on( 'click', function () {
1020 ApiSandbox
.updateUI( baseRequestParams
);
1021 clear
.setDisabled( true );
1022 booklet
.setPage( '|results|' );
1023 } ).setDisabled( !paramsAreForced
) ).$element
,
1024 new OO
.ui
.PopupButtonWidget( {
1028 $content
: $( '<div>' ).append( mw
.message( 'apisandbox-continue-help' ).parse() ),
1035 if ( typeof loadTime
=== 'number' ) {
1037 $( '<div>' ).append(
1038 new OO
.ui
.LabelWidget( {
1039 label
: mw
.message( 'apisandbox-request-time', loadTime
).text()
1045 if ( jqXHR
.getResponseHeader( 'MediaWiki-API-Error' ) === 'badtoken' ) {
1046 // Flush all saved tokens in case one of them is the bad one.
1047 Util
.markTokensBad();
1048 button
= new OO
.ui
.ButtonWidget( {
1049 label
: mw
.message( 'apisandbox-results-fixtoken' ).text()
1051 button
.on( 'click', ApiSandbox
.fixTokenAndResend
)
1052 .on( 'click', button
.setDisabled
, [ true ], button
)
1053 .$element
.appendTo( $result
);
1060 * Handler for the "Correct token and resubmit" button
1062 * Used on a 'badtoken' error, it re-fetches token parameters for all
1063 * pages and then re-submits the query.
1065 fixTokenAndResend: function () {
1066 var page
, subpages
, i
, k
,
1068 tokenWait
= { dummy
: true },
1069 checkPages
= [ pages
.main
],
1070 success = function ( k
) {
1071 delete tokenWait
[ k
];
1072 if ( ok
&& $.isEmptyObject( tokenWait
) ) {
1073 ApiSandbox
.sendRequest();
1076 failure = function ( k
) {
1077 delete tokenWait
[ k
];
1081 while ( checkPages
.length
) {
1082 page
= checkPages
.shift();
1084 if ( page
.tokenWidget
) {
1085 k
= page
.apiModule
+ page
.tokenWidget
.paramInfo
.name
;
1086 tokenWait
[ k
] = page
.tokenWidget
.fetchToken()
1087 .done( success
.bind( page
.tokenWidget
, k
) )
1088 .fail( failure
.bind( page
.tokenWidget
, k
) );
1091 subpages
= page
.getSubpages();
1092 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1093 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1094 checkPages
.push( pages
[ subpages
[ i
].key
] );
1099 success( 'dummy', '' );
1103 * Reset validity indicators for all widgets
1105 updateValidityIndicators: function () {
1106 var page
, subpages
, i
,
1107 checkPages
= [ pages
.main
];
1109 while ( checkPages
.length
) {
1110 page
= checkPages
.shift();
1111 page
.apiCheckValid();
1112 subpages
= page
.getSubpages();
1113 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1114 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1115 checkPages
.push( pages
[ subpages
[ i
].key
] );
1123 * PageLayout for API modules
1127 * @extends OO.ui.PageLayout
1129 * @param {Object} [config] Configuration options
1131 ApiSandbox
.PageLayout = function ( config
) {
1132 config
= $.extend( { prefix
: '' }, config
);
1133 this.displayText
= config
.key
;
1134 this.apiModule
= config
.path
;
1135 this.prefix
= config
.prefix
;
1136 this.paramInfo
= null;
1137 this.apiIsValid
= true;
1138 this.loadFromQueryParams
= null;
1140 this.tokenWidget
= null;
1141 this.indentLevel
= config
.indentLevel
? config
.indentLevel
: 0;
1142 ApiSandbox
.PageLayout
[ 'super' ].call( this, config
.key
, config
);
1143 this.loadParamInfo();
1145 OO
.inheritClass( ApiSandbox
.PageLayout
, OO
.ui
.PageLayout
);
1146 ApiSandbox
.PageLayout
.prototype.setupOutlineItem = function () {
1147 this.outlineItem
.setLevel( this.indentLevel
);
1148 this.outlineItem
.setLabel( this.displayText
);
1149 this.outlineItem
.setIcon( this.apiIsValid
|| suppressErrors
? null : 'alert' );
1150 this.outlineItem
.setIconTitle(
1151 this.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1156 * Fetch module information for this page's module, then create UI
1158 ApiSandbox
.PageLayout
.prototype.loadParamInfo = function () {
1159 var dynamicFieldset
, dynamicParamNameWidget
,
1161 removeDynamicParamWidget = function ( name
, layout
) {
1162 dynamicFieldset
.removeItems( [ layout
] );
1163 delete that
.widgets
[ name
];
1165 addDynamicParamWidget = function () {
1166 var name
, layout
, widget
, button
;
1168 // Check name is filled in
1169 name
= dynamicParamNameWidget
.getValue().trim();
1170 if ( name
=== '' ) {
1171 dynamicParamNameWidget
.focus();
1175 if ( that
.widgets
[ name
] !== undefined ) {
1176 windowManager
.openWindow( 'errorAlert', {
1178 'apisandbox-dynamic-error-exists', name
1183 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
1191 widget
= Util
.createWidgetForParameter( {
1198 button
= new OO
.ui
.ButtonWidget( {
1200 flags
: 'destructive'
1202 layout
= new OO
.ui
.ActionFieldLayout(
1210 button
.on( 'click', removeDynamicParamWidget
, [ name
, layout
] );
1211 that
.widgets
[ name
] = widget
;
1212 dynamicFieldset
.addItems( [ layout
], dynamicFieldset
.getItems().length
- 1 );
1215 dynamicParamNameWidget
.setValue( '' );
1218 this.$element
.empty()
1219 .append( new OO
.ui
.ProgressBarWidget( {
1221 text
: mw
.message( 'apisandbox-loading', this.displayText
).text()
1224 Util
.fetchModuleInfo( this.apiModule
)
1225 .done( function ( pi
) {
1226 var prefix
, i
, j
, dl
, widget
, $widgetLabel
, widgetField
, helpField
, tmp
, flag
, count
,
1228 deprecatedItems
= [],
1230 filterFmModules = function ( v
) {
1231 return v
.substr( -2 ) !== 'fm' ||
1232 !availableFormats
.hasOwnProperty( v
.substr( 0, v
.length
- 2 ) );
1234 widgetLabelOnClick = function () {
1235 var f
= this.getField();
1236 if ( $.isFunction( f
.setDisabled
) ) {
1237 f
.setDisabled( false );
1239 if ( $.isFunction( f
.focus
) ) {
1243 doNothing = function () {};
1245 // This is something of a hack. We always want the 'format' and
1246 // 'action' parameters from the main module to be specified,
1247 // and for 'format' we also want to simplify the dropdown since
1248 // we always send the 'fm' variant.
1249 if ( that
.apiModule
=== 'main' ) {
1250 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1251 if ( pi
.parameters
[ i
].name
=== 'action' ) {
1252 pi
.parameters
[ i
].required
= true;
1253 delete pi
.parameters
[ i
][ 'default' ];
1255 if ( pi
.parameters
[ i
].name
=== 'format' ) {
1256 tmp
= pi
.parameters
[ i
].type
;
1257 for ( j
= 0; j
< tmp
.length
; j
++ ) {
1258 availableFormats
[ tmp
[ j
] ] = true;
1260 pi
.parameters
[ i
].type
= $.grep( tmp
, filterFmModules
);
1261 pi
.parameters
[ i
][ 'default' ] = 'json';
1262 pi
.parameters
[ i
].required
= true;
1267 // Hide the 'wrappedhtml' parameter on format modules
1268 if ( pi
.group
=== 'format' ) {
1269 pi
.parameters
= $.grep( pi
.parameters
, function ( p
) {
1270 return p
.name
!== 'wrappedhtml';
1274 that
.paramInfo
= pi
;
1276 items
.push( new OO
.ui
.FieldLayout(
1277 new OO
.ui
.Widget( {} ).toggle( false ), {
1279 label
: Util
.parseHTML( pi
.description
)
1283 if ( pi
.helpurls
.length
) {
1284 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1285 label
: mw
.message( 'apisandbox-helpurls' ).text(),
1288 $content
: $( '<ul>' ).append( $.map( pi
.helpurls
, function ( link
) {
1289 return $( '<li>' ).append( $( '<a>', {
1299 if ( pi
.examples
.length
) {
1300 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1301 label
: mw
.message( 'apisandbox-examples' ).text(),
1304 $content
: $( '<ul>' ).append( $.map( pi
.examples
, function ( example
) {
1306 href
: '#' + example
.query
,
1307 html
: example
.description
1309 a
.find( 'a' ).contents().unwrap(); // Can't nest links
1310 return $( '<li>' ).append( a
);
1316 if ( buttons
.length
) {
1317 items
.push( new OO
.ui
.FieldLayout(
1318 new OO
.ui
.ButtonGroupWidget( {
1320 } ), { align
: 'top' }
1324 if ( pi
.parameters
.length
) {
1325 prefix
= that
.prefix
+ pi
.prefix
;
1326 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1327 widget
= Util
.createWidgetForParameter( pi
.parameters
[ i
] );
1328 that
.widgets
[ prefix
+ pi
.parameters
[ i
].name
] = widget
;
1329 if ( pi
.parameters
[ i
].tokentype
) {
1330 that
.tokenWidget
= widget
;
1334 dl
.append( $( '<dd>', {
1335 addClass
: 'description',
1336 append
: Util
.parseHTML( pi
.parameters
[ i
].description
)
1338 if ( pi
.parameters
[ i
].info
&& pi
.parameters
[ i
].info
.length
) {
1339 for ( j
= 0; j
< pi
.parameters
[ i
].info
.length
; j
++ ) {
1340 dl
.append( $( '<dd>', {
1342 append
: Util
.parseHTML( pi
.parameters
[ i
].info
[ j
] )
1348 switch ( pi
.parameters
[ i
].type
) {
1351 count
= mw
.config
.get( 'wgFormattedNamespaces' ).length
;
1355 if ( pi
.parameters
[ i
].highmax
!== undefined ) {
1356 dl
.append( $( '<dd>', {
1359 Util
.parseHTML( mw
.message(
1360 'api-help-param-limit2', pi
.parameters
[ i
].max
, pi
.parameters
[ i
].highmax
1363 Util
.parseHTML( mw
.message( 'apisandbox-param-limit' ).parse() )
1367 dl
.append( $( '<dd>', {
1370 Util
.parseHTML( mw
.message(
1371 'api-help-param-limit', pi
.parameters
[ i
].max
1374 Util
.parseHTML( mw
.message( 'apisandbox-param-limit' ).parse() )
1382 if ( pi
.parameters
[ i
].min
!== undefined ) {
1385 if ( pi
.parameters
[ i
].max
!== undefined ) {
1389 dl
.append( $( '<dd>', {
1391 append
: Util
.parseHTML( mw
.message(
1392 'api-help-param-integer-' + tmp
,
1393 Util
.apiBool( pi
.parameters
[ i
].multi
) ? 2 : 1,
1394 pi
.parameters
[ i
].min
, pi
.parameters
[ i
].max
1401 if ( $.isArray( pi
.parameters
[ i
].type
) ) {
1403 count
= pi
.parameters
[ i
].type
.length
;
1407 if ( Util
.apiBool( pi
.parameters
[ i
].multi
) ) {
1409 if ( flag
&& !( widget
instanceof OO
.ui
.CapsuleMultiselectWidget
) &&
1411 widget
instanceof OptionalWidget
&&
1412 widget
.widget
instanceof OO
.ui
.CapsuleMultiselectWidget
1415 tmp
.push( mw
.message( 'api-help-param-multi-separate' ).parse() );
1417 if ( count
> pi
.parameters
[ i
].lowlimit
) {
1419 mw
.message( 'api-help-param-multi-max',
1420 pi
.parameters
[ i
].lowlimit
, pi
.parameters
[ i
].highlimit
1425 dl
.append( $( '<dd>', {
1427 append
: Util
.parseHTML( tmp
.join( ' ' ) )
1431 helpField
= new OO
.ui
.FieldLayout(
1434 classes
: [ 'mw-apisandbox-spacer' ]
1437 classes
: [ 'mw-apisandbox-help-field' ],
1442 $widgetLabel
= $( '<span>' );
1443 widgetField
= new OO
.ui
.FieldLayout(
1447 classes
: [ 'mw-apisandbox-widget-field' ],
1448 label
: prefix
+ pi
.parameters
[ i
].name
,
1449 $label
: $widgetLabel
1453 // FieldLayout only does click for InputElement
1454 // widgets. So supply our own click handler.
1455 $widgetLabel
.on( 'click', widgetLabelOnClick
.bind( widgetField
) );
1457 // Don't grey out the label when the field is disabled,
1458 // it makes it too hard to read and our "disabled"
1459 // isn't really disabled.
1460 widgetField
.onFieldDisable( false );
1461 widgetField
.onFieldDisable
= doNothing
;
1463 if ( Util
.apiBool( pi
.parameters
[ i
].deprecated
) ) {
1464 deprecatedItems
.push( widgetField
, helpField
);
1466 items
.push( widgetField
, helpField
);
1471 if ( !pi
.parameters
.length
&& !Util
.apiBool( pi
.dynamicparameters
) ) {
1472 items
.push( new OO
.ui
.FieldLayout(
1473 new OO
.ui
.Widget( {} ).toggle( false ), {
1475 label
: Util
.parseHTML( mw
.message( 'apisandbox-no-parameters' ).parse() )
1480 that
.$element
.empty();
1482 new OO
.ui
.FieldsetLayout( {
1483 label
: that
.displayText
1484 } ).addItems( items
)
1485 .$element
.appendTo( that
.$element
);
1487 if ( Util
.apiBool( pi
.dynamicparameters
) ) {
1488 dynamicFieldset
= new OO
.ui
.FieldsetLayout();
1489 dynamicParamNameWidget
= new OO
.ui
.TextInputWidget( {
1490 placeholder
: mw
.message( 'apisandbox-dynamic-parameters-add-placeholder' ).text()
1491 } ).on( 'enter', addDynamicParamWidget
);
1492 dynamicFieldset
.addItems( [
1493 new OO
.ui
.FieldLayout(
1494 new OO
.ui
.Widget( {} ).toggle( false ), {
1496 label
: Util
.parseHTML( pi
.dynamicparameters
)
1499 new OO
.ui
.ActionFieldLayout(
1500 dynamicParamNameWidget
,
1501 new OO
.ui
.ButtonWidget( {
1503 flags
: 'progressive'
1504 } ).on( 'click', addDynamicParamWidget
),
1506 label
: mw
.message( 'apisandbox-dynamic-parameters-add-label' ).text(),
1513 $( '<legend>' ).text( mw
.message( 'apisandbox-dynamic-parameters' ).text() ),
1514 dynamicFieldset
.$element
1516 .appendTo( that
.$element
);
1519 if ( deprecatedItems
.length
) {
1520 tmp
= new OO
.ui
.FieldsetLayout().addItems( deprecatedItems
).toggle( false );
1523 $( '<legend>' ).append(
1524 new OO
.ui
.ToggleButtonWidget( {
1525 label
: mw
.message( 'apisandbox-deprecated-parameters' ).text()
1526 } ).on( 'change', tmp
.toggle
, [], tmp
).$element
1530 .appendTo( that
.$element
);
1533 // Load stored params, if any, then update the booklet if we
1534 // have subpages (or else just update our valid-indicator).
1535 tmp
= that
.loadFromQueryParams
;
1536 that
.loadFromQueryParams
= null;
1537 if ( $.isPlainObject( tmp
) ) {
1538 that
.loadQueryParams( tmp
);
1540 if ( that
.getSubpages().length
> 0 ) {
1541 ApiSandbox
.updateUI( tmp
);
1543 that
.apiCheckValid();
1545 } ).fail( function ( code
, detail
) {
1546 that
.$element
.empty()
1548 new OO
.ui
.LabelWidget( {
1549 label
: mw
.message( 'apisandbox-load-error', that
.apiModule
, detail
).text(),
1550 classes
: [ 'error' ]
1552 new OO
.ui
.ButtonWidget( {
1553 label
: mw
.message( 'apisandbox-retry' ).text()
1554 } ).on( 'click', that
.loadParamInfo
, [], that
).$element
1560 * Check that all widgets on the page are in a valid state.
1564 ApiSandbox
.PageLayout
.prototype.apiCheckValid = function () {
1567 if ( this.paramInfo
=== null ) {
1568 return $.Deferred().resolve( false ).promise();
1570 return $.when
.apply( $, $.map( this.widgets
, function ( widget
) {
1571 return widget
.apiCheckValid();
1572 } ) ).then( function () {
1573 that
.apiIsValid
= $.inArray( false, arguments
) === -1;
1574 if ( that
.getOutlineItem() ) {
1575 that
.getOutlineItem().setIcon( that
.apiIsValid
|| suppressErrors
? null : 'alert' );
1576 that
.getOutlineItem().setIconTitle(
1577 that
.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1580 return $.Deferred().resolve( that
.apiIsValid
).promise();
1586 * Load form fields from query parameters
1588 * @param {Object} params
1590 ApiSandbox
.PageLayout
.prototype.loadQueryParams = function ( params
) {
1591 if ( this.paramInfo
=== null ) {
1592 this.loadFromQueryParams
= params
;
1594 $.each( this.widgets
, function ( name
, widget
) {
1595 var v
= params
.hasOwnProperty( name
) ? params
[ name
] : undefined;
1596 widget
.setApiValue( v
);
1602 * Load query params from form fields
1604 * @param {Object} params Write query parameters into this object
1605 * @param {Object} displayParams Write query parameters for display into this object
1607 ApiSandbox
.PageLayout
.prototype.getQueryParams = function ( params
, displayParams
) {
1608 $.each( this.widgets
, function ( name
, widget
) {
1609 var value
= widget
.getApiValue();
1610 if ( value
!== undefined ) {
1611 params
[ name
] = value
;
1612 if ( $.isFunction( widget
.getApiValueForDisplay
) ) {
1613 value
= widget
.getApiValueForDisplay();
1615 displayParams
[ name
] = value
;
1621 * Fetch a list of subpage names loaded by this page
1625 ApiSandbox
.PageLayout
.prototype.getSubpages = function () {
1627 $.each( this.widgets
, function ( name
, widget
) {
1629 if ( $.isFunction( widget
.getSubmodules
) ) {
1630 submodules
= widget
.getSubmodules();
1631 for ( i
= 0; i
< submodules
.length
; i
++ ) {
1633 key
: name
+ '=' + submodules
[ i
].value
,
1634 path
: submodules
[ i
].path
,
1635 prefix
: widget
.paramInfo
.submoduleparamprefix
|| ''
1644 * A text input with a clickable indicator
1649 * @param {Object} [config] Configuration options
1651 function TextInputWithIndicatorWidget( config
) {
1654 config
= config
|| {};
1655 TextInputWithIndicatorWidget
[ 'super' ].call( this, config
);
1657 this.$indicator
= $( '<span>' ).addClass( 'mw-apisandbox-clickable-indicator' );
1658 OO
.ui
.mixin
.TabIndexedElement
.call(
1659 this, $.extend( {}, config
, { $tabIndexed
: this.$indicator
} )
1662 this.input
= new OO
.ui
.TextInputWidget( $.extend( {
1663 $indicator
: this.$indicator
,
1664 disabled
: this.isDisabled()
1665 }, config
.input
) );
1667 // Forward most methods for convenience
1668 for ( k
in this.input
) {
1669 if ( $.isFunction( this.input
[ k
] ) && !this[ k
] ) {
1670 this[ k
] = this.input
[ k
].bind( this.input
);
1674 this.$indicator
.on( {
1675 click
: this.onIndicatorClick
.bind( this ),
1676 keypress
: this.onIndicatorKeyPress
.bind( this )
1679 this.$element
.append( this.input
.$element
);
1681 OO
.inheritClass( TextInputWithIndicatorWidget
, OO
.ui
.Widget
);
1682 OO
.mixinClass( TextInputWithIndicatorWidget
, OO
.ui
.mixin
.TabIndexedElement
);
1683 TextInputWithIndicatorWidget
.prototype.onIndicatorClick = function ( e
) {
1684 if ( !this.isDisabled() && e
.which
=== 1 ) {
1685 this.emit( 'indicator' );
1689 TextInputWithIndicatorWidget
.prototype.onIndicatorKeyPress = function ( e
) {
1690 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
1691 this.emit( 'indicator' );
1695 TextInputWithIndicatorWidget
.prototype.setDisabled = function ( disabled
) {
1696 TextInputWithIndicatorWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1698 this.input
.setDisabled( this.isDisabled() );
1704 * A wrapper for a widget that provides an enable/disable button
1709 * @param {OO.ui.Widget} widget
1710 * @param {Object} [config] Configuration options
1712 function OptionalWidget( widget
, config
) {
1715 config
= config
|| {};
1717 this.widget
= widget
;
1718 this.$overlay
= config
.$overlay
||
1719 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-overlay' );
1720 this.checkbox
= new OO
.ui
.CheckboxInputWidget( config
.checkbox
)
1721 .on( 'change', this.onCheckboxChange
, [], this );
1723 OptionalWidget
[ 'super' ].call( this, config
);
1725 // Forward most methods for convenience
1726 for ( k
in this.widget
) {
1727 if ( $.isFunction( this.widget
[ k
] ) && !this[ k
] ) {
1728 this[ k
] = this.widget
[ k
].bind( this.widget
);
1732 this.$overlay
.on( 'click', this.onOverlayClick
.bind( this ) );
1735 .addClass( 'mw-apisandbox-optionalWidget' )
1738 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-fields' ).append(
1739 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-widget' ).append(
1742 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-checkbox' ).append(
1743 this.checkbox
.$element
1748 this.setDisabled( widget
.isDisabled() );
1750 OO
.inheritClass( OptionalWidget
, OO
.ui
.Widget
);
1751 OptionalWidget
.prototype.onCheckboxChange = function ( checked
) {
1752 this.setDisabled( !checked
);
1754 OptionalWidget
.prototype.onOverlayClick = function () {
1755 this.setDisabled( false );
1756 if ( $.isFunction( this.widget
.focus
) ) {
1757 this.widget
.focus();
1760 OptionalWidget
.prototype.setDisabled = function ( disabled
) {
1761 OptionalWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1762 this.widget
.setDisabled( this.isDisabled() );
1763 this.checkbox
.setSelected( !this.isDisabled() );
1764 this.$overlay
.toggle( this.isDisabled() );
1768 $( ApiSandbox
.init
);
1770 module
.exports
= ApiSandbox
;
1772 }( jQuery
, mediaWiki
, OO
) );