2 ( function ( $, mw
, OO
) {
4 var ApiSandbox
, Util
, WidgetMethods
, Validators
,
5 $content
, panel
, booklet
, oldhash
, windowManager
, fullscreenButton
,
10 suppressErrors
= true,
11 updatingBooklet
= false,
18 getApiValue: function () {
19 return this.getValue();
21 setApiValue: function ( v
) {
22 if ( v
=== undefined ) {
23 v
= this.paramInfo
[ 'default' ];
27 apiCheckValid: function () {
29 return this.getValidity().then( function () {
30 return $.Deferred().resolve( true ).promise();
32 return $.Deferred().resolve( false ).promise();
33 } ).done( function ( ok
) {
34 ok
= ok
|| suppressErrors
;
35 that
.setIcon( ok
? null : 'alert' );
36 that
.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
41 dateTimeInputWidget
: {
42 getValidity: function () {
43 if ( !Util
.apiBool( this.paramInfo
.required
) || this.getApiValue() !== '' ) {
44 return $.Deferred().resolve().promise();
46 return $.Deferred().reject().promise();
52 alertTokenError: function ( code
, error
) {
53 windowManager
.openWindow( 'errorAlert', {
55 'apisandbox-results-fixtoken-fail', this.paramInfo
.tokentype
61 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
67 fetchToken: function () {
69 return api
.getToken( this.paramInfo
.tokentype
)
70 .done( this.setApiValue
.bind( this ) )
71 .fail( this.alertTokenError
.bind( this ) )
72 .always( this.popPending
.bind( this ) );
74 setApiValue: function ( v
) {
75 WidgetMethods
.textInputWidget
.setApiValue
.call( this, v
);
76 if ( v
=== '123ABC' ) {
83 getApiValueForDisplay: function () {
89 getApiValue: function () {
90 return this.getValue() ? 1 : undefined;
92 setApiValue: function ( v
) {
93 this.setValue( Util
.apiBool( v
) );
95 apiCheckValid: function () {
96 return $.Deferred().resolve( true ).promise();
101 getApiValue: function () {
102 var item
= this.getMenu().getSelectedItem();
103 return item
=== null ? undefined : item
.getData();
105 setApiValue: function ( v
) {
106 var menu
= this.getMenu();
108 if ( v
=== undefined ) {
109 v
= this.paramInfo
[ 'default' ];
111 if ( v
=== undefined ) {
114 menu
.selectItemByData( String( v
) );
117 apiCheckValid: function () {
118 var ok
= this.getApiValue() !== undefined || suppressErrors
;
119 this.setIcon( ok
? null : 'alert' );
120 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
121 return $.Deferred().resolve( ok
).promise();
126 getApiValue: function () {
127 var items
= this.getItemsData();
128 if ( items
.join( '' ).indexOf( '|' ) === -1 ) {
129 return items
.join( '|' );
131 return '\x1f' + items
.join( '\x1f' );
134 setApiValue: function ( v
) {
135 if ( v
=== undefined || v
=== '' || v
=== '\x1f' ) {
136 this.setItemsFromData( [] );
139 if ( v
.indexOf( '\x1f' ) !== 0 ) {
140 this.setItemsFromData( v
.split( '|' ) );
142 this.setItemsFromData( v
.substr( 1 ).split( '\x1f' ) );
146 apiCheckValid: function () {
147 var ok
= this.getApiValue() !== undefined || suppressErrors
;
148 this.setIcon( ok
? null : 'alert' );
149 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
150 return $.Deferred().resolve( ok
).promise();
155 getApiValue: function () {
156 return this.isDisabled() ? undefined : this.widget
.getApiValue();
158 setApiValue: function ( v
) {
159 this.setDisabled( v
=== undefined );
160 this.widget
.setApiValue( v
);
162 apiCheckValid: function () {
163 if ( this.isDisabled() ) {
164 return $.Deferred().resolve( true ).promise();
166 return this.widget
.apiCheckValid();
172 single: function () {
173 var v
= this.isDisabled() ? this.paramInfo
[ 'default' ] : this.getApiValue();
174 return v
=== undefined ? [] : [ { value
: v
, path
: this.paramInfo
.submodules
[ v
] } ];
177 var map
= this.paramInfo
.submodules
,
178 v
= this.isDisabled() ? this.paramInfo
[ 'default' ] : this.getApiValue();
179 return v
=== undefined || v
=== '' ? [] : $.map( String( v
).split( '|' ), function ( v
) {
180 return { value
: v
, path
: map
[ v
] };
186 getApiValueForDisplay: function () {
189 getApiValue: function () {
190 return this.getValue();
192 setApiValue: function () {
195 apiCheckValid: function () {
196 var ok
= this.getValue() !== null || suppressErrors
;
197 this.setIcon( ok
? null : 'alert' );
198 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
199 return $.Deferred().resolve( ok
).promise();
205 generic: function () {
206 return !Util
.apiBool( this.paramInfo
.required
) || this.getApiValue() !== '';
211 * @class mw.special.ApiSandbox.Utils
216 * Fetch API module info
218 * @param {string} module Module to fetch data for
219 * @return {jQuery.Promise}
221 fetchModuleInfo: function ( module
) {
223 deferred
= $.Deferred();
225 if ( moduleInfoCache
.hasOwnProperty( module
) ) {
227 .resolve( moduleInfoCache
[ module
] )
228 .promise( { abort: function () {} } );
230 apiPromise
= api
.post( {
234 uselang
: mw
.config
.get( 'wgUserLanguage' )
235 } ).done( function ( data
) {
238 if ( data
.warnings
&& data
.warnings
.paraminfo
) {
239 deferred
.reject( '???', data
.warnings
.paraminfo
[ '*' ] );
243 info
= data
.paraminfo
.modules
;
244 if ( !info
|| info
.length
!== 1 || info
[ 0 ].path
!== module
) {
245 deferred
.reject( '???', 'No module data returned' );
249 moduleInfoCache
[ module
] = info
[ 0 ];
250 deferred
.resolve( info
[ 0 ] );
251 } ).fail( function ( code
, details
) {
252 if ( code
=== 'http' ) {
253 details
= 'HTTP error: ' + details
.exception
;
254 } else if ( details
.error
) {
255 details
= details
.error
.info
;
257 deferred
.reject( code
, details
);
260 .promise( { abort
: apiPromise
.abort
} );
265 * Mark all currently-in-use tokens as bad
267 markTokensBad: function () {
268 var page
, subpages
, i
,
269 checkPages
= [ pages
.main
];
271 while ( checkPages
.length
) {
272 page
= checkPages
.shift();
274 if ( page
.tokenWidget
) {
275 api
.badToken( page
.tokenWidget
.paramInfo
.tokentype
);
278 subpages
= page
.getSubpages();
279 for ( i
= 0; i
< subpages
.length
; i
++ ) {
280 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
281 checkPages
.push( pages
[ subpages
[ i
].key
] );
288 * Test an API boolean
290 * @param {Mixed} value
293 apiBool: function ( value
) {
294 return value
!== undefined && value
!== false;
298 * Create a widget for a parameter.
300 * @param {Object} pi Parameter info from API
301 * @param {Object} opts Additional options
302 * @return {OO.ui.Widget}
304 createWidgetForParameter: function ( pi
, opts
) {
305 var widget
, innerWidget
, finalWidget
, items
, $button
, $content
, func
,
312 widget
= new OO
.ui
.ToggleSwitchWidget();
313 widget
.paramInfo
= pi
;
314 $.extend( widget
, WidgetMethods
.toggleSwitchWidget
);
315 pi
.required
= true; // Avoid wrapping in the non-required widget
320 if ( pi
.tokentype
) {
321 widget
= new TextInputWithIndicatorWidget( {
323 indicator
: 'previous',
324 indicatorTitle
: mw
.message( 'apisandbox-fetch-token' ).text(),
325 required
: Util
.apiBool( pi
.required
)
328 } else if ( Util
.apiBool( pi
.multi
) ) {
329 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
332 widget
.paramInfo
= pi
;
333 $.extend( widget
, WidgetMethods
.capsuleWidget
);
335 widget
= new OO
.ui
.TextInputWidget( {
336 required
: Util
.apiBool( pi
.required
)
339 if ( !Util
.apiBool( pi
.multi
) ) {
340 widget
.paramInfo
= pi
;
341 $.extend( widget
, WidgetMethods
.textInputWidget
);
342 widget
.setValidation( Validators
.generic
);
344 if ( pi
.tokentype
) {
345 $.extend( widget
, WidgetMethods
.tokenWidget
);
346 widget
.input
.paramInfo
= pi
;
347 $.extend( widget
.input
, WidgetMethods
.textInputWidget
);
348 $.extend( widget
.input
, WidgetMethods
.tokenWidget
);
349 widget
.on( 'indicator', widget
.fetchToken
, [], widget
);
354 widget
= new OO
.ui
.TextInputWidget( {
356 required
: Util
.apiBool( pi
.required
)
358 widget
.paramInfo
= pi
;
359 $.extend( widget
, WidgetMethods
.textInputWidget
);
360 widget
.setValidation( Validators
.generic
);
364 widget
= new OO
.ui
.TextInputWidget( {
366 required
: Util
.apiBool( pi
.required
)
368 widget
.paramInfo
= pi
;
369 $.extend( widget
, WidgetMethods
.textInputWidget
);
370 $.extend( widget
, WidgetMethods
.passwordWidget
);
371 widget
.setValidation( Validators
.generic
);
376 widget
= new OO
.ui
.NumberInputWidget( {
377 required
: Util
.apiBool( pi
.required
),
380 widget
.setIcon
= widget
.input
.setIcon
.bind( widget
.input
);
381 widget
.setIconTitle
= widget
.input
.setIconTitle
.bind( widget
.input
);
382 widget
.getValidity
= widget
.input
.getValidity
.bind( widget
.input
);
383 widget
.paramInfo
= pi
;
384 $.extend( widget
, WidgetMethods
.textInputWidget
);
385 if ( Util
.apiBool( pi
.enforcerange
) ) {
386 widget
.setRange( pi
.min
|| -Infinity
, pi
.max
|| Infinity
);
392 widget
= new OO
.ui
.NumberInputWidget( {
393 required
: Util
.apiBool( pi
.required
),
396 widget
.setIcon
= widget
.input
.setIcon
.bind( widget
.input
);
397 widget
.setIconTitle
= widget
.input
.setIconTitle
.bind( widget
.input
);
398 widget
.getValidity
= widget
.input
.getValidity
.bind( widget
.input
);
399 widget
.input
.setValidation( function ( value
) {
400 return value
=== 'max' || widget
.validateNumber( value
);
402 widget
.paramInfo
= pi
;
403 $.extend( widget
, WidgetMethods
.textInputWidget
);
404 widget
.setRange( pi
.min
|| 0, mw
.config
.get( 'apihighlimits' ) ? pi
.highmax
: pi
.max
);
409 widget
= new mw
.widgets
.datetime
.DateTimeInputWidget( {
411 format
: '${year|0}-${month|0}-${day|0}T${hour|0}:${minute|0}:${second|0}${zone|short}'
413 required
: Util
.apiBool( pi
.required
),
416 widget
.paramInfo
= pi
;
417 $.extend( widget
, WidgetMethods
.textInputWidget
);
418 $.extend( widget
, WidgetMethods
.dateTimeInputWidget
);
419 multiMode
= 'indicator';
423 widget
= new OO
.ui
.SelectFileWidget();
424 widget
.paramInfo
= pi
;
425 $.extend( widget
, WidgetMethods
.uploadWidget
);
429 items
= $.map( mw
.config
.get( 'wgFormattedNamespaces' ), function ( name
, ns
) {
431 name
= mw
.message( 'blanknamespace' ).text();
433 return new OO
.ui
.MenuOptionWidget( { data
: ns
, label
: name
} );
434 } ).sort( function ( a
, b
) {
435 return a
.data
- b
.data
;
437 if ( Util
.apiBool( pi
.multi
) ) {
438 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
439 menu
: { items
: items
}
441 widget
.paramInfo
= pi
;
442 $.extend( widget
, WidgetMethods
.capsuleWidget
);
444 widget
= new OO
.ui
.DropdownWidget( {
445 menu
: { items
: items
}
447 widget
.paramInfo
= pi
;
448 $.extend( widget
, WidgetMethods
.dropdownWidget
);
453 if ( !$.isArray( pi
.type
) ) {
454 throw new Error( 'Unknown parameter type ' + pi
.type
);
457 items
= $.map( pi
.type
, function ( v
) {
458 return new OO
.ui
.MenuOptionWidget( { data
: String( v
), label
: String( v
) } );
460 if ( Util
.apiBool( pi
.multi
) ) {
461 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
462 menu
: { items
: items
}
464 widget
.paramInfo
= pi
;
465 $.extend( widget
, WidgetMethods
.capsuleWidget
);
466 if ( Util
.apiBool( pi
.submodules
) ) {
467 widget
.getSubmodules
= WidgetMethods
.submoduleWidget
.multi
;
468 widget
.on( 'change', ApiSandbox
.updateUI
);
471 widget
= new OO
.ui
.DropdownWidget( {
472 menu
: { items
: items
}
474 widget
.paramInfo
= pi
;
475 $.extend( widget
, WidgetMethods
.dropdownWidget
);
476 if ( Util
.apiBool( pi
.submodules
) ) {
477 widget
.getSubmodules
= WidgetMethods
.submoduleWidget
.single
;
478 widget
.getMenu().on( 'choose', ApiSandbox
.updateUI
);
485 if ( Util
.apiBool( pi
.multi
) && multiMode
!== 'none' ) {
486 innerWidget
= widget
;
487 switch ( multiMode
) {
489 $content
= innerWidget
.$element
;
493 $button
= innerWidget
.$indicator
;
494 $button
.css( 'cursor', 'pointer' );
495 $button
.attr( 'tabindex', 0 );
496 $button
.parent().append( $button
);
497 innerWidget
.setIndicator( 'next' );
498 $content
= innerWidget
.$element
;
502 throw new Error( 'Unknown multiMode "' + multiMode
+ '"' );
505 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
506 allowArbitrary
: true,
508 classes
: [ 'mw-apisandbox-popup' ],
512 widget
.paramInfo
= pi
;
513 $.extend( widget
, WidgetMethods
.capsuleWidget
);
516 if ( !innerWidget
.isDisabled() ) {
517 innerWidget
.apiCheckValid().done( function ( ok
) {
519 widget
.addItemsFromData( [ innerWidget
.getApiValue() ] );
520 innerWidget
.setApiValue( undefined );
526 switch ( multiMode
) {
528 innerWidget
.connect( null, { enter
: func
} );
534 keypress: function ( e
) {
535 if ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) {
544 if ( Util
.apiBool( pi
.required
) || opts
.nooptional
) {
545 finalWidget
= widget
;
547 finalWidget
= new OptionalWidget( widget
);
548 finalWidget
.paramInfo
= pi
;
549 $.extend( finalWidget
, WidgetMethods
.optionalWidget
);
550 if ( widget
.getSubmodules
) {
551 finalWidget
.getSubmodules
= widget
.getSubmodules
.bind( widget
);
552 finalWidget
.on( 'disable', function () { setTimeout( ApiSandbox
.updateUI
); } );
554 finalWidget
.setDisabled( true );
557 widget
.setApiValue( pi
[ 'default' ] );
563 * Parse an HTML string, adding target="_blank" to any links
565 * @param {string} html HTML to parse
568 parseHTML: function ( html
) {
569 var $ret
= $( $.parseHTML( html
) );
570 $ret
.filter( 'a' ).add( $ret
.find( 'a' ) )
571 .filter( '[href]:not([target])' )
572 .attr( 'target', '_blank' );
578 * Interface to ApiSandbox UI
580 * @class mw.special.ApiSandbox
586 * Automatically called on $.ready()
591 ApiSandbox
.isFullscreen
= false;
593 $content
= $( '#mw-apisandbox' );
595 windowManager
= new OO
.ui
.WindowManager();
596 $( 'body' ).append( windowManager
.$element
);
597 windowManager
.addWindows( {
598 errorAlert
: new OO
.ui
.MessageDialog()
601 fullscreenButton
= new OO
.ui
.ButtonWidget( {
602 label
: mw
.message( 'apisandbox-fullscreen' ).text(),
603 title
: mw
.message( 'apisandbox-fullscreen-tooltip' ).text()
604 } ).on( 'click', ApiSandbox
.toggleFullscreen
);
606 $toolbar
= $( '<div>' )
607 .addClass( 'mw-apisandbox-toolbar' )
609 fullscreenButton
.$element
,
610 new OO
.ui
.ButtonWidget( {
611 label
: mw
.message( 'apisandbox-submit' ).text(),
612 flags
: [ 'primary', 'progressive' ]
613 } ).on( 'click', ApiSandbox
.sendRequest
).$element
,
614 new OO
.ui
.ButtonWidget( {
615 label
: mw
.message( 'apisandbox-reset' ).text(),
617 } ).on( 'click', ApiSandbox
.resetUI
).$element
620 booklet
= new OO
.ui
.BookletLayout( {
625 panel
= new OO
.ui
.PanelLayout( {
626 classes
: [ 'mw-apisandbox-container' ],
627 content
: [ booklet
],
632 pages
.main
= new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } );
634 // Parse the current hash string
635 if ( !ApiSandbox
.loadFromHash() ) {
636 ApiSandbox
.updateUI();
639 // If the hashchange event exists, use it. Otherwise, fake it.
640 // And, of course, IE has to be dumb.
641 if ( 'onhashchange' in window
&&
642 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
644 $( window
).on( 'hashchange', ApiSandbox
.loadFromHash
);
646 setInterval( function () {
647 if ( oldhash
!== location
.hash
) {
648 ApiSandbox
.loadFromHash();
655 .append( $( '<p>' ).append( mw
.message( 'apisandbox-intro' ).parse() ) )
657 $( '<div>', { id
: 'mw-apisandbox-ui' } )
659 .append( panel
.$element
)
662 $( window
).on( 'resize', ApiSandbox
.resizePanel
);
664 ApiSandbox
.resizePanel();
668 * Toggle "fullscreen" mode
670 toggleFullscreen: function () {
671 var $body
= $( document
.body
),
672 $ui
= $( '#mw-apisandbox-ui' );
674 ApiSandbox
.isFullscreen
= !ApiSandbox
.isFullscreen
;
676 $body
.toggleClass( 'mw-apisandbox-fullscreen', ApiSandbox
.isFullscreen
);
677 $ui
.toggleClass( 'mw-body-content', ApiSandbox
.isFullscreen
);
678 if ( ApiSandbox
.isFullscreen
) {
679 fullscreenButton
.setLabel( mw
.message( 'apisandbox-unfullscreen' ).text() );
680 fullscreenButton
.setTitle( mw
.message( 'apisandbox-unfullscreen-tooltip' ).text() );
683 fullscreenButton
.setLabel( mw
.message( 'apisandbox-fullscreen' ).text() );
684 fullscreenButton
.setTitle( mw
.message( 'apisandbox-fullscreen-tooltip' ).text() );
685 $content
.append( $ui
);
687 ApiSandbox
.resizePanel();
691 * Set the height of the panel based on the current viewport.
693 resizePanel: function () {
694 var height
= $( window
).height(),
695 contentTop
= $content
.offset().top
;
697 if ( ApiSandbox
.isFullscreen
) {
698 height
-= panel
.$element
.offset().top
- $( '#mw-apisandbox-ui' ).offset().top
;
699 panel
.$element
.height( height
- 1 );
701 // Subtract the height of the intro text
702 height
-= panel
.$element
.offset().top
- contentTop
;
704 panel
.$element
.height( height
- 10 );
705 $( window
).scrollTop( contentTop
- 5 );
710 * Update the current query when the page hash changes
712 loadFromHash: function () {
714 hash
= location
.hash
;
716 if ( oldhash
=== hash
) {
724 // I'm surprised this doesn't seem to exist in jQuery or mw.util.
726 hash
= hash
.replace( /\+/g, '%20' );
727 re
= /([^&=#]+)=?([^&#]*)/g;
728 while ( ( m
= re
.exec( hash
) ) ) {
729 params
[ decodeURIComponent( m
[ 1 ] ) ] = decodeURIComponent( m
[ 2 ] );
732 ApiSandbox
.updateUI( params
);
737 * Update the pages in the booklet
739 * @param {Object} [params] Optional query parameters to load
741 updateUI: function ( params
) {
742 var i
, page
, subpages
, j
, removePages
,
745 if ( !$.isPlainObject( params
) ) {
749 if ( updatingBooklet
) {
752 updatingBooklet
= true;
754 if ( params
!== undefined ) {
755 pages
.main
.loadQueryParams( params
);
757 addPages
.push( pages
.main
);
758 if ( resultPage
!== null ) {
759 addPages
.push( resultPage
);
761 pages
.main
.apiCheckValid();
764 while ( addPages
.length
) {
765 page
= addPages
.shift();
766 if ( bookletPages
[ i
] !== page
) {
767 for ( j
= i
; j
< bookletPages
.length
; j
++ ) {
768 if ( bookletPages
[ j
].getName() === page
.getName() ) {
769 bookletPages
.splice( j
, 1 );
772 bookletPages
.splice( i
, 0, page
);
773 booklet
.addPages( [ page
], i
);
777 if ( page
.getSubpages
) {
778 subpages
= page
.getSubpages();
779 for ( j
= 0; j
< subpages
.length
; j
++ ) {
780 if ( !pages
.hasOwnProperty( subpages
[ j
].key
) ) {
781 subpages
[ j
].indentLevel
= page
.indentLevel
+ 1;
782 pages
[ subpages
[ j
].key
] = new ApiSandbox
.PageLayout( subpages
[ j
] );
784 if ( params
!== undefined ) {
785 pages
[ subpages
[ j
].key
].loadQueryParams( params
);
787 addPages
.splice( j
, 0, pages
[ subpages
[ j
].key
] );
788 pages
[ subpages
[ j
].key
].apiCheckValid();
793 if ( bookletPages
.length
> i
) {
794 removePages
= bookletPages
.splice( i
, bookletPages
.length
- i
);
795 booklet
.removePages( removePages
);
798 if ( !booklet
.getCurrentPageName() ) {
799 booklet
.selectFirstSelectablePage();
802 updatingBooklet
= false;
807 * Reset button handler
809 resetUI: function () {
810 suppressErrors
= true;
812 main
: new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } )
815 ApiSandbox
.updateUI();
819 * Submit button handler
821 * @param {Object} [params] Use this set of params instead of those in the form fields.
822 * The form fields will be updated to match.
824 sendRequest: function ( params
) {
825 var page
, subpages
, i
, query
, $result
, $focus
,
826 progress
, $progressText
, progressLoading
,
828 paramsAreForced
= !!params
,
830 checkPages
= [ pages
.main
];
832 // Blur any focused widget before submit, because
833 // OO.ui.ButtonWidget doesn't take focus itself (T128054)
834 $focus
= $( '#mw-apisandbox-ui' ).find( document
.activeElement
);
835 if ( $focus
.length
) {
839 suppressErrors
= false;
841 // save widget state in params (or load from it if we are forced)
842 if ( paramsAreForced
) {
843 ApiSandbox
.updateUI( params
);
846 while ( checkPages
.length
) {
847 page
= checkPages
.shift();
848 deferreds
.push( page
.apiCheckValid() );
849 page
.getQueryParams( params
, displayParams
);
850 subpages
= page
.getSubpages();
851 for ( i
= 0; i
< subpages
.length
; i
++ ) {
852 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
853 checkPages
.push( pages
[ subpages
[ i
].key
] );
858 if ( !paramsAreForced
) {
859 // forced params means we are continuing a query; the base query should be preserved
860 baseRequestParams
= $.extend( {}, params
);
863 $.when
.apply( $, deferreds
).done( function () {
864 if ( $.inArray( false, arguments
) !== -1 ) {
865 windowManager
.openWindow( 'errorAlert', {
866 title
: mw
.message( 'apisandbox-submit-invalid-fields-title' ).parse(),
867 message
: mw
.message( 'apisandbox-submit-invalid-fields-message' ).parse(),
871 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
879 query
= $.param( displayParams
);
881 // Force a 'fm' format with wrappedhtml=1, if available
882 if ( params
.format
!== undefined ) {
883 if ( availableFormats
.hasOwnProperty( params
.format
+ 'fm' ) ) {
884 params
.format
= params
.format
+ 'fm';
886 if ( params
.format
.substr( -2 ) === 'fm' ) {
887 params
.wrappedhtml
= 1;
891 progressLoading
= false;
892 $progressText
= $( '<span>' ).text( mw
.message( 'apisandbox-sending-request' ).text() );
893 progress
= new OO
.ui
.ProgressBarWidget( {
895 $content
: $progressText
898 $result
= $( '<div>' )
899 .append( progress
.$element
);
901 resultPage
= page
= new OO
.ui
.PageLayout( '|results|' );
902 page
.setupOutlineItem = function () {
903 this.outlineItem
.setLabel( mw
.message( 'apisandbox-results' ).text() );
905 page
.$element
.empty()
907 new OO
.ui
.FieldLayout(
908 new OO
.ui
.TextInputWidget( {
910 value
: mw
.util
.wikiScript( 'api' ) + '?' + query
912 label
: mw
.message( 'apisandbox-request-url-label' ).parse()
917 ApiSandbox
.updateUI();
918 booklet
.setPage( '|results|' );
920 location
.href
= oldhash
= '#' + query
;
923 contentType
: 'multipart/form-data',
926 var xhr
= new window
.XMLHttpRequest();
927 xhr
.upload
.addEventListener( 'progress', function ( e
) {
928 if ( !progressLoading
) {
929 if ( e
.lengthComputable
) {
930 progress
.setProgress( e
.loaded
* 100 / e
.total
);
932 progress
.setProgress( false );
936 xhr
.addEventListener( 'progress', function ( e
) {
937 if ( !progressLoading
) {
938 progressLoading
= true;
939 $progressText
.text( mw
.message( 'apisandbox-loading-results' ).text() );
941 if ( e
.lengthComputable
) {
942 progress
.setProgress( e
.loaded
* 100 / e
.total
);
944 progress
.setProgress( false );
950 .then( null, function ( code
, data
, result
, jqXHR
) {
951 if ( code
!== 'http' ) {
952 // Not really an error, work around mw.Api thinking it is.
954 .resolve( result
, jqXHR
)
959 .fail( function ( code
, data
) {
960 var details
= 'HTTP error: ' + data
.exception
;
963 new OO
.ui
.LabelWidget( {
964 label
: mw
.message( 'apisandbox-results-error', details
).text(),
969 .done( function ( data
, jqXHR
) {
970 var m
, loadTime
, button
, clear
,
971 ct
= jqXHR
.getResponseHeader( 'Content-Type' );
974 if ( /^text\/mediawiki-api-prettyprint-wrapped(?:;|$)/.test( ct
) ) {
975 data
= $.parseJSON( data
);
976 if ( data
.modules
.length
) {
977 mw
.loader
.load( data
.modules
);
979 $result
.append( Util
.parseHTML( data
.html
) );
980 loadTime
= data
.time
;
981 } else if ( ( m
= data
.match( /<pre[ >][\s\S]*<\/pre>/ ) ) ) {
982 $result
.append( Util
.parseHTML( m
[ 0 ] ) );
983 if ( ( m
= data
.match( /"wgBackendResponseTime":\s*(\d+)/ ) ) ) {
984 loadTime
= parseInt( m
[ 1 ], 10 );
988 .addClass( 'api-pretty-content' )
990 .appendTo( $result
);
992 if ( paramsAreForced
|| data
[ 'continue' ] ) {
995 new OO
.ui
.ButtonWidget( {
996 label
: mw
.message( 'apisandbox-continue' ).text()
997 } ).on( 'click', function () {
998 ApiSandbox
.sendRequest( $.extend( {}, baseRequestParams
, data
[ 'continue' ] ) );
999 } ).setDisabled( !data
[ 'continue' ] ).$element
,
1000 ( clear
= new OO
.ui
.ButtonWidget( {
1001 label
: mw
.message( 'apisandbox-continue-clear' ).text()
1002 } ).on( 'click', function () {
1003 ApiSandbox
.updateUI( baseRequestParams
);
1004 clear
.setDisabled( true );
1005 booklet
.setPage( '|results|' );
1006 } ).setDisabled( !paramsAreForced
) ).$element
,
1007 new OO
.ui
.PopupButtonWidget( {
1011 $content
: $( '<div>' ).append( mw
.message( 'apisandbox-continue-help' ).parse() ),
1018 if ( typeof loadTime
=== 'number' ) {
1020 $( '<div>' ).append(
1021 new OO
.ui
.LabelWidget( {
1022 label
: mw
.message( 'apisandbox-request-time', loadTime
).text()
1028 if ( jqXHR
.getResponseHeader( 'MediaWiki-API-Error' ) === 'badtoken' ) {
1029 // Flush all saved tokens in case one of them is the bad one.
1030 Util
.markTokensBad();
1031 button
= new OO
.ui
.ButtonWidget( {
1032 label
: mw
.message( 'apisandbox-results-fixtoken' ).text()
1034 button
.on( 'click', ApiSandbox
.fixTokenAndResend
)
1035 .on( 'click', button
.setDisabled
, [ true ], button
)
1036 .$element
.appendTo( $result
);
1043 * Handler for the "Correct token and resubmit" button
1045 * Used on a 'badtoken' error, it re-fetches token parameters for all
1046 * pages and then re-submits the query.
1048 fixTokenAndResend: function () {
1049 var page
, subpages
, i
, k
,
1051 tokenWait
= { dummy
: true },
1052 checkPages
= [ pages
.main
],
1053 success = function ( k
) {
1054 delete tokenWait
[ k
];
1055 if ( ok
&& $.isEmptyObject( tokenWait
) ) {
1056 ApiSandbox
.sendRequest();
1059 failure = function ( k
) {
1060 delete tokenWait
[ k
];
1064 while ( checkPages
.length
) {
1065 page
= checkPages
.shift();
1067 if ( page
.tokenWidget
) {
1068 k
= page
.apiModule
+ page
.tokenWidget
.paramInfo
.name
;
1069 tokenWait
[ k
] = page
.tokenWidget
.fetchToken()
1070 .done( success
.bind( page
.tokenWidget
, k
) )
1071 .fail( failure
.bind( page
.tokenWidget
, k
) );
1074 subpages
= page
.getSubpages();
1075 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1076 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1077 checkPages
.push( pages
[ subpages
[ i
].key
] );
1082 success( 'dummy', '' );
1086 * Reset validity indicators for all widgets
1088 updateValidityIndicators: function () {
1089 var page
, subpages
, i
,
1090 checkPages
= [ pages
.main
];
1092 while ( checkPages
.length
) {
1093 page
= checkPages
.shift();
1094 page
.apiCheckValid();
1095 subpages
= page
.getSubpages();
1096 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1097 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1098 checkPages
.push( pages
[ subpages
[ i
].key
] );
1106 * PageLayout for API modules
1110 * @extends OO.ui.PageLayout
1112 * @param {Object} [config] Configuration options
1114 ApiSandbox
.PageLayout = function ( config
) {
1115 config
= $.extend( { prefix
: '' }, config
);
1116 this.displayText
= config
.key
;
1117 this.apiModule
= config
.path
;
1118 this.prefix
= config
.prefix
;
1119 this.paramInfo
= null;
1120 this.apiIsValid
= true;
1121 this.loadFromQueryParams
= null;
1123 this.tokenWidget
= null;
1124 this.indentLevel
= config
.indentLevel
? config
.indentLevel
: 0;
1125 ApiSandbox
.PageLayout
[ 'super' ].call( this, config
.key
, config
);
1126 this.loadParamInfo();
1128 OO
.inheritClass( ApiSandbox
.PageLayout
, OO
.ui
.PageLayout
);
1129 ApiSandbox
.PageLayout
.prototype.setupOutlineItem = function () {
1130 this.outlineItem
.setLevel( this.indentLevel
);
1131 this.outlineItem
.setLabel( this.displayText
);
1132 this.outlineItem
.setIcon( this.apiIsValid
|| suppressErrors
? null : 'alert' );
1133 this.outlineItem
.setIconTitle(
1134 this.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1139 * Fetch module information for this page's module, then create UI
1141 ApiSandbox
.PageLayout
.prototype.loadParamInfo = function () {
1142 var dynamicFieldset
, dynamicParamNameWidget
,
1144 removeDynamicParamWidget = function ( name
, layout
) {
1145 dynamicFieldset
.removeItems( [ layout
] );
1146 delete that
.widgets
[ name
];
1148 addDynamicParamWidget = function () {
1149 var name
, layout
, widget
, button
;
1151 // Check name is filled in
1152 name
= dynamicParamNameWidget
.getValue().trim();
1153 if ( name
=== '' ) {
1154 dynamicParamNameWidget
.focus();
1158 if ( that
.widgets
[ name
] !== undefined ) {
1159 windowManager
.openWindow( 'errorAlert', {
1161 'apisandbox-dynamic-error-exists', name
1166 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
1174 widget
= Util
.createWidgetForParameter( {
1181 button
= new OO
.ui
.ButtonWidget( {
1183 flags
: 'destructive'
1185 layout
= new OO
.ui
.ActionFieldLayout(
1193 button
.on( 'click', removeDynamicParamWidget
, [ name
, layout
] );
1194 that
.widgets
[ name
] = widget
;
1195 dynamicFieldset
.addItems( [ layout
], dynamicFieldset
.getItems().length
- 1 );
1198 dynamicParamNameWidget
.setValue( '' );
1201 this.$element
.empty()
1202 .append( new OO
.ui
.ProgressBarWidget( {
1204 text
: mw
.message( 'apisandbox-loading', this.displayText
).text()
1207 Util
.fetchModuleInfo( this.apiModule
)
1208 .done( function ( pi
) {
1209 var prefix
, i
, j
, dl
, widget
, $widgetLabel
, widgetField
, helpField
, tmp
, flag
, count
,
1211 deprecatedItems
= [],
1213 filterFmModules = function ( v
) {
1214 return v
.substr( -2 ) !== 'fm' ||
1215 !availableFormats
.hasOwnProperty( v
.substr( 0, v
.length
- 2 ) );
1217 widgetLabelOnClick = function () {
1218 var f
= this.getField();
1219 if ( $.isFunction( f
.setDisabled
) ) {
1220 f
.setDisabled( false );
1222 if ( $.isFunction( f
.focus
) ) {
1226 doNothing = function () {};
1228 // This is something of a hack. We always want the 'format' and
1229 // 'action' parameters from the main module to be specified,
1230 // and for 'format' we also want to simplify the dropdown since
1231 // we always send the 'fm' variant.
1232 if ( that
.apiModule
=== 'main' ) {
1233 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1234 if ( pi
.parameters
[ i
].name
=== 'action' ) {
1235 pi
.parameters
[ i
].required
= true;
1236 delete pi
.parameters
[ i
][ 'default' ];
1238 if ( pi
.parameters
[ i
].name
=== 'format' ) {
1239 tmp
= pi
.parameters
[ i
].type
;
1240 for ( j
= 0; j
< tmp
.length
; j
++ ) {
1241 availableFormats
[ tmp
[ j
] ] = true;
1243 pi
.parameters
[ i
].type
= $.grep( tmp
, filterFmModules
);
1244 pi
.parameters
[ i
][ 'default' ] = 'json';
1245 pi
.parameters
[ i
].required
= true;
1250 // Hide the 'wrappedhtml' parameter on format modules
1251 if ( pi
.group
=== 'format' ) {
1252 pi
.parameters
= $.grep( pi
.parameters
, function ( p
) {
1253 return p
.name
!== 'wrappedhtml';
1257 that
.paramInfo
= pi
;
1259 items
.push( new OO
.ui
.FieldLayout(
1260 new OO
.ui
.Widget( {} ).toggle( false ), {
1262 label
: Util
.parseHTML( pi
.description
)
1266 if ( pi
.helpurls
.length
) {
1267 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1268 label
: mw
.message( 'apisandbox-helpurls' ).text(),
1271 $content
: $( '<ul>' ).append( $.map( pi
.helpurls
, function ( link
) {
1272 return $( '<li>' ).append( $( '<a>', {
1282 if ( pi
.examples
.length
) {
1283 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1284 label
: mw
.message( 'apisandbox-examples' ).text(),
1287 $content
: $( '<ul>' ).append( $.map( pi
.examples
, function ( example
) {
1289 href
: '#' + example
.query
,
1290 html
: example
.description
1292 a
.find( 'a' ).contents().unwrap(); // Can't nest links
1293 return $( '<li>' ).append( a
);
1299 if ( buttons
.length
) {
1300 items
.push( new OO
.ui
.FieldLayout(
1301 new OO
.ui
.ButtonGroupWidget( {
1303 } ), { align
: 'top' }
1307 if ( pi
.parameters
.length
) {
1308 prefix
= that
.prefix
+ pi
.prefix
;
1309 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1310 widget
= Util
.createWidgetForParameter( pi
.parameters
[ i
] );
1311 that
.widgets
[ prefix
+ pi
.parameters
[ i
].name
] = widget
;
1312 if ( pi
.parameters
[ i
].tokentype
) {
1313 that
.tokenWidget
= widget
;
1317 dl
.append( $( '<dd>', {
1318 addClass
: 'description',
1319 append
: Util
.parseHTML( pi
.parameters
[ i
].description
)
1321 if ( pi
.parameters
[ i
].info
&& pi
.parameters
[ i
].info
.length
) {
1322 for ( j
= 0; j
< pi
.parameters
[ i
].info
.length
; j
++ ) {
1323 dl
.append( $( '<dd>', {
1325 append
: Util
.parseHTML( pi
.parameters
[ i
].info
[ j
] )
1331 switch ( pi
.parameters
[ i
].type
) {
1334 count
= mw
.config
.get( 'wgFormattedNamespaces' ).length
;
1338 if ( pi
.parameters
[ i
].highmax
!== undefined ) {
1339 dl
.append( $( '<dd>', {
1341 append
: Util
.parseHTML( mw
.message(
1342 'api-help-param-limit2', pi
.parameters
[ i
].max
, pi
.parameters
[ i
].highmax
1346 dl
.append( $( '<dd>', {
1348 append
: Util
.parseHTML( mw
.message(
1349 'api-help-param-limit', pi
.parameters
[ i
].max
1357 if ( pi
.parameters
[ i
].min
!== undefined ) {
1360 if ( pi
.parameters
[ i
].max
!== undefined ) {
1364 dl
.append( $( '<dd>', {
1366 append
: Util
.parseHTML( mw
.message(
1367 'api-help-param-integer-' + tmp
,
1368 Util
.apiBool( pi
.parameters
[ i
].multi
) ? 2 : 1,
1369 pi
.parameters
[ i
].min
, pi
.parameters
[ i
].max
1376 if ( $.isArray( pi
.parameters
[ i
].type
) ) {
1378 count
= pi
.parameters
[ i
].type
.length
;
1382 if ( Util
.apiBool( pi
.parameters
[ i
].multi
) ) {
1384 if ( flag
&& !( widget
instanceof OO
.ui
.CapsuleMultiselectWidget
) &&
1386 widget
instanceof OptionalWidget
&&
1387 widget
.widget
instanceof OO
.ui
.CapsuleMultiselectWidget
1390 tmp
.push( mw
.message( 'api-help-param-multi-separate' ).parse() );
1392 if ( count
> pi
.parameters
[ i
].lowlimit
) {
1394 mw
.message( 'api-help-param-multi-max',
1395 pi
.parameters
[ i
].lowlimit
, pi
.parameters
[ i
].highlimit
1400 dl
.append( $( '<dd>', {
1402 append
: Util
.parseHTML( tmp
.join( ' ' ) )
1406 helpField
= new OO
.ui
.FieldLayout(
1409 classes
: [ 'mw-apisandbox-spacer' ]
1412 classes
: [ 'mw-apisandbox-help-field' ],
1417 $widgetLabel
= $( '<span>' );
1418 widgetField
= new OO
.ui
.FieldLayout(
1422 classes
: [ 'mw-apisandbox-widget-field' ],
1423 label
: prefix
+ pi
.parameters
[ i
].name
,
1424 $label
: $widgetLabel
1428 // FieldLayout only does click for InputElement
1429 // widgets. So supply our own click handler.
1430 $widgetLabel
.on( 'click', widgetLabelOnClick
.bind( widgetField
) );
1432 // Don't grey out the label when the field is disabled,
1433 // it makes it too hard to read and our "disabled"
1434 // isn't really disabled.
1435 widgetField
.onFieldDisable( false );
1436 widgetField
.onFieldDisable
= doNothing
;
1438 if ( Util
.apiBool( pi
.parameters
[ i
].deprecated
) ) {
1439 deprecatedItems
.push( widgetField
, helpField
);
1441 items
.push( widgetField
, helpField
);
1446 if ( !pi
.parameters
.length
&& !Util
.apiBool( pi
.dynamicparameters
) ) {
1447 items
.push( new OO
.ui
.FieldLayout(
1448 new OO
.ui
.Widget( {} ).toggle( false ), {
1450 label
: Util
.parseHTML( mw
.message( 'apisandbox-no-parameters' ).parse() )
1455 that
.$element
.empty();
1457 new OO
.ui
.FieldsetLayout( {
1458 label
: that
.displayText
1459 } ).addItems( items
)
1460 .$element
.appendTo( that
.$element
);
1462 if ( Util
.apiBool( pi
.dynamicparameters
) ) {
1463 dynamicFieldset
= new OO
.ui
.FieldsetLayout();
1464 dynamicParamNameWidget
= new OO
.ui
.TextInputWidget( {
1465 placeholder
: mw
.message( 'apisandbox-dynamic-parameters-add-placeholder' ).text()
1466 } ).on( 'enter', addDynamicParamWidget
);
1467 dynamicFieldset
.addItems( [
1468 new OO
.ui
.FieldLayout(
1469 new OO
.ui
.Widget( {} ).toggle( false ), {
1471 label
: Util
.parseHTML( pi
.dynamicparameters
)
1474 new OO
.ui
.ActionFieldLayout(
1475 dynamicParamNameWidget
,
1476 new OO
.ui
.ButtonWidget( {
1478 flags
: 'progressive'
1479 } ).on( 'click', addDynamicParamWidget
),
1481 label
: mw
.message( 'apisandbox-dynamic-parameters-add-label' ).text(),
1488 $( '<legend>' ).text( mw
.message( 'apisandbox-dynamic-parameters' ).text() ),
1489 dynamicFieldset
.$element
1491 .appendTo( that
.$element
);
1494 if ( deprecatedItems
.length
) {
1495 tmp
= new OO
.ui
.FieldsetLayout().addItems( deprecatedItems
).toggle( false );
1498 $( '<legend>' ).append(
1499 new OO
.ui
.ToggleButtonWidget( {
1500 label
: mw
.message( 'apisandbox-deprecated-parameters' ).text()
1501 } ).on( 'change', tmp
.toggle
, [], tmp
).$element
1505 .appendTo( that
.$element
);
1508 // Load stored params, if any, then update the booklet if we
1509 // have subpages (or else just update our valid-indicator).
1510 tmp
= that
.loadFromQueryParams
;
1511 that
.loadFromQueryParams
= null;
1512 if ( $.isPlainObject( tmp
) ) {
1513 that
.loadQueryParams( tmp
);
1515 if ( that
.getSubpages().length
> 0 ) {
1516 ApiSandbox
.updateUI( tmp
);
1518 that
.apiCheckValid();
1520 } ).fail( function ( code
, detail
) {
1521 that
.$element
.empty()
1523 new OO
.ui
.LabelWidget( {
1524 label
: mw
.message( 'apisandbox-load-error', that
.apiModule
, detail
).text(),
1525 classes
: [ 'error' ]
1527 new OO
.ui
.ButtonWidget( {
1528 label
: mw
.message( 'apisandbox-retry' ).text()
1529 } ).on( 'click', that
.loadParamInfo
, [], that
).$element
1535 * Check that all widgets on the page are in a valid state.
1539 ApiSandbox
.PageLayout
.prototype.apiCheckValid = function () {
1542 if ( this.paramInfo
=== null ) {
1543 return $.Deferred().resolve( false ).promise();
1545 return $.when
.apply( $, $.map( this.widgets
, function ( widget
) {
1546 return widget
.apiCheckValid();
1547 } ) ).then( function () {
1548 that
.apiIsValid
= $.inArray( false, arguments
) === -1;
1549 if ( that
.getOutlineItem() ) {
1550 that
.getOutlineItem().setIcon( that
.apiIsValid
|| suppressErrors
? null : 'alert' );
1551 that
.getOutlineItem().setIconTitle(
1552 that
.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1555 return $.Deferred().resolve( that
.apiIsValid
).promise();
1561 * Load form fields from query parameters
1563 * @param {Object} params
1565 ApiSandbox
.PageLayout
.prototype.loadQueryParams = function ( params
) {
1566 if ( this.paramInfo
=== null ) {
1567 this.loadFromQueryParams
= params
;
1569 $.each( this.widgets
, function ( name
, widget
) {
1570 var v
= params
.hasOwnProperty( name
) ? params
[ name
] : undefined;
1571 widget
.setApiValue( v
);
1577 * Load query params from form fields
1579 * @param {Object} params Write query parameters into this object
1580 * @param {Object} displayParams Write query parameters for display into this object
1582 ApiSandbox
.PageLayout
.prototype.getQueryParams = function ( params
, displayParams
) {
1583 $.each( this.widgets
, function ( name
, widget
) {
1584 var value
= widget
.getApiValue();
1585 if ( value
!== undefined ) {
1586 params
[ name
] = value
;
1587 if ( $.isFunction( widget
.getApiValueForDisplay
) ) {
1588 value
= widget
.getApiValueForDisplay();
1590 displayParams
[ name
] = value
;
1596 * Fetch a list of subpage names loaded by this page
1600 ApiSandbox
.PageLayout
.prototype.getSubpages = function () {
1602 $.each( this.widgets
, function ( name
, widget
) {
1604 if ( $.isFunction( widget
.getSubmodules
) ) {
1605 submodules
= widget
.getSubmodules();
1606 for ( i
= 0; i
< submodules
.length
; i
++ ) {
1608 key
: name
+ '=' + submodules
[ i
].value
,
1609 path
: submodules
[ i
].path
,
1610 prefix
: widget
.paramInfo
.submoduleparamprefix
|| ''
1619 * A text input with a clickable indicator
1624 * @param {Object} [config] Configuration options
1626 function TextInputWithIndicatorWidget( config
) {
1629 config
= config
|| {};
1630 TextInputWithIndicatorWidget
[ 'super' ].call( this, config
);
1632 this.$indicator
= $( '<span>' ).addClass( 'mw-apisandbox-clickable-indicator' );
1633 OO
.ui
.mixin
.TabIndexedElement
.call(
1634 this, $.extend( {}, config
, { $tabIndexed
: this.$indicator
} )
1637 this.input
= new OO
.ui
.TextInputWidget( $.extend( {
1638 $indicator
: this.$indicator
,
1639 disabled
: this.isDisabled()
1640 }, config
.input
) );
1642 // Forward most methods for convenience
1643 for ( k
in this.input
) {
1644 if ( $.isFunction( this.input
[ k
] ) && !this[ k
] ) {
1645 this[ k
] = this.input
[ k
].bind( this.input
);
1649 this.$indicator
.on( {
1650 click
: this.onIndicatorClick
.bind( this ),
1651 keypress
: this.onIndicatorKeyPress
.bind( this )
1654 this.$element
.append( this.input
.$element
);
1656 OO
.inheritClass( TextInputWithIndicatorWidget
, OO
.ui
.Widget
);
1657 OO
.mixinClass( TextInputWithIndicatorWidget
, OO
.ui
.mixin
.TabIndexedElement
);
1658 TextInputWithIndicatorWidget
.prototype.onIndicatorClick = function ( e
) {
1659 if ( !this.isDisabled() && e
.which
=== 1 ) {
1660 this.emit( 'indicator' );
1664 TextInputWithIndicatorWidget
.prototype.onIndicatorKeyPress = function ( e
) {
1665 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
1666 this.emit( 'indicator' );
1670 TextInputWithIndicatorWidget
.prototype.setDisabled = function ( disabled
) {
1671 TextInputWithIndicatorWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1673 this.input
.setDisabled( this.isDisabled() );
1679 * A wrapper for a widget that provides an enable/disable button
1684 * @param {OO.ui.Widget} widget
1685 * @param {Object} [config] Configuration options
1687 function OptionalWidget( widget
, config
) {
1690 config
= config
|| {};
1692 this.widget
= widget
;
1693 this.$overlay
= config
.$overlay
||
1694 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-overlay' );
1695 this.checkbox
= new OO
.ui
.CheckboxInputWidget( config
.checkbox
)
1696 .on( 'change', this.onCheckboxChange
, [], this );
1698 OptionalWidget
[ 'super' ].call( this, config
);
1700 // Forward most methods for convenience
1701 for ( k
in this.widget
) {
1702 if ( $.isFunction( this.widget
[ k
] ) && !this[ k
] ) {
1703 this[ k
] = this.widget
[ k
].bind( this.widget
);
1707 this.$overlay
.on( 'click', this.onOverlayClick
.bind( this ) );
1710 .addClass( 'mw-apisandbox-optionalWidget' )
1713 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-fields' ).append(
1714 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-widget' ).append(
1717 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-checkbox' ).append(
1718 this.checkbox
.$element
1723 this.setDisabled( widget
.isDisabled() );
1725 OO
.inheritClass( OptionalWidget
, OO
.ui
.Widget
);
1726 OptionalWidget
.prototype.onCheckboxChange = function ( checked
) {
1727 this.setDisabled( !checked
);
1729 OptionalWidget
.prototype.onOverlayClick = function () {
1730 this.setDisabled( false );
1731 if ( $.isFunction( this.widget
.focus
) ) {
1732 this.widget
.focus();
1735 OptionalWidget
.prototype.setDisabled = function ( disabled
) {
1736 OptionalWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1737 this.widget
.setDisabled( this.isDisabled() );
1738 this.checkbox
.setSelected( !this.isDisabled() );
1739 this.$overlay
.toggle( this.isDisabled() );
1743 $( ApiSandbox
.init
);
1745 module
.exports
= ApiSandbox
;
1747 }( jQuery
, mediaWiki
, OO
) );