2 ( function ( $, mw
, OO
) {
4 var ApiSandbox
, Util
, WidgetMethods
, Validators
,
5 $content
, panel
, booklet
, oldhash
, windowManager
, fullscreenButton
,
10 suppressErrors
= true,
11 updatingBooklet
= false,
18 getApiValue: function () {
19 return this.getValue();
21 setApiValue: function ( v
) {
22 if ( v
=== undefined ) {
23 v
= this.paramInfo
[ 'default' ];
27 apiCheckValid: function () {
29 return this.getValidity().then( function () {
30 return $.Deferred().resolve( true ).promise();
32 return $.Deferred().resolve( false ).promise();
33 } ).done( function ( ok
) {
34 ok
= ok
|| suppressErrors
;
35 that
.setIcon( ok
? null : 'alert' );
36 that
.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
41 dateTimeInputWidget
: {
42 getValidity: function () {
43 if ( !Util
.apiBool( this.paramInfo
.required
) || this.getApiValue() !== '' ) {
44 return $.Deferred().resolve().promise();
46 return $.Deferred().reject().promise();
52 alertTokenError: function ( code
, error
) {
53 windowManager
.openWindow( 'errorAlert', {
55 'apisandbox-results-fixtoken-fail', this.paramInfo
.tokentype
61 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
67 fetchToken: function () {
69 return api
.getToken( this.paramInfo
.tokentype
)
70 .done( this.setApiValue
.bind( this ) )
71 .fail( this.alertTokenError
.bind( this ) )
72 .always( this.popPending
.bind( this ) );
74 setApiValue: function ( v
) {
75 WidgetMethods
.textInputWidget
.setApiValue
.call( this, v
);
76 if ( v
=== '123ABC' ) {
83 getApiValueForDisplay: function () {
89 getApiValue: function () {
90 return this.getValue() ? 1 : undefined;
92 setApiValue: function ( v
) {
93 this.setValue( Util
.apiBool( v
) );
95 apiCheckValid: function () {
96 return $.Deferred().resolve( true ).promise();
101 getApiValue: function () {
102 var item
= this.getMenu().getSelectedItem();
103 return item
=== null ? undefined : item
.getData();
105 setApiValue: function ( v
) {
106 var menu
= this.getMenu();
108 if ( v
=== undefined ) {
109 v
= this.paramInfo
[ 'default' ];
111 if ( v
=== undefined ) {
114 menu
.selectItemByData( String( v
) );
117 apiCheckValid: function () {
118 var ok
= this.getApiValue() !== undefined || suppressErrors
;
119 this.setIcon( ok
? null : 'alert' );
120 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
121 return $.Deferred().resolve( ok
).promise();
126 getApiValue: function () {
127 var items
= this.getItemsData();
128 if ( items
.join( '' ).indexOf( '|' ) === -1 ) {
129 return items
.join( '|' );
131 return '\x1f' + items
.join( '\x1f' );
134 setApiValue: function ( v
) {
135 if ( v
=== undefined || v
=== '' || v
=== '\x1f' ) {
136 this.setItemsFromData( [] );
139 if ( v
.indexOf( '\x1f' ) !== 0 ) {
140 this.setItemsFromData( v
.split( '|' ) );
142 this.setItemsFromData( v
.substr( 1 ).split( '\x1f' ) );
146 apiCheckValid: function () {
147 var ok
= this.getApiValue() !== undefined || suppressErrors
;
148 this.setIcon( ok
? null : 'alert' );
149 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
150 return $.Deferred().resolve( ok
).promise();
155 getApiValue: function () {
156 return this.isDisabled() ? undefined : this.widget
.getApiValue();
158 setApiValue: function ( v
) {
159 this.setDisabled( v
=== undefined );
160 this.widget
.setApiValue( v
);
162 apiCheckValid: function () {
163 if ( this.isDisabled() ) {
164 return $.Deferred().resolve( true ).promise();
166 return this.widget
.apiCheckValid();
172 single: function () {
173 var v
= this.isDisabled() ? this.paramInfo
[ 'default' ] : this.getApiValue();
174 return v
=== undefined ? [] : [ { value
: v
, path
: this.paramInfo
.submodules
[ v
] } ];
177 var map
= this.paramInfo
.submodules
,
178 v
= this.isDisabled() ? this.paramInfo
[ 'default' ] : this.getApiValue();
179 return v
=== undefined || v
=== '' ? [] : $.map( String( v
).split( '|' ), function ( v
) {
180 return { value
: v
, path
: map
[ v
] };
186 getApiValueForDisplay: function () {
189 getApiValue: function () {
190 return this.getValue();
192 setApiValue: function () {
195 apiCheckValid: function () {
196 var ok
= this.getValue() !== null || suppressErrors
;
197 this.setIcon( ok
? null : 'alert' );
198 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
199 return $.Deferred().resolve( ok
).promise();
205 generic: function () {
206 return !Util
.apiBool( this.paramInfo
.required
) || this.getApiValue() !== '';
211 * @class mw.special.ApiSandbox.Utils
216 * Fetch API module info
218 * @param {string} module Module to fetch data for
219 * @return {jQuery.Promise}
221 fetchModuleInfo: function ( module
) {
223 deferred
= $.Deferred();
225 if ( moduleInfoCache
.hasOwnProperty( module
) ) {
227 .resolve( moduleInfoCache
[ module
] )
228 .promise( { abort: function () {} } );
230 apiPromise
= api
.post( {
234 uselang
: mw
.config
.get( 'wgUserLanguage' )
235 } ).done( function ( data
) {
238 if ( data
.warnings
&& data
.warnings
.paraminfo
) {
239 deferred
.reject( '???', data
.warnings
.paraminfo
[ '*' ] );
243 info
= data
.paraminfo
.modules
;
244 if ( !info
|| info
.length
!== 1 || info
[ 0 ].path
!== module
) {
245 deferred
.reject( '???', 'No module data returned' );
249 moduleInfoCache
[ module
] = info
[ 0 ];
250 deferred
.resolve( info
[ 0 ] );
251 } ).fail( function ( code
, details
) {
252 if ( code
=== 'http' ) {
253 details
= 'HTTP error: ' + details
.exception
;
254 } else if ( details
.error
) {
255 details
= details
.error
.info
;
257 deferred
.reject( code
, details
);
260 .promise( { abort
: apiPromise
.abort
} );
265 * Mark all currently-in-use tokens as bad
267 markTokensBad: function () {
268 var page
, subpages
, i
,
269 checkPages
= [ pages
.main
];
271 while ( checkPages
.length
) {
272 page
= checkPages
.shift();
274 if ( page
.tokenWidget
) {
275 api
.badToken( page
.tokenWidget
.paramInfo
.tokentype
);
278 subpages
= page
.getSubpages();
279 for ( i
= 0; i
< subpages
.length
; i
++ ) {
280 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
281 checkPages
.push( pages
[ subpages
[ i
].key
] );
288 * Test an API boolean
290 * @param {Mixed} value
293 apiBool: function ( value
) {
294 return value
!== undefined && value
!== false;
298 * Create a widget for a parameter.
300 * @param {Object} pi Parameter info from API
301 * @param {Object} opts Additional options
302 * @return {OO.ui.Widget}
304 createWidgetForParameter: function ( pi
, opts
) {
305 var widget
, innerWidget
, finalWidget
, items
, $button
, $content
, func
,
312 widget
= new OO
.ui
.ToggleSwitchWidget();
313 widget
.paramInfo
= pi
;
314 $.extend( widget
, WidgetMethods
.toggleSwitchWidget
);
315 pi
.required
= true; // Avoid wrapping in the non-required widget
320 if ( pi
.tokentype
) {
321 widget
= new TextInputWithIndicatorWidget( {
323 indicator
: 'previous',
324 indicatorTitle
: mw
.message( 'apisandbox-fetch-token' ).text(),
325 required
: Util
.apiBool( pi
.required
)
328 } else if ( Util
.apiBool( pi
.multi
) ) {
329 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
332 widget
.paramInfo
= pi
;
333 $.extend( widget
, WidgetMethods
.capsuleWidget
);
335 widget
= new OO
.ui
.TextInputWidget( {
336 required
: Util
.apiBool( pi
.required
)
339 if ( !Util
.apiBool( pi
.multi
) ) {
340 widget
.paramInfo
= pi
;
341 $.extend( widget
, WidgetMethods
.textInputWidget
);
342 widget
.setValidation( Validators
.generic
);
344 if ( pi
.tokentype
) {
345 $.extend( widget
, WidgetMethods
.tokenWidget
);
346 widget
.input
.paramInfo
= pi
;
347 $.extend( widget
.input
, WidgetMethods
.textInputWidget
);
348 $.extend( widget
.input
, WidgetMethods
.tokenWidget
);
349 widget
.on( 'indicator', widget
.fetchToken
, [], widget
);
354 widget
= new OO
.ui
.TextInputWidget( {
356 required
: Util
.apiBool( pi
.required
)
358 widget
.paramInfo
= pi
;
359 $.extend( widget
, WidgetMethods
.textInputWidget
);
360 widget
.setValidation( Validators
.generic
);
364 widget
= new OO
.ui
.TextInputWidget( {
366 required
: Util
.apiBool( pi
.required
)
368 widget
.paramInfo
= pi
;
369 $.extend( widget
, WidgetMethods
.textInputWidget
);
370 $.extend( widget
, WidgetMethods
.passwordWidget
);
371 widget
.setValidation( Validators
.generic
);
376 widget
= new OO
.ui
.NumberInputWidget( {
377 required
: Util
.apiBool( pi
.required
),
380 widget
.setIcon
= widget
.input
.setIcon
.bind( widget
.input
);
381 widget
.setIconTitle
= widget
.input
.setIconTitle
.bind( widget
.input
);
382 widget
.getValidity
= widget
.input
.getValidity
.bind( widget
.input
);
383 widget
.paramInfo
= pi
;
384 $.extend( widget
, WidgetMethods
.textInputWidget
);
385 if ( Util
.apiBool( pi
.enforcerange
) ) {
386 widget
.setRange( pi
.min
|| -Infinity
, pi
.max
|| Infinity
);
392 widget
= new OO
.ui
.TextInputWidget( {
393 required
: Util
.apiBool( pi
.required
)
395 widget
.setValidation( function ( value
) {
396 var n
, pi
= this.paramInfo
;
398 if ( value
=== 'max' ) {
402 return !isNaN( n
) && isFinite( n
) &&
403 /* jshint bitwise: false */
405 /* jshint bitwise: true */
406 n
>= pi
.min
&& n
<= pi
.apiSandboxMax
;
409 pi
.min
= pi
.min
|| 0;
410 pi
.apiSandboxMax
= mw
.config
.get( 'apihighlimits' ) ? pi
.highmax
: pi
.max
;
411 widget
.paramInfo
= pi
;
412 $.extend( widget
, WidgetMethods
.textInputWidget
);
417 widget
= new mw
.widgets
.datetime
.DateTimeInputWidget( {
419 format
: '${year|0}-${month|0}-${day|0}T${hour|0}:${minute|0}:${second|0}${zone|short}'
421 required
: Util
.apiBool( pi
.required
),
424 widget
.paramInfo
= pi
;
425 $.extend( widget
, WidgetMethods
.textInputWidget
);
426 $.extend( widget
, WidgetMethods
.dateTimeInputWidget
);
427 multiMode
= 'indicator';
431 widget
= new OO
.ui
.SelectFileWidget();
432 widget
.paramInfo
= pi
;
433 $.extend( widget
, WidgetMethods
.uploadWidget
);
437 items
= $.map( mw
.config
.get( 'wgFormattedNamespaces' ), function ( name
, ns
) {
439 name
= mw
.message( 'blanknamespace' ).text();
441 return new OO
.ui
.MenuOptionWidget( { data
: ns
, label
: name
} );
442 } ).sort( function ( a
, b
) {
443 return a
.data
- b
.data
;
445 if ( Util
.apiBool( pi
.multi
) ) {
446 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
447 menu
: { items
: items
}
449 widget
.paramInfo
= pi
;
450 $.extend( widget
, WidgetMethods
.capsuleWidget
);
452 widget
= new OO
.ui
.DropdownWidget( {
453 menu
: { items
: items
}
455 widget
.paramInfo
= pi
;
456 $.extend( widget
, WidgetMethods
.dropdownWidget
);
461 if ( !$.isArray( pi
.type
) ) {
462 throw new Error( 'Unknown parameter type ' + pi
.type
);
465 items
= $.map( pi
.type
, function ( v
) {
466 return new OO
.ui
.MenuOptionWidget( { data
: String( v
), label
: String( v
) } );
468 if ( Util
.apiBool( pi
.multi
) ) {
469 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
470 menu
: { items
: items
}
472 widget
.paramInfo
= pi
;
473 $.extend( widget
, WidgetMethods
.capsuleWidget
);
474 if ( Util
.apiBool( pi
.submodules
) ) {
475 widget
.getSubmodules
= WidgetMethods
.submoduleWidget
.multi
;
476 widget
.on( 'change', ApiSandbox
.updateUI
);
479 widget
= new OO
.ui
.DropdownWidget( {
480 menu
: { items
: items
}
482 widget
.paramInfo
= pi
;
483 $.extend( widget
, WidgetMethods
.dropdownWidget
);
484 if ( Util
.apiBool( pi
.submodules
) ) {
485 widget
.getSubmodules
= WidgetMethods
.submoduleWidget
.single
;
486 widget
.getMenu().on( 'choose', ApiSandbox
.updateUI
);
493 if ( Util
.apiBool( pi
.multi
) && multiMode
!== 'none' ) {
494 innerWidget
= widget
;
495 switch ( multiMode
) {
497 $content
= innerWidget
.$element
;
501 $button
= innerWidget
.$indicator
;
502 $button
.css( 'cursor', 'pointer' );
503 $button
.attr( 'tabindex', 0 );
504 $button
.parent().append( $button
);
505 innerWidget
.setIndicator( 'next' );
506 $content
= innerWidget
.$element
;
510 throw new Error( 'Unknown multiMode "' + multiMode
+ '"' );
513 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
514 allowArbitrary
: true,
516 classes
: [ 'mw-apisandbox-popup' ],
520 widget
.paramInfo
= pi
;
521 $.extend( widget
, WidgetMethods
.capsuleWidget
);
524 if ( !innerWidget
.isDisabled() ) {
525 innerWidget
.apiCheckValid().done( function ( ok
) {
527 widget
.addItemsFromData( [ innerWidget
.getApiValue() ] );
528 innerWidget
.setApiValue( undefined );
534 switch ( multiMode
) {
536 innerWidget
.connect( null, { enter
: func
} );
542 keypress: function ( e
) {
543 if ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) {
552 if ( Util
.apiBool( pi
.required
) || opts
.nooptional
) {
553 finalWidget
= widget
;
555 finalWidget
= new OptionalWidget( widget
);
556 finalWidget
.paramInfo
= pi
;
557 $.extend( finalWidget
, WidgetMethods
.optionalWidget
);
558 if ( widget
.getSubmodules
) {
559 finalWidget
.getSubmodules
= widget
.getSubmodules
.bind( widget
);
560 finalWidget
.on( 'disable', function () { setTimeout( ApiSandbox
.updateUI
); } );
562 finalWidget
.setDisabled( true );
565 widget
.setApiValue( pi
[ 'default' ] );
571 * Parse an HTML string, adding target="_blank" to any links
573 * @param {string} html HTML to parse
576 parseHTML: function ( html
) {
577 var $ret
= $( $.parseHTML( html
) );
578 $ret
.filter( 'a' ).add( $ret
.find( 'a' ) )
579 .filter( '[href]:not([target])' )
580 .attr( 'target', '_blank' );
586 * Interface to ApiSandbox UI
588 * @class mw.special.ApiSandbox
594 * Automatically called on $.ready()
599 ApiSandbox
.isFullscreen
= false;
601 $content
= $( '#mw-apisandbox' );
603 windowManager
= new OO
.ui
.WindowManager();
604 $( 'body' ).append( windowManager
.$element
);
605 windowManager
.addWindows( {
606 errorAlert
: new OO
.ui
.MessageDialog()
609 fullscreenButton
= new OO
.ui
.ButtonWidget( {
610 label
: mw
.message( 'apisandbox-fullscreen' ).text(),
611 title
: mw
.message( 'apisandbox-fullscreen-tooltip' ).text()
612 } ).on( 'click', ApiSandbox
.toggleFullscreen
);
614 $toolbar
= $( '<div>' )
615 .addClass( 'mw-apisandbox-toolbar' )
617 fullscreenButton
.$element
,
618 new OO
.ui
.ButtonWidget( {
619 label
: mw
.message( 'apisandbox-submit' ).text(),
620 flags
: [ 'primary', 'progressive' ]
621 } ).on( 'click', ApiSandbox
.sendRequest
).$element
,
622 new OO
.ui
.ButtonWidget( {
623 label
: mw
.message( 'apisandbox-reset' ).text(),
625 } ).on( 'click', ApiSandbox
.resetUI
).$element
628 booklet
= new OO
.ui
.BookletLayout( {
633 panel
= new OO
.ui
.PanelLayout( {
634 classes
: [ 'mw-apisandbox-container' ],
635 content
: [ booklet
],
640 pages
.main
= new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } );
642 // Parse the current hash string
643 if ( !ApiSandbox
.loadFromHash() ) {
644 ApiSandbox
.updateUI();
647 // If the hashchange event exists, use it. Otherwise, fake it.
648 // And, of course, IE has to be dumb.
649 if ( 'onhashchange' in window
&&
650 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
652 $( window
).on( 'hashchange', ApiSandbox
.loadFromHash
);
654 setInterval( function () {
655 if ( oldhash
!== location
.hash
) {
656 ApiSandbox
.loadFromHash();
663 .append( $( '<p>' ).append( mw
.message( 'apisandbox-intro' ).parse() ) )
665 $( '<div>', { id
: 'mw-apisandbox-ui' } )
667 .append( panel
.$element
)
670 $( window
).on( 'resize', ApiSandbox
.resizePanel
);
672 ApiSandbox
.resizePanel();
676 * Toggle "fullscreen" mode
678 toggleFullscreen: function () {
679 var $body
= $( document
.body
),
680 $ui
= $( '#mw-apisandbox-ui' );
682 ApiSandbox
.isFullscreen
= !ApiSandbox
.isFullscreen
;
684 $body
.toggleClass( 'mw-apisandbox-fullscreen', ApiSandbox
.isFullscreen
);
685 $ui
.toggleClass( 'mw-body-content', ApiSandbox
.isFullscreen
);
686 if ( ApiSandbox
.isFullscreen
) {
687 fullscreenButton
.setLabel( mw
.message( 'apisandbox-unfullscreen' ).text() );
688 fullscreenButton
.setTitle( mw
.message( 'apisandbox-unfullscreen-tooltip' ).text() );
691 fullscreenButton
.setLabel( mw
.message( 'apisandbox-fullscreen' ).text() );
692 fullscreenButton
.setTitle( mw
.message( 'apisandbox-fullscreen-tooltip' ).text() );
693 $content
.append( $ui
);
695 ApiSandbox
.resizePanel();
699 * Set the height of the panel based on the current viewport.
701 resizePanel: function () {
702 var height
= $( window
).height(),
703 contentTop
= $content
.offset().top
;
705 if ( ApiSandbox
.isFullscreen
) {
706 height
-= panel
.$element
.offset().top
- $( '#mw-apisandbox-ui' ).offset().top
;
707 panel
.$element
.height( height
- 1 );
709 // Subtract the height of the intro text
710 height
-= panel
.$element
.offset().top
- contentTop
;
712 panel
.$element
.height( height
- 10 );
713 $( window
).scrollTop( contentTop
- 5 );
718 * Update the current query when the page hash changes
720 loadFromHash: function () {
722 hash
= location
.hash
;
724 if ( oldhash
=== hash
) {
732 // I'm surprised this doesn't seem to exist in jQuery or mw.util.
734 hash
= hash
.replace( /\+/g, '%20' );
735 re
= /([^&=#]+)=?([^&#]*)/g;
736 while ( ( m
= re
.exec( hash
) ) ) {
737 params
[ decodeURIComponent( m
[ 1 ] ) ] = decodeURIComponent( m
[ 2 ] );
740 ApiSandbox
.updateUI( params
);
745 * Update the pages in the booklet
747 * @param {Object} [params] Optional query parameters to load
749 updateUI: function ( params
) {
750 var i
, page
, subpages
, j
, removePages
,
753 if ( !$.isPlainObject( params
) ) {
757 if ( updatingBooklet
) {
760 updatingBooklet
= true;
762 if ( params
!== undefined ) {
763 pages
.main
.loadQueryParams( params
);
765 addPages
.push( pages
.main
);
766 if ( resultPage
!== null ) {
767 addPages
.push( resultPage
);
769 pages
.main
.apiCheckValid();
772 while ( addPages
.length
) {
773 page
= addPages
.shift();
774 if ( bookletPages
[ i
] !== page
) {
775 for ( j
= i
; j
< bookletPages
.length
; j
++ ) {
776 if ( bookletPages
[ j
].getName() === page
.getName() ) {
777 bookletPages
.splice( j
, 1 );
780 bookletPages
.splice( i
, 0, page
);
781 booklet
.addPages( [ page
], i
);
785 if ( page
.getSubpages
) {
786 subpages
= page
.getSubpages();
787 for ( j
= 0; j
< subpages
.length
; j
++ ) {
788 if ( !pages
.hasOwnProperty( subpages
[ j
].key
) ) {
789 subpages
[ j
].indentLevel
= page
.indentLevel
+ 1;
790 pages
[ subpages
[ j
].key
] = new ApiSandbox
.PageLayout( subpages
[ j
] );
792 if ( params
!== undefined ) {
793 pages
[ subpages
[ j
].key
].loadQueryParams( params
);
795 addPages
.splice( j
, 0, pages
[ subpages
[ j
].key
] );
796 pages
[ subpages
[ j
].key
].apiCheckValid();
801 if ( bookletPages
.length
> i
) {
802 removePages
= bookletPages
.splice( i
, bookletPages
.length
- i
);
803 booklet
.removePages( removePages
);
806 if ( !booklet
.getCurrentPageName() ) {
807 booklet
.selectFirstSelectablePage();
810 updatingBooklet
= false;
815 * Reset button handler
817 resetUI: function () {
818 suppressErrors
= true;
820 main
: new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } )
823 ApiSandbox
.updateUI();
827 * Submit button handler
829 * @param {Object} [params] Use this set of params instead of those in the form fields.
830 * The form fields will be updated to match.
832 sendRequest: function ( params
) {
833 var page
, subpages
, i
, query
, $result
, $focus
,
834 progress
, $progressText
, progressLoading
,
836 paramsAreForced
= !!params
,
838 checkPages
= [ pages
.main
];
840 // Blur any focused widget before submit, because
841 // OO.ui.ButtonWidget doesn't take focus itself (T128054)
842 $focus
= $( '#mw-apisandbox-ui' ).find( document
.activeElement
);
843 if ( $focus
.length
) {
847 suppressErrors
= false;
849 // save widget state in params (or load from it if we are forced)
850 if ( paramsAreForced
) {
851 ApiSandbox
.updateUI( params
);
854 while ( checkPages
.length
) {
855 page
= checkPages
.shift();
856 deferreds
.push( page
.apiCheckValid() );
857 page
.getQueryParams( params
, displayParams
);
858 subpages
= page
.getSubpages();
859 for ( i
= 0; i
< subpages
.length
; i
++ ) {
860 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
861 checkPages
.push( pages
[ subpages
[ i
].key
] );
866 if ( !paramsAreForced
) {
867 // forced params means we are continuing a query; the base query should be preserved
868 baseRequestParams
= $.extend( {}, params
);
871 $.when
.apply( $, deferreds
).done( function () {
872 if ( $.inArray( false, arguments
) !== -1 ) {
873 windowManager
.openWindow( 'errorAlert', {
874 title
: mw
.message( 'apisandbox-submit-invalid-fields-title' ).parse(),
875 message
: mw
.message( 'apisandbox-submit-invalid-fields-message' ).parse(),
879 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
887 query
= $.param( displayParams
);
889 // Force a 'fm' format with wrappedhtml=1, if available
890 if ( params
.format
!== undefined ) {
891 if ( availableFormats
.hasOwnProperty( params
.format
+ 'fm' ) ) {
892 params
.format
= params
.format
+ 'fm';
894 if ( params
.format
.substr( -2 ) === 'fm' ) {
895 params
.wrappedhtml
= 1;
899 progressLoading
= false;
900 $progressText
= $( '<span>' ).text( mw
.message( 'apisandbox-sending-request' ).text() );
901 progress
= new OO
.ui
.ProgressBarWidget( {
903 $content
: $progressText
906 $result
= $( '<div>' )
907 .append( progress
.$element
);
909 resultPage
= page
= new OO
.ui
.PageLayout( '|results|' );
910 page
.setupOutlineItem = function () {
911 this.outlineItem
.setLabel( mw
.message( 'apisandbox-results' ).text() );
913 page
.$element
.empty()
915 new OO
.ui
.FieldLayout(
916 new OO
.ui
.TextInputWidget( {
918 value
: mw
.util
.wikiScript( 'api' ) + '?' + query
920 label
: mw
.message( 'apisandbox-request-url-label' ).parse()
925 ApiSandbox
.updateUI();
926 booklet
.setPage( '|results|' );
928 location
.href
= oldhash
= '#' + query
;
931 contentType
: 'multipart/form-data',
934 var xhr
= new window
.XMLHttpRequest();
935 xhr
.upload
.addEventListener( 'progress', function ( e
) {
936 if ( !progressLoading
) {
937 if ( e
.lengthComputable
) {
938 progress
.setProgress( e
.loaded
* 100 / e
.total
);
940 progress
.setProgress( false );
944 xhr
.addEventListener( 'progress', function ( e
) {
945 if ( !progressLoading
) {
946 progressLoading
= true;
947 $progressText
.text( mw
.message( 'apisandbox-loading-results' ).text() );
949 if ( e
.lengthComputable
) {
950 progress
.setProgress( e
.loaded
* 100 / e
.total
);
952 progress
.setProgress( false );
958 .then( null, function ( code
, data
, result
, jqXHR
) {
959 if ( code
!== 'http' ) {
960 // Not really an error, work around mw.Api thinking it is.
962 .resolve( result
, jqXHR
)
967 .fail( function ( code
, data
) {
968 var details
= 'HTTP error: ' + data
.exception
;
971 new OO
.ui
.LabelWidget( {
972 label
: mw
.message( 'apisandbox-results-error', details
).text(),
977 .done( function ( data
, jqXHR
) {
978 var m
, loadTime
, button
, clear
,
979 ct
= jqXHR
.getResponseHeader( 'Content-Type' );
982 if ( /^text\/mediawiki-api-prettyprint-wrapped(?:;|$)/.test( ct
) ) {
983 data
= $.parseJSON( data
);
984 if ( data
.modules
.length
) {
985 mw
.loader
.load( data
.modules
);
987 if ( data
.status
&& data
.status
!== 200 ) {
989 .addClass( 'api-pretty-header api-pretty-status' )
991 mw
.message( 'api-format-prettyprint-status', data
.status
, data
.statustext
).parse()
993 .appendTo( $result
);
995 $result
.append( Util
.parseHTML( data
.html
) );
996 loadTime
= data
.time
;
997 } else if ( ( m
= data
.match( /<pre[ >][\s\S]*<\/pre>/ ) ) ) {
998 $result
.append( Util
.parseHTML( m
[ 0 ] ) );
999 if ( ( m
= data
.match( /"wgBackendResponseTime":\s*(\d+)/ ) ) ) {
1000 loadTime
= parseInt( m
[ 1 ], 10 );
1004 .addClass( 'api-pretty-content' )
1006 .appendTo( $result
);
1008 if ( paramsAreForced
|| data
[ 'continue' ] ) {
1010 $( '<div>' ).append(
1011 new OO
.ui
.ButtonWidget( {
1012 label
: mw
.message( 'apisandbox-continue' ).text()
1013 } ).on( 'click', function () {
1014 ApiSandbox
.sendRequest( $.extend( {}, baseRequestParams
, data
[ 'continue' ] ) );
1015 } ).setDisabled( !data
[ 'continue' ] ).$element
,
1016 ( clear
= new OO
.ui
.ButtonWidget( {
1017 label
: mw
.message( 'apisandbox-continue-clear' ).text()
1018 } ).on( 'click', function () {
1019 ApiSandbox
.updateUI( baseRequestParams
);
1020 clear
.setDisabled( true );
1021 booklet
.setPage( '|results|' );
1022 } ).setDisabled( !paramsAreForced
) ).$element
,
1023 new OO
.ui
.PopupButtonWidget( {
1027 $content
: $( '<div>' ).append( mw
.message( 'apisandbox-continue-help' ).parse() ),
1034 if ( typeof loadTime
=== 'number' ) {
1036 $( '<div>' ).append(
1037 new OO
.ui
.LabelWidget( {
1038 label
: mw
.message( 'apisandbox-request-time', loadTime
).text()
1044 if ( jqXHR
.getResponseHeader( 'MediaWiki-API-Error' ) === 'badtoken' ) {
1045 // Flush all saved tokens in case one of them is the bad one.
1046 Util
.markTokensBad();
1047 button
= new OO
.ui
.ButtonWidget( {
1048 label
: mw
.message( 'apisandbox-results-fixtoken' ).text()
1050 button
.on( 'click', ApiSandbox
.fixTokenAndResend
)
1051 .on( 'click', button
.setDisabled
, [ true ], button
)
1052 .$element
.appendTo( $result
);
1059 * Handler for the "Correct token and resubmit" button
1061 * Used on a 'badtoken' error, it re-fetches token parameters for all
1062 * pages and then re-submits the query.
1064 fixTokenAndResend: function () {
1065 var page
, subpages
, i
, k
,
1067 tokenWait
= { dummy
: true },
1068 checkPages
= [ pages
.main
],
1069 success = function ( k
) {
1070 delete tokenWait
[ k
];
1071 if ( ok
&& $.isEmptyObject( tokenWait
) ) {
1072 ApiSandbox
.sendRequest();
1075 failure = function ( k
) {
1076 delete tokenWait
[ k
];
1080 while ( checkPages
.length
) {
1081 page
= checkPages
.shift();
1083 if ( page
.tokenWidget
) {
1084 k
= page
.apiModule
+ page
.tokenWidget
.paramInfo
.name
;
1085 tokenWait
[ k
] = page
.tokenWidget
.fetchToken()
1086 .done( success
.bind( page
.tokenWidget
, k
) )
1087 .fail( failure
.bind( page
.tokenWidget
, k
) );
1090 subpages
= page
.getSubpages();
1091 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1092 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1093 checkPages
.push( pages
[ subpages
[ i
].key
] );
1098 success( 'dummy', '' );
1102 * Reset validity indicators for all widgets
1104 updateValidityIndicators: function () {
1105 var page
, subpages
, i
,
1106 checkPages
= [ pages
.main
];
1108 while ( checkPages
.length
) {
1109 page
= checkPages
.shift();
1110 page
.apiCheckValid();
1111 subpages
= page
.getSubpages();
1112 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1113 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1114 checkPages
.push( pages
[ subpages
[ i
].key
] );
1122 * PageLayout for API modules
1126 * @extends OO.ui.PageLayout
1128 * @param {Object} [config] Configuration options
1130 ApiSandbox
.PageLayout = function ( config
) {
1131 config
= $.extend( { prefix
: '' }, config
);
1132 this.displayText
= config
.key
;
1133 this.apiModule
= config
.path
;
1134 this.prefix
= config
.prefix
;
1135 this.paramInfo
= null;
1136 this.apiIsValid
= true;
1137 this.loadFromQueryParams
= null;
1139 this.tokenWidget
= null;
1140 this.indentLevel
= config
.indentLevel
? config
.indentLevel
: 0;
1141 ApiSandbox
.PageLayout
[ 'super' ].call( this, config
.key
, config
);
1142 this.loadParamInfo();
1144 OO
.inheritClass( ApiSandbox
.PageLayout
, OO
.ui
.PageLayout
);
1145 ApiSandbox
.PageLayout
.prototype.setupOutlineItem = function () {
1146 this.outlineItem
.setLevel( this.indentLevel
);
1147 this.outlineItem
.setLabel( this.displayText
);
1148 this.outlineItem
.setIcon( this.apiIsValid
|| suppressErrors
? null : 'alert' );
1149 this.outlineItem
.setIconTitle(
1150 this.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1155 * Fetch module information for this page's module, then create UI
1157 ApiSandbox
.PageLayout
.prototype.loadParamInfo = function () {
1158 var dynamicFieldset
, dynamicParamNameWidget
,
1160 removeDynamicParamWidget = function ( name
, layout
) {
1161 dynamicFieldset
.removeItems( [ layout
] );
1162 delete that
.widgets
[ name
];
1164 addDynamicParamWidget = function () {
1165 var name
, layout
, widget
, button
;
1167 // Check name is filled in
1168 name
= dynamicParamNameWidget
.getValue().trim();
1169 if ( name
=== '' ) {
1170 dynamicParamNameWidget
.focus();
1174 if ( that
.widgets
[ name
] !== undefined ) {
1175 windowManager
.openWindow( 'errorAlert', {
1177 'apisandbox-dynamic-error-exists', name
1182 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
1190 widget
= Util
.createWidgetForParameter( {
1197 button
= new OO
.ui
.ButtonWidget( {
1199 flags
: 'destructive'
1201 layout
= new OO
.ui
.ActionFieldLayout(
1209 button
.on( 'click', removeDynamicParamWidget
, [ name
, layout
] );
1210 that
.widgets
[ name
] = widget
;
1211 dynamicFieldset
.addItems( [ layout
], dynamicFieldset
.getItems().length
- 1 );
1214 dynamicParamNameWidget
.setValue( '' );
1217 this.$element
.empty()
1218 .append( new OO
.ui
.ProgressBarWidget( {
1220 text
: mw
.message( 'apisandbox-loading', this.displayText
).text()
1223 Util
.fetchModuleInfo( this.apiModule
)
1224 .done( function ( pi
) {
1225 var prefix
, i
, j
, dl
, widget
, $widgetLabel
, widgetField
, helpField
, tmp
, flag
, count
,
1227 deprecatedItems
= [],
1229 filterFmModules = function ( v
) {
1230 return v
.substr( -2 ) !== 'fm' ||
1231 !availableFormats
.hasOwnProperty( v
.substr( 0, v
.length
- 2 ) );
1233 widgetLabelOnClick = function () {
1234 var f
= this.getField();
1235 if ( $.isFunction( f
.setDisabled
) ) {
1236 f
.setDisabled( false );
1238 if ( $.isFunction( f
.focus
) ) {
1242 doNothing = function () {};
1244 // This is something of a hack. We always want the 'format' and
1245 // 'action' parameters from the main module to be specified,
1246 // and for 'format' we also want to simplify the dropdown since
1247 // we always send the 'fm' variant.
1248 if ( that
.apiModule
=== 'main' ) {
1249 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1250 if ( pi
.parameters
[ i
].name
=== 'action' ) {
1251 pi
.parameters
[ i
].required
= true;
1252 delete pi
.parameters
[ i
][ 'default' ];
1254 if ( pi
.parameters
[ i
].name
=== 'format' ) {
1255 tmp
= pi
.parameters
[ i
].type
;
1256 for ( j
= 0; j
< tmp
.length
; j
++ ) {
1257 availableFormats
[ tmp
[ j
] ] = true;
1259 pi
.parameters
[ i
].type
= $.grep( tmp
, filterFmModules
);
1260 pi
.parameters
[ i
][ 'default' ] = 'json';
1261 pi
.parameters
[ i
].required
= true;
1266 // Hide the 'wrappedhtml' parameter on format modules
1267 if ( pi
.group
=== 'format' ) {
1268 pi
.parameters
= $.grep( pi
.parameters
, function ( p
) {
1269 return p
.name
!== 'wrappedhtml';
1273 that
.paramInfo
= pi
;
1275 items
.push( new OO
.ui
.FieldLayout(
1276 new OO
.ui
.Widget( {} ).toggle( false ), {
1278 label
: Util
.parseHTML( pi
.description
)
1282 if ( pi
.helpurls
.length
) {
1283 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1284 label
: mw
.message( 'apisandbox-helpurls' ).text(),
1287 $content
: $( '<ul>' ).append( $.map( pi
.helpurls
, function ( link
) {
1288 return $( '<li>' ).append( $( '<a>', {
1298 if ( pi
.examples
.length
) {
1299 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1300 label
: mw
.message( 'apisandbox-examples' ).text(),
1303 $content
: $( '<ul>' ).append( $.map( pi
.examples
, function ( example
) {
1305 href
: '#' + example
.query
,
1306 html
: example
.description
1308 a
.find( 'a' ).contents().unwrap(); // Can't nest links
1309 return $( '<li>' ).append( a
);
1315 if ( buttons
.length
) {
1316 items
.push( new OO
.ui
.FieldLayout(
1317 new OO
.ui
.ButtonGroupWidget( {
1319 } ), { align
: 'top' }
1323 if ( pi
.parameters
.length
) {
1324 prefix
= that
.prefix
+ pi
.prefix
;
1325 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1326 widget
= Util
.createWidgetForParameter( pi
.parameters
[ i
] );
1327 that
.widgets
[ prefix
+ pi
.parameters
[ i
].name
] = widget
;
1328 if ( pi
.parameters
[ i
].tokentype
) {
1329 that
.tokenWidget
= widget
;
1333 dl
.append( $( '<dd>', {
1334 addClass
: 'description',
1335 append
: Util
.parseHTML( pi
.parameters
[ i
].description
)
1337 if ( pi
.parameters
[ i
].info
&& pi
.parameters
[ i
].info
.length
) {
1338 for ( j
= 0; j
< pi
.parameters
[ i
].info
.length
; j
++ ) {
1339 dl
.append( $( '<dd>', {
1341 append
: Util
.parseHTML( pi
.parameters
[ i
].info
[ j
] )
1347 switch ( pi
.parameters
[ i
].type
) {
1350 count
= mw
.config
.get( 'wgFormattedNamespaces' ).length
;
1354 if ( pi
.parameters
[ i
].highmax
!== undefined ) {
1355 dl
.append( $( '<dd>', {
1358 Util
.parseHTML( mw
.message(
1359 'api-help-param-limit2', pi
.parameters
[ i
].max
, pi
.parameters
[ i
].highmax
1362 Util
.parseHTML( mw
.message( 'apisandbox-param-limit' ).parse() )
1366 dl
.append( $( '<dd>', {
1369 Util
.parseHTML( mw
.message(
1370 'api-help-param-limit', pi
.parameters
[ i
].max
1373 Util
.parseHTML( mw
.message( 'apisandbox-param-limit' ).parse() )
1381 if ( pi
.parameters
[ i
].min
!== undefined ) {
1384 if ( pi
.parameters
[ i
].max
!== undefined ) {
1388 dl
.append( $( '<dd>', {
1390 append
: Util
.parseHTML( mw
.message(
1391 'api-help-param-integer-' + tmp
,
1392 Util
.apiBool( pi
.parameters
[ i
].multi
) ? 2 : 1,
1393 pi
.parameters
[ i
].min
, pi
.parameters
[ i
].max
1400 if ( $.isArray( pi
.parameters
[ i
].type
) ) {
1402 count
= pi
.parameters
[ i
].type
.length
;
1406 if ( Util
.apiBool( pi
.parameters
[ i
].multi
) ) {
1408 if ( flag
&& !( widget
instanceof OO
.ui
.CapsuleMultiselectWidget
) &&
1410 widget
instanceof OptionalWidget
&&
1411 widget
.widget
instanceof OO
.ui
.CapsuleMultiselectWidget
1414 tmp
.push( mw
.message( 'api-help-param-multi-separate' ).parse() );
1416 if ( count
> pi
.parameters
[ i
].lowlimit
) {
1418 mw
.message( 'api-help-param-multi-max',
1419 pi
.parameters
[ i
].lowlimit
, pi
.parameters
[ i
].highlimit
1424 dl
.append( $( '<dd>', {
1426 append
: Util
.parseHTML( tmp
.join( ' ' ) )
1430 helpField
= new OO
.ui
.FieldLayout(
1433 classes
: [ 'mw-apisandbox-spacer' ]
1436 classes
: [ 'mw-apisandbox-help-field' ],
1441 $widgetLabel
= $( '<span>' );
1442 widgetField
= new OO
.ui
.FieldLayout(
1446 classes
: [ 'mw-apisandbox-widget-field' ],
1447 label
: prefix
+ pi
.parameters
[ i
].name
,
1448 $label
: $widgetLabel
1452 // FieldLayout only does click for InputElement
1453 // widgets. So supply our own click handler.
1454 $widgetLabel
.on( 'click', widgetLabelOnClick
.bind( widgetField
) );
1456 // Don't grey out the label when the field is disabled,
1457 // it makes it too hard to read and our "disabled"
1458 // isn't really disabled.
1459 widgetField
.onFieldDisable( false );
1460 widgetField
.onFieldDisable
= doNothing
;
1462 if ( Util
.apiBool( pi
.parameters
[ i
].deprecated
) ) {
1463 deprecatedItems
.push( widgetField
, helpField
);
1465 items
.push( widgetField
, helpField
);
1470 if ( !pi
.parameters
.length
&& !Util
.apiBool( pi
.dynamicparameters
) ) {
1471 items
.push( new OO
.ui
.FieldLayout(
1472 new OO
.ui
.Widget( {} ).toggle( false ), {
1474 label
: Util
.parseHTML( mw
.message( 'apisandbox-no-parameters' ).parse() )
1479 that
.$element
.empty();
1481 new OO
.ui
.FieldsetLayout( {
1482 label
: that
.displayText
1483 } ).addItems( items
)
1484 .$element
.appendTo( that
.$element
);
1486 if ( Util
.apiBool( pi
.dynamicparameters
) ) {
1487 dynamicFieldset
= new OO
.ui
.FieldsetLayout();
1488 dynamicParamNameWidget
= new OO
.ui
.TextInputWidget( {
1489 placeholder
: mw
.message( 'apisandbox-dynamic-parameters-add-placeholder' ).text()
1490 } ).on( 'enter', addDynamicParamWidget
);
1491 dynamicFieldset
.addItems( [
1492 new OO
.ui
.FieldLayout(
1493 new OO
.ui
.Widget( {} ).toggle( false ), {
1495 label
: Util
.parseHTML( pi
.dynamicparameters
)
1498 new OO
.ui
.ActionFieldLayout(
1499 dynamicParamNameWidget
,
1500 new OO
.ui
.ButtonWidget( {
1502 flags
: 'progressive'
1503 } ).on( 'click', addDynamicParamWidget
),
1505 label
: mw
.message( 'apisandbox-dynamic-parameters-add-label' ).text(),
1512 $( '<legend>' ).text( mw
.message( 'apisandbox-dynamic-parameters' ).text() ),
1513 dynamicFieldset
.$element
1515 .appendTo( that
.$element
);
1518 if ( deprecatedItems
.length
) {
1519 tmp
= new OO
.ui
.FieldsetLayout().addItems( deprecatedItems
).toggle( false );
1522 $( '<legend>' ).append(
1523 new OO
.ui
.ToggleButtonWidget( {
1524 label
: mw
.message( 'apisandbox-deprecated-parameters' ).text()
1525 } ).on( 'change', tmp
.toggle
, [], tmp
).$element
1529 .appendTo( that
.$element
);
1532 // Load stored params, if any, then update the booklet if we
1533 // have subpages (or else just update our valid-indicator).
1534 tmp
= that
.loadFromQueryParams
;
1535 that
.loadFromQueryParams
= null;
1536 if ( $.isPlainObject( tmp
) ) {
1537 that
.loadQueryParams( tmp
);
1539 if ( that
.getSubpages().length
> 0 ) {
1540 ApiSandbox
.updateUI( tmp
);
1542 that
.apiCheckValid();
1544 } ).fail( function ( code
, detail
) {
1545 that
.$element
.empty()
1547 new OO
.ui
.LabelWidget( {
1548 label
: mw
.message( 'apisandbox-load-error', that
.apiModule
, detail
).text(),
1549 classes
: [ 'error' ]
1551 new OO
.ui
.ButtonWidget( {
1552 label
: mw
.message( 'apisandbox-retry' ).text()
1553 } ).on( 'click', that
.loadParamInfo
, [], that
).$element
1559 * Check that all widgets on the page are in a valid state.
1563 ApiSandbox
.PageLayout
.prototype.apiCheckValid = function () {
1566 if ( this.paramInfo
=== null ) {
1567 return $.Deferred().resolve( false ).promise();
1569 return $.when
.apply( $, $.map( this.widgets
, function ( widget
) {
1570 return widget
.apiCheckValid();
1571 } ) ).then( function () {
1572 that
.apiIsValid
= $.inArray( false, arguments
) === -1;
1573 if ( that
.getOutlineItem() ) {
1574 that
.getOutlineItem().setIcon( that
.apiIsValid
|| suppressErrors
? null : 'alert' );
1575 that
.getOutlineItem().setIconTitle(
1576 that
.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1579 return $.Deferred().resolve( that
.apiIsValid
).promise();
1585 * Load form fields from query parameters
1587 * @param {Object} params
1589 ApiSandbox
.PageLayout
.prototype.loadQueryParams = function ( params
) {
1590 if ( this.paramInfo
=== null ) {
1591 this.loadFromQueryParams
= params
;
1593 $.each( this.widgets
, function ( name
, widget
) {
1594 var v
= params
.hasOwnProperty( name
) ? params
[ name
] : undefined;
1595 widget
.setApiValue( v
);
1601 * Load query params from form fields
1603 * @param {Object} params Write query parameters into this object
1604 * @param {Object} displayParams Write query parameters for display into this object
1606 ApiSandbox
.PageLayout
.prototype.getQueryParams = function ( params
, displayParams
) {
1607 $.each( this.widgets
, function ( name
, widget
) {
1608 var value
= widget
.getApiValue();
1609 if ( value
!== undefined ) {
1610 params
[ name
] = value
;
1611 if ( $.isFunction( widget
.getApiValueForDisplay
) ) {
1612 value
= widget
.getApiValueForDisplay();
1614 displayParams
[ name
] = value
;
1620 * Fetch a list of subpage names loaded by this page
1624 ApiSandbox
.PageLayout
.prototype.getSubpages = function () {
1626 $.each( this.widgets
, function ( name
, widget
) {
1628 if ( $.isFunction( widget
.getSubmodules
) ) {
1629 submodules
= widget
.getSubmodules();
1630 for ( i
= 0; i
< submodules
.length
; i
++ ) {
1632 key
: name
+ '=' + submodules
[ i
].value
,
1633 path
: submodules
[ i
].path
,
1634 prefix
: widget
.paramInfo
.submoduleparamprefix
|| ''
1643 * A text input with a clickable indicator
1648 * @param {Object} [config] Configuration options
1650 function TextInputWithIndicatorWidget( config
) {
1653 config
= config
|| {};
1654 TextInputWithIndicatorWidget
[ 'super' ].call( this, config
);
1656 this.$indicator
= $( '<span>' ).addClass( 'mw-apisandbox-clickable-indicator' );
1657 OO
.ui
.mixin
.TabIndexedElement
.call(
1658 this, $.extend( {}, config
, { $tabIndexed
: this.$indicator
} )
1661 this.input
= new OO
.ui
.TextInputWidget( $.extend( {
1662 $indicator
: this.$indicator
,
1663 disabled
: this.isDisabled()
1664 }, config
.input
) );
1666 // Forward most methods for convenience
1667 for ( k
in this.input
) {
1668 if ( $.isFunction( this.input
[ k
] ) && !this[ k
] ) {
1669 this[ k
] = this.input
[ k
].bind( this.input
);
1673 this.$indicator
.on( {
1674 click
: this.onIndicatorClick
.bind( this ),
1675 keypress
: this.onIndicatorKeyPress
.bind( this )
1678 this.$element
.append( this.input
.$element
);
1680 OO
.inheritClass( TextInputWithIndicatorWidget
, OO
.ui
.Widget
);
1681 OO
.mixinClass( TextInputWithIndicatorWidget
, OO
.ui
.mixin
.TabIndexedElement
);
1682 TextInputWithIndicatorWidget
.prototype.onIndicatorClick = function ( e
) {
1683 if ( !this.isDisabled() && e
.which
=== 1 ) {
1684 this.emit( 'indicator' );
1688 TextInputWithIndicatorWidget
.prototype.onIndicatorKeyPress = function ( e
) {
1689 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
1690 this.emit( 'indicator' );
1694 TextInputWithIndicatorWidget
.prototype.setDisabled = function ( disabled
) {
1695 TextInputWithIndicatorWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1697 this.input
.setDisabled( this.isDisabled() );
1703 * A wrapper for a widget that provides an enable/disable button
1708 * @param {OO.ui.Widget} widget
1709 * @param {Object} [config] Configuration options
1711 function OptionalWidget( widget
, config
) {
1714 config
= config
|| {};
1716 this.widget
= widget
;
1717 this.$overlay
= config
.$overlay
||
1718 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-overlay' );
1719 this.checkbox
= new OO
.ui
.CheckboxInputWidget( config
.checkbox
)
1720 .on( 'change', this.onCheckboxChange
, [], this );
1722 OptionalWidget
[ 'super' ].call( this, config
);
1724 // Forward most methods for convenience
1725 for ( k
in this.widget
) {
1726 if ( $.isFunction( this.widget
[ k
] ) && !this[ k
] ) {
1727 this[ k
] = this.widget
[ k
].bind( this.widget
);
1731 this.$overlay
.on( 'click', this.onOverlayClick
.bind( this ) );
1734 .addClass( 'mw-apisandbox-optionalWidget' )
1737 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-fields' ).append(
1738 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-widget' ).append(
1741 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-checkbox' ).append(
1742 this.checkbox
.$element
1747 this.setDisabled( widget
.isDisabled() );
1749 OO
.inheritClass( OptionalWidget
, OO
.ui
.Widget
);
1750 OptionalWidget
.prototype.onCheckboxChange = function ( checked
) {
1751 this.setDisabled( !checked
);
1753 OptionalWidget
.prototype.onOverlayClick = function () {
1754 this.setDisabled( false );
1755 if ( $.isFunction( this.widget
.focus
) ) {
1756 this.widget
.focus();
1759 OptionalWidget
.prototype.setDisabled = function ( disabled
) {
1760 OptionalWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1761 this.widget
.setDisabled( this.isDisabled() );
1762 this.checkbox
.setSelected( !this.isDisabled() );
1763 this.$overlay
.toggle( this.isDisabled() );
1767 $( ApiSandbox
.init
);
1769 module
.exports
= ApiSandbox
;
1771 }( jQuery
, mediaWiki
, OO
) );