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 Math
.floor( n
) === n
&&
412 n
>= pi
.min
&& n
<= pi
.apiSandboxMax
;
415 pi
.min
= pi
.min
|| 0;
416 pi
.apiSandboxMax
= mw
.config
.get( 'apihighlimits' ) ? pi
.highmax
: pi
.max
;
417 widget
.paramInfo
= pi
;
418 $.extend( widget
, WidgetMethods
.textInputWidget
);
423 widget
= new mw
.widgets
.datetime
.DateTimeInputWidget( {
425 format
: '${year|0}-${month|0}-${day|0}T${hour|0}:${minute|0}:${second|0}${zone|short}'
427 required
: Util
.apiBool( pi
.required
),
430 widget
.paramInfo
= pi
;
431 $.extend( widget
, WidgetMethods
.textInputWidget
);
432 $.extend( widget
, WidgetMethods
.dateTimeInputWidget
);
433 multiMode
= 'indicator';
437 widget
= new OO
.ui
.SelectFileWidget();
438 widget
.paramInfo
= pi
;
439 $.extend( widget
, WidgetMethods
.uploadWidget
);
443 items
= $.map( mw
.config
.get( 'wgFormattedNamespaces' ), function ( name
, ns
) {
445 name
= mw
.message( 'blanknamespace' ).text();
447 return new OO
.ui
.MenuOptionWidget( { data
: ns
, label
: name
} );
448 } ).sort( function ( a
, b
) {
449 return a
.data
- b
.data
;
451 if ( Util
.apiBool( pi
.multi
) ) {
452 if ( pi
.allspecifier
!== undefined ) {
453 items
.unshift( new OO
.ui
.MenuOptionWidget( {
454 data
: pi
.allspecifier
,
455 label
: mw
.message( 'apisandbox-multivalue-all-namespaces', pi
.allspecifier
).text()
459 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
460 menu
: { items
: items
}
462 widget
.paramInfo
= pi
;
463 $.extend( widget
, WidgetMethods
.capsuleWidget
);
465 widget
= new OO
.ui
.DropdownWidget( {
466 menu
: { items
: items
}
468 widget
.paramInfo
= pi
;
469 $.extend( widget
, WidgetMethods
.dropdownWidget
);
474 if ( !$.isArray( pi
.type
) ) {
475 throw new Error( 'Unknown parameter type ' + pi
.type
);
478 items
= $.map( pi
.type
, function ( v
) {
479 return new OO
.ui
.MenuOptionWidget( { data
: String( v
), label
: String( v
) } );
481 if ( Util
.apiBool( pi
.multi
) ) {
482 if ( pi
.allspecifier
!== undefined ) {
483 items
.unshift( new OO
.ui
.MenuOptionWidget( {
484 data
: pi
.allspecifier
,
485 label
: mw
.message( 'apisandbox-multivalue-all-values', pi
.allspecifier
).text()
489 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
490 menu
: { items
: items
}
492 widget
.paramInfo
= pi
;
493 $.extend( widget
, WidgetMethods
.capsuleWidget
);
494 if ( Util
.apiBool( pi
.submodules
) ) {
495 widget
.getSubmodules
= WidgetMethods
.submoduleWidget
.multi
;
496 widget
.on( 'change', ApiSandbox
.updateUI
);
499 widget
= new OO
.ui
.DropdownWidget( {
500 menu
: { items
: items
}
502 widget
.paramInfo
= pi
;
503 $.extend( widget
, WidgetMethods
.dropdownWidget
);
504 if ( Util
.apiBool( pi
.submodules
) ) {
505 widget
.getSubmodules
= WidgetMethods
.submoduleWidget
.single
;
506 widget
.getMenu().on( 'choose', ApiSandbox
.updateUI
);
513 if ( Util
.apiBool( pi
.multi
) && multiMode
!== 'none' ) {
514 innerWidget
= widget
;
515 switch ( multiMode
) {
517 $content
= innerWidget
.$element
;
521 $button
= innerWidget
.$indicator
;
522 $button
.css( 'cursor', 'pointer' );
523 $button
.attr( 'tabindex', 0 );
524 $button
.parent().append( $button
);
525 innerWidget
.setIndicator( 'next' );
526 $content
= innerWidget
.$element
;
530 throw new Error( 'Unknown multiMode "' + multiMode
+ '"' );
533 widget
= new OO
.ui
.CapsuleMultiselectWidget( {
534 allowArbitrary
: true,
536 classes
: [ 'mw-apisandbox-popup' ],
540 widget
.paramInfo
= pi
;
541 $.extend( widget
, WidgetMethods
.capsuleWidget
);
544 if ( !innerWidget
.isDisabled() ) {
545 innerWidget
.apiCheckValid().done( function ( ok
) {
547 widget
.addItemsFromData( [ innerWidget
.getApiValue() ] );
548 innerWidget
.setApiValue( undefined );
554 switch ( multiMode
) {
556 innerWidget
.connect( null, { enter
: func
} );
562 keypress: function ( e
) {
563 if ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) {
572 if ( Util
.apiBool( pi
.required
) || opts
.nooptional
) {
573 finalWidget
= widget
;
575 finalWidget
= new OptionalWidget( widget
);
576 finalWidget
.paramInfo
= pi
;
577 $.extend( finalWidget
, WidgetMethods
.optionalWidget
);
578 if ( widget
.getSubmodules
) {
579 finalWidget
.getSubmodules
= widget
.getSubmodules
.bind( widget
);
580 finalWidget
.on( 'disable', function () { setTimeout( ApiSandbox
.updateUI
); } );
582 finalWidget
.setDisabled( true );
585 widget
.setApiValue( pi
[ 'default' ] );
591 * Parse an HTML string and call Util.fixupHTML()
593 * @param {string} html HTML to parse
596 parseHTML: function ( html
) {
597 var $ret
= $( $.parseHTML( html
) );
598 return Util
.fixupHTML( $ret
);
602 * Parse an i18n message and call Util.fixupHTML()
604 * @param {string} key Key of message to get
605 * @param {...Mixed} parameters Values for $N replacements
608 parseMsg: function () {
609 var $ret
= mw
.message
.apply( mw
.message
, arguments
).parseDom();
610 return Util
.fixupHTML( $ret
);
614 * Fix HTML for ApiSandbox display
617 * - Add target="_blank" to any links
619 * @param {jQuery} $html DOM to process
622 fixupHTML: function ( $html
) {
623 $html
.filter( 'a' ).add( $html
.find( 'a' ) )
624 .filter( '[href]:not([target])' )
625 .attr( 'target', '_blank' );
631 * Interface to ApiSandbox UI
633 * @class mw.special.ApiSandbox
639 * Automatically called on $.ready()
644 ApiSandbox
.isFullscreen
= false;
646 $content
= $( '#mw-apisandbox' );
648 windowManager
= new OO
.ui
.WindowManager();
649 $( 'body' ).append( windowManager
.$element
);
650 windowManager
.addWindows( {
651 errorAlert
: new OO
.ui
.MessageDialog()
654 fullscreenButton
= new OO
.ui
.ButtonWidget( {
655 label
: mw
.message( 'apisandbox-fullscreen' ).text(),
656 title
: mw
.message( 'apisandbox-fullscreen-tooltip' ).text()
657 } ).on( 'click', ApiSandbox
.toggleFullscreen
);
659 $toolbar
= $( '<div>' )
660 .addClass( 'mw-apisandbox-toolbar' )
662 fullscreenButton
.$element
,
663 new OO
.ui
.ButtonWidget( {
664 label
: mw
.message( 'apisandbox-submit' ).text(),
665 flags
: [ 'primary', 'progressive' ]
666 } ).on( 'click', ApiSandbox
.sendRequest
).$element
,
667 new OO
.ui
.ButtonWidget( {
668 label
: mw
.message( 'apisandbox-reset' ).text(),
670 } ).on( 'click', ApiSandbox
.resetUI
).$element
673 booklet
= new OO
.ui
.BookletLayout( {
678 panel
= new OO
.ui
.PanelLayout( {
679 classes
: [ 'mw-apisandbox-container' ],
680 content
: [ booklet
],
685 pages
.main
= new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } );
687 // Parse the current hash string
688 if ( !ApiSandbox
.loadFromHash() ) {
689 ApiSandbox
.updateUI();
692 // If the hashchange event exists, use it. Otherwise, fake it.
693 // And, of course, IE has to be dumb.
694 if ( 'onhashchange' in window
&&
695 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
697 $( window
).on( 'hashchange', ApiSandbox
.loadFromHash
);
699 setInterval( function () {
700 if ( oldhash
!== location
.hash
) {
701 ApiSandbox
.loadFromHash();
708 .append( $( '<p>' ).append( Util
.parseMsg( 'apisandbox-intro' ) ) )
710 $( '<div>', { id
: 'mw-apisandbox-ui' } )
712 .append( panel
.$element
)
715 $( window
).on( 'resize', ApiSandbox
.resizePanel
);
717 ApiSandbox
.resizePanel();
721 * Toggle "fullscreen" mode
723 toggleFullscreen: function () {
724 var $body
= $( document
.body
),
725 $ui
= $( '#mw-apisandbox-ui' );
727 ApiSandbox
.isFullscreen
= !ApiSandbox
.isFullscreen
;
729 $body
.toggleClass( 'mw-apisandbox-fullscreen', ApiSandbox
.isFullscreen
);
730 $ui
.toggleClass( 'mw-body-content', ApiSandbox
.isFullscreen
);
731 if ( ApiSandbox
.isFullscreen
) {
732 fullscreenButton
.setLabel( mw
.message( 'apisandbox-unfullscreen' ).text() );
733 fullscreenButton
.setTitle( mw
.message( 'apisandbox-unfullscreen-tooltip' ).text() );
736 fullscreenButton
.setLabel( mw
.message( 'apisandbox-fullscreen' ).text() );
737 fullscreenButton
.setTitle( mw
.message( 'apisandbox-fullscreen-tooltip' ).text() );
738 $content
.append( $ui
);
740 ApiSandbox
.resizePanel();
744 * Set the height of the panel based on the current viewport.
746 resizePanel: function () {
747 var height
= $( window
).height(),
748 contentTop
= $content
.offset().top
;
750 if ( ApiSandbox
.isFullscreen
) {
751 height
-= panel
.$element
.offset().top
- $( '#mw-apisandbox-ui' ).offset().top
;
752 panel
.$element
.height( height
- 1 );
754 // Subtract the height of the intro text
755 height
-= panel
.$element
.offset().top
- contentTop
;
757 panel
.$element
.height( height
- 10 );
758 $( window
).scrollTop( contentTop
- 5 );
763 * Update the current query when the page hash changes
765 * @return {boolean} Successful
767 loadFromHash: function () {
769 hash
= location
.hash
;
771 if ( oldhash
=== hash
) {
779 // I'm surprised this doesn't seem to exist in jQuery or mw.util.
781 hash
= hash
.replace( /\+/g, '%20' );
782 re
= /([^&=#]+)=?([^&#]*)/g;
783 while ( ( m
= re
.exec( hash
) ) ) {
784 params
[ decodeURIComponent( m
[ 1 ] ) ] = decodeURIComponent( m
[ 2 ] );
787 ApiSandbox
.updateUI( params
);
792 * Update the pages in the booklet
794 * @param {Object} [params] Optional query parameters to load
796 updateUI: function ( params
) {
797 var i
, page
, subpages
, j
, removePages
,
800 if ( !$.isPlainObject( params
) ) {
804 if ( updatingBooklet
) {
807 updatingBooklet
= true;
809 if ( params
!== undefined ) {
810 pages
.main
.loadQueryParams( params
);
812 addPages
.push( pages
.main
);
813 if ( resultPage
!== null ) {
814 addPages
.push( resultPage
);
816 pages
.main
.apiCheckValid();
819 while ( addPages
.length
) {
820 page
= addPages
.shift();
821 if ( bookletPages
[ i
] !== page
) {
822 for ( j
= i
; j
< bookletPages
.length
; j
++ ) {
823 if ( bookletPages
[ j
].getName() === page
.getName() ) {
824 bookletPages
.splice( j
, 1 );
827 bookletPages
.splice( i
, 0, page
);
828 booklet
.addPages( [ page
], i
);
832 if ( page
.getSubpages
) {
833 subpages
= page
.getSubpages();
834 for ( j
= 0; j
< subpages
.length
; j
++ ) {
835 if ( !pages
.hasOwnProperty( subpages
[ j
].key
) ) {
836 subpages
[ j
].indentLevel
= page
.indentLevel
+ 1;
837 pages
[ subpages
[ j
].key
] = new ApiSandbox
.PageLayout( subpages
[ j
] );
839 if ( params
!== undefined ) {
840 pages
[ subpages
[ j
].key
].loadQueryParams( params
);
842 addPages
.splice( j
, 0, pages
[ subpages
[ j
].key
] );
843 pages
[ subpages
[ j
].key
].apiCheckValid();
848 if ( bookletPages
.length
> i
) {
849 removePages
= bookletPages
.splice( i
, bookletPages
.length
- i
);
850 booklet
.removePages( removePages
);
853 if ( !booklet
.getCurrentPageName() ) {
854 booklet
.selectFirstSelectablePage();
857 updatingBooklet
= false;
862 * Reset button handler
864 resetUI: function () {
865 suppressErrors
= true;
867 main
: new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } )
870 ApiSandbox
.updateUI();
874 * Submit button handler
876 * @param {Object} [params] Use this set of params instead of those in the form fields.
877 * The form fields will be updated to match.
879 sendRequest: function ( params
) {
880 var page
, subpages
, i
, query
, $result
, $focus
,
881 progress
, $progressText
, progressLoading
,
883 paramsAreForced
= !!params
,
885 checkPages
= [ pages
.main
];
887 // Blur any focused widget before submit, because
888 // OO.ui.ButtonWidget doesn't take focus itself (T128054)
889 $focus
= $( '#mw-apisandbox-ui' ).find( document
.activeElement
);
890 if ( $focus
.length
) {
894 suppressErrors
= false;
896 // save widget state in params (or load from it if we are forced)
897 if ( paramsAreForced
) {
898 ApiSandbox
.updateUI( params
);
901 while ( checkPages
.length
) {
902 page
= checkPages
.shift();
903 deferreds
.push( page
.apiCheckValid() );
904 page
.getQueryParams( params
, displayParams
);
905 subpages
= page
.getSubpages();
906 for ( i
= 0; i
< subpages
.length
; i
++ ) {
907 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
908 checkPages
.push( pages
[ subpages
[ i
].key
] );
913 if ( !paramsAreForced
) {
914 // forced params means we are continuing a query; the base query should be preserved
915 baseRequestParams
= $.extend( {}, params
);
918 $.when
.apply( $, deferreds
).done( function () {
919 if ( $.inArray( false, arguments
) !== -1 ) {
920 windowManager
.openWindow( 'errorAlert', {
921 title
: Util
.parseMsg( 'apisandbox-submit-invalid-fields-title' ),
922 message
: Util
.parseMsg( 'apisandbox-submit-invalid-fields-message' ),
926 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
934 query
= $.param( displayParams
);
936 // Force a 'fm' format with wrappedhtml=1, if available
937 if ( params
.format
!== undefined ) {
938 if ( availableFormats
.hasOwnProperty( params
.format
+ 'fm' ) ) {
939 params
.format
= params
.format
+ 'fm';
941 if ( params
.format
.substr( -2 ) === 'fm' ) {
942 params
.wrappedhtml
= 1;
946 progressLoading
= false;
947 $progressText
= $( '<span>' ).text( mw
.message( 'apisandbox-sending-request' ).text() );
948 progress
= new OO
.ui
.ProgressBarWidget( {
950 $content
: $progressText
953 $result
= $( '<div>' )
954 .append( progress
.$element
);
956 resultPage
= page
= new OO
.ui
.PageLayout( '|results|' );
957 page
.setupOutlineItem = function () {
958 this.outlineItem
.setLabel( mw
.message( 'apisandbox-results' ).text() );
960 page
.$element
.empty()
962 new OO
.ui
.FieldLayout(
963 new OO
.ui
.TextInputWidget( {
965 value
: mw
.util
.wikiScript( 'api' ) + '?' + query
967 label
: Util
.parseMsg( 'apisandbox-request-url-label' )
972 ApiSandbox
.updateUI();
973 booklet
.setPage( '|results|' );
975 location
.href
= oldhash
= '#' + query
;
978 contentType
: 'multipart/form-data',
981 var xhr
= new window
.XMLHttpRequest();
982 xhr
.upload
.addEventListener( 'progress', function ( e
) {
983 if ( !progressLoading
) {
984 if ( e
.lengthComputable
) {
985 progress
.setProgress( e
.loaded
* 100 / e
.total
);
987 progress
.setProgress( false );
991 xhr
.addEventListener( 'progress', function ( e
) {
992 if ( !progressLoading
) {
993 progressLoading
= true;
994 $progressText
.text( mw
.message( 'apisandbox-loading-results' ).text() );
996 if ( e
.lengthComputable
) {
997 progress
.setProgress( e
.loaded
* 100 / e
.total
);
999 progress
.setProgress( false );
1005 .then( null, function ( code
, data
, result
, jqXHR
) {
1006 if ( code
!== 'http' ) {
1007 // Not really an error, work around mw.Api thinking it is.
1009 .resolve( result
, jqXHR
)
1014 .fail( function ( code
, data
) {
1015 var details
= 'HTTP error: ' + data
.exception
;
1018 new OO
.ui
.LabelWidget( {
1019 label
: mw
.message( 'apisandbox-results-error', details
).text(),
1020 classes
: [ 'error' ]
1024 .done( function ( data
, jqXHR
) {
1025 var m
, loadTime
, button
, clear
,
1026 ct
= jqXHR
.getResponseHeader( 'Content-Type' );
1029 if ( /^text\/mediawiki-api-prettyprint-wrapped(?:;|$)/.test( ct
) ) {
1030 data
= JSON
.parse( data
);
1031 if ( data
.modules
.length
) {
1032 mw
.loader
.load( data
.modules
);
1034 if ( data
.status
&& data
.status
!== 200 ) {
1036 .addClass( 'api-pretty-header api-pretty-status' )
1037 .append( Util
.parseMsg( 'api-format-prettyprint-status', data
.status
, data
.statustext
) )
1038 .appendTo( $result
);
1040 $result
.append( Util
.parseHTML( data
.html
) );
1041 loadTime
= data
.time
;
1042 } else if ( ( m
= data
.match( /<pre[ >][\s\S]*<\/pre>/ ) ) ) {
1043 $result
.append( Util
.parseHTML( m
[ 0 ] ) );
1044 if ( ( m
= data
.match( /"wgBackendResponseTime":\s*(\d+)/ ) ) ) {
1045 loadTime
= parseInt( m
[ 1 ], 10 );
1049 .addClass( 'api-pretty-content' )
1051 .appendTo( $result
);
1053 if ( paramsAreForced
|| data
[ 'continue' ] ) {
1055 $( '<div>' ).append(
1056 new OO
.ui
.ButtonWidget( {
1057 label
: mw
.message( 'apisandbox-continue' ).text()
1058 } ).on( 'click', function () {
1059 ApiSandbox
.sendRequest( $.extend( {}, baseRequestParams
, data
[ 'continue' ] ) );
1060 } ).setDisabled( !data
[ 'continue' ] ).$element
,
1061 ( clear
= new OO
.ui
.ButtonWidget( {
1062 label
: mw
.message( 'apisandbox-continue-clear' ).text()
1063 } ).on( 'click', function () {
1064 ApiSandbox
.updateUI( baseRequestParams
);
1065 clear
.setDisabled( true );
1066 booklet
.setPage( '|results|' );
1067 } ).setDisabled( !paramsAreForced
) ).$element
,
1068 new OO
.ui
.PopupButtonWidget( {
1072 $content
: $( '<div>' ).append( Util
.parseMsg( 'apisandbox-continue-help' ) ),
1079 if ( typeof loadTime
=== 'number' ) {
1081 $( '<div>' ).append(
1082 new OO
.ui
.LabelWidget( {
1083 label
: mw
.message( 'apisandbox-request-time', loadTime
).text()
1089 if ( jqXHR
.getResponseHeader( 'MediaWiki-API-Error' ) === 'badtoken' ) {
1090 // Flush all saved tokens in case one of them is the bad one.
1091 Util
.markTokensBad();
1092 button
= new OO
.ui
.ButtonWidget( {
1093 label
: mw
.message( 'apisandbox-results-fixtoken' ).text()
1095 button
.on( 'click', ApiSandbox
.fixTokenAndResend
)
1096 .on( 'click', button
.setDisabled
, [ true ], button
)
1097 .$element
.appendTo( $result
);
1104 * Handler for the "Correct token and resubmit" button
1106 * Used on a 'badtoken' error, it re-fetches token parameters for all
1107 * pages and then re-submits the query.
1109 fixTokenAndResend: function () {
1110 var page
, subpages
, i
, k
,
1112 tokenWait
= { dummy
: true },
1113 checkPages
= [ pages
.main
],
1114 success = function ( k
) {
1115 delete tokenWait
[ k
];
1116 if ( ok
&& $.isEmptyObject( tokenWait
) ) {
1117 ApiSandbox
.sendRequest();
1120 failure = function ( k
) {
1121 delete tokenWait
[ k
];
1125 while ( checkPages
.length
) {
1126 page
= checkPages
.shift();
1128 if ( page
.tokenWidget
) {
1129 k
= page
.apiModule
+ page
.tokenWidget
.paramInfo
.name
;
1130 tokenWait
[ k
] = page
.tokenWidget
.fetchToken()
1131 .done( success
.bind( page
.tokenWidget
, k
) )
1132 .fail( failure
.bind( page
.tokenWidget
, k
) );
1135 subpages
= page
.getSubpages();
1136 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1137 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1138 checkPages
.push( pages
[ subpages
[ i
].key
] );
1143 success( 'dummy', '' );
1147 * Reset validity indicators for all widgets
1149 updateValidityIndicators: function () {
1150 var page
, subpages
, i
,
1151 checkPages
= [ pages
.main
];
1153 while ( checkPages
.length
) {
1154 page
= checkPages
.shift();
1155 page
.apiCheckValid();
1156 subpages
= page
.getSubpages();
1157 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1158 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1159 checkPages
.push( pages
[ subpages
[ i
].key
] );
1167 * PageLayout for API modules
1171 * @extends OO.ui.PageLayout
1173 * @param {Object} [config] Configuration options
1175 ApiSandbox
.PageLayout = function ( config
) {
1176 config
= $.extend( { prefix
: '' }, config
);
1177 this.displayText
= config
.key
;
1178 this.apiModule
= config
.path
;
1179 this.prefix
= config
.prefix
;
1180 this.paramInfo
= null;
1181 this.apiIsValid
= true;
1182 this.loadFromQueryParams
= null;
1184 this.tokenWidget
= null;
1185 this.indentLevel
= config
.indentLevel
? config
.indentLevel
: 0;
1186 ApiSandbox
.PageLayout
[ 'super' ].call( this, config
.key
, config
);
1187 this.loadParamInfo();
1189 OO
.inheritClass( ApiSandbox
.PageLayout
, OO
.ui
.PageLayout
);
1190 ApiSandbox
.PageLayout
.prototype.setupOutlineItem = function () {
1191 this.outlineItem
.setLevel( this.indentLevel
);
1192 this.outlineItem
.setLabel( this.displayText
);
1193 this.outlineItem
.setIcon( this.apiIsValid
|| suppressErrors
? null : 'alert' );
1194 this.outlineItem
.setIconTitle(
1195 this.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1200 * Fetch module information for this page's module, then create UI
1202 ApiSandbox
.PageLayout
.prototype.loadParamInfo = function () {
1203 var dynamicFieldset
, dynamicParamNameWidget
,
1205 removeDynamicParamWidget = function ( name
, layout
) {
1206 dynamicFieldset
.removeItems( [ layout
] );
1207 delete that
.widgets
[ name
];
1209 addDynamicParamWidget = function () {
1210 var name
, layout
, widget
, button
;
1212 // Check name is filled in
1213 name
= dynamicParamNameWidget
.getValue().trim();
1214 if ( name
=== '' ) {
1215 dynamicParamNameWidget
.focus();
1219 if ( that
.widgets
[ name
] !== undefined ) {
1220 windowManager
.openWindow( 'errorAlert', {
1221 title
: Util
.parseMsg( 'apisandbox-dynamic-error-exists', name
),
1225 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
1233 widget
= Util
.createWidgetForParameter( {
1240 button
= new OO
.ui
.ButtonWidget( {
1242 flags
: 'destructive'
1244 layout
= new OO
.ui
.ActionFieldLayout(
1252 button
.on( 'click', removeDynamicParamWidget
, [ name
, layout
] );
1253 that
.widgets
[ name
] = widget
;
1254 dynamicFieldset
.addItems( [ layout
], dynamicFieldset
.getItems().length
- 1 );
1257 dynamicParamNameWidget
.setValue( '' );
1260 this.$element
.empty()
1261 .append( new OO
.ui
.ProgressBarWidget( {
1263 text
: mw
.message( 'apisandbox-loading', this.displayText
).text()
1266 Util
.fetchModuleInfo( this.apiModule
)
1267 .done( function ( pi
) {
1268 var prefix
, i
, j
, dl
, widget
, $widgetLabel
, widgetField
, helpField
, tmp
, flag
, count
,
1270 deprecatedItems
= [],
1272 filterFmModules = function ( v
) {
1273 return v
.substr( -2 ) !== 'fm' ||
1274 !availableFormats
.hasOwnProperty( v
.substr( 0, v
.length
- 2 ) );
1276 widgetLabelOnClick = function () {
1277 var f
= this.getField();
1278 if ( $.isFunction( f
.setDisabled
) ) {
1279 f
.setDisabled( false );
1281 if ( $.isFunction( f
.focus
) ) {
1285 doNothing = function () {};
1287 // This is something of a hack. We always want the 'format' and
1288 // 'action' parameters from the main module to be specified,
1289 // and for 'format' we also want to simplify the dropdown since
1290 // we always send the 'fm' variant.
1291 if ( that
.apiModule
=== 'main' ) {
1292 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1293 if ( pi
.parameters
[ i
].name
=== 'action' ) {
1294 pi
.parameters
[ i
].required
= true;
1295 delete pi
.parameters
[ i
][ 'default' ];
1297 if ( pi
.parameters
[ i
].name
=== 'format' ) {
1298 tmp
= pi
.parameters
[ i
].type
;
1299 for ( j
= 0; j
< tmp
.length
; j
++ ) {
1300 availableFormats
[ tmp
[ j
] ] = true;
1302 pi
.parameters
[ i
].type
= $.grep( tmp
, filterFmModules
);
1303 pi
.parameters
[ i
][ 'default' ] = 'json';
1304 pi
.parameters
[ i
].required
= true;
1309 // Hide the 'wrappedhtml' parameter on format modules
1310 if ( pi
.group
=== 'format' ) {
1311 pi
.parameters
= $.grep( pi
.parameters
, function ( p
) {
1312 return p
.name
!== 'wrappedhtml';
1316 that
.paramInfo
= pi
;
1318 items
.push( new OO
.ui
.FieldLayout(
1319 new OO
.ui
.Widget( {} ).toggle( false ), {
1321 label
: Util
.parseHTML( pi
.description
)
1325 if ( pi
.helpurls
.length
) {
1326 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1327 label
: mw
.message( 'apisandbox-helpurls' ).text(),
1330 $content
: $( '<ul>' ).append( $.map( pi
.helpurls
, function ( link
) {
1331 return $( '<li>' ).append( $( '<a>', {
1341 if ( pi
.examples
.length
) {
1342 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1343 label
: mw
.message( 'apisandbox-examples' ).text(),
1346 $content
: $( '<ul>' ).append( $.map( pi
.examples
, function ( example
) {
1348 href
: '#' + example
.query
,
1349 html
: example
.description
1351 a
.find( 'a' ).contents().unwrap(); // Can't nest links
1352 return $( '<li>' ).append( a
);
1358 if ( buttons
.length
) {
1359 items
.push( new OO
.ui
.FieldLayout(
1360 new OO
.ui
.ButtonGroupWidget( {
1362 } ), { align
: 'top' }
1366 if ( pi
.parameters
.length
) {
1367 prefix
= that
.prefix
+ pi
.prefix
;
1368 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1369 widget
= Util
.createWidgetForParameter( pi
.parameters
[ i
] );
1370 that
.widgets
[ prefix
+ pi
.parameters
[ i
].name
] = widget
;
1371 if ( pi
.parameters
[ i
].tokentype
) {
1372 that
.tokenWidget
= widget
;
1376 dl
.append( $( '<dd>', {
1377 addClass
: 'description',
1378 append
: Util
.parseHTML( pi
.parameters
[ i
].description
)
1380 if ( pi
.parameters
[ i
].info
&& pi
.parameters
[ i
].info
.length
) {
1381 for ( j
= 0; j
< pi
.parameters
[ i
].info
.length
; j
++ ) {
1382 dl
.append( $( '<dd>', {
1384 append
: Util
.parseHTML( pi
.parameters
[ i
].info
[ j
] )
1390 switch ( pi
.parameters
[ i
].type
) {
1393 count
= mw
.config
.get( 'wgFormattedNamespaces' ).length
;
1397 if ( pi
.parameters
[ i
].highmax
!== undefined ) {
1398 dl
.append( $( '<dd>', {
1402 'api-help-param-limit2', pi
.parameters
[ i
].max
, pi
.parameters
[ i
].highmax
1405 Util
.parseMsg( 'apisandbox-param-limit' )
1409 dl
.append( $( '<dd>', {
1412 Util
.parseMsg( 'api-help-param-limit', pi
.parameters
[ i
].max
),
1414 Util
.parseMsg( 'apisandbox-param-limit' )
1422 if ( pi
.parameters
[ i
].min
!== undefined ) {
1425 if ( pi
.parameters
[ i
].max
!== undefined ) {
1429 dl
.append( $( '<dd>', {
1431 append
: Util
.parseMsg(
1432 'api-help-param-integer-' + tmp
,
1433 Util
.apiBool( pi
.parameters
[ i
].multi
) ? 2 : 1,
1434 pi
.parameters
[ i
].min
, pi
.parameters
[ i
].max
1441 if ( $.isArray( pi
.parameters
[ i
].type
) ) {
1443 count
= pi
.parameters
[ i
].type
.length
;
1447 if ( Util
.apiBool( pi
.parameters
[ i
].multi
) ) {
1449 if ( flag
&& !( widget
instanceof OO
.ui
.CapsuleMultiselectWidget
) &&
1451 widget
instanceof OptionalWidget
&&
1452 widget
.widget
instanceof OO
.ui
.CapsuleMultiselectWidget
1455 tmp
.push( mw
.message( 'api-help-param-multi-separate' ).parse() );
1457 if ( count
> pi
.parameters
[ i
].lowlimit
) {
1459 mw
.message( 'api-help-param-multi-max',
1460 pi
.parameters
[ i
].lowlimit
, pi
.parameters
[ i
].highlimit
1465 dl
.append( $( '<dd>', {
1467 append
: Util
.parseHTML( tmp
.join( ' ' ) )
1471 helpField
= new OO
.ui
.FieldLayout(
1474 classes
: [ 'mw-apisandbox-spacer' ]
1477 classes
: [ 'mw-apisandbox-help-field' ],
1482 $widgetLabel
= $( '<span>' );
1483 widgetField
= new OO
.ui
.FieldLayout(
1487 classes
: [ 'mw-apisandbox-widget-field' ],
1488 label
: prefix
+ pi
.parameters
[ i
].name
,
1489 $label
: $widgetLabel
1493 // FieldLayout only does click for InputElement
1494 // widgets. So supply our own click handler.
1495 $widgetLabel
.on( 'click', widgetLabelOnClick
.bind( widgetField
) );
1497 // Don't grey out the label when the field is disabled,
1498 // it makes it too hard to read and our "disabled"
1499 // isn't really disabled.
1500 widgetField
.onFieldDisable( false );
1501 widgetField
.onFieldDisable
= doNothing
;
1503 if ( Util
.apiBool( pi
.parameters
[ i
].deprecated
) ) {
1504 deprecatedItems
.push( widgetField
, helpField
);
1506 items
.push( widgetField
, helpField
);
1511 if ( !pi
.parameters
.length
&& !Util
.apiBool( pi
.dynamicparameters
) ) {
1512 items
.push( new OO
.ui
.FieldLayout(
1513 new OO
.ui
.Widget( {} ).toggle( false ), {
1515 label
: Util
.parseMsg( 'apisandbox-no-parameters' )
1520 that
.$element
.empty();
1522 new OO
.ui
.FieldsetLayout( {
1523 label
: that
.displayText
1524 } ).addItems( items
)
1525 .$element
.appendTo( that
.$element
);
1527 if ( Util
.apiBool( pi
.dynamicparameters
) ) {
1528 dynamicFieldset
= new OO
.ui
.FieldsetLayout();
1529 dynamicParamNameWidget
= new OO
.ui
.TextInputWidget( {
1530 placeholder
: mw
.message( 'apisandbox-dynamic-parameters-add-placeholder' ).text()
1531 } ).on( 'enter', addDynamicParamWidget
);
1532 dynamicFieldset
.addItems( [
1533 new OO
.ui
.FieldLayout(
1534 new OO
.ui
.Widget( {} ).toggle( false ), {
1536 label
: Util
.parseHTML( pi
.dynamicparameters
)
1539 new OO
.ui
.ActionFieldLayout(
1540 dynamicParamNameWidget
,
1541 new OO
.ui
.ButtonWidget( {
1543 flags
: 'progressive'
1544 } ).on( 'click', addDynamicParamWidget
),
1546 label
: mw
.message( 'apisandbox-dynamic-parameters-add-label' ).text(),
1553 $( '<legend>' ).text( mw
.message( 'apisandbox-dynamic-parameters' ).text() ),
1554 dynamicFieldset
.$element
1556 .appendTo( that
.$element
);
1559 if ( deprecatedItems
.length
) {
1560 tmp
= new OO
.ui
.FieldsetLayout().addItems( deprecatedItems
).toggle( false );
1563 $( '<legend>' ).append(
1564 new OO
.ui
.ToggleButtonWidget( {
1565 label
: mw
.message( 'apisandbox-deprecated-parameters' ).text()
1566 } ).on( 'change', tmp
.toggle
, [], tmp
).$element
1570 .appendTo( that
.$element
);
1573 // Load stored params, if any, then update the booklet if we
1574 // have subpages (or else just update our valid-indicator).
1575 tmp
= that
.loadFromQueryParams
;
1576 that
.loadFromQueryParams
= null;
1577 if ( $.isPlainObject( tmp
) ) {
1578 that
.loadQueryParams( tmp
);
1580 if ( that
.getSubpages().length
> 0 ) {
1581 ApiSandbox
.updateUI( tmp
);
1583 that
.apiCheckValid();
1585 } ).fail( function ( code
, detail
) {
1586 that
.$element
.empty()
1588 new OO
.ui
.LabelWidget( {
1589 label
: mw
.message( 'apisandbox-load-error', that
.apiModule
, detail
).text(),
1590 classes
: [ 'error' ]
1592 new OO
.ui
.ButtonWidget( {
1593 label
: mw
.message( 'apisandbox-retry' ).text()
1594 } ).on( 'click', that
.loadParamInfo
, [], that
).$element
1600 * Check that all widgets on the page are in a valid state.
1604 ApiSandbox
.PageLayout
.prototype.apiCheckValid = function () {
1607 if ( this.paramInfo
=== null ) {
1608 return $.Deferred().resolve( false ).promise();
1610 return $.when
.apply( $, $.map( this.widgets
, function ( widget
) {
1611 return widget
.apiCheckValid();
1612 } ) ).then( function () {
1613 that
.apiIsValid
= $.inArray( false, arguments
) === -1;
1614 if ( that
.getOutlineItem() ) {
1615 that
.getOutlineItem().setIcon( that
.apiIsValid
|| suppressErrors
? null : 'alert' );
1616 that
.getOutlineItem().setIconTitle(
1617 that
.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1620 return $.Deferred().resolve( that
.apiIsValid
).promise();
1626 * Load form fields from query parameters
1628 * @param {Object} params
1630 ApiSandbox
.PageLayout
.prototype.loadQueryParams = function ( params
) {
1631 if ( this.paramInfo
=== null ) {
1632 this.loadFromQueryParams
= params
;
1634 $.each( this.widgets
, function ( name
, widget
) {
1635 var v
= params
.hasOwnProperty( name
) ? params
[ name
] : undefined;
1636 widget
.setApiValue( v
);
1642 * Load query params from form fields
1644 * @param {Object} params Write query parameters into this object
1645 * @param {Object} displayParams Write query parameters for display into this object
1647 ApiSandbox
.PageLayout
.prototype.getQueryParams = function ( params
, displayParams
) {
1648 $.each( this.widgets
, function ( name
, widget
) {
1649 var value
= widget
.getApiValue();
1650 if ( value
!== undefined ) {
1651 params
[ name
] = value
;
1652 if ( $.isFunction( widget
.getApiValueForDisplay
) ) {
1653 value
= widget
.getApiValueForDisplay();
1655 displayParams
[ name
] = value
;
1661 * Fetch a list of subpage names loaded by this page
1665 ApiSandbox
.PageLayout
.prototype.getSubpages = function () {
1667 $.each( this.widgets
, function ( name
, widget
) {
1669 if ( $.isFunction( widget
.getSubmodules
) ) {
1670 submodules
= widget
.getSubmodules();
1671 for ( i
= 0; i
< submodules
.length
; i
++ ) {
1673 key
: name
+ '=' + submodules
[ i
].value
,
1674 path
: submodules
[ i
].path
,
1675 prefix
: widget
.paramInfo
.submoduleparamprefix
|| ''
1684 * A text input with a clickable indicator
1689 * @param {Object} [config] Configuration options
1691 function TextInputWithIndicatorWidget( config
) {
1694 config
= config
|| {};
1695 TextInputWithIndicatorWidget
[ 'super' ].call( this, config
);
1697 this.$indicator
= $( '<span>' ).addClass( 'mw-apisandbox-clickable-indicator' );
1698 OO
.ui
.mixin
.TabIndexedElement
.call(
1699 this, $.extend( {}, config
, { $tabIndexed
: this.$indicator
} )
1702 this.input
= new OO
.ui
.TextInputWidget( $.extend( {
1703 $indicator
: this.$indicator
,
1704 disabled
: this.isDisabled()
1705 }, config
.input
) );
1707 // Forward most methods for convenience
1708 for ( k
in this.input
) {
1709 if ( $.isFunction( this.input
[ k
] ) && !this[ k
] ) {
1710 this[ k
] = this.input
[ k
].bind( this.input
);
1714 this.$indicator
.on( {
1715 click
: this.onIndicatorClick
.bind( this ),
1716 keypress
: this.onIndicatorKeyPress
.bind( this )
1719 this.$element
.append( this.input
.$element
);
1721 OO
.inheritClass( TextInputWithIndicatorWidget
, OO
.ui
.Widget
);
1722 OO
.mixinClass( TextInputWithIndicatorWidget
, OO
.ui
.mixin
.TabIndexedElement
);
1723 TextInputWithIndicatorWidget
.prototype.onIndicatorClick = function ( e
) {
1724 if ( !this.isDisabled() && e
.which
=== 1 ) {
1725 this.emit( 'indicator' );
1729 TextInputWithIndicatorWidget
.prototype.onIndicatorKeyPress = function ( e
) {
1730 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
1731 this.emit( 'indicator' );
1735 TextInputWithIndicatorWidget
.prototype.setDisabled = function ( disabled
) {
1736 TextInputWithIndicatorWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1738 this.input
.setDisabled( this.isDisabled() );
1744 * A wrapper for a widget that provides an enable/disable button
1749 * @param {OO.ui.Widget} widget
1750 * @param {Object} [config] Configuration options
1752 function OptionalWidget( widget
, config
) {
1755 config
= config
|| {};
1757 this.widget
= widget
;
1758 this.$overlay
= config
.$overlay
||
1759 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-overlay' );
1760 this.checkbox
= new OO
.ui
.CheckboxInputWidget( config
.checkbox
)
1761 .on( 'change', this.onCheckboxChange
, [], this );
1763 OptionalWidget
[ 'super' ].call( this, config
);
1765 // Forward most methods for convenience
1766 for ( k
in this.widget
) {
1767 if ( $.isFunction( this.widget
[ k
] ) && !this[ k
] ) {
1768 this[ k
] = this.widget
[ k
].bind( this.widget
);
1772 this.$overlay
.on( 'click', this.onOverlayClick
.bind( this ) );
1775 .addClass( 'mw-apisandbox-optionalWidget' )
1778 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-fields' ).append(
1779 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-widget' ).append(
1782 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-checkbox' ).append(
1783 this.checkbox
.$element
1788 this.setDisabled( widget
.isDisabled() );
1790 OO
.inheritClass( OptionalWidget
, OO
.ui
.Widget
);
1791 OptionalWidget
.prototype.onCheckboxChange = function ( checked
) {
1792 this.setDisabled( !checked
);
1794 OptionalWidget
.prototype.onOverlayClick = function () {
1795 this.setDisabled( false );
1796 if ( $.isFunction( this.widget
.focus
) ) {
1797 this.widget
.focus();
1800 OptionalWidget
.prototype.setDisabled = function ( disabled
) {
1801 OptionalWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1802 this.widget
.setDisabled( this.isDisabled() );
1803 this.checkbox
.setSelected( !this.isDisabled() );
1804 this.$overlay
.toggle( this.isDisabled() );
1808 $( ApiSandbox
.init
);
1810 module
.exports
= ApiSandbox
;
1812 }( jQuery
, mediaWiki
, OO
) );