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', {
54 title
: Util
.parseMsg( 'apisandbox-results-fixtoken-fail', this.paramInfo
.tokentype
),
59 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
65 fetchToken: function () {
67 return api
.getToken( this.paramInfo
.tokentype
)
68 .done( this.setApiValue
.bind( this ) )
69 .fail( this.alertTokenError
.bind( this ) )
70 .always( this.popPending
.bind( this ) );
72 setApiValue: function ( v
) {
73 WidgetMethods
.textInputWidget
.setApiValue
.call( this, v
);
74 if ( v
=== '123ABC' ) {
81 getApiValueForDisplay: function () {
87 getApiValue: function () {
88 return this.getValue() ? 1 : undefined;
90 setApiValue: function ( v
) {
91 this.setValue( Util
.apiBool( v
) );
93 apiCheckValid: function () {
94 return $.Deferred().resolve( true ).promise();
99 getApiValue: function () {
100 var item
= this.getMenu().getSelectedItem();
101 return item
=== null ? undefined : item
.getData();
103 setApiValue: function ( v
) {
104 var menu
= this.getMenu();
106 if ( v
=== undefined ) {
107 v
= this.paramInfo
[ 'default' ];
109 if ( v
=== undefined ) {
112 menu
.selectItemByData( String( v
) );
115 apiCheckValid: function () {
116 var ok
= this.getApiValue() !== undefined || suppressErrors
;
117 this.setIcon( ok
? null : 'alert' );
118 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
119 return $.Deferred().resolve( ok
).promise();
124 getApiValue: function () {
125 var items
= this.getItemsData();
126 if ( items
.join( '' ).indexOf( '|' ) === -1 ) {
127 return items
.join( '|' );
129 return '\x1f' + items
.join( '\x1f' );
132 setApiValue: function ( v
) {
133 if ( v
=== undefined || v
=== '' || v
=== '\x1f' ) {
134 this.setItemsFromData( [] );
137 if ( v
.indexOf( '\x1f' ) !== 0 ) {
138 this.setItemsFromData( v
.split( '|' ) );
140 this.setItemsFromData( v
.substr( 1 ).split( '\x1f' ) );
144 apiCheckValid: function () {
148 if ( !suppressErrors
) {
149 ok
= this.getApiValue() !== undefined && !(
150 pi
.allspecifier
!== undefined &&
151 this.getItemsData().length
> 1 &&
152 this.getItemsData().indexOf( pi
.allspecifier
) !== -1
156 this.setIcon( ok
? null : 'alert' );
157 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
158 return $.Deferred().resolve( ok
).promise();
163 getApiValue: function () {
164 return this.isDisabled() ? undefined : this.widget
.getApiValue();
166 setApiValue: function ( v
) {
167 this.setDisabled( v
=== undefined );
168 this.widget
.setApiValue( v
);
170 apiCheckValid: function () {
171 if ( this.isDisabled() ) {
172 return $.Deferred().resolve( true ).promise();
174 return this.widget
.apiCheckValid();
180 single: function () {
181 var v
= this.isDisabled() ? this.paramInfo
[ 'default' ] : this.getApiValue();
182 return v
=== undefined ? [] : [ { value
: v
, path
: this.paramInfo
.submodules
[ v
] } ];
185 var map
= this.paramInfo
.submodules
,
186 v
= this.isDisabled() ? this.paramInfo
[ 'default' ] : this.getApiValue();
187 return v
=== undefined || v
=== '' ? [] : $.map( String( v
).split( '|' ), function ( v
) {
188 return { value
: v
, path
: map
[ v
] };
194 getApiValueForDisplay: function () {
197 getApiValue: function () {
198 return this.getValue();
200 setApiValue: function () {
203 apiCheckValid: function () {
204 var ok
= this.getValue() !== null || suppressErrors
;
205 this.setIcon( ok
? null : 'alert' );
206 this.setIconTitle( ok
? '' : mw
.message( 'apisandbox-alert-field' ).plain() );
207 return $.Deferred().resolve( ok
).promise();
213 generic: function () {
214 return !Util
.apiBool( this.paramInfo
.required
) || this.getApiValue() !== '';
219 * @class mw.special.ApiSandbox.Util
224 * Fetch API module info
226 * @param {string} module Module to fetch data for
227 * @return {jQuery.Promise}
229 fetchModuleInfo: function ( module
) {
231 deferred
= $.Deferred();
233 if ( moduleInfoCache
.hasOwnProperty( module
) ) {
235 .resolve( moduleInfoCache
[ module
] )
236 .promise( { abort: function () {} } );
238 apiPromise
= api
.post( {
242 uselang
: mw
.config
.get( 'wgUserLanguage' )
243 } ).done( function ( data
) {
246 if ( data
.warnings
&& data
.warnings
.paraminfo
) {
247 deferred
.reject( '???', data
.warnings
.paraminfo
[ '*' ] );
251 info
= data
.paraminfo
.modules
;
252 if ( !info
|| info
.length
!== 1 || info
[ 0 ].path
!== module
) {
253 deferred
.reject( '???', 'No module data returned' );
257 moduleInfoCache
[ module
] = info
[ 0 ];
258 deferred
.resolve( info
[ 0 ] );
259 } ).fail( function ( code
, details
) {
260 if ( code
=== 'http' ) {
261 details
= 'HTTP error: ' + details
.exception
;
262 } else if ( details
.error
) {
263 details
= details
.error
.info
;
265 deferred
.reject( code
, details
);
268 .promise( { abort
: apiPromise
.abort
} );
273 * Mark all currently-in-use tokens as bad
275 markTokensBad: function () {
276 var page
, subpages
, i
,
277 checkPages
= [ pages
.main
];
279 while ( checkPages
.length
) {
280 page
= checkPages
.shift();
282 if ( page
.tokenWidget
) {
283 api
.badToken( page
.tokenWidget
.paramInfo
.tokentype
);
286 subpages
= page
.getSubpages();
287 for ( i
= 0; i
< subpages
.length
; i
++ ) {
288 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
289 checkPages
.push( pages
[ subpages
[ i
].key
] );
296 * Test an API boolean
298 * @param {Mixed} value
301 apiBool: function ( value
) {
302 return value
!== undefined && value
!== false;
306 * Create a widget for a parameter.
308 * @param {Object} pi Parameter info from API
309 * @param {Object} opts Additional options
310 * @return {OO.ui.Widget}
312 createWidgetForParameter: function ( pi
, opts
) {
313 var widget
, innerWidget
, finalWidget
, items
, $button
, $content
, func
,
320 widget
= new OO
.ui
.ToggleSwitchWidget();
321 widget
.paramInfo
= pi
;
322 $.extend( widget
, WidgetMethods
.toggleSwitchWidget
);
323 pi
.required
= true; // Avoid wrapping in the non-required widget
328 if ( pi
.tokentype
) {
329 widget
= new TextInputWithIndicatorWidget( {
331 indicator
: 'previous',
332 indicatorTitle
: mw
.message( 'apisandbox-fetch-token' ).text(),
333 required
: Util
.apiBool( pi
.required
)
336 } else if ( Util
.apiBool( pi
.multi
) ) {
337 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
340 widget
.paramInfo
= pi
;
341 $.extend( widget
, WidgetMethods
.capsuleWidget
);
343 widget
= new OO
.ui
.TextInputWidget( {
344 required
: Util
.apiBool( pi
.required
)
347 if ( !Util
.apiBool( pi
.multi
) ) {
348 widget
.paramInfo
= pi
;
349 $.extend( widget
, WidgetMethods
.textInputWidget
);
350 widget
.setValidation( Validators
.generic
);
352 if ( pi
.tokentype
) {
353 $.extend( widget
, WidgetMethods
.tokenWidget
);
354 widget
.input
.paramInfo
= pi
;
355 $.extend( widget
.input
, WidgetMethods
.textInputWidget
);
356 $.extend( widget
.input
, WidgetMethods
.tokenWidget
);
357 widget
.on( 'indicator', widget
.fetchToken
, [], widget
);
362 widget
= new OO
.ui
.TextInputWidget( {
364 required
: Util
.apiBool( pi
.required
)
366 widget
.paramInfo
= pi
;
367 $.extend( widget
, WidgetMethods
.textInputWidget
);
368 widget
.setValidation( Validators
.generic
);
372 widget
= new OO
.ui
.TextInputWidget( {
374 required
: Util
.apiBool( pi
.required
)
376 widget
.paramInfo
= pi
;
377 $.extend( widget
, WidgetMethods
.textInputWidget
);
378 $.extend( widget
, WidgetMethods
.passwordWidget
);
379 widget
.setValidation( Validators
.generic
);
384 widget
= new OO
.ui
.NumberInputWidget( {
385 required
: Util
.apiBool( pi
.required
),
388 widget
.setIcon
= widget
.input
.setIcon
.bind( widget
.input
);
389 widget
.setIconTitle
= widget
.input
.setIconTitle
.bind( widget
.input
);
390 widget
.getValidity
= widget
.input
.getValidity
.bind( widget
.input
);
391 widget
.paramInfo
= pi
;
392 $.extend( widget
, WidgetMethods
.textInputWidget
);
393 if ( Util
.apiBool( pi
.enforcerange
) ) {
394 widget
.setRange( pi
.min
|| -Infinity
, pi
.max
|| Infinity
);
400 widget
= new OO
.ui
.TextInputWidget( {
401 required
: Util
.apiBool( pi
.required
)
403 widget
.setValidation( function ( value
) {
404 var n
, pi
= this.paramInfo
;
406 if ( value
=== 'max' ) {
410 return !isNaN( n
) && isFinite( n
) &&
411 // eslint-disable-next-line no-bitwise
413 n
>= pi
.min
&& n
<= pi
.apiSandboxMax
;
416 pi
.min
= pi
.min
|| 0;
417 pi
.apiSandboxMax
= mw
.config
.get( 'apihighlimits' ) ? pi
.highmax
: pi
.max
;
418 widget
.paramInfo
= pi
;
419 $.extend( widget
, WidgetMethods
.textInputWidget
);
424 widget
= new mw
.widgets
.datetime
.DateTimeInputWidget( {
426 format
: '${year|0}-${month|0}-${day|0}T${hour|0}:${minute|0}:${second|0}${zone|short}'
428 required
: Util
.apiBool( pi
.required
),
431 widget
.paramInfo
= pi
;
432 $.extend( widget
, WidgetMethods
.textInputWidget
);
433 $.extend( widget
, WidgetMethods
.dateTimeInputWidget
);
434 multiMode
= 'indicator';
438 widget
= new OO
.ui
.SelectFileWidget();
439 widget
.paramInfo
= pi
;
440 $.extend( widget
, WidgetMethods
.uploadWidget
);
444 items
= $.map( mw
.config
.get( 'wgFormattedNamespaces' ), function ( name
, ns
) {
446 name
= mw
.message( 'blanknamespace' ).text();
448 return new OO
.ui
.MenuOptionWidget( { data
: ns
, label
: name
} );
449 } ).sort( function ( a
, b
) {
450 return a
.data
- b
.data
;
452 if ( Util
.apiBool( pi
.multi
) ) {
453 if ( pi
.allspecifier
!== undefined ) {
454 items
.unshift( new OO
.ui
.MenuOptionWidget( {
455 data
: pi
.allspecifier
,
456 label
: mw
.message( 'apisandbox-multivalue-all-namespaces', pi
.allspecifier
).text()
460 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
461 menu
: { items
: items
}
463 widget
.paramInfo
= pi
;
464 $.extend( widget
, WidgetMethods
.capsuleWidget
);
466 widget
= new OO
.ui
.DropdownWidget( {
467 menu
: { items
: items
}
469 widget
.paramInfo
= pi
;
470 $.extend( widget
, WidgetMethods
.dropdownWidget
);
475 if ( !$.isArray( pi
.type
) ) {
476 throw new Error( 'Unknown parameter type ' + pi
.type
);
479 items
= $.map( pi
.type
, function ( v
) {
480 return new OO
.ui
.MenuOptionWidget( { data
: String( v
), label
: String( v
) } );
482 if ( Util
.apiBool( pi
.multi
) ) {
483 if ( pi
.allspecifier
!== undefined ) {
484 items
.unshift( new OO
.ui
.MenuOptionWidget( {
485 data
: pi
.allspecifier
,
486 label
: mw
.message( 'apisandbox-multivalue-all-values', pi
.allspecifier
).text()
490 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
491 menu
: { items
: items
}
493 widget
.paramInfo
= pi
;
494 $.extend( widget
, WidgetMethods
.capsuleWidget
);
495 if ( Util
.apiBool( pi
.submodules
) ) {
496 widget
.getSubmodules
= WidgetMethods
.submoduleWidget
.multi
;
497 widget
.on( 'change', ApiSandbox
.updateUI
);
500 widget
= new OO
.ui
.DropdownWidget( {
501 menu
: { items
: items
}
503 widget
.paramInfo
= pi
;
504 $.extend( widget
, WidgetMethods
.dropdownWidget
);
505 if ( Util
.apiBool( pi
.submodules
) ) {
506 widget
.getSubmodules
= WidgetMethods
.submoduleWidget
.single
;
507 widget
.getMenu().on( 'choose', ApiSandbox
.updateUI
);
514 if ( Util
.apiBool( pi
.multi
) && multiMode
!== 'none' ) {
515 innerWidget
= widget
;
516 switch ( multiMode
) {
518 $content
= innerWidget
.$element
;
522 $button
= innerWidget
.$indicator
;
523 $button
.css( 'cursor', 'pointer' );
524 $button
.attr( 'tabindex', 0 );
525 $button
.parent().append( $button
);
526 innerWidget
.setIndicator( 'next' );
527 $content
= innerWidget
.$element
;
531 throw new Error( 'Unknown multiMode "' + multiMode
+ '"' );
534 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
535 allowArbitrary
: true,
537 classes
: [ 'mw-apisandbox-popup' ],
541 widget
.paramInfo
= pi
;
542 $.extend( widget
, WidgetMethods
.capsuleWidget
);
545 if ( !innerWidget
.isDisabled() ) {
546 innerWidget
.apiCheckValid().done( function ( ok
) {
548 widget
.addItemsFromData( [ innerWidget
.getApiValue() ] );
549 innerWidget
.setApiValue( undefined );
555 switch ( multiMode
) {
557 innerWidget
.connect( null, { enter
: func
} );
563 keypress: function ( e
) {
564 if ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) {
573 if ( Util
.apiBool( pi
.required
) || opts
.nooptional
) {
574 finalWidget
= widget
;
576 finalWidget
= new OptionalWidget( widget
);
577 finalWidget
.paramInfo
= pi
;
578 $.extend( finalWidget
, WidgetMethods
.optionalWidget
);
579 if ( widget
.getSubmodules
) {
580 finalWidget
.getSubmodules
= widget
.getSubmodules
.bind( widget
);
581 finalWidget
.on( 'disable', function () { setTimeout( ApiSandbox
.updateUI
); } );
583 finalWidget
.setDisabled( true );
586 widget
.setApiValue( pi
[ 'default' ] );
592 * Parse an HTML string and call Util.fixupHTML()
594 * @param {string} html HTML to parse
597 parseHTML: function ( html
) {
598 var $ret
= $( $.parseHTML( html
) );
599 return Util
.fixupHTML( $ret
);
603 * Parse an i18n message and call Util.fixupHTML()
605 * @param {string} key Key of message to get
606 * @param {...Mixed} parameters Values for $N replacements
609 parseMsg: function () {
610 var $ret
= mw
.message
.apply( mw
.message
, arguments
).parseDom();
611 return Util
.fixupHTML( $ret
);
615 * Fix HTML for ApiSandbox display
618 * - Add target="_blank" to any links
620 * @param {jQuery} $html DOM to process
623 fixupHTML: function ( $html
) {
624 $html
.filter( 'a' ).add( $html
.find( 'a' ) )
625 .filter( '[href]:not([target])' )
626 .attr( 'target', '_blank' );
632 * Interface to ApiSandbox UI
634 * @class mw.special.ApiSandbox
640 * Automatically called on $.ready()
645 ApiSandbox
.isFullscreen
= false;
647 $content
= $( '#mw-apisandbox' );
649 windowManager
= new OO
.ui
.WindowManager();
650 $( 'body' ).append( windowManager
.$element
);
651 windowManager
.addWindows( {
652 errorAlert
: new OO
.ui
.MessageDialog()
655 fullscreenButton
= new OO
.ui
.ButtonWidget( {
656 label
: mw
.message( 'apisandbox-fullscreen' ).text(),
657 title
: mw
.message( 'apisandbox-fullscreen-tooltip' ).text()
658 } ).on( 'click', ApiSandbox
.toggleFullscreen
);
660 $toolbar
= $( '<div>' )
661 .addClass( 'mw-apisandbox-toolbar' )
663 fullscreenButton
.$element
,
664 new OO
.ui
.ButtonWidget( {
665 label
: mw
.message( 'apisandbox-submit' ).text(),
666 flags
: [ 'primary', 'progressive' ]
667 } ).on( 'click', ApiSandbox
.sendRequest
).$element
,
668 new OO
.ui
.ButtonWidget( {
669 label
: mw
.message( 'apisandbox-reset' ).text(),
671 } ).on( 'click', ApiSandbox
.resetUI
).$element
674 booklet
= new OO
.ui
.BookletLayout( {
679 panel
= new OO
.ui
.PanelLayout( {
680 classes
: [ 'mw-apisandbox-container' ],
681 content
: [ booklet
],
686 pages
.main
= new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } );
688 // Parse the current hash string
689 if ( !ApiSandbox
.loadFromHash() ) {
690 ApiSandbox
.updateUI();
693 // If the hashchange event exists, use it. Otherwise, fake it.
694 // And, of course, IE has to be dumb.
695 if ( 'onhashchange' in window
&&
696 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
698 $( window
).on( 'hashchange', ApiSandbox
.loadFromHash
);
700 setInterval( function () {
701 if ( oldhash
!== location
.hash
) {
702 ApiSandbox
.loadFromHash();
709 .append( $( '<p>' ).append( Util
.parseMsg( 'apisandbox-intro' ) ) )
711 $( '<div>', { id
: 'mw-apisandbox-ui' } )
713 .append( panel
.$element
)
716 $( window
).on( 'resize', ApiSandbox
.resizePanel
);
718 ApiSandbox
.resizePanel();
722 * Toggle "fullscreen" mode
724 toggleFullscreen: function () {
725 var $body
= $( document
.body
),
726 $ui
= $( '#mw-apisandbox-ui' );
728 ApiSandbox
.isFullscreen
= !ApiSandbox
.isFullscreen
;
730 $body
.toggleClass( 'mw-apisandbox-fullscreen', ApiSandbox
.isFullscreen
);
731 $ui
.toggleClass( 'mw-body-content', ApiSandbox
.isFullscreen
);
732 if ( ApiSandbox
.isFullscreen
) {
733 fullscreenButton
.setLabel( mw
.message( 'apisandbox-unfullscreen' ).text() );
734 fullscreenButton
.setTitle( mw
.message( 'apisandbox-unfullscreen-tooltip' ).text() );
737 fullscreenButton
.setLabel( mw
.message( 'apisandbox-fullscreen' ).text() );
738 fullscreenButton
.setTitle( mw
.message( 'apisandbox-fullscreen-tooltip' ).text() );
739 $content
.append( $ui
);
741 ApiSandbox
.resizePanel();
745 * Set the height of the panel based on the current viewport.
747 resizePanel: function () {
748 var height
= $( window
).height(),
749 contentTop
= $content
.offset().top
;
751 if ( ApiSandbox
.isFullscreen
) {
752 height
-= panel
.$element
.offset().top
- $( '#mw-apisandbox-ui' ).offset().top
;
753 panel
.$element
.height( height
- 1 );
755 // Subtract the height of the intro text
756 height
-= panel
.$element
.offset().top
- contentTop
;
758 panel
.$element
.height( height
- 10 );
759 $( window
).scrollTop( contentTop
- 5 );
764 * Update the current query when the page hash changes
766 * @return {boolean} Successful
768 loadFromHash: function () {
770 hash
= location
.hash
;
772 if ( oldhash
=== hash
) {
780 // I'm surprised this doesn't seem to exist in jQuery or mw.util.
782 hash
= hash
.replace( /\+/g, '%20' );
783 re
= /([^&=#]+)=?([^&#]*)/g;
784 while ( ( m
= re
.exec( hash
) ) ) {
785 params
[ decodeURIComponent( m
[ 1 ] ) ] = decodeURIComponent( m
[ 2 ] );
788 ApiSandbox
.updateUI( params
);
793 * Update the pages in the booklet
795 * @param {Object} [params] Optional query parameters to load
797 updateUI: function ( params
) {
798 var i
, page
, subpages
, j
, removePages
,
801 if ( !$.isPlainObject( params
) ) {
805 if ( updatingBooklet
) {
808 updatingBooklet
= true;
810 if ( params
!== undefined ) {
811 pages
.main
.loadQueryParams( params
);
813 addPages
.push( pages
.main
);
814 if ( resultPage
!== null ) {
815 addPages
.push( resultPage
);
817 pages
.main
.apiCheckValid();
820 while ( addPages
.length
) {
821 page
= addPages
.shift();
822 if ( bookletPages
[ i
] !== page
) {
823 for ( j
= i
; j
< bookletPages
.length
; j
++ ) {
824 if ( bookletPages
[ j
].getName() === page
.getName() ) {
825 bookletPages
.splice( j
, 1 );
828 bookletPages
.splice( i
, 0, page
);
829 booklet
.addPages( [ page
], i
);
833 if ( page
.getSubpages
) {
834 subpages
= page
.getSubpages();
835 for ( j
= 0; j
< subpages
.length
; j
++ ) {
836 if ( !pages
.hasOwnProperty( subpages
[ j
].key
) ) {
837 subpages
[ j
].indentLevel
= page
.indentLevel
+ 1;
838 pages
[ subpages
[ j
].key
] = new ApiSandbox
.PageLayout( subpages
[ j
] );
840 if ( params
!== undefined ) {
841 pages
[ subpages
[ j
].key
].loadQueryParams( params
);
843 addPages
.splice( j
, 0, pages
[ subpages
[ j
].key
] );
844 pages
[ subpages
[ j
].key
].apiCheckValid();
849 if ( bookletPages
.length
> i
) {
850 removePages
= bookletPages
.splice( i
, bookletPages
.length
- i
);
851 booklet
.removePages( removePages
);
854 if ( !booklet
.getCurrentPageName() ) {
855 booklet
.selectFirstSelectablePage();
858 updatingBooklet
= false;
863 * Reset button handler
865 resetUI: function () {
866 suppressErrors
= true;
868 main
: new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } )
871 ApiSandbox
.updateUI();
875 * Submit button handler
877 * @param {Object} [params] Use this set of params instead of those in the form fields.
878 * The form fields will be updated to match.
880 sendRequest: function ( params
) {
881 var page
, subpages
, i
, query
, $result
, $focus
,
882 progress
, $progressText
, progressLoading
,
884 paramsAreForced
= !!params
,
886 checkPages
= [ pages
.main
];
888 // Blur any focused widget before submit, because
889 // OO.ui.ButtonWidget doesn't take focus itself (T128054)
890 $focus
= $( '#mw-apisandbox-ui' ).find( document
.activeElement
);
891 if ( $focus
.length
) {
895 suppressErrors
= false;
897 // save widget state in params (or load from it if we are forced)
898 if ( paramsAreForced
) {
899 ApiSandbox
.updateUI( params
);
902 while ( checkPages
.length
) {
903 page
= checkPages
.shift();
904 deferreds
.push( page
.apiCheckValid() );
905 page
.getQueryParams( params
, displayParams
);
906 subpages
= page
.getSubpages();
907 for ( i
= 0; i
< subpages
.length
; i
++ ) {
908 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
909 checkPages
.push( pages
[ subpages
[ i
].key
] );
914 if ( !paramsAreForced
) {
915 // forced params means we are continuing a query; the base query should be preserved
916 baseRequestParams
= $.extend( {}, params
);
919 $.when
.apply( $, deferreds
).done( function () {
920 if ( $.inArray( false, arguments
) !== -1 ) {
921 windowManager
.openWindow( 'errorAlert', {
922 title
: Util
.parseMsg( 'apisandbox-submit-invalid-fields-title' ),
923 message
: Util
.parseMsg( 'apisandbox-submit-invalid-fields-message' ),
927 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
935 query
= $.param( displayParams
);
937 // Force a 'fm' format with wrappedhtml=1, if available
938 if ( params
.format
!== undefined ) {
939 if ( availableFormats
.hasOwnProperty( params
.format
+ 'fm' ) ) {
940 params
.format
= params
.format
+ 'fm';
942 if ( params
.format
.substr( -2 ) === 'fm' ) {
943 params
.wrappedhtml
= 1;
947 progressLoading
= false;
948 $progressText
= $( '<span>' ).text( mw
.message( 'apisandbox-sending-request' ).text() );
949 progress
= new OO
.ui
.ProgressBarWidget( {
951 $content
: $progressText
954 $result
= $( '<div>' )
955 .append( progress
.$element
);
957 resultPage
= page
= new OO
.ui
.PageLayout( '|results|' );
958 page
.setupOutlineItem = function () {
959 this.outlineItem
.setLabel( mw
.message( 'apisandbox-results' ).text() );
961 page
.$element
.empty()
963 new OO
.ui
.FieldLayout(
964 new OO
.ui
.TextInputWidget( {
966 value
: mw
.util
.wikiScript( 'api' ) + '?' + query
968 label
: Util
.parseMsg( 'apisandbox-request-url-label' )
973 ApiSandbox
.updateUI();
974 booklet
.setPage( '|results|' );
976 location
.href
= oldhash
= '#' + query
;
979 contentType
: 'multipart/form-data',
982 var xhr
= new window
.XMLHttpRequest();
983 xhr
.upload
.addEventListener( 'progress', function ( e
) {
984 if ( !progressLoading
) {
985 if ( e
.lengthComputable
) {
986 progress
.setProgress( e
.loaded
* 100 / e
.total
);
988 progress
.setProgress( false );
992 xhr
.addEventListener( 'progress', function ( e
) {
993 if ( !progressLoading
) {
994 progressLoading
= true;
995 $progressText
.text( mw
.message( 'apisandbox-loading-results' ).text() );
997 if ( e
.lengthComputable
) {
998 progress
.setProgress( e
.loaded
* 100 / e
.total
);
1000 progress
.setProgress( false );
1006 .then( null, function ( code
, data
, result
, jqXHR
) {
1007 if ( code
!== 'http' ) {
1008 // Not really an error, work around mw.Api thinking it is.
1010 .resolve( result
, jqXHR
)
1015 .fail( function ( code
, data
) {
1016 var details
= 'HTTP error: ' + data
.exception
;
1019 new OO
.ui
.LabelWidget( {
1020 label
: mw
.message( 'apisandbox-results-error', details
).text(),
1021 classes
: [ 'error' ]
1025 .done( function ( data
, jqXHR
) {
1026 var m
, loadTime
, button
, clear
,
1027 ct
= jqXHR
.getResponseHeader( 'Content-Type' );
1030 if ( /^text\/mediawiki-api-prettyprint-wrapped(?:;|$)/.test( ct
) ) {
1031 data
= JSON
.parse( data
);
1032 if ( data
.modules
.length
) {
1033 mw
.loader
.load( data
.modules
);
1035 if ( data
.status
&& data
.status
!== 200 ) {
1037 .addClass( 'api-pretty-header api-pretty-status' )
1038 .append( Util
.parseMsg( 'api-format-prettyprint-status', data
.status
, data
.statustext
) )
1039 .appendTo( $result
);
1041 $result
.append( Util
.parseHTML( data
.html
) );
1042 loadTime
= data
.time
;
1043 } else if ( ( m
= data
.match( /<pre[ >][\s\S]*<\/pre>/ ) ) ) {
1044 $result
.append( Util
.parseHTML( m
[ 0 ] ) );
1045 if ( ( m
= data
.match( /"wgBackendResponseTime":\s*(\d+)/ ) ) ) {
1046 loadTime
= parseInt( m
[ 1 ], 10 );
1050 .addClass( 'api-pretty-content' )
1052 .appendTo( $result
);
1054 if ( paramsAreForced
|| data
[ 'continue' ] ) {
1056 $( '<div>' ).append(
1057 new OO
.ui
.ButtonWidget( {
1058 label
: mw
.message( 'apisandbox-continue' ).text()
1059 } ).on( 'click', function () {
1060 ApiSandbox
.sendRequest( $.extend( {}, baseRequestParams
, data
[ 'continue' ] ) );
1061 } ).setDisabled( !data
[ 'continue' ] ).$element
,
1062 ( clear
= new OO
.ui
.ButtonWidget( {
1063 label
: mw
.message( 'apisandbox-continue-clear' ).text()
1064 } ).on( 'click', function () {
1065 ApiSandbox
.updateUI( baseRequestParams
);
1066 clear
.setDisabled( true );
1067 booklet
.setPage( '|results|' );
1068 } ).setDisabled( !paramsAreForced
) ).$element
,
1069 new OO
.ui
.PopupButtonWidget( {
1073 $content
: $( '<div>' ).append( Util
.parseMsg( 'apisandbox-continue-help' ) ),
1080 if ( typeof loadTime
=== 'number' ) {
1082 $( '<div>' ).append(
1083 new OO
.ui
.LabelWidget( {
1084 label
: mw
.message( 'apisandbox-request-time', loadTime
).text()
1090 if ( jqXHR
.getResponseHeader( 'MediaWiki-API-Error' ) === 'badtoken' ) {
1091 // Flush all saved tokens in case one of them is the bad one.
1092 Util
.markTokensBad();
1093 button
= new OO
.ui
.ButtonWidget( {
1094 label
: mw
.message( 'apisandbox-results-fixtoken' ).text()
1096 button
.on( 'click', ApiSandbox
.fixTokenAndResend
)
1097 .on( 'click', button
.setDisabled
, [ true ], button
)
1098 .$element
.appendTo( $result
);
1105 * Handler for the "Correct token and resubmit" button
1107 * Used on a 'badtoken' error, it re-fetches token parameters for all
1108 * pages and then re-submits the query.
1110 fixTokenAndResend: function () {
1111 var page
, subpages
, i
, k
,
1113 tokenWait
= { dummy
: true },
1114 checkPages
= [ pages
.main
],
1115 success = function ( k
) {
1116 delete tokenWait
[ k
];
1117 if ( ok
&& $.isEmptyObject( tokenWait
) ) {
1118 ApiSandbox
.sendRequest();
1121 failure = function ( k
) {
1122 delete tokenWait
[ k
];
1126 while ( checkPages
.length
) {
1127 page
= checkPages
.shift();
1129 if ( page
.tokenWidget
) {
1130 k
= page
.apiModule
+ page
.tokenWidget
.paramInfo
.name
;
1131 tokenWait
[ k
] = page
.tokenWidget
.fetchToken()
1132 .done( success
.bind( page
.tokenWidget
, k
) )
1133 .fail( failure
.bind( page
.tokenWidget
, k
) );
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
] );
1144 success( 'dummy', '' );
1148 * Reset validity indicators for all widgets
1150 updateValidityIndicators: function () {
1151 var page
, subpages
, i
,
1152 checkPages
= [ pages
.main
];
1154 while ( checkPages
.length
) {
1155 page
= checkPages
.shift();
1156 page
.apiCheckValid();
1157 subpages
= page
.getSubpages();
1158 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1159 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1160 checkPages
.push( pages
[ subpages
[ i
].key
] );
1168 * PageLayout for API modules
1172 * @extends OO.ui.PageLayout
1174 * @param {Object} [config] Configuration options
1176 ApiSandbox
.PageLayout = function ( config
) {
1177 config
= $.extend( { prefix
: '' }, config
);
1178 this.displayText
= config
.key
;
1179 this.apiModule
= config
.path
;
1180 this.prefix
= config
.prefix
;
1181 this.paramInfo
= null;
1182 this.apiIsValid
= true;
1183 this.loadFromQueryParams
= null;
1185 this.tokenWidget
= null;
1186 this.indentLevel
= config
.indentLevel
? config
.indentLevel
: 0;
1187 ApiSandbox
.PageLayout
[ 'super' ].call( this, config
.key
, config
);
1188 this.loadParamInfo();
1190 OO
.inheritClass( ApiSandbox
.PageLayout
, OO
.ui
.PageLayout
);
1191 ApiSandbox
.PageLayout
.prototype.setupOutlineItem = function () {
1192 this.outlineItem
.setLevel( this.indentLevel
);
1193 this.outlineItem
.setLabel( this.displayText
);
1194 this.outlineItem
.setIcon( this.apiIsValid
|| suppressErrors
? null : 'alert' );
1195 this.outlineItem
.setIconTitle(
1196 this.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1201 * Fetch module information for this page's module, then create UI
1203 ApiSandbox
.PageLayout
.prototype.loadParamInfo = function () {
1204 var dynamicFieldset
, dynamicParamNameWidget
,
1206 removeDynamicParamWidget = function ( name
, layout
) {
1207 dynamicFieldset
.removeItems( [ layout
] );
1208 delete that
.widgets
[ name
];
1210 addDynamicParamWidget = function () {
1211 var name
, layout
, widget
, button
;
1213 // Check name is filled in
1214 name
= dynamicParamNameWidget
.getValue().trim();
1215 if ( name
=== '' ) {
1216 dynamicParamNameWidget
.focus();
1220 if ( that
.widgets
[ name
] !== undefined ) {
1221 windowManager
.openWindow( 'errorAlert', {
1222 title
: Util
.parseMsg( 'apisandbox-dynamic-error-exists', name
),
1226 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
1234 widget
= Util
.createWidgetForParameter( {
1241 button
= new OO
.ui
.ButtonWidget( {
1243 flags
: 'destructive'
1245 layout
= new OO
.ui
.ActionFieldLayout(
1253 button
.on( 'click', removeDynamicParamWidget
, [ name
, layout
] );
1254 that
.widgets
[ name
] = widget
;
1255 dynamicFieldset
.addItems( [ layout
], dynamicFieldset
.getItems().length
- 1 );
1258 dynamicParamNameWidget
.setValue( '' );
1261 this.$element
.empty()
1262 .append( new OO
.ui
.ProgressBarWidget( {
1264 text
: mw
.message( 'apisandbox-loading', this.displayText
).text()
1267 Util
.fetchModuleInfo( this.apiModule
)
1268 .done( function ( pi
) {
1269 var prefix
, i
, j
, dl
, widget
, $widgetLabel
, widgetField
, helpField
, tmp
, flag
, count
,
1271 deprecatedItems
= [],
1273 filterFmModules = function ( v
) {
1274 return v
.substr( -2 ) !== 'fm' ||
1275 !availableFormats
.hasOwnProperty( v
.substr( 0, v
.length
- 2 ) );
1277 widgetLabelOnClick = function () {
1278 var f
= this.getField();
1279 if ( $.isFunction( f
.setDisabled
) ) {
1280 f
.setDisabled( false );
1282 if ( $.isFunction( f
.focus
) ) {
1286 doNothing = function () {};
1288 // This is something of a hack. We always want the 'format' and
1289 // 'action' parameters from the main module to be specified,
1290 // and for 'format' we also want to simplify the dropdown since
1291 // we always send the 'fm' variant.
1292 if ( that
.apiModule
=== 'main' ) {
1293 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1294 if ( pi
.parameters
[ i
].name
=== 'action' ) {
1295 pi
.parameters
[ i
].required
= true;
1296 delete pi
.parameters
[ i
][ 'default' ];
1298 if ( pi
.parameters
[ i
].name
=== 'format' ) {
1299 tmp
= pi
.parameters
[ i
].type
;
1300 for ( j
= 0; j
< tmp
.length
; j
++ ) {
1301 availableFormats
[ tmp
[ j
] ] = true;
1303 pi
.parameters
[ i
].type
= $.grep( tmp
, filterFmModules
);
1304 pi
.parameters
[ i
][ 'default' ] = 'json';
1305 pi
.parameters
[ i
].required
= true;
1310 // Hide the 'wrappedhtml' parameter on format modules
1311 if ( pi
.group
=== 'format' ) {
1312 pi
.parameters
= $.grep( pi
.parameters
, function ( p
) {
1313 return p
.name
!== 'wrappedhtml';
1317 that
.paramInfo
= pi
;
1319 items
.push( new OO
.ui
.FieldLayout(
1320 new OO
.ui
.Widget( {} ).toggle( false ), {
1322 label
: Util
.parseHTML( pi
.description
)
1326 if ( pi
.helpurls
.length
) {
1327 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1328 label
: mw
.message( 'apisandbox-helpurls' ).text(),
1331 $content
: $( '<ul>' ).append( $.map( pi
.helpurls
, function ( link
) {
1332 return $( '<li>' ).append( $( '<a>', {
1342 if ( pi
.examples
.length
) {
1343 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1344 label
: mw
.message( 'apisandbox-examples' ).text(),
1347 $content
: $( '<ul>' ).append( $.map( pi
.examples
, function ( example
) {
1349 href
: '#' + example
.query
,
1350 html
: example
.description
1352 a
.find( 'a' ).contents().unwrap(); // Can't nest links
1353 return $( '<li>' ).append( a
);
1359 if ( buttons
.length
) {
1360 items
.push( new OO
.ui
.FieldLayout(
1361 new OO
.ui
.ButtonGroupWidget( {
1363 } ), { align
: 'top' }
1367 if ( pi
.parameters
.length
) {
1368 prefix
= that
.prefix
+ pi
.prefix
;
1369 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1370 widget
= Util
.createWidgetForParameter( pi
.parameters
[ i
] );
1371 that
.widgets
[ prefix
+ pi
.parameters
[ i
].name
] = widget
;
1372 if ( pi
.parameters
[ i
].tokentype
) {
1373 that
.tokenWidget
= widget
;
1377 dl
.append( $( '<dd>', {
1378 addClass
: 'description',
1379 append
: Util
.parseHTML( pi
.parameters
[ i
].description
)
1381 if ( pi
.parameters
[ i
].info
&& pi
.parameters
[ i
].info
.length
) {
1382 for ( j
= 0; j
< pi
.parameters
[ i
].info
.length
; j
++ ) {
1383 dl
.append( $( '<dd>', {
1385 append
: Util
.parseHTML( pi
.parameters
[ i
].info
[ j
] )
1391 switch ( pi
.parameters
[ i
].type
) {
1394 count
= mw
.config
.get( 'wgFormattedNamespaces' ).length
;
1398 if ( pi
.parameters
[ i
].highmax
!== undefined ) {
1399 dl
.append( $( '<dd>', {
1403 'api-help-param-limit2', pi
.parameters
[ i
].max
, pi
.parameters
[ i
].highmax
1406 Util
.parseMsg( 'apisandbox-param-limit' )
1410 dl
.append( $( '<dd>', {
1413 Util
.parseMsg( 'api-help-param-limit', pi
.parameters
[ i
].max
),
1415 Util
.parseMsg( 'apisandbox-param-limit' )
1423 if ( pi
.parameters
[ i
].min
!== undefined ) {
1426 if ( pi
.parameters
[ i
].max
!== undefined ) {
1430 dl
.append( $( '<dd>', {
1432 append
: Util
.parseMsg(
1433 'api-help-param-integer-' + tmp
,
1434 Util
.apiBool( pi
.parameters
[ i
].multi
) ? 2 : 1,
1435 pi
.parameters
[ i
].min
, pi
.parameters
[ i
].max
1442 if ( $.isArray( pi
.parameters
[ i
].type
) ) {
1444 count
= pi
.parameters
[ i
].type
.length
;
1448 if ( Util
.apiBool( pi
.parameters
[ i
].multi
) ) {
1450 if ( flag
&& !( widget
instanceof OO
.ui
.CapsuleMultiselectWidget
) &&
1452 widget
instanceof OptionalWidget
&&
1453 widget
.widget
instanceof OO
.ui
.CapsuleMultiselectWidget
1456 tmp
.push( mw
.message( 'api-help-param-multi-separate' ).parse() );
1458 if ( count
> pi
.parameters
[ i
].lowlimit
) {
1460 mw
.message( 'api-help-param-multi-max',
1461 pi
.parameters
[ i
].lowlimit
, pi
.parameters
[ i
].highlimit
1466 dl
.append( $( '<dd>', {
1468 append
: Util
.parseHTML( tmp
.join( ' ' ) )
1472 helpField
= new OO
.ui
.FieldLayout(
1475 classes
: [ 'mw-apisandbox-spacer' ]
1478 classes
: [ 'mw-apisandbox-help-field' ],
1483 $widgetLabel
= $( '<span>' );
1484 widgetField
= new OO
.ui
.FieldLayout(
1488 classes
: [ 'mw-apisandbox-widget-field' ],
1489 label
: prefix
+ pi
.parameters
[ i
].name
,
1490 $label
: $widgetLabel
1494 // FieldLayout only does click for InputElement
1495 // widgets. So supply our own click handler.
1496 $widgetLabel
.on( 'click', widgetLabelOnClick
.bind( widgetField
) );
1498 // Don't grey out the label when the field is disabled,
1499 // it makes it too hard to read and our "disabled"
1500 // isn't really disabled.
1501 widgetField
.onFieldDisable( false );
1502 widgetField
.onFieldDisable
= doNothing
;
1504 if ( Util
.apiBool( pi
.parameters
[ i
].deprecated
) ) {
1505 deprecatedItems
.push( widgetField
, helpField
);
1507 items
.push( widgetField
, helpField
);
1512 if ( !pi
.parameters
.length
&& !Util
.apiBool( pi
.dynamicparameters
) ) {
1513 items
.push( new OO
.ui
.FieldLayout(
1514 new OO
.ui
.Widget( {} ).toggle( false ), {
1516 label
: Util
.parseMsg( 'apisandbox-no-parameters' )
1521 that
.$element
.empty();
1523 new OO
.ui
.FieldsetLayout( {
1524 label
: that
.displayText
1525 } ).addItems( items
)
1526 .$element
.appendTo( that
.$element
);
1528 if ( Util
.apiBool( pi
.dynamicparameters
) ) {
1529 dynamicFieldset
= new OO
.ui
.FieldsetLayout();
1530 dynamicParamNameWidget
= new OO
.ui
.TextInputWidget( {
1531 placeholder
: mw
.message( 'apisandbox-dynamic-parameters-add-placeholder' ).text()
1532 } ).on( 'enter', addDynamicParamWidget
);
1533 dynamicFieldset
.addItems( [
1534 new OO
.ui
.FieldLayout(
1535 new OO
.ui
.Widget( {} ).toggle( false ), {
1537 label
: Util
.parseHTML( pi
.dynamicparameters
)
1540 new OO
.ui
.ActionFieldLayout(
1541 dynamicParamNameWidget
,
1542 new OO
.ui
.ButtonWidget( {
1544 flags
: 'progressive'
1545 } ).on( 'click', addDynamicParamWidget
),
1547 label
: mw
.message( 'apisandbox-dynamic-parameters-add-label' ).text(),
1554 $( '<legend>' ).text( mw
.message( 'apisandbox-dynamic-parameters' ).text() ),
1555 dynamicFieldset
.$element
1557 .appendTo( that
.$element
);
1560 if ( deprecatedItems
.length
) {
1561 tmp
= new OO
.ui
.FieldsetLayout().addItems( deprecatedItems
).toggle( false );
1564 $( '<legend>' ).append(
1565 new OO
.ui
.ToggleButtonWidget( {
1566 label
: mw
.message( 'apisandbox-deprecated-parameters' ).text()
1567 } ).on( 'change', tmp
.toggle
, [], tmp
).$element
1571 .appendTo( that
.$element
);
1574 // Load stored params, if any, then update the booklet if we
1575 // have subpages (or else just update our valid-indicator).
1576 tmp
= that
.loadFromQueryParams
;
1577 that
.loadFromQueryParams
= null;
1578 if ( $.isPlainObject( tmp
) ) {
1579 that
.loadQueryParams( tmp
);
1581 if ( that
.getSubpages().length
> 0 ) {
1582 ApiSandbox
.updateUI( tmp
);
1584 that
.apiCheckValid();
1586 } ).fail( function ( code
, detail
) {
1587 that
.$element
.empty()
1589 new OO
.ui
.LabelWidget( {
1590 label
: mw
.message( 'apisandbox-load-error', that
.apiModule
, detail
).text(),
1591 classes
: [ 'error' ]
1593 new OO
.ui
.ButtonWidget( {
1594 label
: mw
.message( 'apisandbox-retry' ).text()
1595 } ).on( 'click', that
.loadParamInfo
, [], that
).$element
1601 * Check that all widgets on the page are in a valid state.
1605 ApiSandbox
.PageLayout
.prototype.apiCheckValid = function () {
1608 if ( this.paramInfo
=== null ) {
1609 return $.Deferred().resolve( false ).promise();
1611 return $.when
.apply( $, $.map( this.widgets
, function ( widget
) {
1612 return widget
.apiCheckValid();
1613 } ) ).then( function () {
1614 that
.apiIsValid
= $.inArray( false, arguments
) === -1;
1615 if ( that
.getOutlineItem() ) {
1616 that
.getOutlineItem().setIcon( that
.apiIsValid
|| suppressErrors
? null : 'alert' );
1617 that
.getOutlineItem().setIconTitle(
1618 that
.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1621 return $.Deferred().resolve( that
.apiIsValid
).promise();
1627 * Load form fields from query parameters
1629 * @param {Object} params
1631 ApiSandbox
.PageLayout
.prototype.loadQueryParams = function ( params
) {
1632 if ( this.paramInfo
=== null ) {
1633 this.loadFromQueryParams
= params
;
1635 $.each( this.widgets
, function ( name
, widget
) {
1636 var v
= params
.hasOwnProperty( name
) ? params
[ name
] : undefined;
1637 widget
.setApiValue( v
);
1643 * Load query params from form fields
1645 * @param {Object} params Write query parameters into this object
1646 * @param {Object} displayParams Write query parameters for display into this object
1648 ApiSandbox
.PageLayout
.prototype.getQueryParams = function ( params
, displayParams
) {
1649 $.each( this.widgets
, function ( name
, widget
) {
1650 var value
= widget
.getApiValue();
1651 if ( value
!== undefined ) {
1652 params
[ name
] = value
;
1653 if ( $.isFunction( widget
.getApiValueForDisplay
) ) {
1654 value
= widget
.getApiValueForDisplay();
1656 displayParams
[ name
] = value
;
1662 * Fetch a list of subpage names loaded by this page
1666 ApiSandbox
.PageLayout
.prototype.getSubpages = function () {
1668 $.each( this.widgets
, function ( name
, widget
) {
1670 if ( $.isFunction( widget
.getSubmodules
) ) {
1671 submodules
= widget
.getSubmodules();
1672 for ( i
= 0; i
< submodules
.length
; i
++ ) {
1674 key
: name
+ '=' + submodules
[ i
].value
,
1675 path
: submodules
[ i
].path
,
1676 prefix
: widget
.paramInfo
.submoduleparamprefix
|| ''
1685 * A text input with a clickable indicator
1690 * @param {Object} [config] Configuration options
1692 function TextInputWithIndicatorWidget( config
) {
1695 config
= config
|| {};
1696 TextInputWithIndicatorWidget
[ 'super' ].call( this, config
);
1698 this.$indicator
= $( '<span>' ).addClass( 'mw-apisandbox-clickable-indicator' );
1699 OO
.ui
.mixin
.TabIndexedElement
.call(
1700 this, $.extend( {}, config
, { $tabIndexed
: this.$indicator
} )
1703 this.input
= new OO
.ui
.TextInputWidget( $.extend( {
1704 $indicator
: this.$indicator
,
1705 disabled
: this.isDisabled()
1706 }, config
.input
) );
1708 // Forward most methods for convenience
1709 for ( k
in this.input
) {
1710 if ( $.isFunction( this.input
[ k
] ) && !this[ k
] ) {
1711 this[ k
] = this.input
[ k
].bind( this.input
);
1715 this.$indicator
.on( {
1716 click
: this.onIndicatorClick
.bind( this ),
1717 keypress
: this.onIndicatorKeyPress
.bind( this )
1720 this.$element
.append( this.input
.$element
);
1722 OO
.inheritClass( TextInputWithIndicatorWidget
, OO
.ui
.Widget
);
1723 OO
.mixinClass( TextInputWithIndicatorWidget
, OO
.ui
.mixin
.TabIndexedElement
);
1724 TextInputWithIndicatorWidget
.prototype.onIndicatorClick = function ( e
) {
1725 if ( !this.isDisabled() && e
.which
=== 1 ) {
1726 this.emit( 'indicator' );
1730 TextInputWithIndicatorWidget
.prototype.onIndicatorKeyPress = function ( e
) {
1731 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
1732 this.emit( 'indicator' );
1736 TextInputWithIndicatorWidget
.prototype.setDisabled = function ( disabled
) {
1737 TextInputWithIndicatorWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1739 this.input
.setDisabled( this.isDisabled() );
1745 * A wrapper for a widget that provides an enable/disable button
1750 * @param {OO.ui.Widget} widget
1751 * @param {Object} [config] Configuration options
1753 function OptionalWidget( widget
, config
) {
1756 config
= config
|| {};
1758 this.widget
= widget
;
1759 this.$overlay
= config
.$overlay
||
1760 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-overlay' );
1761 this.checkbox
= new OO
.ui
.CheckboxInputWidget( config
.checkbox
)
1762 .on( 'change', this.onCheckboxChange
, [], this );
1764 OptionalWidget
[ 'super' ].call( this, config
);
1766 // Forward most methods for convenience
1767 for ( k
in this.widget
) {
1768 if ( $.isFunction( this.widget
[ k
] ) && !this[ k
] ) {
1769 this[ k
] = this.widget
[ k
].bind( this.widget
);
1773 this.$overlay
.on( 'click', this.onOverlayClick
.bind( this ) );
1776 .addClass( 'mw-apisandbox-optionalWidget' )
1779 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-fields' ).append(
1780 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-widget' ).append(
1783 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-checkbox' ).append(
1784 this.checkbox
.$element
1789 this.setDisabled( widget
.isDisabled() );
1791 OO
.inheritClass( OptionalWidget
, OO
.ui
.Widget
);
1792 OptionalWidget
.prototype.onCheckboxChange = function ( checked
) {
1793 this.setDisabled( !checked
);
1795 OptionalWidget
.prototype.onOverlayClick = function () {
1796 this.setDisabled( false );
1797 if ( $.isFunction( this.widget
.focus
) ) {
1798 this.widget
.focus();
1801 OptionalWidget
.prototype.setDisabled = function ( disabled
) {
1802 OptionalWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1803 this.widget
.setDisabled( this.isDisabled() );
1804 this.checkbox
.setSelected( !this.isDisabled() );
1805 this.$overlay
.toggle( this.isDisabled() );
1809 $( ApiSandbox
.init
);
1811 module
.exports
= ApiSandbox
;
1813 }( jQuery
, mediaWiki
, OO
) );