1 /* eslint-disable no-use-before-define */
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 () {
150 if ( !suppressErrors
) {
151 ok
= this.getApiValue() !== undefined && !(
152 pi
.allspecifier
!== undefined &&
153 this.getItemsData().length
> 1 &&
154 this.getItemsData().indexOf( pi
.allspecifier
) !== -1
158 this.setIcon( ok
? null : 'alert' );
159 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
160 return $.Deferred().resolve( ok
).promise();
165 getApiValue: function () {
166 return this.isDisabled() ? undefined : this.widget
.getApiValue();
168 setApiValue: function ( v
) {
169 this.setDisabled( v
=== undefined );
170 this.widget
.setApiValue( v
);
172 apiCheckValid: function () {
173 if ( this.isDisabled() ) {
174 return $.Deferred().resolve( true ).promise();
176 return this.widget
.apiCheckValid();
182 single: function () {
183 var v
= this.isDisabled() ? this.paramInfo
[ 'default' ] : this.getApiValue();
184 return v
=== undefined ? [] : [ { value
: v
, path
: this.paramInfo
.submodules
[ v
] } ];
187 var map
= this.paramInfo
.submodules
,
188 v
= this.isDisabled() ? this.paramInfo
[ 'default' ] : this.getApiValue();
189 return v
=== undefined || v
=== '' ? [] : $.map( String( v
).split( '|' ), function ( v
) {
190 return { value
: v
, path
: map
[ v
] };
196 getApiValueForDisplay: function () {
199 getApiValue: function () {
200 return this.getValue();
202 setApiValue: function () {
205 apiCheckValid: function () {
206 var ok
= this.getValue() !== null || suppressErrors
;
207 this.setIcon( ok
? null : 'alert' );
208 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
209 return $.Deferred().resolve( ok
).promise();
215 generic: function () {
216 return !Util
.apiBool( this.paramInfo
.required
) || this.getApiValue() !== '';
221 * @class mw.special.ApiSandbox.Utils
226 * Fetch API module info
228 * @param {string} module Module to fetch data for
229 * @return {jQuery.Promise}
231 fetchModuleInfo: function ( module
) {
233 deferred
= $.Deferred();
235 if ( moduleInfoCache
.hasOwnProperty( module
) ) {
237 .resolve( moduleInfoCache
[ module
] )
238 .promise( { abort: function () {} } );
240 apiPromise
= api
.post( {
244 uselang
: mw
.config
.get( 'wgUserLanguage' )
245 } ).done( function ( data
) {
248 if ( data
.warnings
&& data
.warnings
.paraminfo
) {
249 deferred
.reject( '???', data
.warnings
.paraminfo
[ '*' ] );
253 info
= data
.paraminfo
.modules
;
254 if ( !info
|| info
.length
!== 1 || info
[ 0 ].path
!== module
) {
255 deferred
.reject( '???', 'No module data returned' );
259 moduleInfoCache
[ module
] = info
[ 0 ];
260 deferred
.resolve( info
[ 0 ] );
261 } ).fail( function ( code
, details
) {
262 if ( code
=== 'http' ) {
263 details
= 'HTTP error: ' + details
.exception
;
264 } else if ( details
.error
) {
265 details
= details
.error
.info
;
267 deferred
.reject( code
, details
);
270 .promise( { abort
: apiPromise
.abort
} );
275 * Mark all currently-in-use tokens as bad
277 markTokensBad: function () {
278 var page
, subpages
, i
,
279 checkPages
= [ pages
.main
];
281 while ( checkPages
.length
) {
282 page
= checkPages
.shift();
284 if ( page
.tokenWidget
) {
285 api
.badToken( page
.tokenWidget
.paramInfo
.tokentype
);
288 subpages
= page
.getSubpages();
289 for ( i
= 0; i
< subpages
.length
; i
++ ) {
290 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
291 checkPages
.push( pages
[ subpages
[ i
].key
] );
298 * Test an API boolean
300 * @param {Mixed} value
303 apiBool: function ( value
) {
304 return value
!== undefined && value
!== false;
308 * Create a widget for a parameter.
310 * @param {Object} pi Parameter info from API
311 * @param {Object} opts Additional options
312 * @return {OO.ui.Widget}
314 createWidgetForParameter: function ( pi
, opts
) {
315 var widget
, innerWidget
, finalWidget
, items
, $button
, $content
, func
,
322 widget
= new OO
.ui
.ToggleSwitchWidget();
323 widget
.paramInfo
= pi
;
324 $.extend( widget
, WidgetMethods
.toggleSwitchWidget
);
325 pi
.required
= true; // Avoid wrapping in the non-required widget
330 if ( pi
.tokentype
) {
331 widget
= new TextInputWithIndicatorWidget( {
333 indicator
: 'previous',
334 indicatorTitle
: mw
.message( 'apisandbox-fetch-token' ).text(),
335 required
: Util
.apiBool( pi
.required
)
338 } else if ( Util
.apiBool( pi
.multi
) ) {
339 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
342 widget
.paramInfo
= pi
;
343 $.extend( widget
, WidgetMethods
.capsuleWidget
);
345 widget
= new OO
.ui
.TextInputWidget( {
346 required
: Util
.apiBool( pi
.required
)
349 if ( !Util
.apiBool( pi
.multi
) ) {
350 widget
.paramInfo
= pi
;
351 $.extend( widget
, WidgetMethods
.textInputWidget
);
352 widget
.setValidation( Validators
.generic
);
354 if ( pi
.tokentype
) {
355 $.extend( widget
, WidgetMethods
.tokenWidget
);
356 widget
.input
.paramInfo
= pi
;
357 $.extend( widget
.input
, WidgetMethods
.textInputWidget
);
358 $.extend( widget
.input
, WidgetMethods
.tokenWidget
);
359 widget
.on( 'indicator', widget
.fetchToken
, [], widget
);
364 widget
= new OO
.ui
.TextInputWidget( {
366 required
: Util
.apiBool( pi
.required
)
368 widget
.paramInfo
= pi
;
369 $.extend( widget
, WidgetMethods
.textInputWidget
);
370 widget
.setValidation( Validators
.generic
);
374 widget
= new OO
.ui
.TextInputWidget( {
376 required
: Util
.apiBool( pi
.required
)
378 widget
.paramInfo
= pi
;
379 $.extend( widget
, WidgetMethods
.textInputWidget
);
380 $.extend( widget
, WidgetMethods
.passwordWidget
);
381 widget
.setValidation( Validators
.generic
);
386 widget
= new OO
.ui
.NumberInputWidget( {
387 required
: Util
.apiBool( pi
.required
),
390 widget
.setIcon
= widget
.input
.setIcon
.bind( widget
.input
);
391 widget
.setIconTitle
= widget
.input
.setIconTitle
.bind( widget
.input
);
392 widget
.getValidity
= widget
.input
.getValidity
.bind( widget
.input
);
393 widget
.paramInfo
= pi
;
394 $.extend( widget
, WidgetMethods
.textInputWidget
);
395 if ( Util
.apiBool( pi
.enforcerange
) ) {
396 widget
.setRange( pi
.min
|| -Infinity
, pi
.max
|| Infinity
);
402 widget
= new OO
.ui
.TextInputWidget( {
403 required
: Util
.apiBool( pi
.required
)
405 widget
.setValidation( function ( value
) {
406 var n
, pi
= this.paramInfo
;
408 if ( value
=== 'max' ) {
412 return !isNaN( n
) && isFinite( n
) &&
413 // eslint-disable-next-line no-bitwise
415 n
>= pi
.min
&& n
<= pi
.apiSandboxMax
;
418 pi
.min
= pi
.min
|| 0;
419 pi
.apiSandboxMax
= mw
.config
.get( 'apihighlimits' ) ? pi
.highmax
: pi
.max
;
420 widget
.paramInfo
= pi
;
421 $.extend( widget
, WidgetMethods
.textInputWidget
);
426 widget
= new mw
.widgets
.datetime
.DateTimeInputWidget( {
428 format
: '${year|0}-${month|0}-${day|0}T${hour|0}:${minute|0}:${second|0}${zone|short}'
430 required
: Util
.apiBool( pi
.required
),
433 widget
.paramInfo
= pi
;
434 $.extend( widget
, WidgetMethods
.textInputWidget
);
435 $.extend( widget
, WidgetMethods
.dateTimeInputWidget
);
436 multiMode
= 'indicator';
440 widget
= new OO
.ui
.SelectFileWidget();
441 widget
.paramInfo
= pi
;
442 $.extend( widget
, WidgetMethods
.uploadWidget
);
446 items
= $.map( mw
.config
.get( 'wgFormattedNamespaces' ), function ( name
, ns
) {
448 name
= mw
.message( 'blanknamespace' ).text();
450 return new OO
.ui
.MenuOptionWidget( { data
: ns
, label
: name
} );
451 } ).sort( function ( a
, b
) {
452 return a
.data
- b
.data
;
454 if ( Util
.apiBool( pi
.multi
) ) {
455 if ( pi
.allspecifier
!== undefined ) {
456 items
.unshift( new OO
.ui
.MenuOptionWidget( {
457 data
: pi
.allspecifier
,
458 label
: mw
.message( 'apisandbox-multivalue-all-namespaces', pi
.allspecifier
).text()
462 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
463 menu
: { items
: items
}
465 widget
.paramInfo
= pi
;
466 $.extend( widget
, WidgetMethods
.capsuleWidget
);
468 widget
= new OO
.ui
.DropdownWidget( {
469 menu
: { items
: items
}
471 widget
.paramInfo
= pi
;
472 $.extend( widget
, WidgetMethods
.dropdownWidget
);
477 if ( !$.isArray( pi
.type
) ) {
478 throw new Error( 'Unknown parameter type ' + pi
.type
);
481 items
= $.map( pi
.type
, function ( v
) {
482 return new OO
.ui
.MenuOptionWidget( { data
: String( v
), label
: String( v
) } );
484 if ( Util
.apiBool( pi
.multi
) ) {
485 if ( pi
.allspecifier
!== undefined ) {
486 items
.unshift( new OO
.ui
.MenuOptionWidget( {
487 data
: pi
.allspecifier
,
488 label
: mw
.message( 'apisandbox-multivalue-all-values', pi
.allspecifier
).text()
492 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
493 menu
: { items
: items
}
495 widget
.paramInfo
= pi
;
496 $.extend( widget
, WidgetMethods
.capsuleWidget
);
497 if ( Util
.apiBool( pi
.submodules
) ) {
498 widget
.getSubmodules
= WidgetMethods
.submoduleWidget
.multi
;
499 widget
.on( 'change', ApiSandbox
.updateUI
);
502 widget
= new OO
.ui
.DropdownWidget( {
503 menu
: { items
: items
}
505 widget
.paramInfo
= pi
;
506 $.extend( widget
, WidgetMethods
.dropdownWidget
);
507 if ( Util
.apiBool( pi
.submodules
) ) {
508 widget
.getSubmodules
= WidgetMethods
.submoduleWidget
.single
;
509 widget
.getMenu().on( 'choose', ApiSandbox
.updateUI
);
516 if ( Util
.apiBool( pi
.multi
) && multiMode
!== 'none' ) {
517 innerWidget
= widget
;
518 switch ( multiMode
) {
520 $content
= innerWidget
.$element
;
524 $button
= innerWidget
.$indicator
;
525 $button
.css( 'cursor', 'pointer' );
526 $button
.attr( 'tabindex', 0 );
527 $button
.parent().append( $button
);
528 innerWidget
.setIndicator( 'next' );
529 $content
= innerWidget
.$element
;
533 throw new Error( 'Unknown multiMode "' + multiMode
+ '"' );
536 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
537 allowArbitrary
: true,
539 classes
: [ 'mw-apisandbox-popup' ],
543 widget
.paramInfo
= pi
;
544 $.extend( widget
, WidgetMethods
.capsuleWidget
);
547 if ( !innerWidget
.isDisabled() ) {
548 innerWidget
.apiCheckValid().done( function ( ok
) {
550 widget
.addItemsFromData( [ innerWidget
.getApiValue() ] );
551 innerWidget
.setApiValue( undefined );
557 switch ( multiMode
) {
559 innerWidget
.connect( null, { enter
: func
} );
565 keypress: function ( e
) {
566 if ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) {
575 if ( Util
.apiBool( pi
.required
) || opts
.nooptional
) {
576 finalWidget
= widget
;
578 finalWidget
= new OptionalWidget( widget
);
579 finalWidget
.paramInfo
= pi
;
580 $.extend( finalWidget
, WidgetMethods
.optionalWidget
);
581 if ( widget
.getSubmodules
) {
582 finalWidget
.getSubmodules
= widget
.getSubmodules
.bind( widget
);
583 finalWidget
.on( 'disable', function () { setTimeout( ApiSandbox
.updateUI
); } );
585 finalWidget
.setDisabled( true );
588 widget
.setApiValue( pi
[ 'default' ] );
594 * Parse an HTML string, adding target="_blank" to any links
596 * @param {string} html HTML to parse
599 parseHTML: function ( html
) {
600 var $ret
= $( $.parseHTML( html
) );
601 $ret
.filter( 'a' ).add( $ret
.find( 'a' ) )
602 .filter( '[href]:not([target])' )
603 .attr( 'target', '_blank' );
609 * Interface to ApiSandbox UI
611 * @class mw.special.ApiSandbox
617 * Automatically called on $.ready()
622 ApiSandbox
.isFullscreen
= false;
624 $content
= $( '#mw-apisandbox' );
626 windowManager
= new OO
.ui
.WindowManager();
627 $( 'body' ).append( windowManager
.$element
);
628 windowManager
.addWindows( {
629 errorAlert
: new OO
.ui
.MessageDialog()
632 fullscreenButton
= new OO
.ui
.ButtonWidget( {
633 label
: mw
.message( 'apisandbox-fullscreen' ).text(),
634 title
: mw
.message( 'apisandbox-fullscreen-tooltip' ).text()
635 } ).on( 'click', ApiSandbox
.toggleFullscreen
);
637 $toolbar
= $( '<div>' )
638 .addClass( 'mw-apisandbox-toolbar' )
640 fullscreenButton
.$element
,
641 new OO
.ui
.ButtonWidget( {
642 label
: mw
.message( 'apisandbox-submit' ).text(),
643 flags
: [ 'primary', 'progressive' ]
644 } ).on( 'click', ApiSandbox
.sendRequest
).$element
,
645 new OO
.ui
.ButtonWidget( {
646 label
: mw
.message( 'apisandbox-reset' ).text(),
648 } ).on( 'click', ApiSandbox
.resetUI
).$element
651 booklet
= new OO
.ui
.BookletLayout( {
656 panel
= new OO
.ui
.PanelLayout( {
657 classes
: [ 'mw-apisandbox-container' ],
658 content
: [ booklet
],
663 pages
.main
= new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } );
665 // Parse the current hash string
666 if ( !ApiSandbox
.loadFromHash() ) {
667 ApiSandbox
.updateUI();
670 // If the hashchange event exists, use it. Otherwise, fake it.
671 // And, of course, IE has to be dumb.
672 if ( 'onhashchange' in window
&&
673 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
675 $( window
).on( 'hashchange', ApiSandbox
.loadFromHash
);
677 setInterval( function () {
678 if ( oldhash
!== location
.hash
) {
679 ApiSandbox
.loadFromHash();
686 .append( $( '<p>' ).append( mw
.message( 'apisandbox-intro' ).parse() ) )
688 $( '<div>', { id
: 'mw-apisandbox-ui' } )
690 .append( panel
.$element
)
693 $( window
).on( 'resize', ApiSandbox
.resizePanel
);
695 ApiSandbox
.resizePanel();
699 * Toggle "fullscreen" mode
701 toggleFullscreen: function () {
702 var $body
= $( document
.body
),
703 $ui
= $( '#mw-apisandbox-ui' );
705 ApiSandbox
.isFullscreen
= !ApiSandbox
.isFullscreen
;
707 $body
.toggleClass( 'mw-apisandbox-fullscreen', ApiSandbox
.isFullscreen
);
708 $ui
.toggleClass( 'mw-body-content', ApiSandbox
.isFullscreen
);
709 if ( ApiSandbox
.isFullscreen
) {
710 fullscreenButton
.setLabel( mw
.message( 'apisandbox-unfullscreen' ).text() );
711 fullscreenButton
.setTitle( mw
.message( 'apisandbox-unfullscreen-tooltip' ).text() );
714 fullscreenButton
.setLabel( mw
.message( 'apisandbox-fullscreen' ).text() );
715 fullscreenButton
.setTitle( mw
.message( 'apisandbox-fullscreen-tooltip' ).text() );
716 $content
.append( $ui
);
718 ApiSandbox
.resizePanel();
722 * Set the height of the panel based on the current viewport.
724 resizePanel: function () {
725 var height
= $( window
).height(),
726 contentTop
= $content
.offset().top
;
728 if ( ApiSandbox
.isFullscreen
) {
729 height
-= panel
.$element
.offset().top
- $( '#mw-apisandbox-ui' ).offset().top
;
730 panel
.$element
.height( height
- 1 );
732 // Subtract the height of the intro text
733 height
-= panel
.$element
.offset().top
- contentTop
;
735 panel
.$element
.height( height
- 10 );
736 $( window
).scrollTop( contentTop
- 5 );
741 * Update the current query when the page hash changes
743 * @return {boolean} Successful
745 loadFromHash: function () {
747 hash
= location
.hash
;
749 if ( oldhash
=== hash
) {
757 // I'm surprised this doesn't seem to exist in jQuery or mw.util.
759 hash
= hash
.replace( /\+/g, '%20' );
760 re
= /([^&=#]+)=?([^&#]*)/g;
761 while ( ( m
= re
.exec( hash
) ) ) {
762 params
[ decodeURIComponent( m
[ 1 ] ) ] = decodeURIComponent( m
[ 2 ] );
765 ApiSandbox
.updateUI( params
);
770 * Update the pages in the booklet
772 * @param {Object} [params] Optional query parameters to load
774 updateUI: function ( params
) {
775 var i
, page
, subpages
, j
, removePages
,
778 if ( !$.isPlainObject( params
) ) {
782 if ( updatingBooklet
) {
785 updatingBooklet
= true;
787 if ( params
!== undefined ) {
788 pages
.main
.loadQueryParams( params
);
790 addPages
.push( pages
.main
);
791 if ( resultPage
!== null ) {
792 addPages
.push( resultPage
);
794 pages
.main
.apiCheckValid();
797 while ( addPages
.length
) {
798 page
= addPages
.shift();
799 if ( bookletPages
[ i
] !== page
) {
800 for ( j
= i
; j
< bookletPages
.length
; j
++ ) {
801 if ( bookletPages
[ j
].getName() === page
.getName() ) {
802 bookletPages
.splice( j
, 1 );
805 bookletPages
.splice( i
, 0, page
);
806 booklet
.addPages( [ page
], i
);
810 if ( page
.getSubpages
) {
811 subpages
= page
.getSubpages();
812 for ( j
= 0; j
< subpages
.length
; j
++ ) {
813 if ( !pages
.hasOwnProperty( subpages
[ j
].key
) ) {
814 subpages
[ j
].indentLevel
= page
.indentLevel
+ 1;
815 pages
[ subpages
[ j
].key
] = new ApiSandbox
.PageLayout( subpages
[ j
] );
817 if ( params
!== undefined ) {
818 pages
[ subpages
[ j
].key
].loadQueryParams( params
);
820 addPages
.splice( j
, 0, pages
[ subpages
[ j
].key
] );
821 pages
[ subpages
[ j
].key
].apiCheckValid();
826 if ( bookletPages
.length
> i
) {
827 removePages
= bookletPages
.splice( i
, bookletPages
.length
- i
);
828 booklet
.removePages( removePages
);
831 if ( !booklet
.getCurrentPageName() ) {
832 booklet
.selectFirstSelectablePage();
835 updatingBooklet
= false;
840 * Reset button handler
842 resetUI: function () {
843 suppressErrors
= true;
845 main
: new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } )
848 ApiSandbox
.updateUI();
852 * Submit button handler
854 * @param {Object} [params] Use this set of params instead of those in the form fields.
855 * The form fields will be updated to match.
857 sendRequest: function ( params
) {
858 var page
, subpages
, i
, query
, $result
, $focus
,
859 progress
, $progressText
, progressLoading
,
861 paramsAreForced
= !!params
,
863 checkPages
= [ pages
.main
];
865 // Blur any focused widget before submit, because
866 // OO.ui.ButtonWidget doesn't take focus itself (T128054)
867 $focus
= $( '#mw-apisandbox-ui' ).find( document
.activeElement
);
868 if ( $focus
.length
) {
872 suppressErrors
= false;
874 // save widget state in params (or load from it if we are forced)
875 if ( paramsAreForced
) {
876 ApiSandbox
.updateUI( params
);
879 while ( checkPages
.length
) {
880 page
= checkPages
.shift();
881 deferreds
.push( page
.apiCheckValid() );
882 page
.getQueryParams( params
, displayParams
);
883 subpages
= page
.getSubpages();
884 for ( i
= 0; i
< subpages
.length
; i
++ ) {
885 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
886 checkPages
.push( pages
[ subpages
[ i
].key
] );
891 if ( !paramsAreForced
) {
892 // forced params means we are continuing a query; the base query should be preserved
893 baseRequestParams
= $.extend( {}, params
);
896 $.when
.apply( $, deferreds
).done( function () {
897 if ( $.inArray( false, arguments
) !== -1 ) {
898 windowManager
.openWindow( 'errorAlert', {
899 title
: mw
.message( 'apisandbox-submit-invalid-fields-title' ).parse(),
900 message
: mw
.message( 'apisandbox-submit-invalid-fields-message' ).parse(),
904 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
912 query
= $.param( displayParams
);
914 // Force a 'fm' format with wrappedhtml=1, if available
915 if ( params
.format
!== undefined ) {
916 if ( availableFormats
.hasOwnProperty( params
.format
+ 'fm' ) ) {
917 params
.format
= params
.format
+ 'fm';
919 if ( params
.format
.substr( -2 ) === 'fm' ) {
920 params
.wrappedhtml
= 1;
924 progressLoading
= false;
925 $progressText
= $( '<span>' ).text( mw
.message( 'apisandbox-sending-request' ).text() );
926 progress
= new OO
.ui
.ProgressBarWidget( {
928 $content
: $progressText
931 $result
= $( '<div>' )
932 .append( progress
.$element
);
934 resultPage
= page
= new OO
.ui
.PageLayout( '|results|' );
935 page
.setupOutlineItem = function () {
936 this.outlineItem
.setLabel( mw
.message( 'apisandbox-results' ).text() );
938 page
.$element
.empty()
940 new OO
.ui
.FieldLayout(
941 new OO
.ui
.TextInputWidget( {
943 value
: mw
.util
.wikiScript( 'api' ) + '?' + query
945 label
: mw
.message( 'apisandbox-request-url-label' ).parse()
950 ApiSandbox
.updateUI();
951 booklet
.setPage( '|results|' );
953 location
.href
= oldhash
= '#' + query
;
956 contentType
: 'multipart/form-data',
959 var xhr
= new window
.XMLHttpRequest();
960 xhr
.upload
.addEventListener( 'progress', function ( e
) {
961 if ( !progressLoading
) {
962 if ( e
.lengthComputable
) {
963 progress
.setProgress( e
.loaded
* 100 / e
.total
);
965 progress
.setProgress( false );
969 xhr
.addEventListener( 'progress', function ( e
) {
970 if ( !progressLoading
) {
971 progressLoading
= true;
972 $progressText
.text( mw
.message( 'apisandbox-loading-results' ).text() );
974 if ( e
.lengthComputable
) {
975 progress
.setProgress( e
.loaded
* 100 / e
.total
);
977 progress
.setProgress( false );
983 .then( null, function ( code
, data
, result
, jqXHR
) {
984 if ( code
!== 'http' ) {
985 // Not really an error, work around mw.Api thinking it is.
987 .resolve( result
, jqXHR
)
992 .fail( function ( code
, data
) {
993 var details
= 'HTTP error: ' + data
.exception
;
996 new OO
.ui
.LabelWidget( {
997 label
: mw
.message( 'apisandbox-results-error', details
).text(),
1002 .done( function ( data
, jqXHR
) {
1003 var m
, loadTime
, button
, clear
,
1004 ct
= jqXHR
.getResponseHeader( 'Content-Type' );
1007 if ( /^text\/mediawiki-api-prettyprint-wrapped(?:;|$)/.test( ct
) ) {
1008 data
= JSON
.parse( data
);
1009 if ( data
.modules
.length
) {
1010 mw
.loader
.load( data
.modules
);
1012 if ( data
.status
&& data
.status
!== 200 ) {
1014 .addClass( 'api-pretty-header api-pretty-status' )
1016 mw
.message( 'api-format-prettyprint-status', data
.status
, data
.statustext
).parse()
1018 .appendTo( $result
);
1020 $result
.append( Util
.parseHTML( data
.html
) );
1021 loadTime
= data
.time
;
1022 } else if ( ( m
= data
.match( /<pre[ >][\s\S]*<\/pre>/ ) ) ) {
1023 $result
.append( Util
.parseHTML( m
[ 0 ] ) );
1024 if ( ( m
= data
.match( /"wgBackendResponseTime":\s*(\d+)/ ) ) ) {
1025 loadTime
= parseInt( m
[ 1 ], 10 );
1029 .addClass( 'api-pretty-content' )
1031 .appendTo( $result
);
1033 if ( paramsAreForced
|| data
[ 'continue' ] ) {
1035 $( '<div>' ).append(
1036 new OO
.ui
.ButtonWidget( {
1037 label
: mw
.message( 'apisandbox-continue' ).text()
1038 } ).on( 'click', function () {
1039 ApiSandbox
.sendRequest( $.extend( {}, baseRequestParams
, data
[ 'continue' ] ) );
1040 } ).setDisabled( !data
[ 'continue' ] ).$element
,
1041 ( clear
= new OO
.ui
.ButtonWidget( {
1042 label
: mw
.message( 'apisandbox-continue-clear' ).text()
1043 } ).on( 'click', function () {
1044 ApiSandbox
.updateUI( baseRequestParams
);
1045 clear
.setDisabled( true );
1046 booklet
.setPage( '|results|' );
1047 } ).setDisabled( !paramsAreForced
) ).$element
,
1048 new OO
.ui
.PopupButtonWidget( {
1052 $content
: $( '<div>' ).append( mw
.message( 'apisandbox-continue-help' ).parse() ),
1059 if ( typeof loadTime
=== 'number' ) {
1061 $( '<div>' ).append(
1062 new OO
.ui
.LabelWidget( {
1063 label
: mw
.message( 'apisandbox-request-time', loadTime
).text()
1069 if ( jqXHR
.getResponseHeader( 'MediaWiki-API-Error' ) === 'badtoken' ) {
1070 // Flush all saved tokens in case one of them is the bad one.
1071 Util
.markTokensBad();
1072 button
= new OO
.ui
.ButtonWidget( {
1073 label
: mw
.message( 'apisandbox-results-fixtoken' ).text()
1075 button
.on( 'click', ApiSandbox
.fixTokenAndResend
)
1076 .on( 'click', button
.setDisabled
, [ true ], button
)
1077 .$element
.appendTo( $result
);
1084 * Handler for the "Correct token and resubmit" button
1086 * Used on a 'badtoken' error, it re-fetches token parameters for all
1087 * pages and then re-submits the query.
1089 fixTokenAndResend: function () {
1090 var page
, subpages
, i
, k
,
1092 tokenWait
= { dummy
: true },
1093 checkPages
= [ pages
.main
],
1094 success = function ( k
) {
1095 delete tokenWait
[ k
];
1096 if ( ok
&& $.isEmptyObject( tokenWait
) ) {
1097 ApiSandbox
.sendRequest();
1100 failure = function ( k
) {
1101 delete tokenWait
[ k
];
1105 while ( checkPages
.length
) {
1106 page
= checkPages
.shift();
1108 if ( page
.tokenWidget
) {
1109 k
= page
.apiModule
+ page
.tokenWidget
.paramInfo
.name
;
1110 tokenWait
[ k
] = page
.tokenWidget
.fetchToken()
1111 .done( success
.bind( page
.tokenWidget
, k
) )
1112 .fail( failure
.bind( page
.tokenWidget
, k
) );
1115 subpages
= page
.getSubpages();
1116 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1117 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1118 checkPages
.push( pages
[ subpages
[ i
].key
] );
1123 success( 'dummy', '' );
1127 * Reset validity indicators for all widgets
1129 updateValidityIndicators: function () {
1130 var page
, subpages
, i
,
1131 checkPages
= [ pages
.main
];
1133 while ( checkPages
.length
) {
1134 page
= checkPages
.shift();
1135 page
.apiCheckValid();
1136 subpages
= page
.getSubpages();
1137 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1138 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1139 checkPages
.push( pages
[ subpages
[ i
].key
] );
1147 * PageLayout for API modules
1151 * @extends OO.ui.PageLayout
1153 * @param {Object} [config] Configuration options
1155 ApiSandbox
.PageLayout = function ( config
) {
1156 config
= $.extend( { prefix
: '' }, config
);
1157 this.displayText
= config
.key
;
1158 this.apiModule
= config
.path
;
1159 this.prefix
= config
.prefix
;
1160 this.paramInfo
= null;
1161 this.apiIsValid
= true;
1162 this.loadFromQueryParams
= null;
1164 this.tokenWidget
= null;
1165 this.indentLevel
= config
.indentLevel
? config
.indentLevel
: 0;
1166 ApiSandbox
.PageLayout
[ 'super' ].call( this, config
.key
, config
);
1167 this.loadParamInfo();
1169 OO
.inheritClass( ApiSandbox
.PageLayout
, OO
.ui
.PageLayout
);
1170 ApiSandbox
.PageLayout
.prototype.setupOutlineItem = function () {
1171 this.outlineItem
.setLevel( this.indentLevel
);
1172 this.outlineItem
.setLabel( this.displayText
);
1173 this.outlineItem
.setIcon( this.apiIsValid
|| suppressErrors
? null : 'alert' );
1174 this.outlineItem
.setIconTitle(
1175 this.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1180 * Fetch module information for this page's module, then create UI
1182 ApiSandbox
.PageLayout
.prototype.loadParamInfo = function () {
1183 var dynamicFieldset
, dynamicParamNameWidget
,
1185 removeDynamicParamWidget = function ( name
, layout
) {
1186 dynamicFieldset
.removeItems( [ layout
] );
1187 delete that
.widgets
[ name
];
1189 addDynamicParamWidget = function () {
1190 var name
, layout
, widget
, button
;
1192 // Check name is filled in
1193 name
= dynamicParamNameWidget
.getValue().trim();
1194 if ( name
=== '' ) {
1195 dynamicParamNameWidget
.focus();
1199 if ( that
.widgets
[ name
] !== undefined ) {
1200 windowManager
.openWindow( 'errorAlert', {
1202 'apisandbox-dynamic-error-exists', name
1207 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
1215 widget
= Util
.createWidgetForParameter( {
1222 button
= new OO
.ui
.ButtonWidget( {
1224 flags
: 'destructive'
1226 layout
= new OO
.ui
.ActionFieldLayout(
1234 button
.on( 'click', removeDynamicParamWidget
, [ name
, layout
] );
1235 that
.widgets
[ name
] = widget
;
1236 dynamicFieldset
.addItems( [ layout
], dynamicFieldset
.getItems().length
- 1 );
1239 dynamicParamNameWidget
.setValue( '' );
1242 this.$element
.empty()
1243 .append( new OO
.ui
.ProgressBarWidget( {
1245 text
: mw
.message( 'apisandbox-loading', this.displayText
).text()
1248 Util
.fetchModuleInfo( this.apiModule
)
1249 .done( function ( pi
) {
1250 var prefix
, i
, j
, dl
, widget
, $widgetLabel
, widgetField
, helpField
, tmp
, flag
, count
,
1252 deprecatedItems
= [],
1254 filterFmModules = function ( v
) {
1255 return v
.substr( -2 ) !== 'fm' ||
1256 !availableFormats
.hasOwnProperty( v
.substr( 0, v
.length
- 2 ) );
1258 widgetLabelOnClick = function () {
1259 var f
= this.getField();
1260 if ( $.isFunction( f
.setDisabled
) ) {
1261 f
.setDisabled( false );
1263 if ( $.isFunction( f
.focus
) ) {
1267 doNothing = function () {};
1269 // This is something of a hack. We always want the 'format' and
1270 // 'action' parameters from the main module to be specified,
1271 // and for 'format' we also want to simplify the dropdown since
1272 // we always send the 'fm' variant.
1273 if ( that
.apiModule
=== 'main' ) {
1274 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1275 if ( pi
.parameters
[ i
].name
=== 'action' ) {
1276 pi
.parameters
[ i
].required
= true;
1277 delete pi
.parameters
[ i
][ 'default' ];
1279 if ( pi
.parameters
[ i
].name
=== 'format' ) {
1280 tmp
= pi
.parameters
[ i
].type
;
1281 for ( j
= 0; j
< tmp
.length
; j
++ ) {
1282 availableFormats
[ tmp
[ j
] ] = true;
1284 pi
.parameters
[ i
].type
= $.grep( tmp
, filterFmModules
);
1285 pi
.parameters
[ i
][ 'default' ] = 'json';
1286 pi
.parameters
[ i
].required
= true;
1291 // Hide the 'wrappedhtml' parameter on format modules
1292 if ( pi
.group
=== 'format' ) {
1293 pi
.parameters
= $.grep( pi
.parameters
, function ( p
) {
1294 return p
.name
!== 'wrappedhtml';
1298 that
.paramInfo
= pi
;
1300 items
.push( new OO
.ui
.FieldLayout(
1301 new OO
.ui
.Widget( {} ).toggle( false ), {
1303 label
: Util
.parseHTML( pi
.description
)
1307 if ( pi
.helpurls
.length
) {
1308 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1309 label
: mw
.message( 'apisandbox-helpurls' ).text(),
1312 $content
: $( '<ul>' ).append( $.map( pi
.helpurls
, function ( link
) {
1313 return $( '<li>' ).append( $( '<a>', {
1323 if ( pi
.examples
.length
) {
1324 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1325 label
: mw
.message( 'apisandbox-examples' ).text(),
1328 $content
: $( '<ul>' ).append( $.map( pi
.examples
, function ( example
) {
1330 href
: '#' + example
.query
,
1331 html
: example
.description
1333 a
.find( 'a' ).contents().unwrap(); // Can't nest links
1334 return $( '<li>' ).append( a
);
1340 if ( buttons
.length
) {
1341 items
.push( new OO
.ui
.FieldLayout(
1342 new OO
.ui
.ButtonGroupWidget( {
1344 } ), { align
: 'top' }
1348 if ( pi
.parameters
.length
) {
1349 prefix
= that
.prefix
+ pi
.prefix
;
1350 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1351 widget
= Util
.createWidgetForParameter( pi
.parameters
[ i
] );
1352 that
.widgets
[ prefix
+ pi
.parameters
[ i
].name
] = widget
;
1353 if ( pi
.parameters
[ i
].tokentype
) {
1354 that
.tokenWidget
= widget
;
1358 dl
.append( $( '<dd>', {
1359 addClass
: 'description',
1360 append
: Util
.parseHTML( pi
.parameters
[ i
].description
)
1362 if ( pi
.parameters
[ i
].info
&& pi
.parameters
[ i
].info
.length
) {
1363 for ( j
= 0; j
< pi
.parameters
[ i
].info
.length
; j
++ ) {
1364 dl
.append( $( '<dd>', {
1366 append
: Util
.parseHTML( pi
.parameters
[ i
].info
[ j
] )
1372 switch ( pi
.parameters
[ i
].type
) {
1375 count
= mw
.config
.get( 'wgFormattedNamespaces' ).length
;
1379 if ( pi
.parameters
[ i
].highmax
!== undefined ) {
1380 dl
.append( $( '<dd>', {
1383 Util
.parseHTML( mw
.message(
1384 'api-help-param-limit2', pi
.parameters
[ i
].max
, pi
.parameters
[ i
].highmax
1387 Util
.parseHTML( mw
.message( 'apisandbox-param-limit' ).parse() )
1391 dl
.append( $( '<dd>', {
1394 Util
.parseHTML( mw
.message(
1395 'api-help-param-limit', pi
.parameters
[ i
].max
1398 Util
.parseHTML( mw
.message( 'apisandbox-param-limit' ).parse() )
1406 if ( pi
.parameters
[ i
].min
!== undefined ) {
1409 if ( pi
.parameters
[ i
].max
!== undefined ) {
1413 dl
.append( $( '<dd>', {
1415 append
: Util
.parseHTML( mw
.message(
1416 'api-help-param-integer-' + tmp
,
1417 Util
.apiBool( pi
.parameters
[ i
].multi
) ? 2 : 1,
1418 pi
.parameters
[ i
].min
, pi
.parameters
[ i
].max
1425 if ( $.isArray( pi
.parameters
[ i
].type
) ) {
1427 count
= pi
.parameters
[ i
].type
.length
;
1431 if ( Util
.apiBool( pi
.parameters
[ i
].multi
) ) {
1433 if ( flag
&& !( widget
instanceof OO
.ui
.CapsuleMultiselectWidget
) &&
1435 widget
instanceof OptionalWidget
&&
1436 widget
.widget
instanceof OO
.ui
.CapsuleMultiselectWidget
1439 tmp
.push( mw
.message( 'api-help-param-multi-separate' ).parse() );
1441 if ( count
> pi
.parameters
[ i
].lowlimit
) {
1443 mw
.message( 'api-help-param-multi-max',
1444 pi
.parameters
[ i
].lowlimit
, pi
.parameters
[ i
].highlimit
1449 dl
.append( $( '<dd>', {
1451 append
: Util
.parseHTML( tmp
.join( ' ' ) )
1455 helpField
= new OO
.ui
.FieldLayout(
1458 classes
: [ 'mw-apisandbox-spacer' ]
1461 classes
: [ 'mw-apisandbox-help-field' ],
1466 $widgetLabel
= $( '<span>' );
1467 widgetField
= new OO
.ui
.FieldLayout(
1471 classes
: [ 'mw-apisandbox-widget-field' ],
1472 label
: prefix
+ pi
.parameters
[ i
].name
,
1473 $label
: $widgetLabel
1477 // FieldLayout only does click for InputElement
1478 // widgets. So supply our own click handler.
1479 $widgetLabel
.on( 'click', widgetLabelOnClick
.bind( widgetField
) );
1481 // Don't grey out the label when the field is disabled,
1482 // it makes it too hard to read and our "disabled"
1483 // isn't really disabled.
1484 widgetField
.onFieldDisable( false );
1485 widgetField
.onFieldDisable
= doNothing
;
1487 if ( Util
.apiBool( pi
.parameters
[ i
].deprecated
) ) {
1488 deprecatedItems
.push( widgetField
, helpField
);
1490 items
.push( widgetField
, helpField
);
1495 if ( !pi
.parameters
.length
&& !Util
.apiBool( pi
.dynamicparameters
) ) {
1496 items
.push( new OO
.ui
.FieldLayout(
1497 new OO
.ui
.Widget( {} ).toggle( false ), {
1499 label
: Util
.parseHTML( mw
.message( 'apisandbox-no-parameters' ).parse() )
1504 that
.$element
.empty();
1506 new OO
.ui
.FieldsetLayout( {
1507 label
: that
.displayText
1508 } ).addItems( items
)
1509 .$element
.appendTo( that
.$element
);
1511 if ( Util
.apiBool( pi
.dynamicparameters
) ) {
1512 dynamicFieldset
= new OO
.ui
.FieldsetLayout();
1513 dynamicParamNameWidget
= new OO
.ui
.TextInputWidget( {
1514 placeholder
: mw
.message( 'apisandbox-dynamic-parameters-add-placeholder' ).text()
1515 } ).on( 'enter', addDynamicParamWidget
);
1516 dynamicFieldset
.addItems( [
1517 new OO
.ui
.FieldLayout(
1518 new OO
.ui
.Widget( {} ).toggle( false ), {
1520 label
: Util
.parseHTML( pi
.dynamicparameters
)
1523 new OO
.ui
.ActionFieldLayout(
1524 dynamicParamNameWidget
,
1525 new OO
.ui
.ButtonWidget( {
1527 flags
: 'progressive'
1528 } ).on( 'click', addDynamicParamWidget
),
1530 label
: mw
.message( 'apisandbox-dynamic-parameters-add-label' ).text(),
1537 $( '<legend>' ).text( mw
.message( 'apisandbox-dynamic-parameters' ).text() ),
1538 dynamicFieldset
.$element
1540 .appendTo( that
.$element
);
1543 if ( deprecatedItems
.length
) {
1544 tmp
= new OO
.ui
.FieldsetLayout().addItems( deprecatedItems
).toggle( false );
1547 $( '<legend>' ).append(
1548 new OO
.ui
.ToggleButtonWidget( {
1549 label
: mw
.message( 'apisandbox-deprecated-parameters' ).text()
1550 } ).on( 'change', tmp
.toggle
, [], tmp
).$element
1554 .appendTo( that
.$element
);
1557 // Load stored params, if any, then update the booklet if we
1558 // have subpages (or else just update our valid-indicator).
1559 tmp
= that
.loadFromQueryParams
;
1560 that
.loadFromQueryParams
= null;
1561 if ( $.isPlainObject( tmp
) ) {
1562 that
.loadQueryParams( tmp
);
1564 if ( that
.getSubpages().length
> 0 ) {
1565 ApiSandbox
.updateUI( tmp
);
1567 that
.apiCheckValid();
1569 } ).fail( function ( code
, detail
) {
1570 that
.$element
.empty()
1572 new OO
.ui
.LabelWidget( {
1573 label
: mw
.message( 'apisandbox-load-error', that
.apiModule
, detail
).text(),
1574 classes
: [ 'error' ]
1576 new OO
.ui
.ButtonWidget( {
1577 label
: mw
.message( 'apisandbox-retry' ).text()
1578 } ).on( 'click', that
.loadParamInfo
, [], that
).$element
1584 * Check that all widgets on the page are in a valid state.
1588 ApiSandbox
.PageLayout
.prototype.apiCheckValid = function () {
1591 if ( this.paramInfo
=== null ) {
1592 return $.Deferred().resolve( false ).promise();
1594 return $.when
.apply( $, $.map( this.widgets
, function ( widget
) {
1595 return widget
.apiCheckValid();
1596 } ) ).then( function () {
1597 that
.apiIsValid
= $.inArray( false, arguments
) === -1;
1598 if ( that
.getOutlineItem() ) {
1599 that
.getOutlineItem().setIcon( that
.apiIsValid
|| suppressErrors
? null : 'alert' );
1600 that
.getOutlineItem().setIconTitle(
1601 that
.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1604 return $.Deferred().resolve( that
.apiIsValid
).promise();
1610 * Load form fields from query parameters
1612 * @param {Object} params
1614 ApiSandbox
.PageLayout
.prototype.loadQueryParams = function ( params
) {
1615 if ( this.paramInfo
=== null ) {
1616 this.loadFromQueryParams
= params
;
1618 $.each( this.widgets
, function ( name
, widget
) {
1619 var v
= params
.hasOwnProperty( name
) ? params
[ name
] : undefined;
1620 widget
.setApiValue( v
);
1626 * Load query params from form fields
1628 * @param {Object} params Write query parameters into this object
1629 * @param {Object} displayParams Write query parameters for display into this object
1631 ApiSandbox
.PageLayout
.prototype.getQueryParams = function ( params
, displayParams
) {
1632 $.each( this.widgets
, function ( name
, widget
) {
1633 var value
= widget
.getApiValue();
1634 if ( value
!== undefined ) {
1635 params
[ name
] = value
;
1636 if ( $.isFunction( widget
.getApiValueForDisplay
) ) {
1637 value
= widget
.getApiValueForDisplay();
1639 displayParams
[ name
] = value
;
1645 * Fetch a list of subpage names loaded by this page
1649 ApiSandbox
.PageLayout
.prototype.getSubpages = function () {
1651 $.each( this.widgets
, function ( name
, widget
) {
1653 if ( $.isFunction( widget
.getSubmodules
) ) {
1654 submodules
= widget
.getSubmodules();
1655 for ( i
= 0; i
< submodules
.length
; i
++ ) {
1657 key
: name
+ '=' + submodules
[ i
].value
,
1658 path
: submodules
[ i
].path
,
1659 prefix
: widget
.paramInfo
.submoduleparamprefix
|| ''
1668 * A text input with a clickable indicator
1673 * @param {Object} [config] Configuration options
1675 function TextInputWithIndicatorWidget( config
) {
1678 config
= config
|| {};
1679 TextInputWithIndicatorWidget
[ 'super' ].call( this, config
);
1681 this.$indicator
= $( '<span>' ).addClass( 'mw-apisandbox-clickable-indicator' );
1682 OO
.ui
.mixin
.TabIndexedElement
.call(
1683 this, $.extend( {}, config
, { $tabIndexed
: this.$indicator
} )
1686 this.input
= new OO
.ui
.TextInputWidget( $.extend( {
1687 $indicator
: this.$indicator
,
1688 disabled
: this.isDisabled()
1689 }, config
.input
) );
1691 // Forward most methods for convenience
1692 for ( k
in this.input
) {
1693 if ( $.isFunction( this.input
[ k
] ) && !this[ k
] ) {
1694 this[ k
] = this.input
[ k
].bind( this.input
);
1698 this.$indicator
.on( {
1699 click
: this.onIndicatorClick
.bind( this ),
1700 keypress
: this.onIndicatorKeyPress
.bind( this )
1703 this.$element
.append( this.input
.$element
);
1705 OO
.inheritClass( TextInputWithIndicatorWidget
, OO
.ui
.Widget
);
1706 OO
.mixinClass( TextInputWithIndicatorWidget
, OO
.ui
.mixin
.TabIndexedElement
);
1707 TextInputWithIndicatorWidget
.prototype.onIndicatorClick = function ( e
) {
1708 if ( !this.isDisabled() && e
.which
=== 1 ) {
1709 this.emit( 'indicator' );
1713 TextInputWithIndicatorWidget
.prototype.onIndicatorKeyPress = function ( e
) {
1714 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
1715 this.emit( 'indicator' );
1719 TextInputWithIndicatorWidget
.prototype.setDisabled = function ( disabled
) {
1720 TextInputWithIndicatorWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1722 this.input
.setDisabled( this.isDisabled() );
1728 * A wrapper for a widget that provides an enable/disable button
1733 * @param {OO.ui.Widget} widget
1734 * @param {Object} [config] Configuration options
1736 function OptionalWidget( widget
, config
) {
1739 config
= config
|| {};
1741 this.widget
= widget
;
1742 this.$overlay
= config
.$overlay
||
1743 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-overlay' );
1744 this.checkbox
= new OO
.ui
.CheckboxInputWidget( config
.checkbox
)
1745 .on( 'change', this.onCheckboxChange
, [], this );
1747 OptionalWidget
[ 'super' ].call( this, config
);
1749 // Forward most methods for convenience
1750 for ( k
in this.widget
) {
1751 if ( $.isFunction( this.widget
[ k
] ) && !this[ k
] ) {
1752 this[ k
] = this.widget
[ k
].bind( this.widget
);
1756 this.$overlay
.on( 'click', this.onOverlayClick
.bind( this ) );
1759 .addClass( 'mw-apisandbox-optionalWidget' )
1762 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-fields' ).append(
1763 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-widget' ).append(
1766 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-checkbox' ).append(
1767 this.checkbox
.$element
1772 this.setDisabled( widget
.isDisabled() );
1774 OO
.inheritClass( OptionalWidget
, OO
.ui
.Widget
);
1775 OptionalWidget
.prototype.onCheckboxChange = function ( checked
) {
1776 this.setDisabled( !checked
);
1778 OptionalWidget
.prototype.onOverlayClick = function () {
1779 this.setDisabled( false );
1780 if ( $.isFunction( this.widget
.focus
) ) {
1781 this.widget
.focus();
1784 OptionalWidget
.prototype.setDisabled = function ( disabled
) {
1785 OptionalWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1786 this.widget
.setDisabled( this.isDisabled() );
1787 this.checkbox
.setSelected( !this.isDisabled() );
1788 this.$overlay
.toggle( this.isDisabled() );
1792 $( ApiSandbox
.init
);
1794 module
.exports
= ApiSandbox
;
1796 }( jQuery
, mediaWiki
, OO
) );