X-Git-Url: https://git.cyclocoop.org/?a=blobdiff_plain;f=resources%2Fsrc%2Fmediawiki.special%2Fmediawiki.special.apisandbox.js;h=217bd92358d415aee541d99fefb6b826a7fe4245;hb=2231d4e859628fc4d2fb63a7a752ead5b91ffef2;hp=5c3715db02112883c65d67da366bb98d4e2d3945;hpb=dc36560cdf22db860133ecb58db1898e285f25ea;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 5c3715db02..217bd92358 100644 --- a/resources/src/mediawiki.special/mediawiki.special.apisandbox.js +++ b/resources/src/mediawiki.special/mediawiki.special.apisandbox.js @@ -1,4 +1,4 @@ -/*global OO */ +/* eslint-disable no-use-before-define */ ( function ( $, mw, OO ) { 'use strict'; var ApiSandbox, Util, WidgetMethods, Validators, @@ -10,7 +10,8 @@ suppressErrors = true, updatingBooklet = false, pages = {}, - moduleInfoCache = {}; + moduleInfoCache = {}, + baseRequestParams; WidgetMethods = { textInputWidget: { @@ -25,7 +26,11 @@ }, apiCheckValid: function () { var that = this; - return this.isValid().done( function ( ok ) { + return this.getValidity().then( function () { + return $.Deferred().resolve( true ).promise(); + }, function () { + return $.Deferred().resolve( false ).promise(); + } ).done( function ( ok ) { ok = ok || suppressErrors; that.setIcon( ok ? null : 'alert' ); that.setIconTitle( ok ? '' : mw.message( 'apisandbox-alert-field' ).plain() ); @@ -34,9 +39,12 @@ }, dateTimeInputWidget: { - isValid: function () { - var ok = !Util.apiBool( this.paramInfo.required ) || this.getApiValue() !== ''; - return $.Deferred().resolve( ok ).promise(); + getValidity: function () { + if ( !Util.apiBool( this.paramInfo.required ) || this.getApiValue() !== '' ) { + return $.Deferred().resolve().promise(); + } else { + return $.Deferred().reject().promise(); + } } }, @@ -371,7 +379,7 @@ } ); widget.setIcon = widget.input.setIcon.bind( widget.input ); widget.setIconTitle = widget.input.setIconTitle.bind( widget.input ); - widget.isValid = widget.input.isValid.bind( widget.input ); + widget.getValidity = widget.input.getValidity.bind( widget.input ); widget.paramInfo = pi; $.extend( widget, WidgetMethods.textInputWidget ); if ( Util.apiBool( pi.enforcerange ) ) { @@ -381,19 +389,26 @@ break; case 'limit': - widget = new OO.ui.NumberInputWidget( { - required: Util.apiBool( pi.required ), - isInteger: true + widget = new OO.ui.TextInputWidget( { + required: Util.apiBool( pi.required ) } ); - widget.setIcon = widget.input.setIcon.bind( widget.input ); - widget.setIconTitle = widget.input.setIconTitle.bind( widget.input ); - widget.isValid = widget.input.isValid.bind( widget.input ); - widget.input.setValidation( function ( value ) { - return value === 'max' || widget.validateNumber( value ); + widget.setValidation( function ( value ) { + var n, pi = this.paramInfo; + + if ( value === 'max' ) { + return true; + } else { + n = +value; + return !isNaN( n ) && isFinite( n ) && + // eslint-disable-next-line no-bitwise + ( n | 0 ) === n && + n >= pi.min && n <= pi.apiSandboxMax; + } } ); + pi.min = pi.min || 0; + pi.apiSandboxMax = mw.config.get( 'apihighlimits' ) ? pi.highmax : pi.max; widget.paramInfo = pi; $.extend( widget, WidgetMethods.textInputWidget ); - widget.setRange( pi.min || 0, mw.config.get( 'apihighlimits' ) ? pi.highmax : pi.max ); multiMode = 'enter'; break; @@ -580,6 +595,8 @@ init: function () { var $toolbar; + ApiSandbox.isFullscreen = false; + $content = $( '#mw-apisandbox' ); windowManager = new OO.ui.WindowManager(); @@ -599,7 +616,7 @@ fullscreenButton.$element, new OO.ui.ButtonWidget( { label: mw.message( 'apisandbox-submit' ).text(), - flags: [ 'primary', 'constructive' ] + flags: [ 'primary', 'progressive' ] } ).on( 'click', ApiSandbox.sendRequest ).$element, new OO.ui.ButtonWidget( { label: mw.message( 'apisandbox-reset' ).text(), @@ -658,17 +675,21 @@ * Toggle "fullscreen" mode */ toggleFullscreen: function () { - var $body = $( document.body ); + var $body = $( document.body ), + $ui = $( '#mw-apisandbox-ui' ); - $body.toggleClass( 'mw-apisandbox-fullscreen' ); - if ( $body.hasClass( 'mw-apisandbox-fullscreen' ) ) { + ApiSandbox.isFullscreen = !ApiSandbox.isFullscreen; + + $body.toggleClass( 'mw-apisandbox-fullscreen', ApiSandbox.isFullscreen ); + $ui.toggleClass( 'mw-body-content', ApiSandbox.isFullscreen ); + if ( ApiSandbox.isFullscreen ) { fullscreenButton.setLabel( mw.message( 'apisandbox-unfullscreen' ).text() ); fullscreenButton.setTitle( mw.message( 'apisandbox-unfullscreen-tooltip' ).text() ); - $body.append( $( '#mw-apisandbox-ui' ) ); + $body.append( $ui ); } else { fullscreenButton.setLabel( mw.message( 'apisandbox-fullscreen' ).text() ); fullscreenButton.setTitle( mw.message( 'apisandbox-fullscreen-tooltip' ).text() ); - $content.append( $( '#mw-apisandbox-ui' ) ); + $content.append( $ui ); } ApiSandbox.resizePanel(); }, @@ -680,7 +701,7 @@ var height = $( window ).height(), contentTop = $content.offset().top; - if ( $( document.body ).hasClass( 'mw-apisandbox-fullscreen' ) ) { + if ( ApiSandbox.isFullscreen ) { height -= panel.$element.offset().top - $( '#mw-apisandbox-ui' ).offset().top; panel.$element.height( height - 1 ); } else { @@ -694,6 +715,8 @@ /** * Update the current query when the page hash changes + * + * @return {boolean} Successful */ loadFromHash: function () { var params, m, re, @@ -803,12 +826,15 @@ /** * Submit button handler + * + * @param {Object} [params] Use this set of params instead of those in the form fields. + * The form fields will be updated to match. */ - sendRequest: function () { + sendRequest: function ( params ) { var page, subpages, i, query, $result, $focus, progress, $progressText, progressLoading, deferreds = [], - params = {}, + paramsAreForced = !!params, displayParams = {}, checkPages = [ pages.main ]; @@ -821,6 +847,11 @@ suppressErrors = false; + // save widget state in params (or load from it if we are forced) + if ( paramsAreForced ) { + ApiSandbox.updateUI( params ); + } + params = {}; while ( checkPages.length ) { page = checkPages.shift(); deferreds.push( page.apiCheckValid() ); @@ -833,6 +864,11 @@ } } + if ( !paramsAreForced ) { + // forced params means we are continuing a query; the base query should be preserved + baseRequestParams = $.extend( {}, params ); + } + $.when.apply( $, deferreds ).done( function () { if ( $.inArray( false, arguments ) !== -1 ) { windowManager.openWindow( 'errorAlert', { @@ -940,7 +976,7 @@ ); } ) .done( function ( data, jqXHR ) { - var m, loadTime, button, + var m, loadTime, button, clear, ct = jqXHR.getResponseHeader( 'Content-Type' ); $result.empty(); @@ -949,6 +985,14 @@ if ( data.modules.length ) { mw.loader.load( data.modules ); } + if ( data.status && data.status !== 200 ) { + $( '
' ) + .addClass( 'api-pretty-header api-pretty-status' ) + .append( + mw.message( 'api-format-prettyprint-status', data.status, data.statustext ).parse() + ) + .appendTo( $result ); + } $result.append( Util.parseHTML( data.html ) ); loadTime = data.time; } else if ( ( m = data.match( /][\s\S]*<\/pre>/ ) ) ) { @@ -962,6 +1006,32 @@ .text( data ) .appendTo( $result ); } + if ( paramsAreForced || data[ 'continue' ] ) { + $result.append( + $( '
' ).append( + new OO.ui.ButtonWidget( { + label: mw.message( 'apisandbox-continue' ).text() + } ).on( 'click', function () { + ApiSandbox.sendRequest( $.extend( {}, baseRequestParams, data[ 'continue' ] ) ); + } ).setDisabled( !data[ 'continue' ] ).$element, + ( clear = new OO.ui.ButtonWidget( { + label: mw.message( 'apisandbox-continue-clear' ).text() + } ).on( 'click', function () { + ApiSandbox.updateUI( baseRequestParams ); + clear.setDisabled( true ); + booklet.setPage( '|results|' ); + } ).setDisabled( !paramsAreForced ) ).$element, + new OO.ui.PopupButtonWidget( { + framed: false, + icon: 'info', + popup: { + $content: $( '
' ).append( mw.message( 'apisandbox-continue-help' ).parse() ), + padded: true + } + } ).$element + ) + ); + } if ( typeof loadTime === 'number' ) { $result.append( $( '
' ).append( @@ -1285,16 +1355,24 @@ if ( pi.parameters[ i ].highmax !== undefined ) { dl.append( $( '
', { addClass: 'info', - append: Util.parseHTML( mw.message( - 'api-help-param-limit2', pi.parameters[ i ].max, pi.parameters[ i ].highmax - ).parse() ) + append: [ + Util.parseHTML( mw.message( + 'api-help-param-limit2', pi.parameters[ i ].max, pi.parameters[ i ].highmax + ).parse() ), + ' ', + Util.parseHTML( mw.message( 'apisandbox-param-limit' ).parse() ) + ] } ) ); } else { dl.append( $( '
', { addClass: 'info', - append: Util.parseHTML( mw.message( - 'api-help-param-limit', pi.parameters[ i ].max - ).parse() ) + append: [ + Util.parseHTML( mw.message( + 'api-help-param-limit', pi.parameters[ i ].max + ).parse() ), + ' ', + Util.parseHTML( mw.message( 'apisandbox-param-limit' ).parse() ) + ] } ) ); } break; @@ -1379,6 +1457,7 @@ // Don't grey out the label when the field is disabled, // it makes it too hard to read and our "disabled" // isn't really disabled. + widgetField.onFieldDisable( false ); widgetField.onFieldDisable = doNothing; if ( Util.apiBool( pi.parameters[ i ].deprecated ) ) { @@ -1421,7 +1500,7 @@ dynamicParamNameWidget, new OO.ui.ButtonWidget( { icon: 'add', - flags: 'constructive' + flags: 'progressive' } ).on( 'click', addDynamicParamWidget ), { label: mw.message( 'apisandbox-dynamic-parameters-add-label' ).text(),