2 ( function ( $, mw
, OO
) {
4 var ApiSandbox
, Util
, WidgetMethods
, Validators
,
5 $content
, panel
, booklet
, oldhash
, windowManager
, fullscreenButton
,
10 suppressErrors
= true,
11 updatingBooklet
= false,
17 getApiValue: function () {
18 return this.getValue();
20 setApiValue: function ( v
) {
21 if ( v
=== undefined ) {
22 v
= this.paramInfo
[ 'default' ];
26 apiCheckValid: function () {
28 return this.isValid().done( function ( ok
) {
29 ok
= ok
|| suppressErrors
;
30 that
.setIcon( ok
? null : 'alert' );
31 that
.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
36 dateTimeInputWidget
: {
37 isValid: function () {
38 var ok
= !Util
.apiBool( this.paramInfo
.required
) || this.getApiValue() !== '';
39 return $.Deferred().resolve( ok
).promise();
44 alertTokenError: function ( code
, error
) {
45 windowManager
.openWindow( 'errorAlert', {
47 'apisandbox-results-fixtoken-fail', this.paramInfo
.tokentype
53 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
59 fetchToken: function () {
61 return api
.getToken( this.paramInfo
.tokentype
)
62 .done( this.setApiValue
.bind( this ) )
63 .fail( this.alertTokenError
.bind( this ) )
64 .always( this.popPending
.bind( this ) );
66 setApiValue: function ( v
) {
67 WidgetMethods
.textInputWidget
.setApiValue
.call( this, v
);
68 if ( v
=== '123ABC' ) {
75 getApiValueForDisplay: function () {
81 getApiValue: function () {
82 return this.getValue() ? 1 : undefined;
84 setApiValue: function ( v
) {
85 this.setValue( Util
.apiBool( v
) );
87 apiCheckValid: function () {
88 return $.Deferred().resolve( true ).promise();
93 getApiValue: function () {
94 var item
= this.getMenu().getSelectedItem();
95 return item
=== null ? undefined : item
.getData();
97 setApiValue: function ( v
) {
98 var menu
= this.getMenu();
100 if ( v
=== undefined ) {
101 v
= this.paramInfo
[ 'default' ];
103 if ( v
=== undefined ) {
106 menu
.selectItemByData( String( v
) );
109 apiCheckValid: function () {
110 var ok
= this.getApiValue() !== undefined || suppressErrors
;
111 this.setIcon( ok
? null : 'alert' );
112 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
113 return $.Deferred().resolve( ok
).promise();
118 getApiValue: function () {
119 var items
= this.getItemsData();
120 if ( items
.join( '' ).indexOf( '|' ) === -1 ) {
121 return items
.join( '|' );
123 return '\x1f' + items
.join( '\x1f' );
126 setApiValue: function ( v
) {
127 if ( v
=== undefined || v
=== '' || v
=== '\x1f' ) {
128 this.setItemsFromData( [] );
131 if ( v
.indexOf( '\x1f' ) !== 0 ) {
132 this.setItemsFromData( v
.split( '|' ) );
134 this.setItemsFromData( v
.substr( 1 ).split( '\x1f' ) );
138 apiCheckValid: function () {
139 var ok
= this.getApiValue() !== undefined || suppressErrors
;
140 this.setIcon( ok
? null : 'alert' );
141 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
142 return $.Deferred().resolve( ok
).promise();
147 getApiValue: function () {
148 return this.isDisabled() ? undefined : this.widget
.getApiValue();
150 setApiValue: function ( v
) {
151 this.setDisabled( v
=== undefined );
152 this.widget
.setApiValue( v
);
154 apiCheckValid: function () {
155 if ( this.isDisabled() ) {
156 return $.Deferred().resolve( true ).promise();
158 return this.widget
.apiCheckValid();
164 single: function () {
165 var v
= this.isDisabled() ? this.paramInfo
[ 'default' ] : this.getApiValue();
166 return v
=== undefined ? [] : [ { value
: v
, path
: this.paramInfo
.submodules
[ v
] } ];
169 var map
= this.paramInfo
.submodules
,
170 v
= this.isDisabled() ? this.paramInfo
[ 'default' ] : this.getApiValue();
171 return v
=== undefined || v
=== '' ? [] : $.map( String( v
).split( '|' ), function ( v
) {
172 return { value
: v
, path
: map
[ v
] };
178 getApiValueForDisplay: function () {
181 getApiValue: function () {
182 return this.getValue();
184 setApiValue: function () {
187 apiCheckValid: function () {
188 var ok
= this.getValue() !== null || suppressErrors
;
189 this.setIcon( ok
? null : 'alert' );
190 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
191 return $.Deferred().resolve( ok
).promise();
197 generic: function () {
198 return !Util
.apiBool( this.paramInfo
.required
) || this.getApiValue() !== '';
203 * @class mw.special.ApiSandbox.Utils
208 * Fetch API module info
210 * @param {string} module Module to fetch data for
211 * @return {jQuery.Promise}
213 fetchModuleInfo: function ( module
) {
215 deferred
= $.Deferred();
217 if ( moduleInfoCache
.hasOwnProperty( module
) ) {
219 .resolve( moduleInfoCache
[ module
] )
220 .promise( { abort: function () {} } );
222 apiPromise
= api
.post( {
226 uselang
: mw
.config
.get( 'wgUserLanguage' )
227 } ).done( function ( data
) {
230 if ( data
.warnings
&& data
.warnings
.paraminfo
) {
231 deferred
.reject( '???', data
.warnings
.paraminfo
[ '*' ] );
235 info
= data
.paraminfo
.modules
;
236 if ( !info
|| info
.length
!== 1 || info
[ 0 ].path
!== module
) {
237 deferred
.reject( '???', 'No module data returned' );
241 moduleInfoCache
[ module
] = info
[ 0 ];
242 deferred
.resolve( info
[ 0 ] );
243 } ).fail( function ( code
, details
) {
244 if ( code
=== 'http' ) {
245 details
= 'HTTP error: ' + details
.exception
;
246 } else if ( details
.error
) {
247 details
= details
.error
.info
;
249 deferred
.reject( code
, details
);
252 .promise( { abort
: apiPromise
.abort
} );
257 * Mark all currently-in-use tokens as bad
259 markTokensBad: function () {
260 var page
, subpages
, i
,
261 checkPages
= [ pages
.main
];
263 while ( checkPages
.length
) {
264 page
= checkPages
.shift();
266 if ( page
.tokenWidget
) {
267 api
.badToken( page
.tokenWidget
.paramInfo
.tokentype
);
270 subpages
= page
.getSubpages();
271 for ( i
= 0; i
< subpages
.length
; i
++ ) {
272 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
273 checkPages
.push( pages
[ subpages
[ i
].key
] );
280 * Test an API boolean
282 * @param {Mixed} value
285 apiBool: function ( value
) {
286 return value
!== undefined && value
!== false;
290 * Create a widget for a parameter.
292 * @param {Object} pi Parameter info from API
293 * @param {Object} opts Additional options
294 * @return {OO.ui.Widget}
296 createWidgetForParameter: function ( pi
, opts
) {
297 var widget
, innerWidget
, finalWidget
, items
, $button
, $content
, func
,
304 widget
= new OO
.ui
.ToggleSwitchWidget();
305 widget
.paramInfo
= pi
;
306 $.extend( widget
, WidgetMethods
.toggleSwitchWidget
);
307 pi
.required
= true; // Avoid wrapping in the non-required widget
312 if ( pi
.tokentype
) {
313 widget
= new TextInputWithIndicatorWidget( {
315 indicator
: 'previous',
316 indicatorTitle
: mw
.message( 'apisandbox-fetch-token' ).text(),
317 required
: Util
.apiBool( pi
.required
)
320 } else if ( Util
.apiBool( pi
.multi
) ) {
321 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
324 widget
.paramInfo
= pi
;
325 $.extend( widget
, WidgetMethods
.capsuleWidget
);
327 widget
= new OO
.ui
.TextInputWidget( {
328 required
: Util
.apiBool( pi
.required
)
331 if ( !Util
.apiBool( pi
.multi
) ) {
332 widget
.paramInfo
= pi
;
333 $.extend( widget
, WidgetMethods
.textInputWidget
);
334 widget
.setValidation( Validators
.generic
);
336 if ( pi
.tokentype
) {
337 $.extend( widget
, WidgetMethods
.tokenWidget
);
338 widget
.input
.paramInfo
= pi
;
339 $.extend( widget
.input
, WidgetMethods
.textInputWidget
);
340 $.extend( widget
.input
, WidgetMethods
.tokenWidget
);
341 widget
.on( 'indicator', widget
.fetchToken
, [], widget
);
346 widget
= new OO
.ui
.TextInputWidget( {
348 required
: Util
.apiBool( pi
.required
)
350 widget
.paramInfo
= pi
;
351 $.extend( widget
, WidgetMethods
.textInputWidget
);
352 widget
.setValidation( Validators
.generic
);
356 widget
= new OO
.ui
.TextInputWidget( {
358 required
: Util
.apiBool( pi
.required
)
360 widget
.paramInfo
= pi
;
361 $.extend( widget
, WidgetMethods
.textInputWidget
);
362 $.extend( widget
, WidgetMethods
.passwordWidget
);
363 widget
.setValidation( Validators
.generic
);
368 widget
= new OO
.ui
.NumberInputWidget( {
369 required
: Util
.apiBool( pi
.required
),
372 widget
.setIcon
= widget
.input
.setIcon
.bind( widget
.input
);
373 widget
.setIconTitle
= widget
.input
.setIconTitle
.bind( widget
.input
);
374 widget
.isValid
= widget
.input
.isValid
.bind( widget
.input
);
375 widget
.paramInfo
= pi
;
376 $.extend( widget
, WidgetMethods
.textInputWidget
);
377 if ( Util
.apiBool( pi
.enforcerange
) ) {
378 widget
.setRange( pi
.min
|| -Infinity
, pi
.max
|| Infinity
);
384 widget
= new OO
.ui
.NumberInputWidget( {
385 required
: Util
.apiBool( pi
.required
),
388 widget
.setIcon
= widget
.input
.setIcon
.bind( widget
.input
);
389 widget
.setIconTitle
= widget
.input
.setIconTitle
.bind( widget
.input
);
390 widget
.isValid
= widget
.input
.isValid
.bind( widget
.input
);
391 widget
.input
.setValidation( function ( value
) {
392 return value
=== 'max' || widget
.validateNumber( value
);
394 widget
.paramInfo
= pi
;
395 $.extend( widget
, WidgetMethods
.textInputWidget
);
396 widget
.setRange( pi
.min
|| 0, mw
.config
.get( 'apihighlimits' ) ? pi
.highmax
: pi
.max
);
401 widget
= new mw
.widgets
.datetime
.DateTimeInputWidget( {
403 format
: '${year|0}-${month|0}-${day|0}T${hour|0}:${minute|0}:${second|0}${zone|short}'
405 required
: Util
.apiBool( pi
.required
),
408 widget
.paramInfo
= pi
;
409 $.extend( widget
, WidgetMethods
.textInputWidget
);
410 $.extend( widget
, WidgetMethods
.dateTimeInputWidget
);
411 multiMode
= 'indicator';
415 widget
= new OO
.ui
.SelectFileWidget();
416 widget
.paramInfo
= pi
;
417 $.extend( widget
, WidgetMethods
.uploadWidget
);
421 items
= $.map( mw
.config
.get( 'wgFormattedNamespaces' ), function ( name
, ns
) {
423 name
= mw
.message( 'blanknamespace' ).text();
425 return new OO
.ui
.MenuOptionWidget( { data
: ns
, label
: name
} );
426 } ).sort( function ( a
, b
) {
427 return a
.data
- b
.data
;
429 if ( Util
.apiBool( pi
.multi
) ) {
430 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
431 menu
: { items
: items
}
433 widget
.paramInfo
= pi
;
434 $.extend( widget
, WidgetMethods
.capsuleWidget
);
436 widget
= new OO
.ui
.DropdownWidget( {
437 menu
: { items
: items
}
439 widget
.paramInfo
= pi
;
440 $.extend( widget
, WidgetMethods
.dropdownWidget
);
445 if ( !$.isArray( pi
.type
) ) {
446 throw new Error( 'Unknown parameter type ' + pi
.type
);
449 items
= $.map( pi
.type
, function ( v
) {
450 return new OO
.ui
.MenuOptionWidget( { data
: String( v
), label
: String( v
) } );
452 if ( Util
.apiBool( pi
.multi
) ) {
453 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
454 menu
: { items
: items
}
456 widget
.paramInfo
= pi
;
457 $.extend( widget
, WidgetMethods
.capsuleWidget
);
458 if ( Util
.apiBool( pi
.submodules
) ) {
459 widget
.getSubmodules
= WidgetMethods
.submoduleWidget
.multi
;
460 widget
.on( 'change', ApiSandbox
.updateUI
);
463 widget
= new OO
.ui
.DropdownWidget( {
464 menu
: { items
: items
}
466 widget
.paramInfo
= pi
;
467 $.extend( widget
, WidgetMethods
.dropdownWidget
);
468 if ( Util
.apiBool( pi
.submodules
) ) {
469 widget
.getSubmodules
= WidgetMethods
.submoduleWidget
.single
;
470 widget
.getMenu().on( 'choose', ApiSandbox
.updateUI
);
477 if ( Util
.apiBool( pi
.multi
) && multiMode
!== 'none' ) {
478 innerWidget
= widget
;
479 switch ( multiMode
) {
481 $content
= innerWidget
.$element
;
485 $button
= innerWidget
.$indicator
;
486 $button
.css( 'cursor', 'pointer' );
487 $button
.attr( 'tabindex', 0 );
488 $button
.parent().append( $button
);
489 innerWidget
.setIndicator( 'next' );
490 $content
= innerWidget
.$element
;
494 throw new Error( 'Unknown multiMode "' + multiMode
+ '"' );
497 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
498 allowArbitrary
: true,
500 classes
: [ 'mw-apisandbox-popup' ],
504 widget
.paramInfo
= pi
;
505 $.extend( widget
, WidgetMethods
.capsuleWidget
);
508 if ( !innerWidget
.isDisabled() ) {
509 innerWidget
.apiCheckValid().done( function ( ok
) {
511 widget
.addItemsFromData( [ innerWidget
.getApiValue() ] );
512 innerWidget
.setApiValue( undefined );
518 switch ( multiMode
) {
520 innerWidget
.connect( null, { enter
: func
} );
526 keypress: function ( e
) {
527 if ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) {
536 if ( Util
.apiBool( pi
.required
) || opts
.nooptional
) {
537 finalWidget
= widget
;
539 finalWidget
= new OptionalWidget( widget
);
540 finalWidget
.paramInfo
= pi
;
541 $.extend( finalWidget
, WidgetMethods
.optionalWidget
);
542 if ( widget
.getSubmodules
) {
543 finalWidget
.getSubmodules
= widget
.getSubmodules
.bind( widget
);
544 finalWidget
.on( 'disable', function () { setTimeout( ApiSandbox
.updateUI
); } );
546 finalWidget
.setDisabled( true );
549 widget
.setApiValue( pi
[ 'default' ] );
555 * Parse an HTML string, adding target="_blank" to any links
557 * @param {string} html HTML to parse
560 parseHTML: function ( html
) {
561 var $ret
= $( $.parseHTML( html
) );
562 $ret
.filter( 'a' ).add( $ret
.find( 'a' ) )
563 .filter( '[href]:not([target])' )
564 .attr( 'target', '_blank' );
570 * Interface to ApiSandbox UI
572 * @class mw.special.ApiSandbox
578 * Automatically called on $.ready()
583 $content
= $( '#mw-apisandbox' );
585 windowManager
= new OO
.ui
.WindowManager();
586 $( 'body' ).append( windowManager
.$element
);
587 windowManager
.addWindows( {
588 errorAlert
: new OO
.ui
.MessageDialog()
591 fullscreenButton
= new OO
.ui
.ButtonWidget( {
592 label
: mw
.message( 'apisandbox-fullscreen' ).text(),
593 title
: mw
.message( 'apisandbox-fullscreen-tooltip' ).text()
594 } ).on( 'click', ApiSandbox
.toggleFullscreen
);
596 $toolbar
= $( '<div>' )
597 .addClass( 'mw-apisandbox-toolbar' )
599 fullscreenButton
.$element
,
600 new OO
.ui
.ButtonWidget( {
601 label
: mw
.message( 'apisandbox-submit' ).text(),
602 flags
: [ 'primary', 'constructive' ]
603 } ).on( 'click', ApiSandbox
.sendRequest
).$element
,
604 new OO
.ui
.ButtonWidget( {
605 label
: mw
.message( 'apisandbox-reset' ).text(),
607 } ).on( 'click', ApiSandbox
.resetUI
).$element
610 booklet
= new OO
.ui
.BookletLayout( {
615 panel
= new OO
.ui
.PanelLayout( {
616 classes
: [ 'mw-apisandbox-container' ],
617 content
: [ booklet
],
622 pages
.main
= new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } );
624 // Parse the current hash string
625 if ( !ApiSandbox
.loadFromHash() ) {
626 ApiSandbox
.updateUI();
629 // If the hashchange event exists, use it. Otherwise, fake it.
630 // And, of course, IE has to be dumb.
631 if ( 'onhashchange' in window
&&
632 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
634 $( window
).on( 'hashchange', ApiSandbox
.loadFromHash
);
636 setInterval( function () {
637 if ( oldhash
!== location
.hash
) {
638 ApiSandbox
.loadFromHash();
645 .append( $( '<p>' ).append( mw
.message( 'apisandbox-intro' ).parse() ) )
647 $( '<div>', { id
: 'mw-apisandbox-ui' } )
649 .append( panel
.$element
)
652 $( window
).on( 'resize', ApiSandbox
.resizePanel
);
654 ApiSandbox
.resizePanel();
658 * Toggle "fullscreen" mode
660 toggleFullscreen: function () {
661 var $body
= $( document
.body
);
663 $body
.toggleClass( 'mw-apisandbox-fullscreen' );
664 if ( $body
.hasClass( 'mw-apisandbox-fullscreen' ) ) {
665 fullscreenButton
.setLabel( mw
.message( 'apisandbox-unfullscreen' ).text() );
666 fullscreenButton
.setTitle( mw
.message( 'apisandbox-unfullscreen-tooltip' ).text() );
667 $body
.append( $( '#mw-apisandbox-ui' ) );
669 fullscreenButton
.setLabel( mw
.message( 'apisandbox-fullscreen' ).text() );
670 fullscreenButton
.setTitle( mw
.message( 'apisandbox-fullscreen-tooltip' ).text() );
671 $content
.append( $( '#mw-apisandbox-ui' ) );
673 ApiSandbox
.resizePanel();
677 * Set the height of the panel based on the current viewport.
679 resizePanel: function () {
680 var height
= $( window
).height(),
681 contentTop
= $content
.offset().top
;
683 if ( $( document
.body
).hasClass( 'mw-apisandbox-fullscreen' ) ) {
684 height
-= panel
.$element
.offset().top
- $( '#mw-apisandbox-ui' ).offset().top
;
685 panel
.$element
.height( height
- 1 );
687 // Subtract the height of the intro text
688 height
-= panel
.$element
.offset().top
- contentTop
;
690 panel
.$element
.height( height
- 10 );
691 $( window
).scrollTop( contentTop
- 5 );
696 * Update the current query when the page hash changes
698 loadFromHash: function () {
700 hash
= location
.hash
;
702 if ( oldhash
=== hash
) {
710 // I'm surprised this doesn't seem to exist in jQuery or mw.util.
712 hash
= hash
.replace( /\+/g, '%20' );
713 re
= /([^&=#]+)=?([^&#]*)/g;
714 while ( ( m
= re
.exec( hash
) ) ) {
715 params
[ decodeURIComponent( m
[ 1 ] ) ] = decodeURIComponent( m
[ 2 ] );
718 ApiSandbox
.updateUI( params
);
723 * Update the pages in the booklet
725 * @param {Object} [params] Optional query parameters to load
727 updateUI: function ( params
) {
728 var i
, page
, subpages
, j
, removePages
,
731 if ( !$.isPlainObject( params
) ) {
735 if ( updatingBooklet
) {
738 updatingBooklet
= true;
740 if ( params
!== undefined ) {
741 pages
.main
.loadQueryParams( params
);
743 addPages
.push( pages
.main
);
744 if ( resultPage
!== null ) {
745 addPages
.push( resultPage
);
747 pages
.main
.apiCheckValid();
750 while ( addPages
.length
) {
751 page
= addPages
.shift();
752 if ( bookletPages
[ i
] !== page
) {
753 for ( j
= i
; j
< bookletPages
.length
; j
++ ) {
754 if ( bookletPages
[ j
].getName() === page
.getName() ) {
755 bookletPages
.splice( j
, 1 );
758 bookletPages
.splice( i
, 0, page
);
759 booklet
.addPages( [ page
], i
);
763 if ( page
.getSubpages
) {
764 subpages
= page
.getSubpages();
765 for ( j
= 0; j
< subpages
.length
; j
++ ) {
766 if ( !pages
.hasOwnProperty( subpages
[ j
].key
) ) {
767 subpages
[ j
].indentLevel
= page
.indentLevel
+ 1;
768 pages
[ subpages
[ j
].key
] = new ApiSandbox
.PageLayout( subpages
[ j
] );
770 if ( params
!== undefined ) {
771 pages
[ subpages
[ j
].key
].loadQueryParams( params
);
773 addPages
.splice( j
, 0, pages
[ subpages
[ j
].key
] );
774 pages
[ subpages
[ j
].key
].apiCheckValid();
779 if ( bookletPages
.length
> i
) {
780 removePages
= bookletPages
.splice( i
, bookletPages
.length
- i
);
781 booklet
.removePages( removePages
);
784 if ( !booklet
.getCurrentPageName() ) {
785 booklet
.selectFirstSelectablePage();
788 updatingBooklet
= false;
793 * Reset button handler
795 resetUI: function () {
796 suppressErrors
= true;
798 main
: new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } )
801 ApiSandbox
.updateUI();
805 * Submit button handler
807 sendRequest: function () {
808 var page
, subpages
, i
, query
, $result
, $focus
,
809 progress
, $progressText
, progressLoading
,
813 checkPages
= [ pages
.main
];
815 // Blur any focused widget before submit, because
816 // OO.ui.ButtonWidget doesn't take focus itself (T128054)
817 $focus
= $( '#mw-apisandbox-ui' ).find( document
.activeElement
);
818 if ( $focus
.length
) {
822 suppressErrors
= false;
824 while ( checkPages
.length
) {
825 page
= checkPages
.shift();
826 deferreds
.push( page
.apiCheckValid() );
827 page
.getQueryParams( params
, displayParams
);
828 subpages
= page
.getSubpages();
829 for ( i
= 0; i
< subpages
.length
; i
++ ) {
830 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
831 checkPages
.push( pages
[ subpages
[ i
].key
] );
836 $.when
.apply( $, deferreds
).done( function () {
837 if ( $.inArray( false, arguments
) !== -1 ) {
838 windowManager
.openWindow( 'errorAlert', {
839 title
: mw
.message( 'apisandbox-submit-invalid-fields-title' ).parse(),
840 message
: mw
.message( 'apisandbox-submit-invalid-fields-message' ).parse(),
844 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
852 query
= $.param( displayParams
);
854 // Force a 'fm' format with wrappedhtml=1, if available
855 if ( params
.format
!== undefined ) {
856 if ( availableFormats
.hasOwnProperty( params
.format
+ 'fm' ) ) {
857 params
.format
= params
.format
+ 'fm';
859 if ( params
.format
.substr( -2 ) === 'fm' ) {
860 params
.wrappedhtml
= 1;
864 progressLoading
= false;
865 $progressText
= $( '<span>' ).text( mw
.message( 'apisandbox-sending-request' ).text() );
866 progress
= new OO
.ui
.ProgressBarWidget( {
868 $content
: $progressText
871 $result
= $( '<div>' )
872 .append( progress
.$element
);
874 resultPage
= page
= new OO
.ui
.PageLayout( '|results|' );
875 page
.setupOutlineItem = function () {
876 this.outlineItem
.setLabel( mw
.message( 'apisandbox-results' ).text() );
878 page
.$element
.empty()
880 new OO
.ui
.FieldLayout(
881 new OO
.ui
.TextInputWidget( {
883 value
: mw
.util
.wikiScript( 'api' ) + '?' + query
885 label
: mw
.message( 'apisandbox-request-url-label' ).parse()
890 ApiSandbox
.updateUI();
891 booklet
.setPage( '|results|' );
893 location
.href
= oldhash
= '#' + query
;
896 contentType
: 'multipart/form-data',
899 var xhr
= new window
.XMLHttpRequest();
900 xhr
.upload
.addEventListener( 'progress', function ( e
) {
901 if ( !progressLoading
) {
902 if ( e
.lengthComputable
) {
903 progress
.setProgress( e
.loaded
* 100 / e
.total
);
905 progress
.setProgress( false );
909 xhr
.addEventListener( 'progress', function ( e
) {
910 if ( !progressLoading
) {
911 progressLoading
= true;
912 $progressText
.text( mw
.message( 'apisandbox-loading-results' ).text() );
914 if ( e
.lengthComputable
) {
915 progress
.setProgress( e
.loaded
* 100 / e
.total
);
917 progress
.setProgress( false );
923 .then( null, function ( code
, data
, result
, jqXHR
) {
924 if ( code
!== 'http' ) {
925 // Not really an error, work around mw.Api thinking it is.
927 .resolve( result
, jqXHR
)
932 .fail( function ( code
, data
) {
933 var details
= 'HTTP error: ' + data
.exception
;
936 new OO
.ui
.LabelWidget( {
937 label
: mw
.message( 'apisandbox-results-error', details
).text(),
942 .done( function ( data
, jqXHR
) {
943 var m
, loadTime
, button
,
944 ct
= jqXHR
.getResponseHeader( 'Content-Type' );
947 if ( /^text\/mediawiki-api-prettyprint-wrapped(?:;|$)/.test( ct
) ) {
948 data
= $.parseJSON( data
);
949 if ( data
.modules
.length
) {
950 mw
.loader
.load( data
.modules
);
952 $result
.append( Util
.parseHTML( data
.html
) );
953 loadTime
= data
.time
;
954 } else if ( ( m
= data
.match( /<pre[ >][\s\S]*<\/pre>/ ) ) ) {
955 $result
.append( Util
.parseHTML( m
[ 0 ] ) );
956 if ( ( m
= data
.match( /"wgBackendResponseTime":\s*(\d+)/ ) ) ) {
957 loadTime
= parseInt( m
[ 1 ], 10 );
961 .addClass( 'api-pretty-content' )
963 .appendTo( $result
);
965 if ( typeof loadTime
=== 'number' ) {
968 new OO
.ui
.LabelWidget( {
969 label
: mw
.message( 'apisandbox-request-time', loadTime
).text()
975 if ( jqXHR
.getResponseHeader( 'MediaWiki-API-Error' ) === 'badtoken' ) {
976 // Flush all saved tokens in case one of them is the bad one.
977 Util
.markTokensBad();
978 button
= new OO
.ui
.ButtonWidget( {
979 label
: mw
.message( 'apisandbox-results-fixtoken' ).text()
981 button
.on( 'click', ApiSandbox
.fixTokenAndResend
)
982 .on( 'click', button
.setDisabled
, [ true ], button
)
983 .$element
.appendTo( $result
);
990 * Handler for the "Correct token and resubmit" button
992 * Used on a 'badtoken' error, it re-fetches token parameters for all
993 * pages and then re-submits the query.
995 fixTokenAndResend: function () {
996 var page
, subpages
, i
, k
,
998 tokenWait
= { dummy
: true },
999 checkPages
= [ pages
.main
],
1000 success = function ( k
) {
1001 delete tokenWait
[ k
];
1002 if ( ok
&& $.isEmptyObject( tokenWait
) ) {
1003 ApiSandbox
.sendRequest();
1006 failure = function ( k
) {
1007 delete tokenWait
[ k
];
1011 while ( checkPages
.length
) {
1012 page
= checkPages
.shift();
1014 if ( page
.tokenWidget
) {
1015 k
= page
.apiModule
+ page
.tokenWidget
.paramInfo
.name
;
1016 tokenWait
[ k
] = page
.tokenWidget
.fetchToken()
1017 .done( success
.bind( page
.tokenWidget
, k
) )
1018 .fail( failure
.bind( page
.tokenWidget
, k
) );
1021 subpages
= page
.getSubpages();
1022 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1023 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1024 checkPages
.push( pages
[ subpages
[ i
].key
] );
1029 success( 'dummy', '' );
1033 * Reset validity indicators for all widgets
1035 updateValidityIndicators: function () {
1036 var page
, subpages
, i
,
1037 checkPages
= [ pages
.main
];
1039 while ( checkPages
.length
) {
1040 page
= checkPages
.shift();
1041 page
.apiCheckValid();
1042 subpages
= page
.getSubpages();
1043 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1044 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1045 checkPages
.push( pages
[ subpages
[ i
].key
] );
1053 * PageLayout for API modules
1057 * @extends OO.ui.PageLayout
1059 * @param {Object} [config] Configuration options
1061 ApiSandbox
.PageLayout = function ( config
) {
1062 config
= $.extend( { prefix
: '' }, config
);
1063 this.displayText
= config
.key
;
1064 this.apiModule
= config
.path
;
1065 this.prefix
= config
.prefix
;
1066 this.paramInfo
= null;
1067 this.apiIsValid
= true;
1068 this.loadFromQueryParams
= null;
1070 this.tokenWidget
= null;
1071 this.indentLevel
= config
.indentLevel
? config
.indentLevel
: 0;
1072 ApiSandbox
.PageLayout
[ 'super' ].call( this, config
.key
, config
);
1073 this.loadParamInfo();
1075 OO
.inheritClass( ApiSandbox
.PageLayout
, OO
.ui
.PageLayout
);
1076 ApiSandbox
.PageLayout
.prototype.setupOutlineItem = function () {
1077 this.outlineItem
.setLevel( this.indentLevel
);
1078 this.outlineItem
.setLabel( this.displayText
);
1079 this.outlineItem
.setIcon( this.apiIsValid
|| suppressErrors
? null : 'alert' );
1080 this.outlineItem
.setIconTitle(
1081 this.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1086 * Fetch module information for this page's module, then create UI
1088 ApiSandbox
.PageLayout
.prototype.loadParamInfo = function () {
1089 var dynamicFieldset
, dynamicParamNameWidget
,
1091 removeDynamicParamWidget = function ( name
, layout
) {
1092 dynamicFieldset
.removeItems( [ layout
] );
1093 delete that
.widgets
[ name
];
1095 addDynamicParamWidget = function () {
1096 var name
, layout
, widget
, button
;
1098 // Check name is filled in
1099 name
= dynamicParamNameWidget
.getValue().trim();
1100 if ( name
=== '' ) {
1101 dynamicParamNameWidget
.focus();
1105 if ( that
.widgets
[ name
] !== undefined ) {
1106 windowManager
.openWindow( 'errorAlert', {
1108 'apisandbox-dynamic-error-exists', name
1113 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
1121 widget
= Util
.createWidgetForParameter( {
1128 button
= new OO
.ui
.ButtonWidget( {
1130 flags
: 'destructive'
1132 layout
= new OO
.ui
.ActionFieldLayout(
1140 button
.on( 'click', removeDynamicParamWidget
, [ name
, layout
] );
1141 that
.widgets
[ name
] = widget
;
1142 dynamicFieldset
.addItems( [ layout
], dynamicFieldset
.getItems().length
- 1 );
1145 dynamicParamNameWidget
.setValue( '' );
1148 this.$element
.empty()
1149 .append( new OO
.ui
.ProgressBarWidget( {
1151 text
: mw
.message( 'apisandbox-loading', this.displayText
).text()
1154 Util
.fetchModuleInfo( this.apiModule
)
1155 .done( function ( pi
) {
1156 var prefix
, i
, j
, dl
, widget
, $widgetLabel
, widgetField
, helpField
, tmp
, flag
, count
,
1158 deprecatedItems
= [],
1160 filterFmModules = function ( v
) {
1161 return v
.substr( -2 ) !== 'fm' ||
1162 !availableFormats
.hasOwnProperty( v
.substr( 0, v
.length
- 2 ) );
1164 widgetLabelOnClick = function () {
1165 var f
= this.getField();
1166 if ( $.isFunction( f
.setDisabled
) ) {
1167 f
.setDisabled( false );
1169 if ( $.isFunction( f
.focus
) ) {
1173 doNothing = function () {};
1175 // This is something of a hack. We always want the 'format' and
1176 // 'action' parameters from the main module to be specified,
1177 // and for 'format' we also want to simplify the dropdown since
1178 // we always send the 'fm' variant.
1179 if ( that
.apiModule
=== 'main' ) {
1180 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1181 if ( pi
.parameters
[ i
].name
=== 'action' ) {
1182 pi
.parameters
[ i
].required
= true;
1183 delete pi
.parameters
[ i
][ 'default' ];
1185 if ( pi
.parameters
[ i
].name
=== 'format' ) {
1186 tmp
= pi
.parameters
[ i
].type
;
1187 for ( j
= 0; j
< tmp
.length
; j
++ ) {
1188 availableFormats
[ tmp
[ j
] ] = true;
1190 pi
.parameters
[ i
].type
= $.grep( tmp
, filterFmModules
);
1191 pi
.parameters
[ i
][ 'default' ] = 'json';
1192 pi
.parameters
[ i
].required
= true;
1197 // Hide the 'wrappedhtml' parameter on format modules
1198 if ( pi
.group
=== 'format' ) {
1199 pi
.parameters
= $.grep( pi
.parameters
, function ( p
) {
1200 return p
.name
!== 'wrappedhtml';
1204 that
.paramInfo
= pi
;
1206 items
.push( new OO
.ui
.FieldLayout(
1207 new OO
.ui
.Widget( {} ).toggle( false ), {
1209 label
: Util
.parseHTML( pi
.description
)
1213 if ( pi
.helpurls
.length
) {
1214 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1215 label
: mw
.message( 'apisandbox-helpurls' ).text(),
1218 $content
: $( '<ul>' ).append( $.map( pi
.helpurls
, function ( link
) {
1219 return $( '<li>' ).append( $( '<a>', {
1229 if ( pi
.examples
.length
) {
1230 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1231 label
: mw
.message( 'apisandbox-examples' ).text(),
1234 $content
: $( '<ul>' ).append( $.map( pi
.examples
, function ( example
) {
1236 href
: '#' + example
.query
,
1237 html
: example
.description
1239 a
.find( 'a' ).contents().unwrap(); // Can't nest links
1240 return $( '<li>' ).append( a
);
1246 if ( buttons
.length
) {
1247 items
.push( new OO
.ui
.FieldLayout(
1248 new OO
.ui
.ButtonGroupWidget( {
1250 } ), { align
: 'top' }
1254 if ( pi
.parameters
.length
) {
1255 prefix
= that
.prefix
+ pi
.prefix
;
1256 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1257 widget
= Util
.createWidgetForParameter( pi
.parameters
[ i
] );
1258 that
.widgets
[ prefix
+ pi
.parameters
[ i
].name
] = widget
;
1259 if ( pi
.parameters
[ i
].tokentype
) {
1260 that
.tokenWidget
= widget
;
1264 dl
.append( $( '<dd>', {
1265 addClass
: 'description',
1266 append
: Util
.parseHTML( pi
.parameters
[ i
].description
)
1268 if ( pi
.parameters
[ i
].info
&& pi
.parameters
[ i
].info
.length
) {
1269 for ( j
= 0; j
< pi
.parameters
[ i
].info
.length
; j
++ ) {
1270 dl
.append( $( '<dd>', {
1272 append
: Util
.parseHTML( pi
.parameters
[ i
].info
[ j
] )
1278 switch ( pi
.parameters
[ i
].type
) {
1281 count
= mw
.config
.get( 'wgFormattedNamespaces' ).length
;
1285 if ( pi
.parameters
[ i
].highmax
!== undefined ) {
1286 dl
.append( $( '<dd>', {
1288 append
: Util
.parseHTML( mw
.message(
1289 'api-help-param-limit2', pi
.parameters
[ i
].max
, pi
.parameters
[ i
].highmax
1293 dl
.append( $( '<dd>', {
1295 append
: Util
.parseHTML( mw
.message(
1296 'api-help-param-limit', pi
.parameters
[ i
].max
1304 if ( pi
.parameters
[ i
].min
!== undefined ) {
1307 if ( pi
.parameters
[ i
].max
!== undefined ) {
1311 dl
.append( $( '<dd>', {
1313 append
: Util
.parseHTML( mw
.message(
1314 'api-help-param-integer-' + tmp
,
1315 Util
.apiBool( pi
.parameters
[ i
].multi
) ? 2 : 1,
1316 pi
.parameters
[ i
].min
, pi
.parameters
[ i
].max
1323 if ( $.isArray( pi
.parameters
[ i
].type
) ) {
1325 count
= pi
.parameters
[ i
].type
.length
;
1329 if ( Util
.apiBool( pi
.parameters
[ i
].multi
) ) {
1331 if ( flag
&& !( widget
instanceof OO
.ui
.CapsuleMultiselectWidget
) &&
1333 widget
instanceof OptionalWidget
&&
1334 widget
.widget
instanceof OO
.ui
.CapsuleMultiselectWidget
1337 tmp
.push( mw
.message( 'api-help-param-multi-separate' ).parse() );
1339 if ( count
> pi
.parameters
[ i
].lowlimit
) {
1341 mw
.message( 'api-help-param-multi-max',
1342 pi
.parameters
[ i
].lowlimit
, pi
.parameters
[ i
].highlimit
1347 dl
.append( $( '<dd>', {
1349 append
: Util
.parseHTML( tmp
.join( ' ' ) )
1353 helpField
= new OO
.ui
.FieldLayout(
1356 classes
: [ 'mw-apisandbox-spacer' ]
1359 classes
: [ 'mw-apisandbox-help-field' ],
1364 $widgetLabel
= $( '<span>' );
1365 widgetField
= new OO
.ui
.FieldLayout(
1369 classes
: [ 'mw-apisandbox-widget-field' ],
1370 label
: prefix
+ pi
.parameters
[ i
].name
,
1371 $label
: $widgetLabel
1375 // FieldLayout only does click for InputElement
1376 // widgets. So supply our own click handler.
1377 $widgetLabel
.on( 'click', widgetLabelOnClick
.bind( widgetField
) );
1379 // Don't grey out the label when the field is disabled,
1380 // it makes it too hard to read and our "disabled"
1381 // isn't really disabled.
1382 widgetField
.onFieldDisable
= doNothing
;
1384 if ( Util
.apiBool( pi
.parameters
[ i
].deprecated
) ) {
1385 deprecatedItems
.push( widgetField
, helpField
);
1387 items
.push( widgetField
, helpField
);
1392 if ( !pi
.parameters
.length
&& !Util
.apiBool( pi
.dynamicparameters
) ) {
1393 items
.push( new OO
.ui
.FieldLayout(
1394 new OO
.ui
.Widget( {} ).toggle( false ), {
1396 label
: Util
.parseHTML( mw
.message( 'apisandbox-no-parameters' ).parse() )
1401 that
.$element
.empty();
1403 new OO
.ui
.FieldsetLayout( {
1404 label
: that
.displayText
1405 } ).addItems( items
)
1406 .$element
.appendTo( that
.$element
);
1408 if ( Util
.apiBool( pi
.dynamicparameters
) ) {
1409 dynamicFieldset
= new OO
.ui
.FieldsetLayout();
1410 dynamicParamNameWidget
= new OO
.ui
.TextInputWidget( {
1411 placeholder
: mw
.message( 'apisandbox-dynamic-parameters-add-placeholder' ).text()
1412 } ).on( 'enter', addDynamicParamWidget
);
1413 dynamicFieldset
.addItems( [
1414 new OO
.ui
.FieldLayout(
1415 new OO
.ui
.Widget( {} ).toggle( false ), {
1417 label
: Util
.parseHTML( pi
.dynamicparameters
)
1420 new OO
.ui
.ActionFieldLayout(
1421 dynamicParamNameWidget
,
1422 new OO
.ui
.ButtonWidget( {
1424 flags
: 'constructive'
1425 } ).on( 'click', addDynamicParamWidget
),
1427 label
: mw
.message( 'apisandbox-dynamic-parameters-add-label' ).text(),
1434 $( '<legend>' ).text( mw
.message( 'apisandbox-dynamic-parameters' ).text() ),
1435 dynamicFieldset
.$element
1437 .appendTo( that
.$element
);
1440 if ( deprecatedItems
.length
) {
1441 tmp
= new OO
.ui
.FieldsetLayout().addItems( deprecatedItems
).toggle( false );
1444 $( '<legend>' ).append(
1445 new OO
.ui
.ToggleButtonWidget( {
1446 label
: mw
.message( 'apisandbox-deprecated-parameters' ).text()
1447 } ).on( 'change', tmp
.toggle
, [], tmp
).$element
1451 .appendTo( that
.$element
);
1454 // Load stored params, if any, then update the booklet if we
1455 // have subpages (or else just update our valid-indicator).
1456 tmp
= that
.loadFromQueryParams
;
1457 that
.loadFromQueryParams
= null;
1458 if ( $.isPlainObject( tmp
) ) {
1459 that
.loadQueryParams( tmp
);
1461 if ( that
.getSubpages().length
> 0 ) {
1462 ApiSandbox
.updateUI( tmp
);
1464 that
.apiCheckValid();
1466 } ).fail( function ( code
, detail
) {
1467 that
.$element
.empty()
1469 new OO
.ui
.LabelWidget( {
1470 label
: mw
.message( 'apisandbox-load-error', that
.apiModule
, detail
).text(),
1471 classes
: [ 'error' ]
1473 new OO
.ui
.ButtonWidget( {
1474 label
: mw
.message( 'apisandbox-retry' ).text()
1475 } ).on( 'click', that
.loadParamInfo
, [], that
).$element
1481 * Check that all widgets on the page are in a valid state.
1485 ApiSandbox
.PageLayout
.prototype.apiCheckValid = function () {
1488 if ( this.paramInfo
=== null ) {
1489 return $.Deferred().resolve( false ).promise();
1491 return $.when
.apply( $, $.map( this.widgets
, function ( widget
) {
1492 return widget
.apiCheckValid();
1493 } ) ).then( function () {
1494 that
.apiIsValid
= $.inArray( false, arguments
) === -1;
1495 if ( that
.getOutlineItem() ) {
1496 that
.getOutlineItem().setIcon( that
.apiIsValid
|| suppressErrors
? null : 'alert' );
1497 that
.getOutlineItem().setIconTitle(
1498 that
.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1501 return $.Deferred().resolve( that
.apiIsValid
).promise();
1507 * Load form fields from query parameters
1509 * @param {Object} params
1511 ApiSandbox
.PageLayout
.prototype.loadQueryParams = function ( params
) {
1512 if ( this.paramInfo
=== null ) {
1513 this.loadFromQueryParams
= params
;
1515 $.each( this.widgets
, function ( name
, widget
) {
1516 var v
= params
.hasOwnProperty( name
) ? params
[ name
] : undefined;
1517 widget
.setApiValue( v
);
1523 * Load query params from form fields
1525 * @param {Object} params Write query parameters into this object
1526 * @param {Object} displayParams Write query parameters for display into this object
1528 ApiSandbox
.PageLayout
.prototype.getQueryParams = function ( params
, displayParams
) {
1529 $.each( this.widgets
, function ( name
, widget
) {
1530 var value
= widget
.getApiValue();
1531 if ( value
!== undefined ) {
1532 params
[ name
] = value
;
1533 if ( $.isFunction( widget
.getApiValueForDisplay
) ) {
1534 value
= widget
.getApiValueForDisplay();
1536 displayParams
[ name
] = value
;
1542 * Fetch a list of subpage names loaded by this page
1546 ApiSandbox
.PageLayout
.prototype.getSubpages = function () {
1548 $.each( this.widgets
, function ( name
, widget
) {
1550 if ( $.isFunction( widget
.getSubmodules
) ) {
1551 submodules
= widget
.getSubmodules();
1552 for ( i
= 0; i
< submodules
.length
; i
++ ) {
1554 key
: name
+ '=' + submodules
[ i
].value
,
1555 path
: submodules
[ i
].path
,
1556 prefix
: widget
.paramInfo
.submoduleparamprefix
|| ''
1565 * A text input with a clickable indicator
1570 * @param {Object} [config] Configuration options
1572 function TextInputWithIndicatorWidget( config
) {
1575 config
= config
|| {};
1576 TextInputWithIndicatorWidget
[ 'super' ].call( this, config
);
1578 this.$indicator
= $( '<span>' ).addClass( 'mw-apisandbox-clickable-indicator' );
1579 OO
.ui
.mixin
.TabIndexedElement
.call(
1580 this, $.extend( {}, config
, { $tabIndexed
: this.$indicator
} )
1583 this.input
= new OO
.ui
.TextInputWidget( $.extend( {
1584 $indicator
: this.$indicator
,
1585 disabled
: this.isDisabled()
1586 }, config
.input
) );
1588 // Forward most methods for convenience
1589 for ( k
in this.input
) {
1590 if ( $.isFunction( this.input
[ k
] ) && !this[ k
] ) {
1591 this[ k
] = this.input
[ k
].bind( this.input
);
1595 this.$indicator
.on( {
1596 click
: this.onIndicatorClick
.bind( this ),
1597 keypress
: this.onIndicatorKeyPress
.bind( this )
1600 this.$element
.append( this.input
.$element
);
1602 OO
.inheritClass( TextInputWithIndicatorWidget
, OO
.ui
.Widget
);
1603 OO
.mixinClass( TextInputWithIndicatorWidget
, OO
.ui
.mixin
.TabIndexedElement
);
1604 TextInputWithIndicatorWidget
.prototype.onIndicatorClick = function ( e
) {
1605 if ( !this.isDisabled() && e
.which
=== 1 ) {
1606 this.emit( 'indicator' );
1610 TextInputWithIndicatorWidget
.prototype.onIndicatorKeyPress = function ( e
) {
1611 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
1612 this.emit( 'indicator' );
1616 TextInputWithIndicatorWidget
.prototype.setDisabled = function ( disabled
) {
1617 TextInputWithIndicatorWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1619 this.input
.setDisabled( this.isDisabled() );
1625 * A wrapper for a widget that provides an enable/disable button
1630 * @param {OO.ui.Widget} widget
1631 * @param {Object} [config] Configuration options
1633 function OptionalWidget( widget
, config
) {
1636 config
= config
|| {};
1638 this.widget
= widget
;
1639 this.$overlay
= config
.$overlay
||
1640 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-overlay' );
1641 this.checkbox
= new OO
.ui
.CheckboxInputWidget( config
.checkbox
)
1642 .on( 'change', this.onCheckboxChange
, [], this );
1644 OptionalWidget
[ 'super' ].call( this, config
);
1646 // Forward most methods for convenience
1647 for ( k
in this.widget
) {
1648 if ( $.isFunction( this.widget
[ k
] ) && !this[ k
] ) {
1649 this[ k
] = this.widget
[ k
].bind( this.widget
);
1653 this.$overlay
.on( 'click', this.onOverlayClick
.bind( this ) );
1656 .addClass( 'mw-apisandbox-optionalWidget' )
1659 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-fields' ).append(
1660 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-widget' ).append(
1663 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-checkbox' ).append(
1664 this.checkbox
.$element
1669 this.setDisabled( widget
.isDisabled() );
1671 OO
.inheritClass( OptionalWidget
, OO
.ui
.Widget
);
1672 OptionalWidget
.prototype.onCheckboxChange = function ( checked
) {
1673 this.setDisabled( !checked
);
1675 OptionalWidget
.prototype.onOverlayClick = function () {
1676 this.setDisabled( false );
1677 if ( $.isFunction( this.widget
.focus
) ) {
1678 this.widget
.focus();
1681 OptionalWidget
.prototype.setDisabled = function ( disabled
) {
1682 OptionalWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1683 this.widget
.setDisabled( this.isDisabled() );
1684 this.checkbox
.setSelected( !this.isDisabled() );
1685 this.$overlay
.toggle( this.isDisabled() );
1689 $( ApiSandbox
.init
);
1691 module
.exports
= ApiSandbox
;
1693 }( jQuery
, mediaWiki
, OO
) );