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( {
338 allowArbitrary
: true,
339 allowDuplicates
: Util
.apiBool( pi
.allowsduplicates
)
341 widget
.paramInfo
= pi
;
342 $.extend( widget
, WidgetMethods
.capsuleWidget
);
344 widget
= new OO
.ui
.TextInputWidget( {
345 required
: Util
.apiBool( pi
.required
)
348 if ( !Util
.apiBool( pi
.multi
) ) {
349 widget
.paramInfo
= pi
;
350 $.extend( widget
, WidgetMethods
.textInputWidget
);
351 widget
.setValidation( Validators
.generic
);
353 if ( pi
.tokentype
) {
354 $.extend( widget
, WidgetMethods
.tokenWidget
);
355 widget
.input
.paramInfo
= pi
;
356 $.extend( widget
.input
, WidgetMethods
.textInputWidget
);
357 $.extend( widget
.input
, WidgetMethods
.tokenWidget
);
358 widget
.on( 'indicator', widget
.fetchToken
, [], widget
);
363 widget
= new OO
.ui
.TextInputWidget( {
365 required
: Util
.apiBool( pi
.required
)
367 widget
.paramInfo
= pi
;
368 $.extend( widget
, WidgetMethods
.textInputWidget
);
369 widget
.setValidation( Validators
.generic
);
373 widget
= new OO
.ui
.TextInputWidget( {
375 required
: Util
.apiBool( pi
.required
)
377 widget
.paramInfo
= pi
;
378 $.extend( widget
, WidgetMethods
.textInputWidget
);
379 $.extend( widget
, WidgetMethods
.passwordWidget
);
380 widget
.setValidation( Validators
.generic
);
385 widget
= new OO
.ui
.NumberInputWidget( {
386 required
: Util
.apiBool( pi
.required
),
389 widget
.setIcon
= widget
.input
.setIcon
.bind( widget
.input
);
390 widget
.setIconTitle
= widget
.input
.setIconTitle
.bind( widget
.input
);
391 widget
.getValidity
= widget
.input
.getValidity
.bind( widget
.input
);
392 widget
.paramInfo
= pi
;
393 $.extend( widget
, WidgetMethods
.textInputWidget
);
394 if ( Util
.apiBool( pi
.enforcerange
) ) {
395 widget
.setRange( pi
.min
|| -Infinity
, pi
.max
|| Infinity
);
401 widget
= new OO
.ui
.TextInputWidget( {
402 required
: Util
.apiBool( pi
.required
)
404 widget
.setValidation( function ( value
) {
405 var n
, pi
= this.paramInfo
;
407 if ( value
=== 'max' ) {
411 return !isNaN( n
) && isFinite( n
) &&
412 Math
.floor( n
) === n
&&
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,
536 allowDuplicates
: Util
.apiBool( pi
.allowsduplicates
),
538 classes
: [ 'mw-apisandbox-popup' ],
542 widget
.paramInfo
= pi
;
543 $.extend( widget
, WidgetMethods
.capsuleWidget
);
546 if ( !innerWidget
.isDisabled() ) {
547 innerWidget
.apiCheckValid().done( function ( ok
) {
549 widget
.addItemsFromData( [ innerWidget
.getApiValue() ] );
550 innerWidget
.setApiValue( undefined );
556 switch ( multiMode
) {
558 innerWidget
.connect( null, { enter
: func
} );
564 keypress: function ( e
) {
565 if ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) {
574 if ( Util
.apiBool( pi
.required
) || opts
.nooptional
) {
575 finalWidget
= widget
;
577 finalWidget
= new OptionalWidget( widget
);
578 finalWidget
.paramInfo
= pi
;
579 $.extend( finalWidget
, WidgetMethods
.optionalWidget
);
580 if ( widget
.getSubmodules
) {
581 finalWidget
.getSubmodules
= widget
.getSubmodules
.bind( widget
);
582 finalWidget
.on( 'disable', function () { setTimeout( ApiSandbox
.updateUI
); } );
584 finalWidget
.setDisabled( true );
587 widget
.setApiValue( pi
[ 'default' ] );
593 * Parse an HTML string and call Util.fixupHTML()
595 * @param {string} html HTML to parse
598 parseHTML: function ( html
) {
599 var $ret
= $( $.parseHTML( html
) );
600 return Util
.fixupHTML( $ret
);
604 * Parse an i18n message and call Util.fixupHTML()
606 * @param {string} key Key of message to get
607 * @param {...Mixed} parameters Values for $N replacements
610 parseMsg: function () {
611 var $ret
= mw
.message
.apply( mw
.message
, arguments
).parseDom();
612 return Util
.fixupHTML( $ret
);
616 * Fix HTML for ApiSandbox display
619 * - Add target="_blank" to any links
621 * @param {jQuery} $html DOM to process
624 fixupHTML: function ( $html
) {
625 $html
.filter( 'a' ).add( $html
.find( 'a' ) )
626 .filter( '[href]:not([target])' )
627 .attr( 'target', '_blank' );
633 * Interface to ApiSandbox UI
635 * @class mw.special.ApiSandbox
641 * Automatically called on $.ready()
646 ApiSandbox
.isFullscreen
= false;
648 $content
= $( '#mw-apisandbox' );
650 windowManager
= new OO
.ui
.WindowManager();
651 $( 'body' ).append( windowManager
.$element
);
652 windowManager
.addWindows( {
653 errorAlert
: new OO
.ui
.MessageDialog()
656 fullscreenButton
= new OO
.ui
.ButtonWidget( {
657 label
: mw
.message( 'apisandbox-fullscreen' ).text(),
658 title
: mw
.message( 'apisandbox-fullscreen-tooltip' ).text()
659 } ).on( 'click', ApiSandbox
.toggleFullscreen
);
661 $toolbar
= $( '<div>' )
662 .addClass( 'mw-apisandbox-toolbar' )
664 fullscreenButton
.$element
,
665 new OO
.ui
.ButtonWidget( {
666 label
: mw
.message( 'apisandbox-submit' ).text(),
667 flags
: [ 'primary', 'progressive' ]
668 } ).on( 'click', ApiSandbox
.sendRequest
).$element
,
669 new OO
.ui
.ButtonWidget( {
670 label
: mw
.message( 'apisandbox-reset' ).text(),
672 } ).on( 'click', ApiSandbox
.resetUI
).$element
675 booklet
= new OO
.ui
.BookletLayout( {
680 panel
= new OO
.ui
.PanelLayout( {
681 classes
: [ 'mw-apisandbox-container' ],
682 content
: [ booklet
],
687 pages
.main
= new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } );
689 // Parse the current hash string
690 if ( !ApiSandbox
.loadFromHash() ) {
691 ApiSandbox
.updateUI();
694 // If the hashchange event exists, use it. Otherwise, fake it.
695 // And, of course, IE has to be dumb.
696 if ( 'onhashchange' in window
&&
697 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
699 $( window
).on( 'hashchange', ApiSandbox
.loadFromHash
);
701 setInterval( function () {
702 if ( oldhash
!== location
.hash
) {
703 ApiSandbox
.loadFromHash();
710 .append( $( '<p>' ).append( Util
.parseMsg( 'apisandbox-intro' ) ) )
712 $( '<div>', { id
: 'mw-apisandbox-ui' } )
714 .append( panel
.$element
)
717 $( window
).on( 'resize', ApiSandbox
.resizePanel
);
719 ApiSandbox
.resizePanel();
723 * Toggle "fullscreen" mode
725 toggleFullscreen: function () {
726 var $body
= $( document
.body
),
727 $ui
= $( '#mw-apisandbox-ui' );
729 ApiSandbox
.isFullscreen
= !ApiSandbox
.isFullscreen
;
731 $body
.toggleClass( 'mw-apisandbox-fullscreen', ApiSandbox
.isFullscreen
);
732 $ui
.toggleClass( 'mw-body-content', ApiSandbox
.isFullscreen
);
733 if ( ApiSandbox
.isFullscreen
) {
734 fullscreenButton
.setLabel( mw
.message( 'apisandbox-unfullscreen' ).text() );
735 fullscreenButton
.setTitle( mw
.message( 'apisandbox-unfullscreen-tooltip' ).text() );
738 fullscreenButton
.setLabel( mw
.message( 'apisandbox-fullscreen' ).text() );
739 fullscreenButton
.setTitle( mw
.message( 'apisandbox-fullscreen-tooltip' ).text() );
740 $content
.append( $ui
);
742 ApiSandbox
.resizePanel();
746 * Set the height of the panel based on the current viewport.
748 resizePanel: function () {
749 var height
= $( window
).height(),
750 contentTop
= $content
.offset().top
;
752 if ( ApiSandbox
.isFullscreen
) {
753 height
-= panel
.$element
.offset().top
- $( '#mw-apisandbox-ui' ).offset().top
;
754 panel
.$element
.height( height
- 1 );
756 // Subtract the height of the intro text
757 height
-= panel
.$element
.offset().top
- contentTop
;
759 panel
.$element
.height( height
- 10 );
760 $( window
).scrollTop( contentTop
- 5 );
765 * Update the current query when the page hash changes
767 * @return {boolean} Successful
769 loadFromHash: function () {
771 hash
= location
.hash
;
773 if ( oldhash
=== hash
) {
781 // I'm surprised this doesn't seem to exist in jQuery or mw.util.
783 hash
= hash
.replace( /\+/g, '%20' );
784 re
= /([^&=#]+)=?([^&#]*)/g;
785 while ( ( m
= re
.exec( hash
) ) ) {
786 params
[ decodeURIComponent( m
[ 1 ] ) ] = decodeURIComponent( m
[ 2 ] );
789 ApiSandbox
.updateUI( params
);
794 * Update the pages in the booklet
796 * @param {Object} [params] Optional query parameters to load
798 updateUI: function ( params
) {
799 var i
, page
, subpages
, j
, removePages
,
802 if ( !$.isPlainObject( params
) ) {
806 if ( updatingBooklet
) {
809 updatingBooklet
= true;
811 if ( params
!== undefined ) {
812 pages
.main
.loadQueryParams( params
);
814 addPages
.push( pages
.main
);
815 if ( resultPage
!== null ) {
816 addPages
.push( resultPage
);
818 pages
.main
.apiCheckValid();
821 while ( addPages
.length
) {
822 page
= addPages
.shift();
823 if ( bookletPages
[ i
] !== page
) {
824 for ( j
= i
; j
< bookletPages
.length
; j
++ ) {
825 if ( bookletPages
[ j
].getName() === page
.getName() ) {
826 bookletPages
.splice( j
, 1 );
829 bookletPages
.splice( i
, 0, page
);
830 booklet
.addPages( [ page
], i
);
834 if ( page
.getSubpages
) {
835 subpages
= page
.getSubpages();
836 for ( j
= 0; j
< subpages
.length
; j
++ ) {
837 if ( !pages
.hasOwnProperty( subpages
[ j
].key
) ) {
838 subpages
[ j
].indentLevel
= page
.indentLevel
+ 1;
839 pages
[ subpages
[ j
].key
] = new ApiSandbox
.PageLayout( subpages
[ j
] );
841 if ( params
!== undefined ) {
842 pages
[ subpages
[ j
].key
].loadQueryParams( params
);
844 addPages
.splice( j
, 0, pages
[ subpages
[ j
].key
] );
845 pages
[ subpages
[ j
].key
].apiCheckValid();
850 if ( bookletPages
.length
> i
) {
851 removePages
= bookletPages
.splice( i
, bookletPages
.length
- i
);
852 booklet
.removePages( removePages
);
855 if ( !booklet
.getCurrentPageName() ) {
856 booklet
.selectFirstSelectablePage();
859 updatingBooklet
= false;
864 * Reset button handler
866 resetUI: function () {
867 suppressErrors
= true;
869 main
: new ApiSandbox
.PageLayout( { key
: 'main', path
: 'main' } )
872 ApiSandbox
.updateUI();
876 * Submit button handler
878 * @param {Object} [params] Use this set of params instead of those in the form fields.
879 * The form fields will be updated to match.
881 sendRequest: function ( params
) {
882 var page
, subpages
, i
, query
, $result
, $focus
,
883 progress
, $progressText
, progressLoading
,
885 paramsAreForced
= !!params
,
887 checkPages
= [ pages
.main
];
889 // Blur any focused widget before submit, because
890 // OO.ui.ButtonWidget doesn't take focus itself (T128054)
891 $focus
= $( '#mw-apisandbox-ui' ).find( document
.activeElement
);
892 if ( $focus
.length
) {
896 suppressErrors
= false;
898 // save widget state in params (or load from it if we are forced)
899 if ( paramsAreForced
) {
900 ApiSandbox
.updateUI( params
);
903 while ( checkPages
.length
) {
904 page
= checkPages
.shift();
905 deferreds
.push( page
.apiCheckValid() );
906 page
.getQueryParams( params
, displayParams
);
907 subpages
= page
.getSubpages();
908 for ( i
= 0; i
< subpages
.length
; i
++ ) {
909 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
910 checkPages
.push( pages
[ subpages
[ i
].key
] );
915 if ( !paramsAreForced
) {
916 // forced params means we are continuing a query; the base query should be preserved
917 baseRequestParams
= $.extend( {}, params
);
920 $.when
.apply( $, deferreds
).done( function () {
921 if ( $.inArray( false, arguments
) !== -1 ) {
922 windowManager
.openWindow( 'errorAlert', {
923 title
: Util
.parseMsg( 'apisandbox-submit-invalid-fields-title' ),
924 message
: Util
.parseMsg( 'apisandbox-submit-invalid-fields-message' ),
928 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
936 query
= $.param( displayParams
);
938 // Force a 'fm' format with wrappedhtml=1, if available
939 if ( params
.format
!== undefined ) {
940 if ( availableFormats
.hasOwnProperty( params
.format
+ 'fm' ) ) {
941 params
.format
= params
.format
+ 'fm';
943 if ( params
.format
.substr( -2 ) === 'fm' ) {
944 params
.wrappedhtml
= 1;
948 progressLoading
= false;
949 $progressText
= $( '<span>' ).text( mw
.message( 'apisandbox-sending-request' ).text() );
950 progress
= new OO
.ui
.ProgressBarWidget( {
952 $content
: $progressText
955 $result
= $( '<div>' )
956 .append( progress
.$element
);
958 resultPage
= page
= new OO
.ui
.PageLayout( '|results|' );
959 page
.setupOutlineItem = function () {
960 this.outlineItem
.setLabel( mw
.message( 'apisandbox-results' ).text() );
962 page
.$element
.empty()
964 new OO
.ui
.FieldLayout(
965 new OO
.ui
.TextInputWidget( {
967 value
: mw
.util
.wikiScript( 'api' ) + '?' + query
969 label
: Util
.parseMsg( 'apisandbox-request-url-label' )
974 ApiSandbox
.updateUI();
975 booklet
.setPage( '|results|' );
977 location
.href
= oldhash
= '#' + query
;
980 contentType
: 'multipart/form-data',
983 var xhr
= new window
.XMLHttpRequest();
984 xhr
.upload
.addEventListener( 'progress', function ( e
) {
985 if ( !progressLoading
) {
986 if ( e
.lengthComputable
) {
987 progress
.setProgress( e
.loaded
* 100 / e
.total
);
989 progress
.setProgress( false );
993 xhr
.addEventListener( 'progress', function ( e
) {
994 if ( !progressLoading
) {
995 progressLoading
= true;
996 $progressText
.text( mw
.message( 'apisandbox-loading-results' ).text() );
998 if ( e
.lengthComputable
) {
999 progress
.setProgress( e
.loaded
* 100 / e
.total
);
1001 progress
.setProgress( false );
1007 .then( null, function ( code
, data
, result
, jqXHR
) {
1008 if ( code
!== 'http' ) {
1009 // Not really an error, work around mw.Api thinking it is.
1011 .resolve( result
, jqXHR
)
1016 .fail( function ( code
, data
) {
1017 var details
= 'HTTP error: ' + data
.exception
;
1020 new OO
.ui
.LabelWidget( {
1021 label
: mw
.message( 'apisandbox-results-error', details
).text(),
1022 classes
: [ 'error' ]
1026 .done( function ( data
, jqXHR
) {
1027 var m
, loadTime
, button
, clear
,
1028 ct
= jqXHR
.getResponseHeader( 'Content-Type' );
1031 if ( /^text\/mediawiki-api-prettyprint-wrapped(?:;|$)/.test( ct
) ) {
1032 data
= JSON
.parse( data
);
1033 if ( data
.modules
.length
) {
1034 mw
.loader
.load( data
.modules
);
1036 if ( data
.status
&& data
.status
!== 200 ) {
1038 .addClass( 'api-pretty-header api-pretty-status' )
1039 .append( Util
.parseMsg( 'api-format-prettyprint-status', data
.status
, data
.statustext
) )
1040 .appendTo( $result
);
1042 $result
.append( Util
.parseHTML( data
.html
) );
1043 loadTime
= data
.time
;
1044 } else if ( ( m
= data
.match( /<pre[ >][\s\S]*<\/pre>/ ) ) ) {
1045 $result
.append( Util
.parseHTML( m
[ 0 ] ) );
1046 if ( ( m
= data
.match( /"wgBackendResponseTime":\s*(\d+)/ ) ) ) {
1047 loadTime
= parseInt( m
[ 1 ], 10 );
1051 .addClass( 'api-pretty-content' )
1053 .appendTo( $result
);
1055 if ( paramsAreForced
|| data
[ 'continue' ] ) {
1057 $( '<div>' ).append(
1058 new OO
.ui
.ButtonWidget( {
1059 label
: mw
.message( 'apisandbox-continue' ).text()
1060 } ).on( 'click', function () {
1061 ApiSandbox
.sendRequest( $.extend( {}, baseRequestParams
, data
[ 'continue' ] ) );
1062 } ).setDisabled( !data
[ 'continue' ] ).$element
,
1063 ( clear
= new OO
.ui
.ButtonWidget( {
1064 label
: mw
.message( 'apisandbox-continue-clear' ).text()
1065 } ).on( 'click', function () {
1066 ApiSandbox
.updateUI( baseRequestParams
);
1067 clear
.setDisabled( true );
1068 booklet
.setPage( '|results|' );
1069 } ).setDisabled( !paramsAreForced
) ).$element
,
1070 new OO
.ui
.PopupButtonWidget( {
1074 $content
: $( '<div>' ).append( Util
.parseMsg( 'apisandbox-continue-help' ) ),
1081 if ( typeof loadTime
=== 'number' ) {
1083 $( '<div>' ).append(
1084 new OO
.ui
.LabelWidget( {
1085 label
: mw
.message( 'apisandbox-request-time', loadTime
).text()
1091 if ( jqXHR
.getResponseHeader( 'MediaWiki-API-Error' ) === 'badtoken' ) {
1092 // Flush all saved tokens in case one of them is the bad one.
1093 Util
.markTokensBad();
1094 button
= new OO
.ui
.ButtonWidget( {
1095 label
: mw
.message( 'apisandbox-results-fixtoken' ).text()
1097 button
.on( 'click', ApiSandbox
.fixTokenAndResend
)
1098 .on( 'click', button
.setDisabled
, [ true ], button
)
1099 .$element
.appendTo( $result
);
1106 * Handler for the "Correct token and resubmit" button
1108 * Used on a 'badtoken' error, it re-fetches token parameters for all
1109 * pages and then re-submits the query.
1111 fixTokenAndResend: function () {
1112 var page
, subpages
, i
, k
,
1114 tokenWait
= { dummy
: true },
1115 checkPages
= [ pages
.main
],
1116 success = function ( k
) {
1117 delete tokenWait
[ k
];
1118 if ( ok
&& $.isEmptyObject( tokenWait
) ) {
1119 ApiSandbox
.sendRequest();
1122 failure = function ( k
) {
1123 delete tokenWait
[ k
];
1127 while ( checkPages
.length
) {
1128 page
= checkPages
.shift();
1130 if ( page
.tokenWidget
) {
1131 k
= page
.apiModule
+ page
.tokenWidget
.paramInfo
.name
;
1132 tokenWait
[ k
] = page
.tokenWidget
.fetchToken()
1133 .done( success
.bind( page
.tokenWidget
, k
) )
1134 .fail( failure
.bind( page
.tokenWidget
, k
) );
1137 subpages
= page
.getSubpages();
1138 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1139 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1140 checkPages
.push( pages
[ subpages
[ i
].key
] );
1145 success( 'dummy', '' );
1149 * Reset validity indicators for all widgets
1151 updateValidityIndicators: function () {
1152 var page
, subpages
, i
,
1153 checkPages
= [ pages
.main
];
1155 while ( checkPages
.length
) {
1156 page
= checkPages
.shift();
1157 page
.apiCheckValid();
1158 subpages
= page
.getSubpages();
1159 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1160 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1161 checkPages
.push( pages
[ subpages
[ i
].key
] );
1169 * PageLayout for API modules
1173 * @extends OO.ui.PageLayout
1175 * @param {Object} [config] Configuration options
1177 ApiSandbox
.PageLayout = function ( config
) {
1178 config
= $.extend( { prefix
: '' }, config
);
1179 this.displayText
= config
.key
;
1180 this.apiModule
= config
.path
;
1181 this.prefix
= config
.prefix
;
1182 this.paramInfo
= null;
1183 this.apiIsValid
= true;
1184 this.loadFromQueryParams
= null;
1186 this.tokenWidget
= null;
1187 this.indentLevel
= config
.indentLevel
? config
.indentLevel
: 0;
1188 ApiSandbox
.PageLayout
[ 'super' ].call( this, config
.key
, config
);
1189 this.loadParamInfo();
1191 OO
.inheritClass( ApiSandbox
.PageLayout
, OO
.ui
.PageLayout
);
1192 ApiSandbox
.PageLayout
.prototype.setupOutlineItem = function () {
1193 this.outlineItem
.setLevel( this.indentLevel
);
1194 this.outlineItem
.setLabel( this.displayText
);
1195 this.outlineItem
.setIcon( this.apiIsValid
|| suppressErrors
? null : 'alert' );
1196 this.outlineItem
.setIconTitle(
1197 this.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1202 * Fetch module information for this page's module, then create UI
1204 ApiSandbox
.PageLayout
.prototype.loadParamInfo = function () {
1205 var dynamicFieldset
, dynamicParamNameWidget
,
1207 removeDynamicParamWidget = function ( name
, layout
) {
1208 dynamicFieldset
.removeItems( [ layout
] );
1209 delete that
.widgets
[ name
];
1211 addDynamicParamWidget = function () {
1212 var name
, layout
, widget
, button
;
1214 // Check name is filled in
1215 name
= dynamicParamNameWidget
.getValue().trim();
1216 if ( name
=== '' ) {
1217 dynamicParamNameWidget
.focus();
1221 if ( that
.widgets
[ name
] !== undefined ) {
1222 windowManager
.openWindow( 'errorAlert', {
1223 title
: Util
.parseMsg( 'apisandbox-dynamic-error-exists', name
),
1227 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
1235 widget
= Util
.createWidgetForParameter( {
1242 button
= new OO
.ui
.ButtonWidget( {
1244 flags
: 'destructive'
1246 layout
= new OO
.ui
.ActionFieldLayout(
1254 button
.on( 'click', removeDynamicParamWidget
, [ name
, layout
] );
1255 that
.widgets
[ name
] = widget
;
1256 dynamicFieldset
.addItems( [ layout
], dynamicFieldset
.getItems().length
- 1 );
1259 dynamicParamNameWidget
.setValue( '' );
1262 this.$element
.empty()
1263 .append( new OO
.ui
.ProgressBarWidget( {
1265 text
: mw
.message( 'apisandbox-loading', this.displayText
).text()
1268 Util
.fetchModuleInfo( this.apiModule
)
1269 .done( function ( pi
) {
1270 var prefix
, i
, j
, dl
, widget
, $widgetLabel
, widgetField
, helpField
, tmp
, flag
, count
,
1272 deprecatedItems
= [],
1274 filterFmModules = function ( v
) {
1275 return v
.substr( -2 ) !== 'fm' ||
1276 !availableFormats
.hasOwnProperty( v
.substr( 0, v
.length
- 2 ) );
1278 widgetLabelOnClick = function () {
1279 var f
= this.getField();
1280 if ( $.isFunction( f
.setDisabled
) ) {
1281 f
.setDisabled( false );
1283 if ( $.isFunction( f
.focus
) ) {
1287 doNothing = function () {};
1289 // This is something of a hack. We always want the 'format' and
1290 // 'action' parameters from the main module to be specified,
1291 // and for 'format' we also want to simplify the dropdown since
1292 // we always send the 'fm' variant.
1293 if ( that
.apiModule
=== 'main' ) {
1294 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1295 if ( pi
.parameters
[ i
].name
=== 'action' ) {
1296 pi
.parameters
[ i
].required
= true;
1297 delete pi
.parameters
[ i
][ 'default' ];
1299 if ( pi
.parameters
[ i
].name
=== 'format' ) {
1300 tmp
= pi
.parameters
[ i
].type
;
1301 for ( j
= 0; j
< tmp
.length
; j
++ ) {
1302 availableFormats
[ tmp
[ j
] ] = true;
1304 pi
.parameters
[ i
].type
= $.grep( tmp
, filterFmModules
);
1305 pi
.parameters
[ i
][ 'default' ] = 'json';
1306 pi
.parameters
[ i
].required
= true;
1311 // Hide the 'wrappedhtml' parameter on format modules
1312 if ( pi
.group
=== 'format' ) {
1313 pi
.parameters
= $.grep( pi
.parameters
, function ( p
) {
1314 return p
.name
!== 'wrappedhtml';
1318 that
.paramInfo
= pi
;
1320 items
.push( new OO
.ui
.FieldLayout(
1321 new OO
.ui
.Widget( {} ).toggle( false ), {
1323 label
: Util
.parseHTML( pi
.description
)
1327 if ( pi
.helpurls
.length
) {
1328 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1329 label
: mw
.message( 'apisandbox-helpurls' ).text(),
1332 $content
: $( '<ul>' ).append( $.map( pi
.helpurls
, function ( link
) {
1333 return $( '<li>' ).append( $( '<a>', {
1343 if ( pi
.examples
.length
) {
1344 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1345 label
: mw
.message( 'apisandbox-examples' ).text(),
1348 $content
: $( '<ul>' ).append( $.map( pi
.examples
, function ( example
) {
1350 href
: '#' + example
.query
,
1351 html
: example
.description
1353 a
.find( 'a' ).contents().unwrap(); // Can't nest links
1354 return $( '<li>' ).append( a
);
1360 if ( buttons
.length
) {
1361 items
.push( new OO
.ui
.FieldLayout(
1362 new OO
.ui
.ButtonGroupWidget( {
1364 } ), { align
: 'top' }
1368 if ( pi
.parameters
.length
) {
1369 prefix
= that
.prefix
+ pi
.prefix
;
1370 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1371 widget
= Util
.createWidgetForParameter( pi
.parameters
[ i
] );
1372 that
.widgets
[ prefix
+ pi
.parameters
[ i
].name
] = widget
;
1373 if ( pi
.parameters
[ i
].tokentype
) {
1374 that
.tokenWidget
= widget
;
1378 dl
.append( $( '<dd>', {
1379 addClass
: 'description',
1380 append
: Util
.parseHTML( pi
.parameters
[ i
].description
)
1382 if ( pi
.parameters
[ i
].info
&& pi
.parameters
[ i
].info
.length
) {
1383 for ( j
= 0; j
< pi
.parameters
[ i
].info
.length
; j
++ ) {
1384 dl
.append( $( '<dd>', {
1386 append
: Util
.parseHTML( pi
.parameters
[ i
].info
[ j
] )
1392 switch ( pi
.parameters
[ i
].type
) {
1395 count
= mw
.config
.get( 'wgFormattedNamespaces' ).length
;
1399 if ( pi
.parameters
[ i
].highmax
!== undefined ) {
1400 dl
.append( $( '<dd>', {
1404 'api-help-param-limit2', pi
.parameters
[ i
].max
, pi
.parameters
[ i
].highmax
1407 Util
.parseMsg( 'apisandbox-param-limit' )
1411 dl
.append( $( '<dd>', {
1414 Util
.parseMsg( 'api-help-param-limit', pi
.parameters
[ i
].max
),
1416 Util
.parseMsg( 'apisandbox-param-limit' )
1424 if ( pi
.parameters
[ i
].min
!== undefined ) {
1427 if ( pi
.parameters
[ i
].max
!== undefined ) {
1431 dl
.append( $( '<dd>', {
1433 append
: Util
.parseMsg(
1434 'api-help-param-integer-' + tmp
,
1435 Util
.apiBool( pi
.parameters
[ i
].multi
) ? 2 : 1,
1436 pi
.parameters
[ i
].min
, pi
.parameters
[ i
].max
1443 if ( $.isArray( pi
.parameters
[ i
].type
) ) {
1445 count
= pi
.parameters
[ i
].type
.length
;
1449 if ( Util
.apiBool( pi
.parameters
[ i
].multi
) ) {
1451 if ( flag
&& !( widget
instanceof OO
.ui
.CapsuleMultiselectWidget
) &&
1453 widget
instanceof OptionalWidget
&&
1454 widget
.widget
instanceof OO
.ui
.CapsuleMultiselectWidget
1457 tmp
.push( mw
.message( 'api-help-param-multi-separate' ).parse() );
1459 if ( count
> pi
.parameters
[ i
].lowlimit
) {
1461 mw
.message( 'api-help-param-multi-max',
1462 pi
.parameters
[ i
].lowlimit
, pi
.parameters
[ i
].highlimit
1467 dl
.append( $( '<dd>', {
1469 append
: Util
.parseHTML( tmp
.join( ' ' ) )
1473 helpField
= new OO
.ui
.FieldLayout(
1476 classes
: [ 'mw-apisandbox-spacer' ]
1479 classes
: [ 'mw-apisandbox-help-field' ],
1484 $widgetLabel
= $( '<span>' );
1485 widgetField
= new OO
.ui
.FieldLayout(
1489 classes
: [ 'mw-apisandbox-widget-field' ],
1490 label
: prefix
+ pi
.parameters
[ i
].name
,
1491 $label
: $widgetLabel
1495 // FieldLayout only does click for InputElement
1496 // widgets. So supply our own click handler.
1497 $widgetLabel
.on( 'click', widgetLabelOnClick
.bind( widgetField
) );
1499 // Don't grey out the label when the field is disabled,
1500 // it makes it too hard to read and our "disabled"
1501 // isn't really disabled.
1502 widgetField
.onFieldDisable( false );
1503 widgetField
.onFieldDisable
= doNothing
;
1505 if ( Util
.apiBool( pi
.parameters
[ i
].deprecated
) ) {
1506 deprecatedItems
.push( widgetField
, helpField
);
1508 items
.push( widgetField
, helpField
);
1513 if ( !pi
.parameters
.length
&& !Util
.apiBool( pi
.dynamicparameters
) ) {
1514 items
.push( new OO
.ui
.FieldLayout(
1515 new OO
.ui
.Widget( {} ).toggle( false ), {
1517 label
: Util
.parseMsg( 'apisandbox-no-parameters' )
1522 that
.$element
.empty();
1524 new OO
.ui
.FieldsetLayout( {
1525 label
: that
.displayText
1526 } ).addItems( items
)
1527 .$element
.appendTo( that
.$element
);
1529 if ( Util
.apiBool( pi
.dynamicparameters
) ) {
1530 dynamicFieldset
= new OO
.ui
.FieldsetLayout();
1531 dynamicParamNameWidget
= new OO
.ui
.TextInputWidget( {
1532 placeholder
: mw
.message( 'apisandbox-dynamic-parameters-add-placeholder' ).text()
1533 } ).on( 'enter', addDynamicParamWidget
);
1534 dynamicFieldset
.addItems( [
1535 new OO
.ui
.FieldLayout(
1536 new OO
.ui
.Widget( {} ).toggle( false ), {
1538 label
: Util
.parseHTML( pi
.dynamicparameters
)
1541 new OO
.ui
.ActionFieldLayout(
1542 dynamicParamNameWidget
,
1543 new OO
.ui
.ButtonWidget( {
1545 flags
: 'progressive'
1546 } ).on( 'click', addDynamicParamWidget
),
1548 label
: mw
.message( 'apisandbox-dynamic-parameters-add-label' ).text(),
1555 $( '<legend>' ).text( mw
.message( 'apisandbox-dynamic-parameters' ).text() ),
1556 dynamicFieldset
.$element
1558 .appendTo( that
.$element
);
1561 if ( deprecatedItems
.length
) {
1562 tmp
= new OO
.ui
.FieldsetLayout().addItems( deprecatedItems
).toggle( false );
1565 $( '<legend>' ).append(
1566 new OO
.ui
.ToggleButtonWidget( {
1567 label
: mw
.message( 'apisandbox-deprecated-parameters' ).text()
1568 } ).on( 'change', tmp
.toggle
, [], tmp
).$element
1572 .appendTo( that
.$element
);
1575 // Load stored params, if any, then update the booklet if we
1576 // have subpages (or else just update our valid-indicator).
1577 tmp
= that
.loadFromQueryParams
;
1578 that
.loadFromQueryParams
= null;
1579 if ( $.isPlainObject( tmp
) ) {
1580 that
.loadQueryParams( tmp
);
1582 if ( that
.getSubpages().length
> 0 ) {
1583 ApiSandbox
.updateUI( tmp
);
1585 that
.apiCheckValid();
1587 } ).fail( function ( code
, detail
) {
1588 that
.$element
.empty()
1590 new OO
.ui
.LabelWidget( {
1591 label
: mw
.message( 'apisandbox-load-error', that
.apiModule
, detail
).text(),
1592 classes
: [ 'error' ]
1594 new OO
.ui
.ButtonWidget( {
1595 label
: mw
.message( 'apisandbox-retry' ).text()
1596 } ).on( 'click', that
.loadParamInfo
, [], that
).$element
1602 * Check that all widgets on the page are in a valid state.
1606 ApiSandbox
.PageLayout
.prototype.apiCheckValid = function () {
1609 if ( this.paramInfo
=== null ) {
1610 return $.Deferred().resolve( false ).promise();
1612 return $.when
.apply( $, $.map( this.widgets
, function ( widget
) {
1613 return widget
.apiCheckValid();
1614 } ) ).then( function () {
1615 that
.apiIsValid
= $.inArray( false, arguments
) === -1;
1616 if ( that
.getOutlineItem() ) {
1617 that
.getOutlineItem().setIcon( that
.apiIsValid
|| suppressErrors
? null : 'alert' );
1618 that
.getOutlineItem().setIconTitle(
1619 that
.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1622 return $.Deferred().resolve( that
.apiIsValid
).promise();
1628 * Load form fields from query parameters
1630 * @param {Object} params
1632 ApiSandbox
.PageLayout
.prototype.loadQueryParams = function ( params
) {
1633 if ( this.paramInfo
=== null ) {
1634 this.loadFromQueryParams
= params
;
1636 $.each( this.widgets
, function ( name
, widget
) {
1637 var v
= params
.hasOwnProperty( name
) ? params
[ name
] : undefined;
1638 widget
.setApiValue( v
);
1644 * Load query params from form fields
1646 * @param {Object} params Write query parameters into this object
1647 * @param {Object} displayParams Write query parameters for display into this object
1649 ApiSandbox
.PageLayout
.prototype.getQueryParams = function ( params
, displayParams
) {
1650 $.each( this.widgets
, function ( name
, widget
) {
1651 var value
= widget
.getApiValue();
1652 if ( value
!== undefined ) {
1653 params
[ name
] = value
;
1654 if ( $.isFunction( widget
.getApiValueForDisplay
) ) {
1655 value
= widget
.getApiValueForDisplay();
1657 displayParams
[ name
] = value
;
1663 * Fetch a list of subpage names loaded by this page
1667 ApiSandbox
.PageLayout
.prototype.getSubpages = function () {
1669 $.each( this.widgets
, function ( name
, widget
) {
1671 if ( $.isFunction( widget
.getSubmodules
) ) {
1672 submodules
= widget
.getSubmodules();
1673 for ( i
= 0; i
< submodules
.length
; i
++ ) {
1675 key
: name
+ '=' + submodules
[ i
].value
,
1676 path
: submodules
[ i
].path
,
1677 prefix
: widget
.paramInfo
.submoduleparamprefix
|| ''
1686 * A text input with a clickable indicator
1691 * @param {Object} [config] Configuration options
1693 function TextInputWithIndicatorWidget( config
) {
1696 config
= config
|| {};
1697 TextInputWithIndicatorWidget
[ 'super' ].call( this, config
);
1699 this.$indicator
= $( '<span>' ).addClass( 'mw-apisandbox-clickable-indicator' );
1700 OO
.ui
.mixin
.TabIndexedElement
.call(
1701 this, $.extend( {}, config
, { $tabIndexed
: this.$indicator
} )
1704 this.input
= new OO
.ui
.TextInputWidget( $.extend( {
1705 $indicator
: this.$indicator
,
1706 disabled
: this.isDisabled()
1707 }, config
.input
) );
1709 // Forward most methods for convenience
1710 for ( k
in this.input
) {
1711 if ( $.isFunction( this.input
[ k
] ) && !this[ k
] ) {
1712 this[ k
] = this.input
[ k
].bind( this.input
);
1716 this.$indicator
.on( {
1717 click
: this.onIndicatorClick
.bind( this ),
1718 keypress
: this.onIndicatorKeyPress
.bind( this )
1721 this.$element
.append( this.input
.$element
);
1723 OO
.inheritClass( TextInputWithIndicatorWidget
, OO
.ui
.Widget
);
1724 OO
.mixinClass( TextInputWithIndicatorWidget
, OO
.ui
.mixin
.TabIndexedElement
);
1725 TextInputWithIndicatorWidget
.prototype.onIndicatorClick = function ( e
) {
1726 if ( !this.isDisabled() && e
.which
=== 1 ) {
1727 this.emit( 'indicator' );
1731 TextInputWithIndicatorWidget
.prototype.onIndicatorKeyPress = function ( e
) {
1732 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
1733 this.emit( 'indicator' );
1737 TextInputWithIndicatorWidget
.prototype.setDisabled = function ( disabled
) {
1738 TextInputWithIndicatorWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1740 this.input
.setDisabled( this.isDisabled() );
1746 * A wrapper for a widget that provides an enable/disable button
1751 * @param {OO.ui.Widget} widget
1752 * @param {Object} [config] Configuration options
1754 function OptionalWidget( widget
, config
) {
1757 config
= config
|| {};
1759 this.widget
= widget
;
1760 this.$overlay
= config
.$overlay
||
1761 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-overlay' );
1762 this.checkbox
= new OO
.ui
.CheckboxInputWidget( config
.checkbox
)
1763 .on( 'change', this.onCheckboxChange
, [], this );
1765 OptionalWidget
[ 'super' ].call( this, config
);
1767 // Forward most methods for convenience
1768 for ( k
in this.widget
) {
1769 if ( $.isFunction( this.widget
[ k
] ) && !this[ k
] ) {
1770 this[ k
] = this.widget
[ k
].bind( this.widget
);
1774 this.$overlay
.on( 'click', this.onOverlayClick
.bind( this ) );
1777 .addClass( 'mw-apisandbox-optionalWidget' )
1780 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-fields' ).append(
1781 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-widget' ).append(
1784 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-checkbox' ).append(
1785 this.checkbox
.$element
1790 this.setDisabled( widget
.isDisabled() );
1792 OO
.inheritClass( OptionalWidget
, OO
.ui
.Widget
);
1793 OptionalWidget
.prototype.onCheckboxChange = function ( checked
) {
1794 this.setDisabled( !checked
);
1796 OptionalWidget
.prototype.onOverlayClick = function () {
1797 this.setDisabled( false );
1798 if ( $.isFunction( this.widget
.focus
) ) {
1799 this.widget
.focus();
1802 OptionalWidget
.prototype.setDisabled = function ( disabled
) {
1803 OptionalWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1804 this.widget
.setDisabled( this.isDisabled() );
1805 this.checkbox
.setSelected( !this.isDisabled() );
1806 this.$overlay
.toggle( this.isDisabled() );
1810 $( ApiSandbox
.init
);
1812 module
.exports
= ApiSandbox
;
1814 }( jQuery
, mediaWiki
, OO
) );