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.isValid().done( function ( ok
) {
30 ok
= ok
|| suppressErrors
;
31 that
.setIcon( ok
? null : 'alert' );
32 that
.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
37 dateTimeInputWidget
: {
38 isValid: function () {
39 var ok
= !Util
.apiBool( this.paramInfo
.required
) || this.getApiValue() !== '';
40 return $.Deferred().resolve( ok
).promise();
45 alertTokenError: function ( code
, error
) {
46 windowManager
.openWindow( 'errorAlert', {
48 'apisandbox-results-fixtoken-fail', this.paramInfo
.tokentype
54 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
60 fetchToken: function () {
62 return api
.getToken( this.paramInfo
.tokentype
)
63 .done( this.setApiValue
.bind( this ) )
64 .fail( this.alertTokenError
.bind( this ) )
65 .always( this.popPending
.bind( this ) );
67 setApiValue: function ( v
) {
68 WidgetMethods
.textInputWidget
.setApiValue
.call( this, v
);
69 if ( v
=== '123ABC' ) {
76 getApiValueForDisplay: function () {
82 getApiValue: function () {
83 return this.getValue() ? 1 : undefined;
85 setApiValue: function ( v
) {
86 this.setValue( Util
.apiBool( v
) );
88 apiCheckValid: function () {
89 return $.Deferred().resolve( true ).promise();
94 getApiValue: function () {
95 var item
= this.getMenu().getSelectedItem();
96 return item
=== null ? undefined : item
.getData();
98 setApiValue: function ( v
) {
99 var menu
= this.getMenu();
101 if ( v
=== undefined ) {
102 v
= this.paramInfo
[ 'default' ];
104 if ( v
=== undefined ) {
107 menu
.selectItemByData( String( v
) );
110 apiCheckValid: function () {
111 var ok
= this.getApiValue() !== undefined || suppressErrors
;
112 this.setIcon( ok
? null : 'alert' );
113 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
114 return $.Deferred().resolve( ok
).promise();
119 getApiValue: function () {
120 var items
= this.getItemsData();
121 if ( items
.join( '' ).indexOf( '|' ) === -1 ) {
122 return items
.join( '|' );
124 return '\x1f' + items
.join( '\x1f' );
127 setApiValue: function ( v
) {
128 if ( v
=== undefined || v
=== '' || v
=== '\x1f' ) {
129 this.setItemsFromData( [] );
132 if ( v
.indexOf( '\x1f' ) !== 0 ) {
133 this.setItemsFromData( v
.split( '|' ) );
135 this.setItemsFromData( v
.substr( 1 ).split( '\x1f' ) );
139 apiCheckValid: function () {
140 var ok
= this.getApiValue() !== undefined || suppressErrors
;
141 this.setIcon( ok
? null : 'alert' );
142 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
143 return $.Deferred().resolve( ok
).promise();
148 getApiValue: function () {
149 return this.isDisabled() ? undefined : this.widget
.getApiValue();
151 setApiValue: function ( v
) {
152 this.setDisabled( v
=== undefined );
153 this.widget
.setApiValue( v
);
155 apiCheckValid: function () {
156 if ( this.isDisabled() ) {
157 return $.Deferred().resolve( true ).promise();
159 return this.widget
.apiCheckValid();
165 single: function () {
166 var v
= this.isDisabled() ? this.paramInfo
[ 'default' ] : this.getApiValue();
167 return v
=== undefined ? [] : [ { value
: v
, path
: this.paramInfo
.submodules
[ v
] } ];
170 var map
= this.paramInfo
.submodules
,
171 v
= this.isDisabled() ? this.paramInfo
[ 'default' ] : this.getApiValue();
172 return v
=== undefined || v
=== '' ? [] : $.map( String( v
).split( '|' ), function ( v
) {
173 return { value
: v
, path
: map
[ v
] };
179 getApiValueForDisplay: function () {
182 getApiValue: function () {
183 return this.getValue();
185 setApiValue: function () {
188 apiCheckValid: function () {
189 var ok
= this.getValue() !== null || suppressErrors
;
190 this.setIcon( ok
? null : 'alert' );
191 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
192 return $.Deferred().resolve( ok
).promise();
198 generic: function () {
199 return !Util
.apiBool( this.paramInfo
.required
) || this.getApiValue() !== '';
204 * @class mw.special.ApiSandbox.Utils
209 * Fetch API module info
211 * @param {string} module Module to fetch data for
212 * @return {jQuery.Promise}
214 fetchModuleInfo: function ( module
) {
216 deferred
= $.Deferred();
218 if ( moduleInfoCache
.hasOwnProperty( module
) ) {
220 .resolve( moduleInfoCache
[ module
] )
221 .promise( { abort: function () {} } );
223 apiPromise
= api
.post( {
227 uselang
: mw
.config
.get( 'wgUserLanguage' )
228 } ).done( function ( data
) {
231 if ( data
.warnings
&& data
.warnings
.paraminfo
) {
232 deferred
.reject( '???', data
.warnings
.paraminfo
[ '*' ] );
236 info
= data
.paraminfo
.modules
;
237 if ( !info
|| info
.length
!== 1 || info
[ 0 ].path
!== module
) {
238 deferred
.reject( '???', 'No module data returned' );
242 moduleInfoCache
[ module
] = info
[ 0 ];
243 deferred
.resolve( info
[ 0 ] );
244 } ).fail( function ( code
, details
) {
245 if ( code
=== 'http' ) {
246 details
= 'HTTP error: ' + details
.exception
;
247 } else if ( details
.error
) {
248 details
= details
.error
.info
;
250 deferred
.reject( code
, details
);
253 .promise( { abort
: apiPromise
.abort
} );
258 * Mark all currently-in-use tokens as bad
260 markTokensBad: function () {
261 var page
, subpages
, i
,
262 checkPages
= [ pages
.main
];
264 while ( checkPages
.length
) {
265 page
= checkPages
.shift();
267 if ( page
.tokenWidget
) {
268 api
.badToken( page
.tokenWidget
.paramInfo
.tokentype
);
271 subpages
= page
.getSubpages();
272 for ( i
= 0; i
< subpages
.length
; i
++ ) {
273 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
274 checkPages
.push( pages
[ subpages
[ i
].key
] );
281 * Test an API boolean
283 * @param {Mixed} value
286 apiBool: function ( value
) {
287 return value
!== undefined && value
!== false;
291 * Create a widget for a parameter.
293 * @param {Object} pi Parameter info from API
294 * @param {Object} opts Additional options
295 * @return {OO.ui.Widget}
297 createWidgetForParameter: function ( pi
, opts
) {
298 var widget
, innerWidget
, finalWidget
, items
, $button
, $content
, func
,
305 widget
= new OO
.ui
.ToggleSwitchWidget();
306 widget
.paramInfo
= pi
;
307 $.extend( widget
, WidgetMethods
.toggleSwitchWidget
);
308 pi
.required
= true; // Avoid wrapping in the non-required widget
313 if ( pi
.tokentype
) {
314 widget
= new TextInputWithIndicatorWidget( {
316 indicator
: 'previous',
317 indicatorTitle
: mw
.message( 'apisandbox-fetch-token' ).text(),
318 required
: Util
.apiBool( pi
.required
)
321 } else if ( Util
.apiBool( pi
.multi
) ) {
322 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
325 widget
.paramInfo
= pi
;
326 $.extend( widget
, WidgetMethods
.capsuleWidget
);
328 widget
= new OO
.ui
.TextInputWidget( {
329 required
: Util
.apiBool( pi
.required
)
332 if ( !Util
.apiBool( pi
.multi
) ) {
333 widget
.paramInfo
= pi
;
334 $.extend( widget
, WidgetMethods
.textInputWidget
);
335 widget
.setValidation( Validators
.generic
);
337 if ( pi
.tokentype
) {
338 $.extend( widget
, WidgetMethods
.tokenWidget
);
339 widget
.input
.paramInfo
= pi
;
340 $.extend( widget
.input
, WidgetMethods
.textInputWidget
);
341 $.extend( widget
.input
, WidgetMethods
.tokenWidget
);
342 widget
.on( 'indicator', widget
.fetchToken
, [], widget
);
347 widget
= new OO
.ui
.TextInputWidget( {
349 required
: Util
.apiBool( pi
.required
)
351 widget
.paramInfo
= pi
;
352 $.extend( widget
, WidgetMethods
.textInputWidget
);
353 widget
.setValidation( Validators
.generic
);
357 widget
= new OO
.ui
.TextInputWidget( {
359 required
: Util
.apiBool( pi
.required
)
361 widget
.paramInfo
= pi
;
362 $.extend( widget
, WidgetMethods
.textInputWidget
);
363 $.extend( widget
, WidgetMethods
.passwordWidget
);
364 widget
.setValidation( Validators
.generic
);
369 widget
= new OO
.ui
.NumberInputWidget( {
370 required
: Util
.apiBool( pi
.required
),
373 widget
.setIcon
= widget
.input
.setIcon
.bind( widget
.input
);
374 widget
.setIconTitle
= widget
.input
.setIconTitle
.bind( widget
.input
);
375 widget
.isValid
= widget
.input
.isValid
.bind( widget
.input
);
376 widget
.paramInfo
= pi
;
377 $.extend( widget
, WidgetMethods
.textInputWidget
);
378 if ( Util
.apiBool( pi
.enforcerange
) ) {
379 widget
.setRange( pi
.min
|| -Infinity
, pi
.max
|| Infinity
);
385 widget
= new OO
.ui
.NumberInputWidget( {
386 required
: Util
.apiBool( pi
.required
),
389 widget
.setIcon
= widget
.input
.setIcon
.bind( widget
.input
);
390 widget
.setIconTitle
= widget
.input
.setIconTitle
.bind( widget
.input
);
391 widget
.isValid
= widget
.input
.isValid
.bind( widget
.input
);
392 widget
.input
.setValidation( function ( value
) {
393 return value
=== 'max' || widget
.validateNumber( value
);
395 widget
.paramInfo
= pi
;
396 $.extend( widget
, WidgetMethods
.textInputWidget
);
397 widget
.setRange( pi
.min
|| 0, mw
.config
.get( 'apihighlimits' ) ? pi
.highmax
: pi
.max
);
402 widget
= new mw
.widgets
.datetime
.DateTimeInputWidget( {
404 format
: '${year|0}-${month|0}-${day|0}T${hour|0}:${minute|0}:${second|0}${zone|short}'
406 required
: Util
.apiBool( pi
.required
),
409 widget
.paramInfo
= pi
;
410 $.extend( widget
, WidgetMethods
.textInputWidget
);
411 $.extend( widget
, WidgetMethods
.dateTimeInputWidget
);
412 multiMode
= 'indicator';
416 widget
= new OO
.ui
.SelectFileWidget();
417 widget
.paramInfo
= pi
;
418 $.extend( widget
, WidgetMethods
.uploadWidget
);
422 items
= $.map( mw
.config
.get( 'wgFormattedNamespaces' ), function ( name
, ns
) {
424 name
= mw
.message( 'blanknamespace' ).text();
426 return new OO
.ui
.MenuOptionWidget( { data
: ns
, label
: name
} );
427 } ).sort( function ( a
, b
) {
428 return a
.data
- b
.data
;
430 if ( Util
.apiBool( pi
.multi
) ) {
431 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
432 menu
: { items
: items
}
434 widget
.paramInfo
= pi
;
435 $.extend( widget
, WidgetMethods
.capsuleWidget
);
437 widget
= new OO
.ui
.DropdownWidget( {
438 menu
: { items
: items
}
440 widget
.paramInfo
= pi
;
441 $.extend( widget
, WidgetMethods
.dropdownWidget
);
446 if ( !$.isArray( pi
.type
) ) {
447 throw new Error( 'Unknown parameter type ' + pi
.type
);
450 items
= $.map( pi
.type
, function ( v
) {
451 return new OO
.ui
.MenuOptionWidget( { data
: String( v
), label
: String( v
) } );
453 if ( Util
.apiBool( pi
.multi
) ) {
454 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
455 menu
: { items
: items
}
457 widget
.paramInfo
= pi
;
458 $.extend( widget
, WidgetMethods
.capsuleWidget
);
459 if ( Util
.apiBool( pi
.submodules
) ) {
460 widget
.getSubmodules
= WidgetMethods
.submoduleWidget
.multi
;
461 widget
.on( 'change', ApiSandbox
.updateUI
);
464 widget
= new OO
.ui
.DropdownWidget( {
465 menu
: { items
: items
}
467 widget
.paramInfo
= pi
;
468 $.extend( widget
, WidgetMethods
.dropdownWidget
);
469 if ( Util
.apiBool( pi
.submodules
) ) {
470 widget
.getSubmodules
= WidgetMethods
.submoduleWidget
.single
;
471 widget
.getMenu().on( 'choose', ApiSandbox
.updateUI
);
478 if ( Util
.apiBool( pi
.multi
) && multiMode
!== 'none' ) {
479 innerWidget
= widget
;
480 switch ( multiMode
) {
482 $content
= innerWidget
.$element
;
486 $button
= innerWidget
.$indicator
;
487 $button
.css( 'cursor', 'pointer' );
488 $button
.attr( 'tabindex', 0 );
489 $button
.parent().append( $button
);
490 innerWidget
.setIndicator( 'next' );
491 $content
= innerWidget
.$element
;
495 throw new Error( 'Unknown multiMode "' + multiMode
+ '"' );
498 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
499 allowArbitrary
: true,
501 classes
: [ 'mw-apisandbox-popup' ],
505 widget
.paramInfo
= pi
;
506 $.extend( widget
, WidgetMethods
.capsuleWidget
);
509 if ( !innerWidget
.isDisabled() ) {
510 innerWidget
.apiCheckValid().done( function ( ok
) {
512 widget
.addItemsFromData( [ innerWidget
.getApiValue() ] );
513 innerWidget
.setApiValue( undefined );
519 switch ( multiMode
) {
521 innerWidget
.connect( null, { enter
: func
} );
527 keypress: function ( e
) {
528 if ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) {
537 if ( Util
.apiBool( pi
.required
) || opts
.nooptional
) {
538 finalWidget
= widget
;
540 finalWidget
= new OptionalWidget( widget
);
541 finalWidget
.paramInfo
= pi
;
542 $.extend( finalWidget
, WidgetMethods
.optionalWidget
);
543 if ( widget
.getSubmodules
) {
544 finalWidget
.getSubmodules
= widget
.getSubmodules
.bind( widget
);
545 finalWidget
.on( 'disable', function () { setTimeout( ApiSandbox
.updateUI
); } );
547 finalWidget
.setDisabled( true );
550 widget
.setApiValue( pi
[ 'default' ] );
556 * Parse an HTML string, adding target="_blank" to any links
558 * @param {string} html HTML to parse
561 parseHTML: function ( html
) {
562 var $ret
= $( $.parseHTML( html
) );
563 $ret
.filter( 'a' ).add( $ret
.find( 'a' ) )
564 .filter( '[href]:not([target])' )
565 .attr( 'target', '_blank' );
571 * Interface to ApiSandbox UI
573 * @class mw.special.ApiSandbox
579 * Automatically called on $.ready()
584 $content
= $( '#mw-apisandbox' );
586 windowManager
= new OO
.ui
.WindowManager();
587 $( 'body' ).append( windowManager
.$element
);
588 windowManager
.addWindows( {
589 errorAlert
: new OO
.ui
.MessageDialog()
592 fullscreenButton
= new OO
.ui
.ButtonWidget( {
593 label
: mw
.message( 'apisandbox-fullscreen' ).text(),
594 title
: mw
.message( 'apisandbox-fullscreen-tooltip' ).text()
595 } ).on( 'click', ApiSandbox
.toggleFullscreen
);
597 $toolbar
= $( '<div>' )
598 .addClass( 'mw-apisandbox-toolbar' )
600 fullscreenButton
.$element
,
601 new OO
.ui
.ButtonWidget( {
602 label
: mw
.message( 'apisandbox-submit' ).text(),
603 flags
: [ 'primary', 'progressive' ]
604 } ).on( 'click', ApiSandbox
.sendRequest
).$element
,
605 new OO
.ui
.ButtonWidget( {
606 label
: mw
.message( 'apisandbox-reset' ).text(),
608 } ).on( 'click', ApiSandbox
.resetUI
).$element
611 booklet
= new OO
.ui
.BookletLayout( {
616 panel
= new OO
.ui
.PanelLayout( {
617 classes
: [ 'mw-apisandbox-container' ],
618 content
: [ booklet
],
623 pages
.main
= new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } );
625 // Parse the current hash string
626 if ( !ApiSandbox
.loadFromHash() ) {
627 ApiSandbox
.updateUI();
630 // If the hashchange event exists, use it. Otherwise, fake it.
631 // And, of course, IE has to be dumb.
632 if ( 'onhashchange' in window
&&
633 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
635 $( window
).on( 'hashchange', ApiSandbox
.loadFromHash
);
637 setInterval( function () {
638 if ( oldhash
!== location
.hash
) {
639 ApiSandbox
.loadFromHash();
646 .append( $( '<p>' ).append( mw
.message( 'apisandbox-intro' ).parse() ) )
648 $( '<div>', { id
: 'mw-apisandbox-ui' } )
650 .append( panel
.$element
)
653 $( window
).on( 'resize', ApiSandbox
.resizePanel
);
655 ApiSandbox
.resizePanel();
659 * Toggle "fullscreen" mode
661 toggleFullscreen: function () {
662 var $body
= $( document
.body
);
664 $body
.toggleClass( 'mw-apisandbox-fullscreen' );
665 if ( $body
.hasClass( 'mw-apisandbox-fullscreen' ) ) {
666 fullscreenButton
.setLabel( mw
.message( 'apisandbox-unfullscreen' ).text() );
667 fullscreenButton
.setTitle( mw
.message( 'apisandbox-unfullscreen-tooltip' ).text() );
668 $body
.append( $( '#mw-apisandbox-ui' ) );
670 fullscreenButton
.setLabel( mw
.message( 'apisandbox-fullscreen' ).text() );
671 fullscreenButton
.setTitle( mw
.message( 'apisandbox-fullscreen-tooltip' ).text() );
672 $content
.append( $( '#mw-apisandbox-ui' ) );
674 ApiSandbox
.resizePanel();
678 * Set the height of the panel based on the current viewport.
680 resizePanel: function () {
681 var height
= $( window
).height(),
682 contentTop
= $content
.offset().top
;
684 if ( $( document
.body
).hasClass( 'mw-apisandbox-fullscreen' ) ) {
685 height
-= panel
.$element
.offset().top
- $( '#mw-apisandbox-ui' ).offset().top
;
686 panel
.$element
.height( height
- 1 );
688 // Subtract the height of the intro text
689 height
-= panel
.$element
.offset().top
- contentTop
;
691 panel
.$element
.height( height
- 10 );
692 $( window
).scrollTop( contentTop
- 5 );
697 * Update the current query when the page hash changes
699 loadFromHash: function () {
701 hash
= location
.hash
;
703 if ( oldhash
=== hash
) {
711 // I'm surprised this doesn't seem to exist in jQuery or mw.util.
713 hash
= hash
.replace( /\+/g, '%20' );
714 re
= /([^&=#]+)=?([^&#]*)/g;
715 while ( ( m
= re
.exec( hash
) ) ) {
716 params
[ decodeURIComponent( m
[ 1 ] ) ] = decodeURIComponent( m
[ 2 ] );
719 ApiSandbox
.updateUI( params
);
724 * Update the pages in the booklet
726 * @param {Object} [params] Optional query parameters to load
728 updateUI: function ( params
) {
729 var i
, page
, subpages
, j
, removePages
,
732 if ( !$.isPlainObject( params
) ) {
736 if ( updatingBooklet
) {
739 updatingBooklet
= true;
741 if ( params
!== undefined ) {
742 pages
.main
.loadQueryParams( params
);
744 addPages
.push( pages
.main
);
745 if ( resultPage
!== null ) {
746 addPages
.push( resultPage
);
748 pages
.main
.apiCheckValid();
751 while ( addPages
.length
) {
752 page
= addPages
.shift();
753 if ( bookletPages
[ i
] !== page
) {
754 for ( j
= i
; j
< bookletPages
.length
; j
++ ) {
755 if ( bookletPages
[ j
].getName() === page
.getName() ) {
756 bookletPages
.splice( j
, 1 );
759 bookletPages
.splice( i
, 0, page
);
760 booklet
.addPages( [ page
], i
);
764 if ( page
.getSubpages
) {
765 subpages
= page
.getSubpages();
766 for ( j
= 0; j
< subpages
.length
; j
++ ) {
767 if ( !pages
.hasOwnProperty( subpages
[ j
].key
) ) {
768 subpages
[ j
].indentLevel
= page
.indentLevel
+ 1;
769 pages
[ subpages
[ j
].key
] = new ApiSandbox
.PageLayout( subpages
[ j
] );
771 if ( params
!== undefined ) {
772 pages
[ subpages
[ j
].key
].loadQueryParams( params
);
774 addPages
.splice( j
, 0, pages
[ subpages
[ j
].key
] );
775 pages
[ subpages
[ j
].key
].apiCheckValid();
780 if ( bookletPages
.length
> i
) {
781 removePages
= bookletPages
.splice( i
, bookletPages
.length
- i
);
782 booklet
.removePages( removePages
);
785 if ( !booklet
.getCurrentPageName() ) {
786 booklet
.selectFirstSelectablePage();
789 updatingBooklet
= false;
794 * Reset button handler
796 resetUI: function () {
797 suppressErrors
= true;
799 main
: new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } )
802 ApiSandbox
.updateUI();
806 * Submit button handler
808 * @param {Object} [params] Use this set of params instead of those in the form fields.
809 * The form fields will be updated to match.
811 sendRequest: function ( params
) {
812 var page
, subpages
, i
, query
, $result
, $focus
,
813 progress
, $progressText
, progressLoading
,
815 paramsAreForced
= !!params
,
817 checkPages
= [ pages
.main
];
819 // Blur any focused widget before submit, because
820 // OO.ui.ButtonWidget doesn't take focus itself (T128054)
821 $focus
= $( '#mw-apisandbox-ui' ).find( document
.activeElement
);
822 if ( $focus
.length
) {
826 suppressErrors
= false;
828 // save widget state in params (or load from it if we are forced)
829 if ( paramsAreForced
) {
830 ApiSandbox
.updateUI( params
);
833 while ( checkPages
.length
) {
834 page
= checkPages
.shift();
835 deferreds
.push( page
.apiCheckValid() );
836 page
.getQueryParams( params
, displayParams
);
837 subpages
= page
.getSubpages();
838 for ( i
= 0; i
< subpages
.length
; i
++ ) {
839 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
840 checkPages
.push( pages
[ subpages
[ i
].key
] );
845 if ( !paramsAreForced
) {
846 // forced params means we are continuing a query; the base query should be preserved
847 baseRequestParams
= $.extend( {}, params
);
850 $.when
.apply( $, deferreds
).done( function () {
851 if ( $.inArray( false, arguments
) !== -1 ) {
852 windowManager
.openWindow( 'errorAlert', {
853 title
: mw
.message( 'apisandbox-submit-invalid-fields-title' ).parse(),
854 message
: mw
.message( 'apisandbox-submit-invalid-fields-message' ).parse(),
858 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
866 query
= $.param( displayParams
);
868 // Force a 'fm' format with wrappedhtml=1, if available
869 if ( params
.format
!== undefined ) {
870 if ( availableFormats
.hasOwnProperty( params
.format
+ 'fm' ) ) {
871 params
.format
= params
.format
+ 'fm';
873 if ( params
.format
.substr( -2 ) === 'fm' ) {
874 params
.wrappedhtml
= 1;
878 progressLoading
= false;
879 $progressText
= $( '<span>' ).text( mw
.message( 'apisandbox-sending-request' ).text() );
880 progress
= new OO
.ui
.ProgressBarWidget( {
882 $content
: $progressText
885 $result
= $( '<div>' )
886 .append( progress
.$element
);
888 resultPage
= page
= new OO
.ui
.PageLayout( '|results|' );
889 page
.setupOutlineItem = function () {
890 this.outlineItem
.setLabel( mw
.message( 'apisandbox-results' ).text() );
892 page
.$element
.empty()
894 new OO
.ui
.FieldLayout(
895 new OO
.ui
.TextInputWidget( {
897 value
: mw
.util
.wikiScript( 'api' ) + '?' + query
899 label
: mw
.message( 'apisandbox-request-url-label' ).parse()
904 ApiSandbox
.updateUI();
905 booklet
.setPage( '|results|' );
907 location
.href
= oldhash
= '#' + query
;
910 contentType
: 'multipart/form-data',
913 var xhr
= new window
.XMLHttpRequest();
914 xhr
.upload
.addEventListener( 'progress', function ( e
) {
915 if ( !progressLoading
) {
916 if ( e
.lengthComputable
) {
917 progress
.setProgress( e
.loaded
* 100 / e
.total
);
919 progress
.setProgress( false );
923 xhr
.addEventListener( 'progress', function ( e
) {
924 if ( !progressLoading
) {
925 progressLoading
= true;
926 $progressText
.text( mw
.message( 'apisandbox-loading-results' ).text() );
928 if ( e
.lengthComputable
) {
929 progress
.setProgress( e
.loaded
* 100 / e
.total
);
931 progress
.setProgress( false );
937 .then( null, function ( code
, data
, result
, jqXHR
) {
938 if ( code
!== 'http' ) {
939 // Not really an error, work around mw.Api thinking it is.
941 .resolve( result
, jqXHR
)
946 .fail( function ( code
, data
) {
947 var details
= 'HTTP error: ' + data
.exception
;
950 new OO
.ui
.LabelWidget( {
951 label
: mw
.message( 'apisandbox-results-error', details
).text(),
956 .done( function ( data
, jqXHR
) {
957 var m
, loadTime
, button
, clear
,
958 ct
= jqXHR
.getResponseHeader( 'Content-Type' );
961 if ( /^text\/mediawiki-api-prettyprint-wrapped(?:;|$)/.test( ct
) ) {
962 data
= $.parseJSON( data
);
963 if ( data
.modules
.length
) {
964 mw
.loader
.load( data
.modules
);
966 $result
.append( Util
.parseHTML( data
.html
) );
967 loadTime
= data
.time
;
968 } else if ( ( m
= data
.match( /<pre[ >][\s\S]*<\/pre>/ ) ) ) {
969 $result
.append( Util
.parseHTML( m
[ 0 ] ) );
970 if ( ( m
= data
.match( /"wgBackendResponseTime":\s*(\d+)/ ) ) ) {
971 loadTime
= parseInt( m
[ 1 ], 10 );
975 .addClass( 'api-pretty-content' )
977 .appendTo( $result
);
979 if ( paramsAreForced
|| data
[ 'continue' ] ) {
982 new OO
.ui
.ButtonWidget( {
983 label
: mw
.message( 'apisandbox-continue' ).text()
984 } ).on( 'click', function () {
985 ApiSandbox
.sendRequest( $.extend( {}, baseRequestParams
, data
[ 'continue' ] ) );
986 } ).setDisabled( !data
[ 'continue' ] ).$element
,
987 ( clear
= new OO
.ui
.ButtonWidget( {
988 label
: mw
.message( 'apisandbox-continue-clear' ).text()
989 } ).on( 'click', function () {
990 ApiSandbox
.updateUI( baseRequestParams
);
991 clear
.setDisabled( true );
992 booklet
.setPage( '|results|' );
993 } ).setDisabled( !paramsAreForced
) ).$element
,
994 new OO
.ui
.PopupButtonWidget( {
998 $content
: $( '<div>' ).append( mw
.message( 'apisandbox-continue-help' ).parse() ),
1005 if ( typeof loadTime
=== 'number' ) {
1007 $( '<div>' ).append(
1008 new OO
.ui
.LabelWidget( {
1009 label
: mw
.message( 'apisandbox-request-time', loadTime
).text()
1015 if ( jqXHR
.getResponseHeader( 'MediaWiki-API-Error' ) === 'badtoken' ) {
1016 // Flush all saved tokens in case one of them is the bad one.
1017 Util
.markTokensBad();
1018 button
= new OO
.ui
.ButtonWidget( {
1019 label
: mw
.message( 'apisandbox-results-fixtoken' ).text()
1021 button
.on( 'click', ApiSandbox
.fixTokenAndResend
)
1022 .on( 'click', button
.setDisabled
, [ true ], button
)
1023 .$element
.appendTo( $result
);
1030 * Handler for the "Correct token and resubmit" button
1032 * Used on a 'badtoken' error, it re-fetches token parameters for all
1033 * pages and then re-submits the query.
1035 fixTokenAndResend: function () {
1036 var page
, subpages
, i
, k
,
1038 tokenWait
= { dummy
: true },
1039 checkPages
= [ pages
.main
],
1040 success = function ( k
) {
1041 delete tokenWait
[ k
];
1042 if ( ok
&& $.isEmptyObject( tokenWait
) ) {
1043 ApiSandbox
.sendRequest();
1046 failure = function ( k
) {
1047 delete tokenWait
[ k
];
1051 while ( checkPages
.length
) {
1052 page
= checkPages
.shift();
1054 if ( page
.tokenWidget
) {
1055 k
= page
.apiModule
+ page
.tokenWidget
.paramInfo
.name
;
1056 tokenWait
[ k
] = page
.tokenWidget
.fetchToken()
1057 .done( success
.bind( page
.tokenWidget
, k
) )
1058 .fail( failure
.bind( page
.tokenWidget
, k
) );
1061 subpages
= page
.getSubpages();
1062 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1063 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1064 checkPages
.push( pages
[ subpages
[ i
].key
] );
1069 success( 'dummy', '' );
1073 * Reset validity indicators for all widgets
1075 updateValidityIndicators: function () {
1076 var page
, subpages
, i
,
1077 checkPages
= [ pages
.main
];
1079 while ( checkPages
.length
) {
1080 page
= checkPages
.shift();
1081 page
.apiCheckValid();
1082 subpages
= page
.getSubpages();
1083 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1084 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1085 checkPages
.push( pages
[ subpages
[ i
].key
] );
1093 * PageLayout for API modules
1097 * @extends OO.ui.PageLayout
1099 * @param {Object} [config] Configuration options
1101 ApiSandbox
.PageLayout = function ( config
) {
1102 config
= $.extend( { prefix
: '' }, config
);
1103 this.displayText
= config
.key
;
1104 this.apiModule
= config
.path
;
1105 this.prefix
= config
.prefix
;
1106 this.paramInfo
= null;
1107 this.apiIsValid
= true;
1108 this.loadFromQueryParams
= null;
1110 this.tokenWidget
= null;
1111 this.indentLevel
= config
.indentLevel
? config
.indentLevel
: 0;
1112 ApiSandbox
.PageLayout
[ 'super' ].call( this, config
.key
, config
);
1113 this.loadParamInfo();
1115 OO
.inheritClass( ApiSandbox
.PageLayout
, OO
.ui
.PageLayout
);
1116 ApiSandbox
.PageLayout
.prototype.setupOutlineItem = function () {
1117 this.outlineItem
.setLevel( this.indentLevel
);
1118 this.outlineItem
.setLabel( this.displayText
);
1119 this.outlineItem
.setIcon( this.apiIsValid
|| suppressErrors
? null : 'alert' );
1120 this.outlineItem
.setIconTitle(
1121 this.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1126 * Fetch module information for this page's module, then create UI
1128 ApiSandbox
.PageLayout
.prototype.loadParamInfo = function () {
1129 var dynamicFieldset
, dynamicParamNameWidget
,
1131 removeDynamicParamWidget = function ( name
, layout
) {
1132 dynamicFieldset
.removeItems( [ layout
] );
1133 delete that
.widgets
[ name
];
1135 addDynamicParamWidget = function () {
1136 var name
, layout
, widget
, button
;
1138 // Check name is filled in
1139 name
= dynamicParamNameWidget
.getValue().trim();
1140 if ( name
=== '' ) {
1141 dynamicParamNameWidget
.focus();
1145 if ( that
.widgets
[ name
] !== undefined ) {
1146 windowManager
.openWindow( 'errorAlert', {
1148 'apisandbox-dynamic-error-exists', name
1153 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
1161 widget
= Util
.createWidgetForParameter( {
1168 button
= new OO
.ui
.ButtonWidget( {
1170 flags
: 'destructive'
1172 layout
= new OO
.ui
.ActionFieldLayout(
1180 button
.on( 'click', removeDynamicParamWidget
, [ name
, layout
] );
1181 that
.widgets
[ name
] = widget
;
1182 dynamicFieldset
.addItems( [ layout
], dynamicFieldset
.getItems().length
- 1 );
1185 dynamicParamNameWidget
.setValue( '' );
1188 this.$element
.empty()
1189 .append( new OO
.ui
.ProgressBarWidget( {
1191 text
: mw
.message( 'apisandbox-loading', this.displayText
).text()
1194 Util
.fetchModuleInfo( this.apiModule
)
1195 .done( function ( pi
) {
1196 var prefix
, i
, j
, dl
, widget
, $widgetLabel
, widgetField
, helpField
, tmp
, flag
, count
,
1198 deprecatedItems
= [],
1200 filterFmModules = function ( v
) {
1201 return v
.substr( -2 ) !== 'fm' ||
1202 !availableFormats
.hasOwnProperty( v
.substr( 0, v
.length
- 2 ) );
1204 widgetLabelOnClick = function () {
1205 var f
= this.getField();
1206 if ( $.isFunction( f
.setDisabled
) ) {
1207 f
.setDisabled( false );
1209 if ( $.isFunction( f
.focus
) ) {
1213 doNothing = function () {};
1215 // This is something of a hack. We always want the 'format' and
1216 // 'action' parameters from the main module to be specified,
1217 // and for 'format' we also want to simplify the dropdown since
1218 // we always send the 'fm' variant.
1219 if ( that
.apiModule
=== 'main' ) {
1220 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1221 if ( pi
.parameters
[ i
].name
=== 'action' ) {
1222 pi
.parameters
[ i
].required
= true;
1223 delete pi
.parameters
[ i
][ 'default' ];
1225 if ( pi
.parameters
[ i
].name
=== 'format' ) {
1226 tmp
= pi
.parameters
[ i
].type
;
1227 for ( j
= 0; j
< tmp
.length
; j
++ ) {
1228 availableFormats
[ tmp
[ j
] ] = true;
1230 pi
.parameters
[ i
].type
= $.grep( tmp
, filterFmModules
);
1231 pi
.parameters
[ i
][ 'default' ] = 'json';
1232 pi
.parameters
[ i
].required
= true;
1237 // Hide the 'wrappedhtml' parameter on format modules
1238 if ( pi
.group
=== 'format' ) {
1239 pi
.parameters
= $.grep( pi
.parameters
, function ( p
) {
1240 return p
.name
!== 'wrappedhtml';
1244 that
.paramInfo
= pi
;
1246 items
.push( new OO
.ui
.FieldLayout(
1247 new OO
.ui
.Widget( {} ).toggle( false ), {
1249 label
: Util
.parseHTML( pi
.description
)
1253 if ( pi
.helpurls
.length
) {
1254 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1255 label
: mw
.message( 'apisandbox-helpurls' ).text(),
1258 $content
: $( '<ul>' ).append( $.map( pi
.helpurls
, function ( link
) {
1259 return $( '<li>' ).append( $( '<a>', {
1269 if ( pi
.examples
.length
) {
1270 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1271 label
: mw
.message( 'apisandbox-examples' ).text(),
1274 $content
: $( '<ul>' ).append( $.map( pi
.examples
, function ( example
) {
1276 href
: '#' + example
.query
,
1277 html
: example
.description
1279 a
.find( 'a' ).contents().unwrap(); // Can't nest links
1280 return $( '<li>' ).append( a
);
1286 if ( buttons
.length
) {
1287 items
.push( new OO
.ui
.FieldLayout(
1288 new OO
.ui
.ButtonGroupWidget( {
1290 } ), { align
: 'top' }
1294 if ( pi
.parameters
.length
) {
1295 prefix
= that
.prefix
+ pi
.prefix
;
1296 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1297 widget
= Util
.createWidgetForParameter( pi
.parameters
[ i
] );
1298 that
.widgets
[ prefix
+ pi
.parameters
[ i
].name
] = widget
;
1299 if ( pi
.parameters
[ i
].tokentype
) {
1300 that
.tokenWidget
= widget
;
1304 dl
.append( $( '<dd>', {
1305 addClass
: 'description',
1306 append
: Util
.parseHTML( pi
.parameters
[ i
].description
)
1308 if ( pi
.parameters
[ i
].info
&& pi
.parameters
[ i
].info
.length
) {
1309 for ( j
= 0; j
< pi
.parameters
[ i
].info
.length
; j
++ ) {
1310 dl
.append( $( '<dd>', {
1312 append
: Util
.parseHTML( pi
.parameters
[ i
].info
[ j
] )
1318 switch ( pi
.parameters
[ i
].type
) {
1321 count
= mw
.config
.get( 'wgFormattedNamespaces' ).length
;
1325 if ( pi
.parameters
[ i
].highmax
!== undefined ) {
1326 dl
.append( $( '<dd>', {
1328 append
: Util
.parseHTML( mw
.message(
1329 'api-help-param-limit2', pi
.parameters
[ i
].max
, pi
.parameters
[ i
].highmax
1333 dl
.append( $( '<dd>', {
1335 append
: Util
.parseHTML( mw
.message(
1336 'api-help-param-limit', pi
.parameters
[ i
].max
1344 if ( pi
.parameters
[ i
].min
!== undefined ) {
1347 if ( pi
.parameters
[ i
].max
!== undefined ) {
1351 dl
.append( $( '<dd>', {
1353 append
: Util
.parseHTML( mw
.message(
1354 'api-help-param-integer-' + tmp
,
1355 Util
.apiBool( pi
.parameters
[ i
].multi
) ? 2 : 1,
1356 pi
.parameters
[ i
].min
, pi
.parameters
[ i
].max
1363 if ( $.isArray( pi
.parameters
[ i
].type
) ) {
1365 count
= pi
.parameters
[ i
].type
.length
;
1369 if ( Util
.apiBool( pi
.parameters
[ i
].multi
) ) {
1371 if ( flag
&& !( widget
instanceof OO
.ui
.CapsuleMultiselectWidget
) &&
1373 widget
instanceof OptionalWidget
&&
1374 widget
.widget
instanceof OO
.ui
.CapsuleMultiselectWidget
1377 tmp
.push( mw
.message( 'api-help-param-multi-separate' ).parse() );
1379 if ( count
> pi
.parameters
[ i
].lowlimit
) {
1381 mw
.message( 'api-help-param-multi-max',
1382 pi
.parameters
[ i
].lowlimit
, pi
.parameters
[ i
].highlimit
1387 dl
.append( $( '<dd>', {
1389 append
: Util
.parseHTML( tmp
.join( ' ' ) )
1393 helpField
= new OO
.ui
.FieldLayout(
1396 classes
: [ 'mw-apisandbox-spacer' ]
1399 classes
: [ 'mw-apisandbox-help-field' ],
1404 $widgetLabel
= $( '<span>' );
1405 widgetField
= new OO
.ui
.FieldLayout(
1409 classes
: [ 'mw-apisandbox-widget-field' ],
1410 label
: prefix
+ pi
.parameters
[ i
].name
,
1411 $label
: $widgetLabel
1415 // FieldLayout only does click for InputElement
1416 // widgets. So supply our own click handler.
1417 $widgetLabel
.on( 'click', widgetLabelOnClick
.bind( widgetField
) );
1419 // Don't grey out the label when the field is disabled,
1420 // it makes it too hard to read and our "disabled"
1421 // isn't really disabled.
1422 widgetField
.onFieldDisable
= doNothing
;
1424 if ( Util
.apiBool( pi
.parameters
[ i
].deprecated
) ) {
1425 deprecatedItems
.push( widgetField
, helpField
);
1427 items
.push( widgetField
, helpField
);
1432 if ( !pi
.parameters
.length
&& !Util
.apiBool( pi
.dynamicparameters
) ) {
1433 items
.push( new OO
.ui
.FieldLayout(
1434 new OO
.ui
.Widget( {} ).toggle( false ), {
1436 label
: Util
.parseHTML( mw
.message( 'apisandbox-no-parameters' ).parse() )
1441 that
.$element
.empty();
1443 new OO
.ui
.FieldsetLayout( {
1444 label
: that
.displayText
1445 } ).addItems( items
)
1446 .$element
.appendTo( that
.$element
);
1448 if ( Util
.apiBool( pi
.dynamicparameters
) ) {
1449 dynamicFieldset
= new OO
.ui
.FieldsetLayout();
1450 dynamicParamNameWidget
= new OO
.ui
.TextInputWidget( {
1451 placeholder
: mw
.message( 'apisandbox-dynamic-parameters-add-placeholder' ).text()
1452 } ).on( 'enter', addDynamicParamWidget
);
1453 dynamicFieldset
.addItems( [
1454 new OO
.ui
.FieldLayout(
1455 new OO
.ui
.Widget( {} ).toggle( false ), {
1457 label
: Util
.parseHTML( pi
.dynamicparameters
)
1460 new OO
.ui
.ActionFieldLayout(
1461 dynamicParamNameWidget
,
1462 new OO
.ui
.ButtonWidget( {
1464 flags
: 'progressive'
1465 } ).on( 'click', addDynamicParamWidget
),
1467 label
: mw
.message( 'apisandbox-dynamic-parameters-add-label' ).text(),
1474 $( '<legend>' ).text( mw
.message( 'apisandbox-dynamic-parameters' ).text() ),
1475 dynamicFieldset
.$element
1477 .appendTo( that
.$element
);
1480 if ( deprecatedItems
.length
) {
1481 tmp
= new OO
.ui
.FieldsetLayout().addItems( deprecatedItems
).toggle( false );
1484 $( '<legend>' ).append(
1485 new OO
.ui
.ToggleButtonWidget( {
1486 label
: mw
.message( 'apisandbox-deprecated-parameters' ).text()
1487 } ).on( 'change', tmp
.toggle
, [], tmp
).$element
1491 .appendTo( that
.$element
);
1494 // Load stored params, if any, then update the booklet if we
1495 // have subpages (or else just update our valid-indicator).
1496 tmp
= that
.loadFromQueryParams
;
1497 that
.loadFromQueryParams
= null;
1498 if ( $.isPlainObject( tmp
) ) {
1499 that
.loadQueryParams( tmp
);
1501 if ( that
.getSubpages().length
> 0 ) {
1502 ApiSandbox
.updateUI( tmp
);
1504 that
.apiCheckValid();
1506 } ).fail( function ( code
, detail
) {
1507 that
.$element
.empty()
1509 new OO
.ui
.LabelWidget( {
1510 label
: mw
.message( 'apisandbox-load-error', that
.apiModule
, detail
).text(),
1511 classes
: [ 'error' ]
1513 new OO
.ui
.ButtonWidget( {
1514 label
: mw
.message( 'apisandbox-retry' ).text()
1515 } ).on( 'click', that
.loadParamInfo
, [], that
).$element
1521 * Check that all widgets on the page are in a valid state.
1525 ApiSandbox
.PageLayout
.prototype.apiCheckValid = function () {
1528 if ( this.paramInfo
=== null ) {
1529 return $.Deferred().resolve( false ).promise();
1531 return $.when
.apply( $, $.map( this.widgets
, function ( widget
) {
1532 return widget
.apiCheckValid();
1533 } ) ).then( function () {
1534 that
.apiIsValid
= $.inArray( false, arguments
) === -1;
1535 if ( that
.getOutlineItem() ) {
1536 that
.getOutlineItem().setIcon( that
.apiIsValid
|| suppressErrors
? null : 'alert' );
1537 that
.getOutlineItem().setIconTitle(
1538 that
.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1541 return $.Deferred().resolve( that
.apiIsValid
).promise();
1547 * Load form fields from query parameters
1549 * @param {Object} params
1551 ApiSandbox
.PageLayout
.prototype.loadQueryParams = function ( params
) {
1552 if ( this.paramInfo
=== null ) {
1553 this.loadFromQueryParams
= params
;
1555 $.each( this.widgets
, function ( name
, widget
) {
1556 var v
= params
.hasOwnProperty( name
) ? params
[ name
] : undefined;
1557 widget
.setApiValue( v
);
1563 * Load query params from form fields
1565 * @param {Object} params Write query parameters into this object
1566 * @param {Object} displayParams Write query parameters for display into this object
1568 ApiSandbox
.PageLayout
.prototype.getQueryParams = function ( params
, displayParams
) {
1569 $.each( this.widgets
, function ( name
, widget
) {
1570 var value
= widget
.getApiValue();
1571 if ( value
!== undefined ) {
1572 params
[ name
] = value
;
1573 if ( $.isFunction( widget
.getApiValueForDisplay
) ) {
1574 value
= widget
.getApiValueForDisplay();
1576 displayParams
[ name
] = value
;
1582 * Fetch a list of subpage names loaded by this page
1586 ApiSandbox
.PageLayout
.prototype.getSubpages = function () {
1588 $.each( this.widgets
, function ( name
, widget
) {
1590 if ( $.isFunction( widget
.getSubmodules
) ) {
1591 submodules
= widget
.getSubmodules();
1592 for ( i
= 0; i
< submodules
.length
; i
++ ) {
1594 key
: name
+ '=' + submodules
[ i
].value
,
1595 path
: submodules
[ i
].path
,
1596 prefix
: widget
.paramInfo
.submoduleparamprefix
|| ''
1605 * A text input with a clickable indicator
1610 * @param {Object} [config] Configuration options
1612 function TextInputWithIndicatorWidget( config
) {
1615 config
= config
|| {};
1616 TextInputWithIndicatorWidget
[ 'super' ].call( this, config
);
1618 this.$indicator
= $( '<span>' ).addClass( 'mw-apisandbox-clickable-indicator' );
1619 OO
.ui
.mixin
.TabIndexedElement
.call(
1620 this, $.extend( {}, config
, { $tabIndexed
: this.$indicator
} )
1623 this.input
= new OO
.ui
.TextInputWidget( $.extend( {
1624 $indicator
: this.$indicator
,
1625 disabled
: this.isDisabled()
1626 }, config
.input
) );
1628 // Forward most methods for convenience
1629 for ( k
in this.input
) {
1630 if ( $.isFunction( this.input
[ k
] ) && !this[ k
] ) {
1631 this[ k
] = this.input
[ k
].bind( this.input
);
1635 this.$indicator
.on( {
1636 click
: this.onIndicatorClick
.bind( this ),
1637 keypress
: this.onIndicatorKeyPress
.bind( this )
1640 this.$element
.append( this.input
.$element
);
1642 OO
.inheritClass( TextInputWithIndicatorWidget
, OO
.ui
.Widget
);
1643 OO
.mixinClass( TextInputWithIndicatorWidget
, OO
.ui
.mixin
.TabIndexedElement
);
1644 TextInputWithIndicatorWidget
.prototype.onIndicatorClick = function ( e
) {
1645 if ( !this.isDisabled() && e
.which
=== 1 ) {
1646 this.emit( 'indicator' );
1650 TextInputWithIndicatorWidget
.prototype.onIndicatorKeyPress = function ( e
) {
1651 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
1652 this.emit( 'indicator' );
1656 TextInputWithIndicatorWidget
.prototype.setDisabled = function ( disabled
) {
1657 TextInputWithIndicatorWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1659 this.input
.setDisabled( this.isDisabled() );
1665 * A wrapper for a widget that provides an enable/disable button
1670 * @param {OO.ui.Widget} widget
1671 * @param {Object} [config] Configuration options
1673 function OptionalWidget( widget
, config
) {
1676 config
= config
|| {};
1678 this.widget
= widget
;
1679 this.$overlay
= config
.$overlay
||
1680 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-overlay' );
1681 this.checkbox
= new OO
.ui
.CheckboxInputWidget( config
.checkbox
)
1682 .on( 'change', this.onCheckboxChange
, [], this );
1684 OptionalWidget
[ 'super' ].call( this, config
);
1686 // Forward most methods for convenience
1687 for ( k
in this.widget
) {
1688 if ( $.isFunction( this.widget
[ k
] ) && !this[ k
] ) {
1689 this[ k
] = this.widget
[ k
].bind( this.widget
);
1693 this.$overlay
.on( 'click', this.onOverlayClick
.bind( this ) );
1696 .addClass( 'mw-apisandbox-optionalWidget' )
1699 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-fields' ).append(
1700 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-widget' ).append(
1703 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-checkbox' ).append(
1704 this.checkbox
.$element
1709 this.setDisabled( widget
.isDisabled() );
1711 OO
.inheritClass( OptionalWidget
, OO
.ui
.Widget
);
1712 OptionalWidget
.prototype.onCheckboxChange = function ( checked
) {
1713 this.setDisabled( !checked
);
1715 OptionalWidget
.prototype.onOverlayClick = function () {
1716 this.setDisabled( false );
1717 if ( $.isFunction( this.widget
.focus
) ) {
1718 this.widget
.focus();
1721 OptionalWidget
.prototype.setDisabled = function ( disabled
) {
1722 OptionalWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1723 this.widget
.setDisabled( this.isDisabled() );
1724 this.checkbox
.setSelected( !this.isDisabled() );
1725 this.$overlay
.toggle( this.isDisabled() );
1729 $( ApiSandbox
.init
);
1731 module
.exports
= ApiSandbox
;
1733 }( jQuery
, mediaWiki
, OO
) );