diff options
author | Ed Sanders <esanders@wikimedia.org> | 2021-12-22 15:18:43 +0000 |
---|---|---|
committer | Ed Sanders <esanders@wikimedia.org> | 2021-12-22 15:18:43 +0000 |
commit | b33524cc93ee3e1a31c045f3c2f934ddaf9c186d (patch) | |
tree | c087ed5c788765141b2d1b80e66719e402c9c1bd | |
parent | 16bd6eaeb497ba577460775038a530e202ef3b0f (diff) |
ApiSandbox JS: Move var declarations inline
Change-Id: If44197491539358c1abaab6c292c523c9d1a3853
-rw-r--r-- | resources/src/mediawiki.special.apisandbox/apisandbox.js | 445 |
1 files changed, 214 insertions, 231 deletions
diff --git a/resources/src/mediawiki.special.apisandbox/apisandbox.js b/resources/src/mediawiki.special.apisandbox/apisandbox.js index f9324e178410..b0e5f0dfb62f 100644 --- a/resources/src/mediawiki.special.apisandbox/apisandbox.js +++ b/resources/src/mediawiki.special.apisandbox/apisandbox.js @@ -1,7 +1,7 @@ ( function () { 'use strict'; var ApiSandbox, Util, WidgetMethods, Validators, - panel, booklet, oldhash, windowManager, + windowManager, formatDropdown, api = new mw.Api(), bookletPages = [], @@ -11,7 +11,6 @@ updatingBooklet = false, pages = {}, moduleInfoCache = {}, - baseRequestParams, OptionalParamWidget = require( './OptionalParamWidget.js' ), ParamLabelWidget = require( './ParamLabelWidget.js' ), BooleanToggleSwitchParamWidget = require( './BooleanToggleSwitchParamWidget.js' ), @@ -33,15 +32,15 @@ this.setValue( v ); }, apiCheckValid: function ( shouldSuppressErrors ) { - var that = this; + var widget = this; return this.getValidity().then( function () { return $.Deferred().resolve( true ).promise(); }, function () { return $.Deferred().resolve( false ).promise(); } ).done( function ( ok ) { ok = ok || shouldSuppressErrors; - that.setIcon( ok ? null : 'alert' ); - that.setTitle( ok ? '' : mw.message( 'apisandbox-alert-field' ).plain() ); + widget.setIcon( ok ? null : 'alert' ); + widget.setTitle( ok ? '' : mw.message( 'apisandbox-alert-field' ).plain() ); } ); } }, @@ -84,11 +83,10 @@ return item === null ? undefined : item.getData(); }, setApiValue: function ( v ) { - var menu = this.getMenu(); - if ( v === undefined ) { v = this.paramInfo.default; } + var menu = this.getMenu(); if ( v === undefined ) { menu.selectItem(); } else { @@ -134,10 +132,9 @@ this.setValue( this.parseApiValue( v ) ); }, apiCheckValid: function ( shouldSuppressErrors ) { - var ok = true, - pi = this.paramInfo; - + var ok = true; if ( !shouldSuppressErrors ) { + var pi = this.paramInfo; ok = this.getApiValue() !== undefined && !( pi.allspecifier !== undefined && this.getValue().length > 1 && @@ -198,28 +195,25 @@ * @return {jQuery.Promise} */ fetchModuleInfo: function ( module ) { - var apiPromise, - deferred = $.Deferred(); + var deferred = $.Deferred(); if ( Object.prototype.hasOwnProperty.call( moduleInfoCache, module ) ) { return deferred .resolve( moduleInfoCache[ module ] ) .promise( { abort: function () {} } ); } else { - apiPromise = api.post( { + var apiPromise = api.post( { action: 'paraminfo', modules: module, helpformat: 'html', uselang: mw.config.get( 'wgUserLanguage' ) } ).done( function ( data ) { - var info; - if ( data.warnings && data.warnings.paraminfo ) { deferred.reject( '???', data.warnings.paraminfo[ '*' ] ); return; } - info = data.paraminfo.modules; + var info = data.paraminfo.modules; if ( !info || info.length !== 1 || info[ 0 ].path !== module ) { deferred.reject( '???', 'No module data returned' ); return; @@ -244,22 +238,22 @@ * Mark all currently-in-use tokens as bad */ markTokensBad: function () { - var page, subpages, i, - checkPages = [ pages.main ]; + var checkPages = [ pages.main ]; while ( checkPages.length ) { - page = checkPages.shift(); + var page = checkPages.shift(); if ( page.tokenWidget ) { api.badToken( page.tokenWidget.paramInfo.tokentype ); } - subpages = page.getSubpages(); - for ( i = 0; i < subpages.length; i++ ) { - if ( Object.prototype.hasOwnProperty.call( pages, subpages[ i ].key ) ) { - checkPages.push( pages[ subpages[ i ].key ] ); + var subpages = page.getSubpages(); + // eslint-disable-next-line no-loop-func + subpages.forEach( function ( subpage ) { + if ( Object.prototype.hasOwnProperty.call( pages, subpage.key ) ) { + checkPages.push( pages[ subpage.key ] ); } - } + } ); } }, @@ -281,13 +275,13 @@ * @return {OO.ui.Widget} */ createWidgetForParameter: function ( pi, opts ) { - var widget, innerWidget, finalWidget, items, $content, func, - multiModeButton = null, + var multiModeButton = null, multiModeInput = null, multiModeAllowed = false; opts = opts || {}; + var widget, items; switch ( pi.type ) { case 'boolean': widget = new BooleanToggleSwitchParamWidget(); @@ -521,12 +515,12 @@ } if ( Util.apiBool( pi.multi ) && multiModeAllowed ) { - innerWidget = widget; + var innerWidget = widget; multiModeButton = new OO.ui.ButtonWidget( { label: mw.msg( 'apisandbox-add-multi' ) } ); - $content = innerWidget.$element.add( multiModeButton.$element ); + var $content = innerWidget.$element.add( multiModeButton.$element ); widget = new OO.ui.PopupTagMultiselectWidget( { allowArbitrary: true, @@ -541,7 +535,7 @@ widget.paramInfo = pi; $.extend( widget, WidgetMethods.tagWidget ); - func = function () { + var func = function () { if ( !innerWidget.isDisabled() ) { innerWidget.apiCheckValid( suppressErrors ).done( function ( ok ) { if ( ok ) { @@ -559,6 +553,7 @@ multiModeButton.on( 'click', func ); } + var finalWidget; if ( Util.apiBool( pi.required ) || opts.nooptional ) { finalWidget = widget; } else { @@ -669,17 +664,17 @@ * Event handler for when formatDropdown's selection changes */ onFormatDropdownChange: function () { - var i, - menu = formatDropdown.getMenu(), + var menu = formatDropdown.getMenu(), items = menu.getItems(), selectedField = menu.findSelectedItem() ? menu.findSelectedItem().getData() : null; - for ( i = 0; i < items.length; i++ ) { - items[ i ].getData().toggle( items[ i ].getData() === selectedField ); - } + items.forEach( function ( item ) { + item.getData().toggle( item.getData() === selectedField ); + } ); } }; + var booklet, panel, oldhash; /** * Interface to ApiSandbox UI * @@ -692,16 +687,13 @@ * Automatically called on $.ready() */ init: function () { - var $toolbar, - $content = $( '#mw-apisandbox' ); - windowManager = new OO.ui.WindowManager(); $( document.body ).append( windowManager.$element ); windowManager.addWindows( { errorAlert: new OO.ui.MessageDialog() } ); - $toolbar = $( '<div>' ) + var $toolbar = $( '<div>' ) .addClass( 'mw-apisandbox-toolbar' ) .append( new OO.ui.ButtonWidget( { @@ -735,7 +727,7 @@ $( window ).on( 'hashchange', ApiSandbox.loadFromHash ); - $content + $( '#mw-apisandbox' ) .empty() .append( $( '<p>' ).append( Util.parseMsg( 'apisandbox-intro' ) ) ) .append( @@ -751,8 +743,7 @@ * @return {boolean} Successful */ loadFromHash: function () { - var params, m, re, - hash = location.hash; + var hash = location.hash; if ( oldhash === hash ) { return false; @@ -763,11 +754,12 @@ } // I'm surprised this doesn't seem to exist in jQuery or mw.util. - params = {}; + var params = {}; hash = hash.replace( /\+/g, '%20' ); - re = /([^&=#]+)=?([^&#]*)/g; - while ( ( m = re.exec( hash ) ) ) { - params[ decodeURIComponent( m[ 1 ] ) ] = decodeURIComponent( m[ 2 ] ); + var pattern = /([^&=#]+)=?([^&#]*)/g; + var match; + while ( ( match = pattern.exec( hash ) ) ) { + params[ decodeURIComponent( match[ 1 ] ) ] = decodeURIComponent( match[ 2 ] ); } ApiSandbox.updateUI( params ); @@ -780,8 +772,7 @@ * @param {Object} [params] Optional query parameters to load */ updateUI: function ( params ) { - var i, page, subpages, j, removePages, - addPages = []; + var addPages = []; if ( !$.isPlainObject( params ) ) { params = undefined; @@ -801,11 +792,11 @@ } pages.main.apiCheckValid(); - i = 0; + var i = 0; while ( addPages.length ) { - page = addPages.shift(); + var page = addPages.shift(); if ( bookletPages[ i ] !== page ) { - for ( j = i; j < bookletPages.length; j++ ) { + for ( var j = i; j < bookletPages.length; j++ ) { if ( bookletPages[ j ].getName() === page.getName() ) { bookletPages.splice( j, 1 ); } @@ -816,23 +807,24 @@ i++; if ( page.getSubpages ) { - subpages = page.getSubpages(); - for ( j = 0; j < subpages.length; j++ ) { - if ( !Object.prototype.hasOwnProperty.call( pages, subpages[ j ].key ) ) { - subpages[ j ].indentLevel = page.indentLevel + 1; - pages[ subpages[ j ].key ] = new ApiSandbox.PageLayout( subpages[ j ] ); + var subpages = page.getSubpages(); + // eslint-disable-next-line no-loop-func + subpages.forEach( function ( subpage, k ) { + if ( !Object.prototype.hasOwnProperty.call( pages, subpage.key ) ) { + subpage.indentLevel = page.indentLevel + 1; + pages[ subpage.key ] = new ApiSandbox.PageLayout( subpage ); } if ( params !== undefined ) { - pages[ subpages[ j ].key ].loadQueryParams( params ); + pages[ subpage.key ].loadQueryParams( params ); } - addPages.splice( j, 0, pages[ subpages[ j ].key ] ); - pages[ subpages[ j ].key ].apiCheckValid(); - } + addPages.splice( k, 0, pages[ subpage.key ] ); + pages[ subpage.key ].apiCheckValid(); + } ); } } if ( bookletPages.length > i ) { - removePages = bookletPages.splice( i, bookletPages.length - i ); + var removePages = bookletPages.splice( i, bookletPages.length - i ); booklet.removePages( removePages ); } @@ -863,9 +855,7 @@ * The form fields will be updated to match. */ sendRequest: function ( params ) { - var page, subpages, i, query, $result, $focus, - progress, $progressText, progressLoading, - deferreds = [], + var deferreds = [], paramsAreForced = !!params, displayParams = {}, tokenWidgets = [], @@ -873,7 +863,7 @@ // Blur any focused widget before submit, because // OO.ui.ButtonWidget doesn't take focus itself (T128054) - $focus = $( '#mw-apisandbox-ui' ).find( document.activeElement ); + var $focus = $( '#mw-apisandbox-ui' ).find( document.activeElement ); if ( $focus.length ) { $focus[ 0 ].blur(); } @@ -886,33 +876,32 @@ } params = {}; while ( checkPages.length ) { - page = checkPages.shift(); - if ( page.tokenWidget ) { - tokenWidgets.push( page.tokenWidget ); + var checkPage = checkPages.shift(); + if ( checkPage.tokenWidget ) { + tokenWidgets.push( checkPage.tokenWidget ); } - deferreds = deferreds.concat( page.apiCheckValid() ); - page.getQueryParams( params, displayParams ); - subpages = page.getSubpages(); - for ( i = 0; i < subpages.length; i++ ) { - if ( Object.prototype.hasOwnProperty.call( pages, subpages[ i ].key ) ) { - checkPages.push( pages[ subpages[ i ].key ] ); + deferreds = deferreds.concat( checkPage.apiCheckValid() ); + checkPage.getQueryParams( params, displayParams ); + var subpages = checkPage.getSubpages(); + // eslint-disable-next-line no-loop-func + subpages.forEach( function ( subpage ) { + if ( Object.prototype.hasOwnProperty.call( pages, subpage.key ) ) { + checkPages.push( pages[ subpage.key ] ); } - } + } ); } + var baseRequestParams; if ( !paramsAreForced ) { // forced params means we are continuing a query; the base query should be preserved baseRequestParams = $.extend( {}, params ); } $.when.apply( $, deferreds ).done( function () { - var formatItems, menu, selectedLabel, deferred, actions, errorCount; - // Count how many times `value` occurs in `array`. function countValues( value, array ) { - var count, n; - count = 0; - for ( n = 0; n < array.length; n++ ) { + var count = 0; + for ( var n = 0; n < array.length; n++ ) { if ( array[ n ] === value ) { count++; } @@ -920,15 +909,16 @@ return count; } - errorCount = countValues( false, arguments ); + var errorCount = countValues( false, arguments ); if ( errorCount > 0 ) { - actions = [ + var actions = [ { action: 'accept', label: OO.ui.msg( 'ooui-dialog-process-dismiss' ), flags: 'primary' } ]; + var deferred; if ( tokenWidgets.length ) { // Check all token widgets' validity separately deferred = $.when.apply( $, tokenWidgets.map( function ( w ) { @@ -964,9 +954,9 @@ return; } - query = $.param( displayParams ); + var query = $.param( displayParams ); - formatItems = Util.formatRequest( displayParams, params ); + var formatItems = Util.formatRequest( displayParams, params ); // Force a 'fm' format with wrappedhtml=1, if available if ( params.format !== undefined ) { @@ -978,16 +968,16 @@ } } - progressLoading = false; - $progressText = $( '<span>' ).text( mw.msg( 'apisandbox-sending-request' ) ); - progress = new OO.ui.ProgressBarWidget( { + var progressLoading = false; + var $progressText = $( '<span>' ).text( mw.msg( 'apisandbox-sending-request' ) ); + var progress = new OO.ui.ProgressBarWidget( { progress: false } ); - $result = $( '<div>' ) + var $result = $( '<div>' ) .append( $progressText, progress.$element ); - resultPage = page = new OO.ui.PageLayout( '|results|', { expanded: false } ); + var page = resultPage = new OO.ui.PageLayout( '|results|', { expanded: false } ); page.setupOutlineItem = function () { this.outlineItem.setLabel( mw.msg( 'apisandbox-results' ) ); }; @@ -1000,8 +990,8 @@ formatDropdown.getMenu().on( 'select', Util.onFormatDropdownChange ); } - menu = formatDropdown.getMenu(); - selectedLabel = menu.findSelectedItem() ? menu.findSelectedItem().getLabel() : ''; + var menu = formatDropdown.getMenu(); + var selectedLabel = menu.findSelectedItem() ? menu.findSelectedItem().getLabel() : ''; if ( typeof selectedLabel !== 'string' ) { selectedLabel = selectedLabel.text(); } @@ -1069,8 +1059,7 @@ return d.promise(); } ) .then( function ( data, jqXHR ) { - var m, loadTime, button, clear, - ct = jqXHR.getResponseHeader( 'Content-Type' ), + var ct = jqXHR.getResponseHeader( 'Content-Type' ), loginSuppressed = jqXHR.getResponseHeader( 'MediaWiki-Login-Suppressed' ) || 'false'; $result.empty(); @@ -1080,6 +1069,7 @@ .append( Util.parseMsg( 'apisandbox-results-login-suppressed' ) ) .appendTo( $result ); } + var loadTime, match; if ( /^text\/mediawiki-api-prettyprint-wrapped(?:;|$)/.test( ct ) ) { try { data = JSON.parse( data ); @@ -1098,10 +1088,10 @@ } $result.append( Util.parseHTML( data.html ) ); loadTime = data.time; - } else if ( ( m = data.match( /<pre[ >][\s\S]*<\/pre>/ ) ) ) { - $result.append( Util.parseHTML( m[ 0 ] ) ); - if ( ( m = data.match( /"wgBackendResponseTime":\s*(\d+)/ ) ) ) { - loadTime = parseInt( m[ 1 ], 10 ); + } else if ( ( match = data.match( /<pre[ >][\s\S]*<\/pre>/ ) ) ) { + $result.append( Util.parseHTML( match[ 0 ] ) ); + if ( ( match = data.match( /"wgBackendResponseTime":\s*(\d+)/ ) ) ) { + loadTime = parseInt( match[ 1 ], 10 ); } } else { $( '<pre>' ) @@ -1110,6 +1100,7 @@ .appendTo( $result ); } if ( paramsAreForced || data.continue ) { + var clear; $result.append( $( '<div>' ).append( new OO.ui.ButtonWidget( { @@ -1150,7 +1141,7 @@ if ( jqXHR.getResponseHeader( 'MediaWiki-API-Error' ) === 'badtoken' ) { // Flush all saved tokens in case one of them is the bad one. Util.markTokensBad(); - button = new OO.ui.ButtonWidget( { + var button = new OO.ui.ButtonWidget( { label: mw.msg( 'apisandbox-results-fixtoken' ) } ); button.on( 'click', ApiSandbox.fixTokenAndResend ) @@ -1177,8 +1168,7 @@ * pages and then re-submits the query. */ fixTokenAndResend: function () { - var page, subpages, i, key, - ok = true, + var ok = true, tokenWait = { dummy: true }, checkPages = [ pages.main ], success = function ( k ) { @@ -1193,22 +1183,23 @@ }; while ( checkPages.length ) { - page = checkPages.shift(); + var page = checkPages.shift(); if ( page.tokenWidget ) { - key = page.apiModule + page.tokenWidget.paramInfo.name; + var key = page.apiModule + page.tokenWidget.paramInfo.name; tokenWait[ key ] = page.tokenWidget.fetchToken(); tokenWait[ key ] .done( success.bind( page.tokenWidget, key ) ) .fail( failure.bind( page.tokenWidget, key ) ); } - subpages = page.getSubpages(); - for ( i = 0; i < subpages.length; i++ ) { - if ( Object.prototype.hasOwnProperty.call( pages, subpages[ i ].key ) ) { - checkPages.push( pages[ subpages[ i ].key ] ); + var subpages = page.getSubpages(); + // eslint-disable-next-line no-loop-func + subpages.forEach( function ( subpage ) { + if ( Object.prototype.hasOwnProperty.call( pages, subpage.key ) ) { + checkPages.push( pages[ subpage.key ] ); } - } + } ); } success( 'dummy', '' ); @@ -1218,18 +1209,18 @@ * Reset validity indicators for all widgets */ updateValidityIndicators: function () { - var page, subpages, i, - checkPages = [ pages.main ]; + var checkPages = [ pages.main ]; while ( checkPages.length ) { - page = checkPages.shift(); + var page = checkPages.shift(); page.apiCheckValid(); - subpages = page.getSubpages(); - for ( i = 0; i < subpages.length; i++ ) { - if ( Object.prototype.hasOwnProperty.call( pages, subpages[ i ].key ) ) { - checkPages.push( pages[ subpages[ i ].key ] ); + var subpages = page.getSubpages(); + // eslint-disable-next-line no-loop-func + subpages.forEach( function ( subpage ) { + if ( Object.prototype.hasOwnProperty.call( pages, subpage.key ) ) { + checkPages.push( pages[ subpage.key ] ); } - } + } ); } } }; @@ -1292,9 +1283,7 @@ * @return {OO.ui.FieldLayout} return.helpField */ ApiSandbox.PageLayout.prototype.makeWidgetFieldLayouts = function ( ppi, name ) { - var j, l, widget, helpLabel, tmp, $tmp, flag, count, button, widgetField, helpField, layoutConfig; - - widget = Util.createWidgetForParameter( ppi ); + var widget = Util.createWidgetForParameter( ppi ); if ( ppi.tokentype ) { this.tokenWidget = widget; } @@ -1302,9 +1291,9 @@ widget.on( 'change', this.updateTemplatedParameters, [ null ], this ); } - helpLabel = new ParamLabelWidget(); + var helpLabel = new ParamLabelWidget(); - $tmp = Util.parseHTML( ppi.description ); + var $tmp = Util.parseHTML( ppi.description ); $tmp.filter( 'dl' ).makeCollapsible( { collapsed: true } ).children( '.mw-collapsible-toggle' ).each( function () { @@ -1314,15 +1303,16 @@ helpLabel.addDescription( $tmp ); if ( ppi.info && ppi.info.length ) { - for ( j = 0; j < ppi.info.length; j++ ) { + for ( var i = 0; i < ppi.info.length; i++ ) { helpLabel.$element.append( $( '<div>' ) .addClass( 'info' ) - .append( Util.parseHTML( ppi.info[ j ] ) ) + .append( Util.parseHTML( ppi.info[ i ] ) ) ); } } - flag = true; - count = Infinity; + var flag = true; + var count = Infinity; + var tmp; switch ( ppi.type ) { case 'namespace': flag = false; @@ -1393,7 +1383,7 @@ } if ( ppi.usedTemplateVars && ppi.usedTemplateVars.length ) { $tmp = $(); - for ( j = 0, l = ppi.usedTemplateVars.length; j < l; j++ ) { + for ( var j = 0, l = ppi.usedTemplateVars.length; j < l; j++ ) { $tmp = $tmp.add( $( '<var>' ).text( ppi.usedTemplateVars[ j ] ) ); if ( j === l - 2 ) { $tmp = $tmp.add( mw.message( 'and' ).parseDom() ); @@ -1414,7 +1404,7 @@ // TODO: Consder adding more options for the position of helpInline // so that this can become part of the widgetField, instead of // having to use a separate field. - helpField = new OO.ui.FieldLayout( + var helpField = new OO.ui.FieldLayout( helpLabel, { align: 'top', @@ -1422,14 +1412,15 @@ } ); - layoutConfig = { + var layoutConfig = { align: 'left', classes: [ 'mw-apisandbox-widget-field' ], label: name }; + var widgetField; if ( ppi.tokentype ) { - button = new OO.ui.ButtonWidget( { + var button = new OO.ui.ButtonWidget( { label: mw.msg( 'apisandbox-fetch-token' ) } ); button.on( 'click', widget.fetchToken, [], widget ); @@ -1465,10 +1456,9 @@ * @param {Object} [params] Query parameters for initializing the widgets */ ApiSandbox.PageLayout.prototype.updateTemplatedParameters = function ( params ) { - var p, toProcess, doProcess, tmp, toRemove, - that = this, + var layout = this, pi = this.paramInfo, - prefix = that.prefix + pi.prefix; + prefix = layout.prefix + pi.prefix; if ( !pi || !pi.templatedparameters.length ) { return; @@ -1478,7 +1468,7 @@ params = null; } - toRemove = {}; + var toRemove = {}; // eslint-disable-next-line no-jquery/no-each-util $.each( this.templatedItemsCache, function ( k, el ) { if ( el.widget.isElementAttached() ) { @@ -1488,7 +1478,7 @@ // This bit duplicates the PHP logic in ApiBase::extractRequestParams(). // If you update this, see if that needs updating too. - toProcess = pi.templatedparameters.map( function ( info ) { + var toProcess = pi.templatedparameters.map( function ( info ) { return { name: prefix + info.name, info: info, @@ -1496,23 +1486,22 @@ usedVars: [] }; } ); - doProcess = function ( placeholder, target ) { - var values, container, index, usedVars, done, items, i; - + var p; + var doProcess = function ( placeholder, target ) { target = prefix + target; - if ( !that.widgets[ target ] ) { + if ( !layout.widgets[ target ] ) { // The target wasn't processed yet, try the next one. // If all hit this case, the parameter has no expansions. return true; } - if ( !that.widgets[ target ].getApiValueForTemplates ) { + if ( !layout.widgets[ target ].getApiValueForTemplates ) { // Not a multi-valued widget, so it can't have expansions. return false; } - values = that.widgets[ target ].getApiValueForTemplates(); + var values = layout.widgets[ target ].getApiValueForTemplates(); if ( !Array.isArray( values ) || !values.length ) { // The target was processed but has no (valid) values. // That means it has no expansions. @@ -1523,14 +1512,14 @@ // then requeue if there are more targets left or create the widget // and add it to the form if all are done. delete p.vars[ placeholder ]; - usedVars = p.usedVars.concat( [ target ] ); + var usedVars = p.usedVars.concat( [ target ] ); placeholder = '{' + placeholder + '}'; - done = $.isEmptyObject( p.vars ); + var done = $.isEmptyObject( p.vars ); + var index, container; if ( done ) { - container = Util.apiBool( p.info.deprecated ) ? that.deprecatedItemsFieldset : that.itemsFieldset; - items = container.getItems(); - index = undefined; - for ( i = 0; i < items.length; i++ ) { + container = Util.apiBool( p.info.deprecated ) ? layout.deprecatedItemsFieldset : layout.itemsFieldset; + var items = container.getItems(); + for ( var i = 0; i < items.length; i++ ) { if ( items[ i ].apiParamIndex !== undefined && items[ i ].apiParamIndex > p.info.index ) { index = i; break; @@ -1538,26 +1527,25 @@ } } values.forEach( function ( value ) { - var name, newVars; - if ( !/^[^{}]*$/.exec( value ) ) { // Skip values that make invalid parameter names return; } - name = p.name.replace( placeholder, value ); + var name = p.name.replace( placeholder, value ); if ( done ) { - if ( that.templatedItemsCache[ name ] ) { - tmp = that.templatedItemsCache[ name ]; + var tmp; + if ( layout.templatedItemsCache[ name ] ) { + tmp = layout.templatedItemsCache[ name ]; } else { - tmp = that.makeWidgetFieldLayouts( + tmp = layout.makeWidgetFieldLayouts( $.extend( {}, p.info, { usedTemplateVars: usedVars } ), name ); - that.templatedItemsCache[ name ] = tmp; + layout.templatedItemsCache[ name ] = tmp; } delete toRemove[ name ]; if ( !tmp.widget.isElementAttached() ) { - that.widgets[ name ] = tmp.widget; + layout.widgets[ name ] = tmp.widget; container.addItems( [ tmp.widgetField, tmp.helpField ], index ); if ( index !== undefined ) { index += 2; @@ -1567,7 +1555,7 @@ tmp.widget.setApiValue( Object.prototype.hasOwnProperty.call( params, name ) ? params[ name ] : undefined ); } } else { - newVars = {}; + var newVars = {}; // eslint-disable-next-line no-jquery/no-each-util $.each( p.vars, function ( k, v ) { newVars[ k ] = v.replace( placeholder, value ); @@ -1590,7 +1578,7 @@ // eslint-disable-next-line no-jquery/no-map-util toRemove = $.map( toRemove, function ( el, name ) { - delete that.widgets[ name ]; + delete layout.widgets[ name ]; return [ el.widgetField, el.helpField ]; } ); if ( toRemove.length ) { @@ -1604,22 +1592,20 @@ */ ApiSandbox.PageLayout.prototype.loadParamInfo = function () { var dynamicFieldset, dynamicParamNameWidget, - that = this, - removeDynamicParamWidget = function ( name, layout ) { - dynamicFieldset.removeItems( [ layout ] ); - delete that.widgets[ name ]; + layout = this, + removeDynamicParamWidget = function ( name, item ) { + dynamicFieldset.removeItems( [ item ] ); + delete layout.widgets[ name ]; }, addDynamicParamWidget = function () { - var name, layout, widget, button; - // Check name is filled in - name = dynamicParamNameWidget.getValue().trim(); + var name = dynamicParamNameWidget.getValue().trim(); if ( name === '' ) { dynamicParamNameWidget.focus(); return; } - if ( that.widgets[ name ] !== undefined ) { + if ( layout.widgets[ name ] !== undefined ) { windowManager.openWindow( 'errorAlert', { title: Util.parseMsg( 'apisandbox-dynamic-error-exists', name ), actions: [ @@ -1633,18 +1619,18 @@ return; } - widget = Util.createWidgetForParameter( { + var widget = Util.createWidgetForParameter( { name: name, type: 'string', default: '' }, { nooptional: true } ); - button = new OO.ui.ButtonWidget( { + var button = new OO.ui.ButtonWidget( { icon: 'trash', flags: 'destructive' } ); - layout = new OO.ui.ActionFieldLayout( + var actionFieldLayout = new OO.ui.ActionFieldLayout( widget, button, { @@ -1652,9 +1638,9 @@ align: 'left' } ); - button.on( 'click', removeDynamicParamWidget, [ name, layout ] ); - that.widgets[ name ] = widget; - dynamicFieldset.addItems( [ layout ], dynamicFieldset.getItemCount() - 1 ); + button.on( 'click', removeDynamicParamWidget, [ name, actionFieldLayout ] ); + layout.widgets[ name ] = widget; + dynamicFieldset.addItems( [ actionFieldLayout ], dynamicFieldset.getItemCount() - 1 ); widget.focus(); dynamicParamNameWidget.setValue( '' ); @@ -1670,8 +1656,7 @@ Util.fetchModuleInfo( this.apiModule ) .done( function ( pi ) { - var prefix, i, j, tmp, $tmp, - items = [], + var items = [], deprecatedItems = [], buttons = [], filterFmModules = function ( v ) { @@ -1683,22 +1668,22 @@ // 'action' parameters from the main module to be specified, // and for 'format' we also want to simplify the dropdown since // we always send the 'fm' variant. - if ( that.apiModule === 'main' ) { - for ( i = 0; i < pi.parameters.length; i++ ) { - if ( pi.parameters[ i ].name === 'action' ) { - pi.parameters[ i ].required = true; - delete pi.parameters[ i ].default; + if ( layout.apiModule === 'main' ) { + pi.parameters.forEach( function ( parameter ) { + if ( parameter.name === 'action' ) { + parameter.required = true; + delete parameter.default; } - if ( pi.parameters[ i ].name === 'format' ) { - tmp = pi.parameters[ i ].type; - for ( j = 0; j < tmp.length; j++ ) { - availableFormats[ tmp[ j ] ] = true; - } - pi.parameters[ i ].type = tmp.filter( filterFmModules ); - pi.parameters[ i ].default = 'json'; - pi.parameters[ i ].required = true; + if ( parameter.name === 'format' ) { + var types = parameter.type; + types.forEach( function ( type ) { + availableFormats[ type ] = true; + } ); + parameter.type = types.filter( filterFmModules ); + parameter.default = 'json'; + parameter.required = true; } - } + } ); } // Hide the 'wrappedhtml' parameter on format modules @@ -1708,7 +1693,7 @@ } ); } - that.paramInfo = pi; + layout.paramInfo = pi; items.push( new OO.ui.FieldLayout( new OO.ui.Widget( {} ).toggle( false ), { @@ -1765,16 +1750,16 @@ } if ( pi.parameters.length ) { - prefix = that.prefix + pi.prefix; - for ( i = 0; i < pi.parameters.length; i++ ) { - tmp = that.makeWidgetFieldLayouts( pi.parameters[ i ], prefix + pi.parameters[ i ].name ); - that.widgets[ prefix + pi.parameters[ i ].name ] = tmp.widget; - if ( Util.apiBool( pi.parameters[ i ].deprecated ) ) { - deprecatedItems.push( tmp.widgetField, tmp.helpField ); + var prefix = layout.prefix + pi.prefix; + pi.parameters.forEach( function ( parameter ) { + var tmpLayout = layout.makeWidgetFieldLayouts( parameter, prefix + parameter.name ); + layout.widgets[ prefix + parameter.name ] = tmpLayout.widget; + if ( Util.apiBool( parameter.deprecated ) ) { + deprecatedItems.push( tmpLayout.widgetField, tmpLayout.helpField ); } else { - items.push( tmp.widgetField, tmp.helpField ); + items.push( tmpLayout.widgetField, tmpLayout.helpField ); } - } + } ); } if ( !pi.parameters.length && !Util.apiBool( pi.dynamicparameters ) ) { @@ -1786,13 +1771,13 @@ ) ); } - that.$element.empty(); + layout.$element.empty(); - that.itemsFieldset = new OO.ui.FieldsetLayout( { - label: that.displayText + layout.itemsFieldset = new OO.ui.FieldsetLayout( { + label: layout.displayText } ); - that.itemsFieldset.addItems( items ); - that.itemsFieldset.$element.appendTo( that.$element ); + layout.itemsFieldset.addItems( items ); + layout.itemsFieldset.$element.appendTo( layout.$element ); if ( Util.apiBool( pi.dynamicparameters ) ) { dynamicFieldset = new OO.ui.FieldsetLayout(); @@ -1823,52 +1808,52 @@ $( '<legend>' ).text( mw.msg( 'apisandbox-dynamic-parameters' ) ), dynamicFieldset.$element ) - .appendTo( that.$element ); + .appendTo( layout.$element ); } - that.deprecatedItemsFieldset = new OO.ui.FieldsetLayout().addItems( deprecatedItems ).toggle( false ); - $tmp = $( '<fieldset>' ) - .toggle( !that.deprecatedItemsFieldset.isEmpty() ) + layout.deprecatedItemsFieldset = new OO.ui.FieldsetLayout().addItems( deprecatedItems ).toggle( false ); + var $tmp = $( '<fieldset>' ) + .toggle( !layout.deprecatedItemsFieldset.isEmpty() ) .append( $( '<legend>' ).append( new OO.ui.ToggleButtonWidget( { label: mw.msg( 'apisandbox-deprecated-parameters' ) - } ).on( 'change', that.deprecatedItemsFieldset.toggle, [], that.deprecatedItemsFieldset ).$element + } ).on( 'change', layout.deprecatedItemsFieldset.toggle, [], layout.deprecatedItemsFieldset ).$element ), - that.deprecatedItemsFieldset.$element + layout.deprecatedItemsFieldset.$element ) - .appendTo( that.$element ); - that.deprecatedItemsFieldset.on( 'add', function () { - this.toggle( !that.deprecatedItemsFieldset.isEmpty() ); + .appendTo( layout.$element ); + layout.deprecatedItemsFieldset.on( 'add', function () { + this.toggle( !layout.deprecatedItemsFieldset.isEmpty() ); }, [], $tmp ); - that.deprecatedItemsFieldset.on( 'remove', function () { - this.toggle( !that.deprecatedItemsFieldset.isEmpty() ); + layout.deprecatedItemsFieldset.on( 'remove', function () { + this.toggle( !layout.deprecatedItemsFieldset.isEmpty() ); }, [], $tmp ); // Load stored params, if any, then update the booklet if we // have subpages (or else just update our valid-indicator). - tmp = that.loadFromQueryParams; - that.loadFromQueryParams = null; + var tmp = layout.loadFromQueryParams; + layout.loadFromQueryParams = null; if ( $.isPlainObject( tmp ) ) { - that.loadQueryParams( tmp ); + layout.loadQueryParams( tmp ); } else { - that.updateTemplatedParameters(); + layout.updateTemplatedParameters(); } - if ( that.getSubpages().length > 0 ) { + if ( layout.getSubpages().length > 0 ) { ApiSandbox.updateUI( tmp ); } else { - that.apiCheckValid(); + layout.apiCheckValid(); } } ).fail( function ( code, detail ) { - that.$element.empty() + layout.$element.empty() .append( new OO.ui.LabelWidget( { - label: mw.msg( 'apisandbox-load-error', that.apiModule, detail ), + label: mw.msg( 'apisandbox-load-error', layout.apiModule, detail ), classes: [ 'error' ] } ).$element, new OO.ui.ButtonWidget( { label: mw.msg( 'apisandbox-retry' ) - } ).on( 'click', that.loadParamInfo, [], that ).$element + } ).on( 'click', layout.loadParamInfo, [], layout ).$element ); } ); }; @@ -1879,21 +1864,21 @@ * @return {jQuery.Promise[]} One promise for each widget, resolved with `false` if invalid */ ApiSandbox.PageLayout.prototype.apiCheckValid = function () { - var promises, that = this; + var layout = this; if ( this.paramInfo === null ) { return []; } else { // eslint-disable-next-line no-jquery/no-map-util - promises = $.map( this.widgets, function ( widget ) { + var promises = $.map( this.widgets, function ( widget ) { return widget.apiCheckValid( suppressErrors ); } ); $.when.apply( $, promises ).then( function () { - that.apiIsValid = Array.prototype.indexOf.call( arguments, false ) === -1; - if ( that.getOutlineItem() ) { - that.getOutlineItem().setIcon( that.apiIsValid || suppressErrors ? null : 'alert' ); - that.getOutlineItem().setTitle( - that.apiIsValid || suppressErrors ? '' : mw.message( 'apisandbox-alert-page' ).plain() + layout.apiIsValid = Array.prototype.indexOf.call( arguments, false ) === -1; + if ( layout.getOutlineItem() ) { + layout.getOutlineItem().setIcon( layout.apiIsValid || suppressErrors ? null : 'alert' ); + layout.getOutlineItem().setTitle( + layout.apiIsValid || suppressErrors ? '' : mw.message( 'apisandbox-alert-page' ).plain() ); } } ); @@ -1948,16 +1933,14 @@ var ret = []; // eslint-disable-next-line no-jquery/no-each-util $.each( this.widgets, function ( name, widget ) { - var submodules, i; if ( typeof widget.getSubmodules === 'function' ) { - submodules = widget.getSubmodules(); - for ( i = 0; i < submodules.length; i++ ) { + widget.getSubmodules().forEach( function ( submodule ) { ret.push( { - key: name + '=' + submodules[ i ].value, - path: submodules[ i ].path, + key: name + '=' + submodule.value, + path: submodule.path, prefix: widget.paramInfo.submoduleparamprefix || '' } ); - } + } ); } } ); return ret; |