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 () {
923 if ( $.inArray( false, arguments
) !== -1 ) {
924 windowManager
.openWindow( 'errorAlert', {
925 title
: Util
.parseMsg( 'apisandbox-submit-invalid-fields-title' ),
926 message
: Util
.parseMsg( 'apisandbox-submit-invalid-fields-message' ),
930 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
938 query
= $.param( displayParams
);
940 // Force a 'fm' format with wrappedhtml=1, if available
941 if ( params
.format
!== undefined ) {
942 if ( availableFormats
.hasOwnProperty( params
.format
+ 'fm' ) ) {
943 params
.format
= params
.format
+ 'fm';
945 if ( params
.format
.substr( -2 ) === 'fm' ) {
946 params
.wrappedhtml
= 1;
950 progressLoading
= false;
951 $progressText
= $( '<span>' ).text( mw
.message( 'apisandbox-sending-request' ).text() );
952 progress
= new OO
.ui
.ProgressBarWidget( {
954 $content
: $progressText
957 $result
= $( '<div>' )
958 .append( progress
.$element
);
960 resultPage
= page
= new OO
.ui
.PageLayout( '|results|' );
961 page
.setupOutlineItem = function () {
962 this.outlineItem
.setLabel( mw
.message( 'apisandbox-results' ).text() );
964 page
.$element
.empty()
966 new OO
.ui
.FieldLayout(
967 new OO
.ui
.TextInputWidget( {
969 value
: mw
.util
.wikiScript( 'api' ) + '?' + query
971 label
: Util
.parseMsg( 'apisandbox-request-url-label' )
974 new OO
.ui
.FieldLayout(
975 jsonInput
= new OO
.ui
.TextInputWidget( {
976 classes
: [ 'mw-apisandbox-textInputCode' ],
981 value
: JSON
.stringify( displayParams
, null, '\t' )
983 label
: Util
.parseMsg( 'apisandbox-request-params-json' )
988 ApiSandbox
.updateUI();
989 booklet
.setPage( '|results|' );
991 // Resize the multiline input once visible
992 jsonInput
.adjustSize();
994 location
.href
= oldhash
= '#' + query
;
997 contentType
: 'multipart/form-data',
1000 var xhr
= new window
.XMLHttpRequest();
1001 xhr
.upload
.addEventListener( 'progress', function ( e
) {
1002 if ( !progressLoading
) {
1003 if ( e
.lengthComputable
) {
1004 progress
.setProgress( e
.loaded
* 100 / e
.total
);
1006 progress
.setProgress( false );
1010 xhr
.addEventListener( 'progress', function ( e
) {
1011 if ( !progressLoading
) {
1012 progressLoading
= true;
1013 $progressText
.text( mw
.message( 'apisandbox-loading-results' ).text() );
1015 if ( e
.lengthComputable
) {
1016 progress
.setProgress( e
.loaded
* 100 / e
.total
);
1018 progress
.setProgress( false );
1024 .then( null, function ( code
, data
, result
, jqXHR
) {
1025 if ( code
!== 'http' ) {
1026 // Not really an error, work around mw.Api thinking it is.
1028 .resolve( result
, jqXHR
)
1033 .fail( function ( code
, data
) {
1034 var details
= 'HTTP error: ' + data
.exception
;
1037 new OO
.ui
.LabelWidget( {
1038 label
: mw
.message( 'apisandbox-results-error', details
).text(),
1039 classes
: [ 'error' ]
1043 .done( function ( data
, jqXHR
) {
1044 var m
, loadTime
, button
, clear
,
1045 ct
= jqXHR
.getResponseHeader( 'Content-Type' );
1048 if ( /^text\/mediawiki-api-prettyprint-wrapped(?:;|$)/.test( ct
) ) {
1049 data
= JSON
.parse( data
);
1050 if ( data
.modules
.length
) {
1051 mw
.loader
.load( data
.modules
);
1053 if ( data
.status
&& data
.status
!== 200 ) {
1055 .addClass( 'api-pretty-header api-pretty-status' )
1056 .append( Util
.parseMsg( 'api-format-prettyprint-status', data
.status
, data
.statustext
) )
1057 .appendTo( $result
);
1059 $result
.append( Util
.parseHTML( data
.html
) );
1060 loadTime
= data
.time
;
1061 } else if ( ( m
= data
.match( /<pre[ >][\s\S]*<\/pre>/ ) ) ) {
1062 $result
.append( Util
.parseHTML( m
[ 0 ] ) );
1063 if ( ( m
= data
.match( /"wgBackendResponseTime":\s*(\d+)/ ) ) ) {
1064 loadTime
= parseInt( m
[ 1 ], 10 );
1068 .addClass( 'api-pretty-content' )
1070 .appendTo( $result
);
1072 if ( paramsAreForced
|| data
[ 'continue' ] ) {
1074 $( '<div>' ).append(
1075 new OO
.ui
.ButtonWidget( {
1076 label
: mw
.message( 'apisandbox-continue' ).text()
1077 } ).on( 'click', function () {
1078 ApiSandbox
.sendRequest( $.extend( {}, baseRequestParams
, data
[ 'continue' ] ) );
1079 } ).setDisabled( !data
[ 'continue' ] ).$element
,
1080 ( clear
= new OO
.ui
.ButtonWidget( {
1081 label
: mw
.message( 'apisandbox-continue-clear' ).text()
1082 } ).on( 'click', function () {
1083 ApiSandbox
.updateUI( baseRequestParams
);
1084 clear
.setDisabled( true );
1085 booklet
.setPage( '|results|' );
1086 } ).setDisabled( !paramsAreForced
) ).$element
,
1087 new OO
.ui
.PopupButtonWidget( {
1091 $content
: $( '<div>' ).append( Util
.parseMsg( 'apisandbox-continue-help' ) ),
1098 if ( typeof loadTime
=== 'number' ) {
1100 $( '<div>' ).append(
1101 new OO
.ui
.LabelWidget( {
1102 label
: mw
.message( 'apisandbox-request-time', loadTime
).text()
1108 if ( jqXHR
.getResponseHeader( 'MediaWiki-API-Error' ) === 'badtoken' ) {
1109 // Flush all saved tokens in case one of them is the bad one.
1110 Util
.markTokensBad();
1111 button
= new OO
.ui
.ButtonWidget( {
1112 label
: mw
.message( 'apisandbox-results-fixtoken' ).text()
1114 button
.on( 'click', ApiSandbox
.fixTokenAndResend
)
1115 .on( 'click', button
.setDisabled
, [ true ], button
)
1116 .$element
.appendTo( $result
);
1123 * Handler for the "Correct token and resubmit" button
1125 * Used on a 'badtoken' error, it re-fetches token parameters for all
1126 * pages and then re-submits the query.
1128 fixTokenAndResend: function () {
1129 var page
, subpages
, i
, k
,
1131 tokenWait
= { dummy
: true },
1132 checkPages
= [ pages
.main
],
1133 success = function ( k
) {
1134 delete tokenWait
[ k
];
1135 if ( ok
&& $.isEmptyObject( tokenWait
) ) {
1136 ApiSandbox
.sendRequest();
1139 failure = function ( k
) {
1140 delete tokenWait
[ k
];
1144 while ( checkPages
.length
) {
1145 page
= checkPages
.shift();
1147 if ( page
.tokenWidget
) {
1148 k
= page
.apiModule
+ page
.tokenWidget
.paramInfo
.name
;
1149 tokenWait
[ k
] = page
.tokenWidget
.fetchToken()
1150 .done( success
.bind( page
.tokenWidget
, k
) )
1151 .fail( failure
.bind( page
.tokenWidget
, k
) );
1154 subpages
= page
.getSubpages();
1155 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1156 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1157 checkPages
.push( pages
[ subpages
[ i
].key
] );
1162 success( 'dummy', '' );
1166 * Reset validity indicators for all widgets
1168 updateValidityIndicators: function () {
1169 var page
, subpages
, i
,
1170 checkPages
= [ pages
.main
];
1172 while ( checkPages
.length
) {
1173 page
= checkPages
.shift();
1174 page
.apiCheckValid();
1175 subpages
= page
.getSubpages();
1176 for ( i
= 0; i
< subpages
.length
; i
++ ) {
1177 if ( pages
.hasOwnProperty( subpages
[ i
].key
) ) {
1178 checkPages
.push( pages
[ subpages
[ i
].key
] );
1186 * PageLayout for API modules
1190 * @extends OO.ui.PageLayout
1192 * @param {Object} [config] Configuration options
1194 ApiSandbox
.PageLayout = function ( config
) {
1195 config
= $.extend( { prefix
: '' }, config
);
1196 this.displayText
= config
.key
;
1197 this.apiModule
= config
.path
;
1198 this.prefix
= config
.prefix
;
1199 this.paramInfo
= null;
1200 this.apiIsValid
= true;
1201 this.loadFromQueryParams
= null;
1203 this.tokenWidget
= null;
1204 this.indentLevel
= config
.indentLevel
? config
.indentLevel
: 0;
1205 ApiSandbox
.PageLayout
[ 'super' ].call( this, config
.key
, config
);
1206 this.loadParamInfo();
1208 OO
.inheritClass( ApiSandbox
.PageLayout
, OO
.ui
.PageLayout
);
1209 ApiSandbox
.PageLayout
.prototype.setupOutlineItem = function () {
1210 this.outlineItem
.setLevel( this.indentLevel
);
1211 this.outlineItem
.setLabel( this.displayText
);
1212 this.outlineItem
.setIcon( this.apiIsValid
|| suppressErrors
? null : 'alert' );
1213 this.outlineItem
.setIconTitle(
1214 this.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1219 * Fetch module information for this page's module, then create UI
1221 ApiSandbox
.PageLayout
.prototype.loadParamInfo = function () {
1222 var dynamicFieldset
, dynamicParamNameWidget
,
1224 removeDynamicParamWidget = function ( name
, layout
) {
1225 dynamicFieldset
.removeItems( [ layout
] );
1226 delete that
.widgets
[ name
];
1228 addDynamicParamWidget = function () {
1229 var name
, layout
, widget
, button
;
1231 // Check name is filled in
1232 name
= dynamicParamNameWidget
.getValue().trim();
1233 if ( name
=== '' ) {
1234 dynamicParamNameWidget
.focus();
1238 if ( that
.widgets
[ name
] !== undefined ) {
1239 windowManager
.openWindow( 'errorAlert', {
1240 title
: Util
.parseMsg( 'apisandbox-dynamic-error-exists', name
),
1244 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' ),
1252 widget
= Util
.createWidgetForParameter( {
1259 button
= new OO
.ui
.ButtonWidget( {
1261 flags
: 'destructive'
1263 layout
= new OO
.ui
.ActionFieldLayout(
1271 button
.on( 'click', removeDynamicParamWidget
, [ name
, layout
] );
1272 that
.widgets
[ name
] = widget
;
1273 dynamicFieldset
.addItems( [ layout
], dynamicFieldset
.getItems().length
- 1 );
1276 dynamicParamNameWidget
.setValue( '' );
1279 this.$element
.empty()
1280 .append( new OO
.ui
.ProgressBarWidget( {
1282 text
: mw
.message( 'apisandbox-loading', this.displayText
).text()
1285 Util
.fetchModuleInfo( this.apiModule
)
1286 .done( function ( pi
) {
1287 var prefix
, i
, j
, dl
, widget
, $widgetLabel
, widgetField
, helpField
, tmp
, flag
, count
,
1289 deprecatedItems
= [],
1291 filterFmModules = function ( v
) {
1292 return v
.substr( -2 ) !== 'fm' ||
1293 !availableFormats
.hasOwnProperty( v
.substr( 0, v
.length
- 2 ) );
1295 widgetLabelOnClick = function () {
1296 var f
= this.getField();
1297 if ( $.isFunction( f
.setDisabled
) ) {
1298 f
.setDisabled( false );
1300 if ( $.isFunction( f
.focus
) ) {
1304 doNothing = function () {};
1306 // This is something of a hack. We always want the 'format' and
1307 // 'action' parameters from the main module to be specified,
1308 // and for 'format' we also want to simplify the dropdown since
1309 // we always send the 'fm' variant.
1310 if ( that
.apiModule
=== 'main' ) {
1311 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1312 if ( pi
.parameters
[ i
].name
=== 'action' ) {
1313 pi
.parameters
[ i
].required
= true;
1314 delete pi
.parameters
[ i
][ 'default' ];
1316 if ( pi
.parameters
[ i
].name
=== 'format' ) {
1317 tmp
= pi
.parameters
[ i
].type
;
1318 for ( j
= 0; j
< tmp
.length
; j
++ ) {
1319 availableFormats
[ tmp
[ j
] ] = true;
1321 pi
.parameters
[ i
].type
= $.grep( tmp
, filterFmModules
);
1322 pi
.parameters
[ i
][ 'default' ] = 'json';
1323 pi
.parameters
[ i
].required
= true;
1328 // Hide the 'wrappedhtml' parameter on format modules
1329 if ( pi
.group
=== 'format' ) {
1330 pi
.parameters
= $.grep( pi
.parameters
, function ( p
) {
1331 return p
.name
!== 'wrappedhtml';
1335 that
.paramInfo
= pi
;
1337 items
.push( new OO
.ui
.FieldLayout(
1338 new OO
.ui
.Widget( {} ).toggle( false ), {
1340 label
: Util
.parseHTML( pi
.description
)
1344 if ( pi
.helpurls
.length
) {
1345 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1346 label
: mw
.message( 'apisandbox-helpurls' ).text(),
1349 $content
: $( '<ul>' ).append( $.map( pi
.helpurls
, function ( link
) {
1350 return $( '<li>' ).append( $( '<a>', {
1360 if ( pi
.examples
.length
) {
1361 buttons
.push( new OO
.ui
.PopupButtonWidget( {
1362 label
: mw
.message( 'apisandbox-examples' ).text(),
1365 $content
: $( '<ul>' ).append( $.map( pi
.examples
, function ( example
) {
1367 href
: '#' + example
.query
,
1368 html
: example
.description
1370 a
.find( 'a' ).contents().unwrap(); // Can't nest links
1371 return $( '<li>' ).append( a
);
1377 if ( buttons
.length
) {
1378 items
.push( new OO
.ui
.FieldLayout(
1379 new OO
.ui
.ButtonGroupWidget( {
1381 } ), { align
: 'top' }
1385 if ( pi
.parameters
.length
) {
1386 prefix
= that
.prefix
+ pi
.prefix
;
1387 for ( i
= 0; i
< pi
.parameters
.length
; i
++ ) {
1388 widget
= Util
.createWidgetForParameter( pi
.parameters
[ i
] );
1389 that
.widgets
[ prefix
+ pi
.parameters
[ i
].name
] = widget
;
1390 if ( pi
.parameters
[ i
].tokentype
) {
1391 that
.tokenWidget
= widget
;
1395 dl
.append( $( '<dd>', {
1396 addClass
: 'description',
1397 append
: Util
.parseHTML( pi
.parameters
[ i
].description
)
1399 if ( pi
.parameters
[ i
].info
&& pi
.parameters
[ i
].info
.length
) {
1400 for ( j
= 0; j
< pi
.parameters
[ i
].info
.length
; j
++ ) {
1401 dl
.append( $( '<dd>', {
1403 append
: Util
.parseHTML( pi
.parameters
[ i
].info
[ j
] )
1409 switch ( pi
.parameters
[ i
].type
) {
1412 count
= mw
.config
.get( 'wgFormattedNamespaces' ).length
;
1416 if ( pi
.parameters
[ i
].highmax
!== undefined ) {
1417 dl
.append( $( '<dd>', {
1421 'api-help-param-limit2', pi
.parameters
[ i
].max
, pi
.parameters
[ i
].highmax
1424 Util
.parseMsg( 'apisandbox-param-limit' )
1428 dl
.append( $( '<dd>', {
1431 Util
.parseMsg( 'api-help-param-limit', pi
.parameters
[ i
].max
),
1433 Util
.parseMsg( 'apisandbox-param-limit' )
1441 if ( pi
.parameters
[ i
].min
!== undefined ) {
1444 if ( pi
.parameters
[ i
].max
!== undefined ) {
1448 dl
.append( $( '<dd>', {
1450 append
: Util
.parseMsg(
1451 'api-help-param-integer-' + tmp
,
1452 Util
.apiBool( pi
.parameters
[ i
].multi
) ? 2 : 1,
1453 pi
.parameters
[ i
].min
, pi
.parameters
[ i
].max
1460 if ( $.isArray( pi
.parameters
[ i
].type
) ) {
1462 count
= pi
.parameters
[ i
].type
.length
;
1466 if ( Util
.apiBool( pi
.parameters
[ i
].multi
) ) {
1468 if ( flag
&& !( widget
instanceof OO
.ui
.CapsuleMultiselectWidget
) &&
1470 widget
instanceof OptionalWidget
&&
1471 widget
.widget
instanceof OO
.ui
.CapsuleMultiselectWidget
1474 tmp
.push( mw
.message( 'api-help-param-multi-separate' ).parse() );
1476 if ( count
> pi
.parameters
[ i
].lowlimit
) {
1478 mw
.message( 'api-help-param-multi-max',
1479 pi
.parameters
[ i
].lowlimit
, pi
.parameters
[ i
].highlimit
1484 dl
.append( $( '<dd>', {
1486 append
: Util
.parseHTML( tmp
.join( ' ' ) )
1490 helpField
= new OO
.ui
.FieldLayout(
1493 classes
: [ 'mw-apisandbox-spacer' ]
1496 classes
: [ 'mw-apisandbox-help-field' ],
1501 $widgetLabel
= $( '<span>' );
1502 widgetField
= new OO
.ui
.FieldLayout(
1506 classes
: [ 'mw-apisandbox-widget-field' ],
1507 label
: prefix
+ pi
.parameters
[ i
].name
,
1508 $label
: $widgetLabel
1512 // FieldLayout only does click for InputElement
1513 // widgets. So supply our own click handler.
1514 $widgetLabel
.on( 'click', widgetLabelOnClick
.bind( widgetField
) );
1516 // Don't grey out the label when the field is disabled,
1517 // it makes it too hard to read and our "disabled"
1518 // isn't really disabled.
1519 widgetField
.onFieldDisable( false );
1520 widgetField
.onFieldDisable
= doNothing
;
1522 if ( Util
.apiBool( pi
.parameters
[ i
].deprecated
) ) {
1523 deprecatedItems
.push( widgetField
, helpField
);
1525 items
.push( widgetField
, helpField
);
1530 if ( !pi
.parameters
.length
&& !Util
.apiBool( pi
.dynamicparameters
) ) {
1531 items
.push( new OO
.ui
.FieldLayout(
1532 new OO
.ui
.Widget( {} ).toggle( false ), {
1534 label
: Util
.parseMsg( 'apisandbox-no-parameters' )
1539 that
.$element
.empty();
1541 new OO
.ui
.FieldsetLayout( {
1542 label
: that
.displayText
1543 } ).addItems( items
)
1544 .$element
.appendTo( that
.$element
);
1546 if ( Util
.apiBool( pi
.dynamicparameters
) ) {
1547 dynamicFieldset
= new OO
.ui
.FieldsetLayout();
1548 dynamicParamNameWidget
= new OO
.ui
.TextInputWidget( {
1549 placeholder
: mw
.message( 'apisandbox-dynamic-parameters-add-placeholder' ).text()
1550 } ).on( 'enter', addDynamicParamWidget
);
1551 dynamicFieldset
.addItems( [
1552 new OO
.ui
.FieldLayout(
1553 new OO
.ui
.Widget( {} ).toggle( false ), {
1555 label
: Util
.parseHTML( pi
.dynamicparameters
)
1558 new OO
.ui
.ActionFieldLayout(
1559 dynamicParamNameWidget
,
1560 new OO
.ui
.ButtonWidget( {
1562 flags
: 'progressive'
1563 } ).on( 'click', addDynamicParamWidget
),
1565 label
: mw
.message( 'apisandbox-dynamic-parameters-add-label' ).text(),
1572 $( '<legend>' ).text( mw
.message( 'apisandbox-dynamic-parameters' ).text() ),
1573 dynamicFieldset
.$element
1575 .appendTo( that
.$element
);
1578 if ( deprecatedItems
.length
) {
1579 tmp
= new OO
.ui
.FieldsetLayout().addItems( deprecatedItems
).toggle( false );
1582 $( '<legend>' ).append(
1583 new OO
.ui
.ToggleButtonWidget( {
1584 label
: mw
.message( 'apisandbox-deprecated-parameters' ).text()
1585 } ).on( 'change', tmp
.toggle
, [], tmp
).$element
1589 .appendTo( that
.$element
);
1592 // Load stored params, if any, then update the booklet if we
1593 // have subpages (or else just update our valid-indicator).
1594 tmp
= that
.loadFromQueryParams
;
1595 that
.loadFromQueryParams
= null;
1596 if ( $.isPlainObject( tmp
) ) {
1597 that
.loadQueryParams( tmp
);
1599 if ( that
.getSubpages().length
> 0 ) {
1600 ApiSandbox
.updateUI( tmp
);
1602 that
.apiCheckValid();
1604 } ).fail( function ( code
, detail
) {
1605 that
.$element
.empty()
1607 new OO
.ui
.LabelWidget( {
1608 label
: mw
.message( 'apisandbox-load-error', that
.apiModule
, detail
).text(),
1609 classes
: [ 'error' ]
1611 new OO
.ui
.ButtonWidget( {
1612 label
: mw
.message( 'apisandbox-retry' ).text()
1613 } ).on( 'click', that
.loadParamInfo
, [], that
).$element
1619 * Check that all widgets on the page are in a valid state.
1623 ApiSandbox
.PageLayout
.prototype.apiCheckValid = function () {
1626 if ( this.paramInfo
=== null ) {
1627 return $.Deferred().resolve( false ).promise();
1629 return $.when
.apply( $, $.map( this.widgets
, function ( widget
) {
1630 return widget
.apiCheckValid();
1631 } ) ).then( function () {
1632 that
.apiIsValid
= $.inArray( false, arguments
) === -1;
1633 if ( that
.getOutlineItem() ) {
1634 that
.getOutlineItem().setIcon( that
.apiIsValid
|| suppressErrors
? null : 'alert' );
1635 that
.getOutlineItem().setIconTitle(
1636 that
.apiIsValid
|| suppressErrors
? '' : mw
.message( 'apisandbox-alert-page' ).plain()
1639 return $.Deferred().resolve( that
.apiIsValid
).promise();
1645 * Load form fields from query parameters
1647 * @param {Object} params
1649 ApiSandbox
.PageLayout
.prototype.loadQueryParams = function ( params
) {
1650 if ( this.paramInfo
=== null ) {
1651 this.loadFromQueryParams
= params
;
1653 $.each( this.widgets
, function ( name
, widget
) {
1654 var v
= params
.hasOwnProperty( name
) ? params
[ name
] : undefined;
1655 widget
.setApiValue( v
);
1661 * Load query params from form fields
1663 * @param {Object} params Write query parameters into this object
1664 * @param {Object} displayParams Write query parameters for display into this object
1666 ApiSandbox
.PageLayout
.prototype.getQueryParams = function ( params
, displayParams
) {
1667 $.each( this.widgets
, function ( name
, widget
) {
1668 var value
= widget
.getApiValue();
1669 if ( value
!== undefined ) {
1670 params
[ name
] = value
;
1671 if ( $.isFunction( widget
.getApiValueForDisplay
) ) {
1672 value
= widget
.getApiValueForDisplay();
1674 displayParams
[ name
] = value
;
1680 * Fetch a list of subpage names loaded by this page
1684 ApiSandbox
.PageLayout
.prototype.getSubpages = function () {
1686 $.each( this.widgets
, function ( name
, widget
) {
1688 if ( $.isFunction( widget
.getSubmodules
) ) {
1689 submodules
= widget
.getSubmodules();
1690 for ( i
= 0; i
< submodules
.length
; i
++ ) {
1692 key
: name
+ '=' + submodules
[ i
].value
,
1693 path
: submodules
[ i
].path
,
1694 prefix
: widget
.paramInfo
.submoduleparamprefix
|| ''
1703 * A text input with a clickable indicator
1708 * @param {Object} [config] Configuration options
1710 function TextInputWithIndicatorWidget( config
) {
1713 config
= config
|| {};
1714 TextInputWithIndicatorWidget
[ 'super' ].call( this, config
);
1716 this.$indicator
= $( '<span>' ).addClass( 'mw-apisandbox-clickable-indicator' );
1717 OO
.ui
.mixin
.TabIndexedElement
.call(
1718 this, $.extend( {}, config
, { $tabIndexed
: this.$indicator
} )
1721 this.input
= new OO
.ui
.TextInputWidget( $.extend( {
1722 $indicator
: this.$indicator
,
1723 disabled
: this.isDisabled()
1724 }, config
.input
) );
1726 // Forward most methods for convenience
1727 for ( k
in this.input
) {
1728 if ( $.isFunction( this.input
[ k
] ) && !this[ k
] ) {
1729 this[ k
] = this.input
[ k
].bind( this.input
);
1733 this.$indicator
.on( {
1734 click
: this.onIndicatorClick
.bind( this ),
1735 keypress
: this.onIndicatorKeyPress
.bind( this )
1738 this.$element
.append( this.input
.$element
);
1740 OO
.inheritClass( TextInputWithIndicatorWidget
, OO
.ui
.Widget
);
1741 OO
.mixinClass( TextInputWithIndicatorWidget
, OO
.ui
.mixin
.TabIndexedElement
);
1742 TextInputWithIndicatorWidget
.prototype.onIndicatorClick = function ( e
) {
1743 if ( !this.isDisabled() && e
.which
=== 1 ) {
1744 this.emit( 'indicator' );
1748 TextInputWithIndicatorWidget
.prototype.onIndicatorKeyPress = function ( e
) {
1749 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
1750 this.emit( 'indicator' );
1754 TextInputWithIndicatorWidget
.prototype.setDisabled = function ( disabled
) {
1755 TextInputWithIndicatorWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1757 this.input
.setDisabled( this.isDisabled() );
1763 * A wrapper for a widget that provides an enable/disable button
1768 * @param {OO.ui.Widget} widget
1769 * @param {Object} [config] Configuration options
1771 function OptionalWidget( widget
, config
) {
1774 config
= config
|| {};
1776 this.widget
= widget
;
1777 this.$overlay
= config
.$overlay
||
1778 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-overlay' );
1779 this.checkbox
= new OO
.ui
.CheckboxInputWidget( config
.checkbox
)
1780 .on( 'change', this.onCheckboxChange
, [], this );
1782 OptionalWidget
[ 'super' ].call( this, config
);
1784 // Forward most methods for convenience
1785 for ( k
in this.widget
) {
1786 if ( $.isFunction( this.widget
[ k
] ) && !this[ k
] ) {
1787 this[ k
] = this.widget
[ k
].bind( this.widget
);
1791 this.$overlay
.on( 'click', this.onOverlayClick
.bind( this ) );
1794 .addClass( 'mw-apisandbox-optionalWidget' )
1797 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-fields' ).append(
1798 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-widget' ).append(
1801 $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-checkbox' ).append(
1802 this.checkbox
.$element
1807 this.setDisabled( widget
.isDisabled() );
1809 OO
.inheritClass( OptionalWidget
, OO
.ui
.Widget
);
1810 OptionalWidget
.prototype.onCheckboxChange = function ( checked
) {
1811 this.setDisabled( !checked
);
1813 OptionalWidget
.prototype.onOverlayClick = function () {
1814 this.setDisabled( false );
1815 if ( $.isFunction( this.widget
.focus
) ) {
1816 this.widget
.focus();
1819 OptionalWidget
.prototype.setDisabled = function ( disabled
) {
1820 OptionalWidget
[ 'super' ].prototype.setDisabled
.call( this, disabled
);
1821 this.widget
.setDisabled( this.isDisabled() );
1822 this.checkbox
.setSelected( !this.isDisabled() );
1823 this.$overlay
.toggle( this.isDisabled() );
1827 $( ApiSandbox
.init
);
1829 module
.exports
= ApiSandbox
;
1831 }( jQuery
, mediaWiki
, OO
) );