X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=resources%2Fsrc%2Fmediawiki.special%2Fmediawiki.special.apisandbox.js;h=97659edca352110d291ee9d61929ba5748fbd452;hb=716d331f29587eaa5d86de43f9fe098d360af88e;hp=e9922f81e88220e7159f87f7957a22eaff27a1e6;hpb=c31df03dc137a791fde75f71c183aaa4277092a6;p=lhc%2Fweb%2Fwiklou.git diff --git a/resources/src/mediawiki.special/mediawiki.special.apisandbox.js b/resources/src/mediawiki.special/mediawiki.special.apisandbox.js index e9922f81e8..97659edca3 100644 --- a/resources/src/mediawiki.special/mediawiki.special.apisandbox.js +++ b/resources/src/mediawiki.special/mediawiki.special.apisandbox.js @@ -3,6 +3,7 @@ 'use strict'; var ApiSandbox, Util, WidgetMethods, Validators, $content, panel, booklet, oldhash, windowManager, fullscreenButton, + formatDropdown, api = new mw.Api(), bookletPages = [], availableFormats = {}, @@ -626,6 +627,70 @@ .filter( '[href]:not([target])' ) .attr( 'target', '_blank' ); return $html; + }, + + /** + * Format a request and return a bunch of menu option widgets + * + * @param {Object} displayParams Query parameters, sanitized for display. + * @param {Object} rawParams Query parameters. You should probably use displayParams instead. + * @return {OO.ui.MenuOptionWidget[]} Each item's data should be an OO.ui.FieldLayout + */ + formatRequest: function ( displayParams, rawParams ) { + var jsonInput, + items = [ + new OO.ui.MenuOptionWidget( { + label: Util.parseMsg( 'apisandbox-request-format-url-label' ), + data: new OO.ui.FieldLayout( + new OO.ui.TextInputWidget( { + readOnly: true, + value: mw.util.wikiScript( 'api' ) + '?' + $.param( displayParams ) + } ), { + label: Util.parseMsg( 'apisandbox-request-url-label' ) + } + ) + } ), + new OO.ui.MenuOptionWidget( { + label: Util.parseMsg( 'apisandbox-request-format-json-label' ), + data: new OO.ui.FieldLayout( + jsonInput = new OO.ui.TextInputWidget( { + classes: [ 'mw-apisandbox-textInputCode' ], + readOnly: true, + multiline: true, + autosize: true, + maxRows: 6, + value: JSON.stringify( displayParams, null, '\t' ) + } ), { + label: Util.parseMsg( 'apisandbox-request-json-label' ) + } + ).on( 'toggle', function ( visible ) { + if ( visible ) { + // Call updatePosition instead of adjustSize + // because the latter has weird caching + // behavior and the former bypasses it. + jsonInput.updatePosition(); + } + } ) + } ) + ]; + + mw.hook( 'apisandbox.formatRequest' ).fire( items, displayParams, rawParams ); + + return items; + }, + + /** + * Event handler for when formatDropdown's selection changes + */ + onFormatDropdownChange: function () { + var i, + menu = formatDropdown.getMenu(), + items = menu.getItems(), + selectedField = menu.getSelectedItem() ? menu.getSelectedItem().getData() : null; + + for ( i = 0; i < items.length; i++ ) { + items[ i ].getData().toggle( items[ i ].getData() === selectedField ); + } } }; @@ -918,6 +983,8 @@ } $.when.apply( $, deferreds ).done( function () { + var formatItems, menu, selectedLabel; + if ( $.inArray( false, arguments ) !== -1 ) { windowManager.openWindow( 'errorAlert', { title: Util.parseMsg( 'apisandbox-submit-invalid-fields-title' ), @@ -935,6 +1002,8 @@ query = $.param( displayParams ); + formatItems = Util.formatRequest( displayParams, params ); + // Force a 'fm' format with wrappedhtml=1, if available if ( params.format !== undefined ) { if ( availableFormats.hasOwnProperty( params.format + 'fm' ) ) { @@ -959,16 +1028,35 @@ page.setupOutlineItem = function () { this.outlineItem.setLabel( mw.message( 'apisandbox-results' ).text() ); }; + + if ( !formatDropdown ) { + formatDropdown = new OO.ui.DropdownWidget( { + menu: { items: [] } + } ); + formatDropdown.getMenu().on( 'choose', Util.onFormatDropdownChange ); + } + + menu = formatDropdown.getMenu(); + selectedLabel = menu.getSelectedItem() ? menu.getSelectedItem().getLabel() : ''; + if ( typeof selectedLabel !== 'string' ) { + selectedLabel = selectedLabel.text(); + } + menu.clearItems().addItems( formatItems ); + menu.chooseItem( menu.getItemFromLabel( selectedLabel ) || menu.getFirstSelectableItem() ); + + // Fire the event to update field visibilities + Util.onFormatDropdownChange(); + page.$element.empty() .append( new OO.ui.FieldLayout( - new OO.ui.TextInputWidget( { - readOnly: true, - value: mw.util.wikiScript( 'api' ) + '?' + query - } ), { - label: Util.parseMsg( 'apisandbox-request-url-label' ) + formatDropdown, { + label: Util.parseMsg( 'apisandbox-request-selectformat-label' ) } ).$element, + $.map( formatItems, function ( item ) { + return item.getData().$element; + } ), $result ); ApiSandbox.updateUI(); @@ -1068,6 +1156,7 @@ booklet.setPage( '|results|' ); } ).setDisabled( !paramsAreForced ) ).$element, new OO.ui.PopupButtonWidget( { + $overlay: $( '#mw-apisandbox-ui' ), framed: false, icon: 'info', popup: { @@ -1129,7 +1218,8 @@ if ( page.tokenWidget ) { k = page.apiModule + page.tokenWidget.paramInfo.name; - tokenWait[ k ] = page.tokenWidget.fetchToken() + tokenWait[ k ] = page.tokenWidget.fetchToken(); + tokenWait[ k ] .done( success.bind( page.tokenWidget, k ) ) .fail( failure.bind( page.tokenWidget, k ) ); } @@ -1267,7 +1357,7 @@ Util.fetchModuleInfo( this.apiModule ) .done( function ( pi ) { - var prefix, i, j, dl, widget, $widgetLabel, widgetField, helpField, tmp, flag, count, + var prefix, i, j, descriptionContainer, widget, $widgetLabel, widgetField, helpField, tmp, flag, count, items = [], deprecatedItems = [], buttons = [], @@ -1326,6 +1416,7 @@ if ( pi.helpurls.length ) { buttons.push( new OO.ui.PopupButtonWidget( { + $overlay: $( '#mw-apisandbox-ui' ), label: mw.message( 'apisandbox-helpurls' ).text(), icon: 'help', popup: { @@ -1342,6 +1433,7 @@ if ( pi.examples.length ) { buttons.push( new OO.ui.PopupButtonWidget( { + $overlay: $( '#mw-apisandbox-ui' ), label: mw.message( 'apisandbox-examples' ).text(), icon: 'code', popup: { @@ -1374,14 +1466,14 @@ that.tokenWidget = widget; } - dl = $( '
' ); - dl.append( $( '
', { + descriptionContainer = $( '
' ); + descriptionContainer.append( $( '
', { addClass: 'description', append: Util.parseHTML( pi.parameters[ i ].description ) } ) ); if ( pi.parameters[ i ].info && pi.parameters[ i ].info.length ) { for ( j = 0; j < pi.parameters[ i ].info.length; j++ ) { - dl.append( $( '
', { + descriptionContainer.append( $( '
', { addClass: 'info', append: Util.parseHTML( pi.parameters[ i ].info[ j ] ) } ) ); @@ -1397,7 +1489,7 @@ case 'limit': if ( pi.parameters[ i ].highmax !== undefined ) { - dl.append( $( '
', { + descriptionContainer.append( $( '
', { addClass: 'info', append: [ Util.parseMsg( @@ -1408,7 +1500,7 @@ ] } ) ); } else { - dl.append( $( '
', { + descriptionContainer.append( $( '
', { addClass: 'info', append: [ Util.parseMsg( 'api-help-param-limit', pi.parameters[ i ].max ), @@ -1428,7 +1520,7 @@ tmp += 'max'; } if ( tmp !== '' ) { - dl.append( $( '
', { + descriptionContainer.append( $( '
', { addClass: 'info', append: Util.parseMsg( 'api-help-param-integer-' + tmp, @@ -1464,7 +1556,7 @@ ); } if ( tmp.length ) { - dl.append( $( '
', { + descriptionContainer.append( $( '
', { addClass: 'info', append: Util.parseHTML( tmp.join( ' ' ) ) } ) ); @@ -1477,7 +1569,7 @@ } ), { align: 'inline', classes: [ 'mw-apisandbox-help-field' ], - label: dl + label: descriptionContainer } );