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 ApiSandbox
.isFullscreen
= false;
585 $content
= $( '#mw-apisandbox' );
587 windowManager
= new OO
.ui
.WindowManager();
588 $( 'body' ).append( windowManager
.$element
);
589 windowManager
.addWindows( {
590 errorAlert
: new OO
.ui
.MessageDialog()
593 fullscreenButton
= new OO
.ui
.ButtonWidget( {
594 label
: mw
.message( 'apisandbox-fullscreen' ).text(),
595 title
: mw
.message( 'apisandbox-fullscreen-tooltip' ).text()
596 } ).on( 'click', ApiSandbox
.toggleFullscreen
);
598 $toolbar
= $( '<div>' )
599 .addClass( 'mw-apisandbox-toolbar' )
601 fullscreenButton
.$element
,
602 new OO
.ui
.ButtonWidget( {
603 label
: mw
.message( 'apisandbox-submit' ).text(),
604 flags
: [ 'primary', 'constructive' ]
605 } ).on( 'click', ApiSandbox
.sendRequest
).$element
,
606 new OO
.ui
.ButtonWidget( {
607 label
: mw
.message( 'apisandbox-reset' ).text(),
609 } ).on( 'click', ApiSandbox
.resetUI
).$element
612 booklet
= new OO
.ui
.BookletLayout( {
617 panel
= new OO
.ui
.PanelLayout( {
618 classes
: [ 'mw-apisandbox-container' ],
619 content
: [ booklet
],
624 pages
.main
= new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } );
626 // Parse the current hash string
627 if ( !ApiSandbox
.loadFromHash() ) {
628 ApiSandbox
.updateUI();
631 // If the hashchange event exists, use it. Otherwise, fake it.
632 // And, of course, IE has to be dumb.
633 if ( 'onhashchange' in window
&&
634 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
636 $( window
).on( 'hashchange', ApiSandbox
.loadFromHash
);
638 setInterval( function () {
639 if ( oldhash
!== location
.hash
) {
640 ApiSandbox
.loadFromHash();
647 .append( $( '<p>' ).append( mw
.message( 'apisandbox-intro' ).parse() ) )
649 $( '<div>', { id
: 'mw-apisandbox-ui' } )
651 .append( panel
.$element
)
654 $( window
).on( 'resize', ApiSandbox
.resizePanel
);
656 ApiSandbox
.resizePanel();
660 * Toggle "fullscreen" mode
662 toggleFullscreen: function () {
663 var $body
= $( document
.body
),
664 $ui
= $( '#mw-apisandbox-ui' );
666 ApiSandbox
.isFullscreen
= !ApiSandbox
.isFullscreen
;
668 $body
.toggleClass( 'mw-apisandbox-fullscreen', ApiSandbox
.isFullscreen
);
669 $ui
.toggleClass( 'mw-body-content', ApiSandbox
.isFullscreen
);
670 if ( ApiSandbox
.isFullscreen
) {
671 fullscreenButton
.setLabel( mw
.message( 'apisandbox-unfullscreen' ).text() );
672 fullscreenButton
.setTitle( mw
.message( 'apisandbox-unfullscreen-tooltip' ).text() );
675 fullscreenButton
.setLabel( mw
.message( 'apisandbox-fullscreen' ).text() );
676 fullscreenButton
.setTitle( mw
.message( 'apisandbox-fullscreen-tooltip' ).text() );
677 $content
.append( $ui
);
679 ApiSandbox
.resizePanel();
683 * Set the height of the panel based on the current viewport.
685 resizePanel: function () {
686 var height
= $( window
).height(),
687 contentTop
= $content
.offset().top
;
689 if ( ApiSandbox
.isFullscreen
) {
690 height
-= panel
.$element
.offset().top
- $( '#mw-apisandbox-ui' ).offset().top
;
691 panel
.$element
.height( height
- 1 );
693 // Subtract the height of the intro text
694 height
-= panel
.$element
.offset().top
- contentTop
;
696 panel
.$element
.height( height
- 10 );
697 $( window
).scrollTop( contentTop
- 5 );
702 * Update the current query when the page hash changes
704 loadFromHash: function () {
706 hash
= location
.hash
;
708 if ( oldhash
=== hash
) {
716 // I'm surprised this doesn't seem to exist in jQuery or mw.util.
718 hash
= hash
.replace( /\+/g, '%20' );
719 re
= /([^&=#]+)=?([^&#]*)/g;
720 while ( ( m
= re
.exec( hash
) ) ) {
721 params
[ decodeURIComponent( m
[ 1 ] ) ] = decodeURIComponent( m
[ 2 ] );
724 ApiSandbox
.updateUI( params
);
729 * Update the pages in the booklet
731 * @param {Object} [params] Optional query parameters to load
733 updateUI: function ( params
) {
734 var i
, page
, subpages
, j
, removePages
,
737 if ( !$.isPlainObject( params
) ) {
741 if ( updatingBooklet
) {
744 updatingBooklet
= true;
746 if ( params
!== undefined ) {
747 pages
.main
.loadQueryParams( params
);
749 addPages
.push( pages
.main
);
750 if ( resultPage
!== null ) {
751 addPages
.push( resultPage
);
753 pages
.main
.apiCheckValid();
756 while ( addPages
.length
) {
757 page
= addPages
.shift();
758 if ( bookletPages
[ i
] !== page
) {
759 for ( j
= i
; j
< bookletPages
.length
; j
++ ) {
760 if ( bookletPages
[ j
].getName() === page
.getName() ) {
761 bookletPages
.splice( j
, 1 );
764 bookletPages
.splice( i
, 0, page
);
765 booklet
.addPages( [ page
], i
);
769 if ( page
.getSubpages
) {
770 subpages
= page
.getSubpages();
771 for ( j
= 0; j
< subpages
.length
; j
++ ) {
772 if ( !pages
.hasOwnProperty( subpages
[ j
].key
) ) {
773 subpages
[ j
].indentLevel
= page
.indentLevel
+ 1;
774 pages
[ subpages
[ j
].key
] = new ApiSandbox
.PageLayout( subpages
[ j
] );
776 if ( params
!== undefined ) {
777 pages
[ subpages
[ j
].key
].loadQueryParams( params
);
779 addPages
.splice( j
, 0, pages
[ subpages
[ j
].key
] );
780 pages
[ subpages
[ j
].key
].apiCheckValid();
785 if ( bookletPages
.length
> i
) {
786 removePages
= bookletPages
.splice( i
, bookletPages
.length
- i
);
787 booklet
.removePages( removePages
);
790 if ( !booklet
.getCurrentPageName() ) {
791 booklet
.selectFirstSelectablePage();
794 updatingBooklet
= false;
799 * Reset button handler
801 resetUI: function () {
802 suppressErrors
= true;
804 main
: new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } )
807 ApiSandbox
.updateUI();
811 * Submit button handler
813 sendRequest: function () {
814 var page
, subpages
, i
, query
, $result
, $focus
,
815 progress
, $progressText
, progressLoading
,
819 checkPages
= [ pages
.main
];
821 // Blur any focused widget before submit, because
822 // OO.ui.ButtonWidget doesn't take focus itself (T128054)
823 $focus
= $( '#mw-apisandbox-ui' ).find( document
.activeElement
);
824 if ( $focus
.length
) {
828 suppressErrors
= false;
830 while ( checkPages
.length
) {
831 page
= checkPages
.shift();
832 deferreds
.push( page
.apiCheckValid() );
833 page
.getQueryParams( params
, displayParams
);
834 subpages
= page
.getSubpages();
835 for ( i
= 0; i
< subpages
.length
; i
++ ) {
836 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
837 checkPages
.push( pages
[ subpages
[ i
].key
] );
842 $.when
.apply( $, deferreds
).done( function () {
843 if ( $.inArray( false, arguments
) !== -1 ) {
844 windowManager
.openWindow( 'errorAlert', {
845 title
: mw
.message( 'apisandbox-submit-invalid-fields-title' ).parse(),
846 message
: mw
.message( 'apisandbox-submit-invalid-fields-message' ).parse(),
850 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
858 query
= $.param( displayParams
);
860 // Force a 'fm' format with wrappedhtml=1, if available
861 if ( params
.format
!== undefined ) {
862 if ( availableFormats
.hasOwnProperty( params
.format
+ 'fm' ) ) {
863 params
.format
= params
.format
+ 'fm';
865 if ( params
.format
.substr( -2 ) === 'fm' ) {
866 params
.wrappedhtml
= 1;
870 progressLoading
= false;
871 $progressText
= $( '<span>' ).text( mw
.message( 'apisandbox-sending-request' ).text() );
872 progress
= new OO
.ui
.ProgressBarWidget( {
874 $content
: $progressText
877 $result
= $( '<div>' )
878 .append( progress
.$element
);
880 resultPage
= page
= new OO
.ui
.PageLayout( '|results|' );
881 page
.setupOutlineItem = function () {
882 this.outlineItem
.setLabel( mw
.message( 'apisandbox-results' ).text() );
884 page
.$element
.empty()
886 new OO
.ui
.FieldLayout(
887 new OO
.ui
.TextInputWidget( {
889 value
: mw
.util
.wikiScript( 'api' ) + '?' + query
891 label
: mw
.message( 'apisandbox-request-url-label' ).parse()
896 ApiSandbox
.updateUI();
897 booklet
.setPage( '|results|' );
899 location
.href
= oldhash
= '#' + query
;
902 contentType
: 'multipart/form-data',
905 var xhr
= new window
.XMLHttpRequest();
906 xhr
.upload
.addEventListener( 'progress', function ( e
) {
907 if ( !progressLoading
) {
908 if ( e
.lengthComputable
) {
909 progress
.setProgress( e
.loaded
* 100 / e
.total
);
911 progress
.setProgress( false );
915 xhr
.addEventListener( 'progress', function ( e
) {
916 if ( !progressLoading
) {
917 progressLoading
= true;
918 $progressText
.text( mw
.message( 'apisandbox-loading-results' ).text() );
920 if ( e
.lengthComputable
) {
921 progress
.setProgress( e
.loaded
* 100 / e
.total
);
923 progress
.setProgress( false );
929 .then( null, function ( code
, data
, result
, jqXHR
) {
930 if ( code
!== 'http' ) {
931 // Not really an error, work around mw.Api thinking it is.
933 .resolve( result
, jqXHR
)
938 .fail( function ( code
, data
) {
939 var details
= 'HTTP error: ' + data
.exception
;
942 new OO
.ui
.LabelWidget( {
943 label
: mw
.message( 'apisandbox-results-error', details
).text(),
948 .done( function ( data
, jqXHR
) {
949 var m
, loadTime
, button
,
950 ct
= jqXHR
.getResponseHeader( 'Content-Type' );
953 if ( /^text\/mediawiki-api-prettyprint-wrapped(?:;|$)/.test( ct
) ) {
954 data
= $.parseJSON( data
);
955 if ( data
.modules
.length
) {
956 mw
.loader
.load( data
.modules
);
958 $result
.append( Util
.parseHTML( data
.html
) );
959 loadTime
= data
.time
;
960 } else if ( ( m
= data
.match( /<pre[ >][\s\S]*<\/pre>/ ) ) ) {
961 $result
.append( Util
.parseHTML( m
[ 0 ] ) );
962 if ( ( m
= data
.match( /"wgBackendResponseTime":\s*(\d+)/ ) ) ) {
963 loadTime
= parseInt( m
[ 1 ], 10 );
967 .addClass( 'api-pretty-content' )
969 .appendTo( $result
);
971 if ( typeof loadTime
=== 'number' ) {
974 new OO
.ui
.LabelWidget( {
975 label
: mw
.message( 'apisandbox-request-time', loadTime
).text()
981 if ( jqXHR
.getResponseHeader( 'MediaWiki-API-Error' ) === 'badtoken' ) {
982 // Flush all saved tokens in case one of them is the bad one.
983 Util
.markTokensBad();
984 button
= new OO
.ui
.ButtonWidget( {
985 label
: mw
.message( 'apisandbox-results-fixtoken' ).text()
987 button
.on( 'click', ApiSandbox
.fixTokenAndResend
)
988 .on( 'click', button
.setDisabled
, [ true ], button
)
989 .$element
.appendTo( $result
);
996 * Handler for the "Correct token and resubmit" button
998 * Used on a 'badtoken' error, it re-fetches token parameters for all
999 * pages and then re-submits the query.
1001 fixTokenAndResend: function () {
1002 var page
, subpages
, i
, k
,
1004 tokenWait
= { dummy
: true },
1005 checkPages
= [ pages
.main
],
1006 success = function ( k
) {
1007 delete tokenWait
[ k
];
1008 if ( ok
&& $.isEmptyObject( tokenWait
) ) {
1009 ApiSandbox
.sendRequest();
1012 failure = function ( k
) {
1013 delete tokenWait
[ k
];
1017 while ( checkPages
.length
) {
1018 page
= checkPages
.shift();
1020 if ( page
.tokenWidget
) {
1021 k
= page
.apiModule
+ page
.tokenWidget
.paramInfo
.name
;
1022 tokenWait
[ k
] = page
.tokenWidget
.fetchToken()
1023 .done( success
.bind( page
.tokenWidget
, k
) )
1024 .fail( failure
.bind( page
.tokenWidget
, k
) );
1027 subpages
= page
.getSubpages();
1028 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1029 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1030 checkPages
.push( pages
[ subpages
[ i
].key
] );
1035 success( 'dummy', '' );
1039 * Reset validity indicators for all widgets
1041 updateValidityIndicators: function () {
1042 var page
, subpages
, i
,
1043 checkPages
= [ pages
.main
];
1045 while ( checkPages
.length
) {
1046 page
= checkPages
.shift();
1047 page
.apiCheckValid();
1048 subpages
= page
.getSubpages();
1049 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1050 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1051 checkPages
.push( pages
[ subpages
[ i
].key
] );
1059 * PageLayout for API modules
1063 * @extends OO.ui.PageLayout
1065 * @param {Object} [config] Configuration options
1067 ApiSandbox
.PageLayout = function ( config
) {
1068 config
= $.extend( { prefix
: '' }, config
);
1069 this.displayText
= config
.key
;
1070 this.apiModule
= config
.path
;
1071 this.prefix
= config
.prefix
;
1072 this.paramInfo
= null;
1073 this.apiIsValid
= true;
1074 this.loadFromQueryParams
= null;
1076 this.tokenWidget
= null;
1077 this.indentLevel
= config
.indentLevel
? config
.indentLevel
: 0;
1078 ApiSandbox
.PageLayout
[ 'super' ].call( this, config
.key
, config
);
1079 this.loadParamInfo();
1081 OO
.inheritClass( ApiSandbox
.PageLayout
, OO
.ui
.PageLayout
);
1082 ApiSandbox
.PageLayout
.prototype.setupOutlineItem = function () {
1083 this.outlineItem
.setLevel( this.indentLevel
);
1084 this.outlineItem
.setLabel( this.displayText
);
1085 this.outlineItem
.setIcon( this.apiIsValid
|| suppressErrors
? null : 'alert' );
1086 this.outlineItem
.setIconTitle(
1087 this.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1092 * Fetch module information for this page's module, then create UI
1094 ApiSandbox
.PageLayout
.prototype.loadParamInfo = function () {
1095 var dynamicFieldset
, dynamicParamNameWidget
,
1097 removeDynamicParamWidget = function ( name
, layout
) {
1098 dynamicFieldset
.removeItems( [ layout
] );
1099 delete that
.widgets
[ name
];
1101 addDynamicParamWidget = function () {
1102 var name
, layout
, widget
, button
;
1104 // Check name is filled in
1105 name
= dynamicParamNameWidget
.getValue().trim();
1106 if ( name
=== '' ) {
1107 dynamicParamNameWidget
.focus();
1111 if ( that
.widgets
[ name
] !== undefined ) {
1112 windowManager
.openWindow( 'errorAlert', {
1114 'apisandbox-dynamic-error-exists', name
1119 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
1127 widget
= Util
.createWidgetForParameter( {
1134 button
= new OO
.ui
.ButtonWidget( {
1136 flags
: 'destructive'
1138 layout
= new OO
.ui
.ActionFieldLayout(
1146 button
.on( 'click', removeDynamicParamWidget
, [ name
, layout
] );
1147 that
.widgets
[ name
] = widget
;
1148 dynamicFieldset
.addItems( [ layout
], dynamicFieldset
.getItems().length
- 1 );
1151 dynamicParamNameWidget
.setValue( '' );
1154 this.$element
.empty()
1155 .append( new OO
.ui
.ProgressBarWidget( {
1157 text
: mw
.message( 'apisandbox-loading', this.displayText
).text()
1160 Util
.fetchModuleInfo( this.apiModule
)
1161 .done( function ( pi
) {
1162 var prefix
, i
, j
, dl
, widget
, $widgetLabel
, widgetField
, helpField
, tmp
, flag
, count
,
1164 deprecatedItems
= [],
1166 filterFmModules = function ( v
) {
1167 return v
.substr( -2 ) !== 'fm' ||
1168 !availableFormats
.hasOwnProperty( v
.substr( 0, v
.length
- 2 ) );
1170 widgetLabelOnClick = function () {
1171 var f
= this.getField();
1172 if ( $.isFunction( f
.setDisabled
) ) {
1173 f
.setDisabled( false );
1175 if ( $.isFunction( f
.focus
) ) {
1179 doNothing = function () {};
1181 // This is something of a hack. We always want the 'format' and
1182 // 'action' parameters from the main module to be specified,
1183 // and for 'format' we also want to simplify the dropdown since
1184 // we always send the 'fm' variant.
1185 if ( that
.apiModule
=== 'main' ) {
1186 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1187 if ( pi
.parameters
[ i
].name
=== 'action' ) {
1188 pi
.parameters
[ i
].required
= true;
1189 delete pi
.parameters
[ i
][ 'default' ];
1191 if ( pi
.parameters
[ i
].name
=== 'format' ) {
1192 tmp
= pi
.parameters
[ i
].type
;
1193 for ( j
= 0; j
< tmp
.length
; j
++ ) {
1194 availableFormats
[ tmp
[ j
] ] = true;
1196 pi
.parameters
[ i
].type
= $.grep( tmp
, filterFmModules
);
1197 pi
.parameters
[ i
][ 'default' ] = 'json';
1198 pi
.parameters
[ i
].required
= true;
1203 // Hide the 'wrappedhtml' parameter on format modules
1204 if ( pi
.group
=== 'format' ) {
1205 pi
.parameters
= $.grep( pi
.parameters
, function ( p
) {
1206 return p
.name
!== 'wrappedhtml';
1210 that
.paramInfo
= pi
;
1212 items
.push( new OO
.ui
.FieldLayout(
1213 new OO
.ui
.Widget( {} ).toggle( false ), {
1215 label
: Util
.parseHTML( pi
.description
)
1219 if ( pi
.helpurls
.length
) {
1220 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1221 label
: mw
.message( 'apisandbox-helpurls' ).text(),
1224 $content
: $( '<ul>' ).append( $.map( pi
.helpurls
, function ( link
) {
1225 return $( '<li>' ).append( $( '<a>', {
1235 if ( pi
.examples
.length
) {
1236 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1237 label
: mw
.message( 'apisandbox-examples' ).text(),
1240 $content
: $( '<ul>' ).append( $.map( pi
.examples
, function ( example
) {
1242 href
: '#' + example
.query
,
1243 html
: example
.description
1245 a
.find( 'a' ).contents().unwrap(); // Can't nest links
1246 return $( '<li>' ).append( a
);
1252 if ( buttons
.length
) {
1253 items
.push( new OO
.ui
.FieldLayout(
1254 new OO
.ui
.ButtonGroupWidget( {
1256 } ), { align
: 'top' }
1260 if ( pi
.parameters
.length
) {
1261 prefix
= that
.prefix
+ pi
.prefix
;
1262 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1263 widget
= Util
.createWidgetForParameter( pi
.parameters
[ i
] );
1264 that
.widgets
[ prefix
+ pi
.parameters
[ i
].name
] = widget
;
1265 if ( pi
.parameters
[ i
].tokentype
) {
1266 that
.tokenWidget
= widget
;
1270 dl
.append( $( '<dd>', {
1271 addClass
: 'description',
1272 append
: Util
.parseHTML( pi
.parameters
[ i
].description
)
1274 if ( pi
.parameters
[ i
].info
&& pi
.parameters
[ i
].info
.length
) {
1275 for ( j
= 0; j
< pi
.parameters
[ i
].info
.length
; j
++ ) {
1276 dl
.append( $( '<dd>', {
1278 append
: Util
.parseHTML( pi
.parameters
[ i
].info
[ j
] )
1284 switch ( pi
.parameters
[ i
].type
) {
1287 count
= mw
.config
.get( 'wgFormattedNamespaces' ).length
;
1291 if ( pi
.parameters
[ i
].highmax
!== undefined ) {
1292 dl
.append( $( '<dd>', {
1294 append
: Util
.parseHTML( mw
.message(
1295 'api-help-param-limit2', pi
.parameters
[ i
].max
, pi
.parameters
[ i
].highmax
1299 dl
.append( $( '<dd>', {
1301 append
: Util
.parseHTML( mw
.message(
1302 'api-help-param-limit', pi
.parameters
[ i
].max
1310 if ( pi
.parameters
[ i
].min
!== undefined ) {
1313 if ( pi
.parameters
[ i
].max
!== undefined ) {
1317 dl
.append( $( '<dd>', {
1319 append
: Util
.parseHTML( mw
.message(
1320 'api-help-param-integer-' + tmp
,
1321 Util
.apiBool( pi
.parameters
[ i
].multi
) ? 2 : 1,
1322 pi
.parameters
[ i
].min
, pi
.parameters
[ i
].max
1329 if ( $.isArray( pi
.parameters
[ i
].type
) ) {
1331 count
= pi
.parameters
[ i
].type
.length
;
1335 if ( Util
.apiBool( pi
.parameters
[ i
].multi
) ) {
1337 if ( flag
&& !( widget
instanceof OO
.ui
.CapsuleMultiselectWidget
) &&
1339 widget
instanceof OptionalWidget
&&
1340 widget
.widget
instanceof OO
.ui
.CapsuleMultiselectWidget
1343 tmp
.push( mw
.message( 'api-help-param-multi-separate' ).parse() );
1345 if ( count
> pi
.parameters
[ i
].lowlimit
) {
1347 mw
.message( 'api-help-param-multi-max',
1348 pi
.parameters
[ i
].lowlimit
, pi
.parameters
[ i
].highlimit
1353 dl
.append( $( '<dd>', {
1355 append
: Util
.parseHTML( tmp
.join( ' ' ) )
1359 helpField
= new OO
.ui
.FieldLayout(
1362 classes
: [ 'mw-apisandbox-spacer' ]
1365 classes
: [ 'mw-apisandbox-help-field' ],
1370 $widgetLabel
= $( '<span>' );
1371 widgetField
= new OO
.ui
.FieldLayout(
1375 classes
: [ 'mw-apisandbox-widget-field' ],
1376 label
: prefix
+ pi
.parameters
[ i
].name
,
1377 $label
: $widgetLabel
1381 // FieldLayout only does click for InputElement
1382 // widgets. So supply our own click handler.
1383 $widgetLabel
.on( 'click', widgetLabelOnClick
.bind( widgetField
) );
1385 // Don't grey out the label when the field is disabled,
1386 // it makes it too hard to read and our "disabled"
1387 // isn't really disabled.
1388 widgetField
.onFieldDisable
= doNothing
;
1390 if ( Util
.apiBool( pi
.parameters
[ i
].deprecated
) ) {
1391 deprecatedItems
.push( widgetField
, helpField
);
1393 items
.push( widgetField
, helpField
);
1398 if ( !pi
.parameters
.length
&& !Util
.apiBool( pi
.dynamicparameters
) ) {
1399 items
.push( new OO
.ui
.FieldLayout(
1400 new OO
.ui
.Widget( {} ).toggle( false ), {
1402 label
: Util
.parseHTML( mw
.message( 'apisandbox-no-parameters' ).parse() )
1407 that
.$element
.empty();
1409 new OO
.ui
.FieldsetLayout( {
1410 label
: that
.displayText
1411 } ).addItems( items
)
1412 .$element
.appendTo( that
.$element
);
1414 if ( Util
.apiBool( pi
.dynamicparameters
) ) {
1415 dynamicFieldset
= new OO
.ui
.FieldsetLayout();
1416 dynamicParamNameWidget
= new OO
.ui
.TextInputWidget( {
1417 placeholder
: mw
.message( 'apisandbox-dynamic-parameters-add-placeholder' ).text()
1418 } ).on( 'enter', addDynamicParamWidget
);
1419 dynamicFieldset
.addItems( [
1420 new OO
.ui
.FieldLayout(
1421 new OO
.ui
.Widget( {} ).toggle( false ), {
1423 label
: Util
.parseHTML( pi
.dynamicparameters
)
1426 new OO
.ui
.ActionFieldLayout(
1427 dynamicParamNameWidget
,
1428 new OO
.ui
.ButtonWidget( {
1430 flags
: 'constructive'
1431 } ).on( 'click', addDynamicParamWidget
),
1433 label
: mw
.message( 'apisandbox-dynamic-parameters-add-label' ).text(),
1440 $( '<legend>' ).text( mw
.message( 'apisandbox-dynamic-parameters' ).text() ),
1441 dynamicFieldset
.$element
1443 .appendTo( that
.$element
);
1446 if ( deprecatedItems
.length
) {
1447 tmp
= new OO
.ui
.FieldsetLayout().addItems( deprecatedItems
).toggle( false );
1450 $( '<legend>' ).append(
1451 new OO
.ui
.ToggleButtonWidget( {
1452 label
: mw
.message( 'apisandbox-deprecated-parameters' ).text()
1453 } ).on( 'change', tmp
.toggle
, [], tmp
).$element
1457 .appendTo( that
.$element
);
1460 // Load stored params, if any, then update the booklet if we
1461 // have subpages (or else just update our valid-indicator).
1462 tmp
= that
.loadFromQueryParams
;
1463 that
.loadFromQueryParams
= null;
1464 if ( $.isPlainObject( tmp
) ) {
1465 that
.loadQueryParams( tmp
);
1467 if ( that
.getSubpages().length
> 0 ) {
1468 ApiSandbox
.updateUI( tmp
);
1470 that
.apiCheckValid();
1472 } ).fail( function ( code
, detail
) {
1473 that
.$element
.empty()
1475 new OO
.ui
.LabelWidget( {
1476 label
: mw
.message( 'apisandbox-load-error', that
.apiModule
, detail
).text(),
1477 classes
: [ 'error' ]
1479 new OO
.ui
.ButtonWidget( {
1480 label
: mw
.message( 'apisandbox-retry' ).text()
1481 } ).on( 'click', that
.loadParamInfo
, [], that
).$element
1487 * Check that all widgets on the page are in a valid state.
1491 ApiSandbox
.PageLayout
.prototype.apiCheckValid = function () {
1494 if ( this.paramInfo
=== null ) {
1495 return $.Deferred().resolve( false ).promise();
1497 return $.when
.apply( $, $.map( this.widgets
, function ( widget
) {
1498 return widget
.apiCheckValid();
1499 } ) ).then( function () {
1500 that
.apiIsValid
= $.inArray( false, arguments
) === -1;
1501 if ( that
.getOutlineItem() ) {
1502 that
.getOutlineItem().setIcon( that
.apiIsValid
|| suppressErrors
? null : 'alert' );
1503 that
.getOutlineItem().setIconTitle(
1504 that
.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1507 return $.Deferred().resolve( that
.apiIsValid
).promise();
1513 * Load form fields from query parameters
1515 * @param {Object} params
1517 ApiSandbox
.PageLayout
.prototype.loadQueryParams = function ( params
) {
1518 if ( this.paramInfo
=== null ) {
1519 this.loadFromQueryParams
= params
;
1521 $.each( this.widgets
, function ( name
, widget
) {
1522 var v
= params
.hasOwnProperty( name
) ? params
[ name
] : undefined;
1523 widget
.setApiValue( v
);
1529 * Load query params from form fields
1531 * @param {Object} params Write query parameters into this object
1532 * @param {Object} displayParams Write query parameters for display into this object
1534 ApiSandbox
.PageLayout
.prototype.getQueryParams = function ( params
, displayParams
) {
1535 $.each( this.widgets
, function ( name
, widget
) {
1536 var value
= widget
.getApiValue();
1537 if ( value
!== undefined ) {
1538 params
[ name
] = value
;
1539 if ( $.isFunction( widget
.getApiValueForDisplay
) ) {
1540 value
= widget
.getApiValueForDisplay();
1542 displayParams
[ name
] = value
;
1548 * Fetch a list of subpage names loaded by this page
1552 ApiSandbox
.PageLayout
.prototype.getSubpages = function () {
1554 $.each( this.widgets
, function ( name
, widget
) {
1556 if ( $.isFunction( widget
.getSubmodules
) ) {
1557 submodules
= widget
.getSubmodules();
1558 for ( i
= 0; i
< submodules
.length
; i
++ ) {
1560 key
: name
+ '=' + submodules
[ i
].value
,
1561 path
: submodules
[ i
].path
,
1562 prefix
: widget
.paramInfo
.submoduleparamprefix
|| ''
1571 * A text input with a clickable indicator
1576 * @param {Object} [config] Configuration options
1578 function TextInputWithIndicatorWidget( config
) {
1581 config
= config
|| {};
1582 TextInputWithIndicatorWidget
[ 'super' ].call( this, config
);
1584 this.$indicator
= $( '<span>' ).addClass( 'mw-apisandbox-clickable-indicator' );
1585 OO
.ui
.mixin
.TabIndexedElement
.call(
1586 this, $.extend( {}, config
, { $tabIndexed
: this.$indicator
} )
1589 this.input
= new OO
.ui
.TextInputWidget( $.extend( {
1590 $indicator
: this.$indicator
,
1591 disabled
: this.isDisabled()
1592 }, config
.input
) );
1594 // Forward most methods for convenience
1595 for ( k
in this.input
) {
1596 if ( $.isFunction( this.input
[ k
] ) && !this[ k
] ) {
1597 this[ k
] = this.input
[ k
].bind( this.input
);
1601 this.$indicator
.on( {
1602 click
: this.onIndicatorClick
.bind( this ),
1603 keypress
: this.onIndicatorKeyPress
.bind( this )
1606 this.$element
.append( this.input
.$element
);
1608 OO
.inheritClass( TextInputWithIndicatorWidget
, OO
.ui
.Widget
);
1609 OO
.mixinClass( TextInputWithIndicatorWidget
, OO
.ui
.mixin
.TabIndexedElement
);
1610 TextInputWithIndicatorWidget
.prototype.onIndicatorClick = function ( e
) {
1611 if ( !this.isDisabled() && e
.which
=== 1 ) {
1612 this.emit( 'indicator' );
1616 TextInputWithIndicatorWidget
.prototype.onIndicatorKeyPress = function ( e
) {
1617 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
1618 this.emit( 'indicator' );
1622 TextInputWithIndicatorWidget
.prototype.setDisabled = function ( disabled
) {
1623 TextInputWithIndicatorWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1625 this.input
.setDisabled( this.isDisabled() );
1631 * A wrapper for a widget that provides an enable/disable button
1636 * @param {OO.ui.Widget} widget
1637 * @param {Object} [config] Configuration options
1639 function OptionalWidget( widget
, config
) {
1642 config
= config
|| {};
1644 this.widget
= widget
;
1645 this.$overlay
= config
.$overlay
||
1646 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-overlay' );
1647 this.checkbox
= new OO
.ui
.CheckboxInputWidget( config
.checkbox
)
1648 .on( 'change', this.onCheckboxChange
, [], this );
1650 OptionalWidget
[ 'super' ].call( this, config
);
1652 // Forward most methods for convenience
1653 for ( k
in this.widget
) {
1654 if ( $.isFunction( this.widget
[ k
] ) && !this[ k
] ) {
1655 this[ k
] = this.widget
[ k
].bind( this.widget
);
1659 this.$overlay
.on( 'click', this.onOverlayClick
.bind( this ) );
1662 .addClass( 'mw-apisandbox-optionalWidget' )
1665 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-fields' ).append(
1666 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-widget' ).append(
1669 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-checkbox' ).append(
1670 this.checkbox
.$element
1675 this.setDisabled( widget
.isDisabled() );
1677 OO
.inheritClass( OptionalWidget
, OO
.ui
.Widget
);
1678 OptionalWidget
.prototype.onCheckboxChange = function ( checked
) {
1679 this.setDisabled( !checked
);
1681 OptionalWidget
.prototype.onOverlayClick = function () {
1682 this.setDisabled( false );
1683 if ( $.isFunction( this.widget
.focus
) ) {
1684 this.widget
.focus();
1687 OptionalWidget
.prototype.setDisabled = function ( disabled
) {
1688 OptionalWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1689 this.widget
.setDisabled( this.isDisabled() );
1690 this.checkbox
.setSelected( !this.isDisabled() );
1691 this.$overlay
.toggle( this.isDisabled() );
1695 $( ApiSandbox
.init
);
1697 module
.exports
= ApiSandbox
;
1699 }( jQuery
, mediaWiki
, OO
) );