Merge "Remove unused messages"
[lhc/web/wiklou.git] / resources / src / mediawiki.special / mediawiki.special.apisandbox.js
index e58a6cf..d2015d8 100644 (file)
@@ -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: {
                        },
                        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() );
                },
 
                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();
+                               }
                        }
                },
 
                                }
                        },
                        apiCheckValid: function () {
-                               var ok = this.getApiValue() !== undefined || suppressErrors;
+                               var ok = true,
+                                       pi = this.paramInfo;
+
+                               if ( !suppressErrors ) {
+                                       ok = this.getApiValue() !== undefined && !(
+                                               pi.allspecifier !== undefined &&
+                                               this.getItemsData().length > 1 &&
+                                               this.getItemsData().indexOf( pi.allspecifier ) !== -1
+                                       );
+                               }
+
                                this.setIcon( ok ? null : 'alert' );
                                this.setIconTitle( ok ? '' : mw.message( 'apisandbox-alert-field' ).plain() );
                                return $.Deferred().resolve( ok ).promise();
                                        } );
                                        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 ) ) {
                                        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;
 
                                                return a.data - b.data;
                                        } );
                                        if ( Util.apiBool( pi.multi ) ) {
+                                               if ( pi.allspecifier !== undefined ) {
+                                                       items.unshift( new OO.ui.MenuOptionWidget( {
+                                                               data: pi.allspecifier,
+                                                               label: mw.message( 'apisandbox-multivalue-all-namespaces', pi.allspecifier ).text()
+                                                       } ) );
+                                               }
+
                                                widget = new OO.ui.CapsuleMultiselectWidget( {
                                                        menu: { items: items }
                                                } );
                                                return new OO.ui.MenuOptionWidget( { data: String( v ), label: String( v ) } );
                                        } );
                                        if ( Util.apiBool( pi.multi ) ) {
+                                               if ( pi.allspecifier !== undefined ) {
+                                                       items.unshift( new OO.ui.MenuOptionWidget( {
+                                                               data: pi.allspecifier,
+                                                               label: mw.message( 'apisandbox-multivalue-all-values', pi.allspecifier ).text()
+                                                       } ) );
+                                               }
+
                                                widget = new OO.ui.CapsuleMultiselectWidget( {
                                                        menu: { items: items }
                                                } );
                init: function () {
                        var $toolbar;
 
+                       ApiSandbox.isFullscreen = false;
+
                        $content = $( '#mw-apisandbox' );
 
                        windowManager = new OO.ui.WindowManager();
                 * Toggle "fullscreen" mode
                 */
                toggleFullscreen: function () {
-                       var $body = $( document.body );
+                       var $body = $( document.body ),
+                               $ui = $( '#mw-apisandbox-ui' );
+
+                       ApiSandbox.isFullscreen = !ApiSandbox.isFullscreen;
 
-                       $body.toggleClass( 'mw-apisandbox-fullscreen' );
-                       if ( $body.hasClass( 'mw-apisandbox-fullscreen' ) ) {
+                       $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();
                },
                        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 {
 
                /**
                 * Update the current query when the page hash changes
+                *
+                * @return {boolean} Successful
                 */
                loadFromHash: function () {
                        var params, m, re,
 
                /**
                 * 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 ];
 
 
                        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() );
                                }
                        }
 
+                       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', {
                                                        );
                                        } )
                                        .done( function ( data, jqXHR ) {
-                                               var m, loadTime, button,
+                                               var m, loadTime, button, clear,
                                                        ct = jqXHR.getResponseHeader( 'Content-Type' );
 
                                                $result.empty();
                                                if ( /^text\/mediawiki-api-prettyprint-wrapped(?:;|$)/.test( ct ) ) {
-                                                       data = $.parseJSON( data );
+                                                       data = JSON.parse( data );
                                                        if ( data.modules.length ) {
                                                                mw.loader.load( data.modules );
                                                        }
+                                                       if ( data.status && data.status !== 200 ) {
+                                                               $( '<div>' )
+                                                                       .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( /<pre[ >][\s\S]*<\/pre>/ ) ) ) {
                                                                .text( data )
                                                                .appendTo( $result );
                                                }
+                                               if ( paramsAreForced || data[ 'continue' ] ) {
+                                                       $result.append(
+                                                               $( '<div>' ).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: $( '<div>' ).append( mw.message( 'apisandbox-continue-help' ).parse() ),
+                                                                                       padded: true
+                                                                               }
+                                                                       } ).$element
+                                                               )
+                                                       );
+                                               }
                                                if ( typeof loadTime === 'number' ) {
                                                        $result.append(
                                                                $( '<div>' ).append(
                                                                if ( pi.parameters[ i ].highmax !== undefined ) {
                                                                        dl.append( $( '<dd>', {
                                                                                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( $( '<dd>', {
                                                                                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;
                                                // 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 ) ) {