X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=resources%2Fsrc%2Fmediawiki.special%2Fmediawiki.special.apisandbox.js;h=523a62e75b796d7fb9a91ed90d50d9bfd3c2aecb;hb=c81b662def0b47b40333fe19d8afa4f7e4e3f0e1;hp=a7470da768a4ad4b2850a5a3d8927ab5a4b5d0a5;hpb=c29357e7e81b631668c8bd3ccd4b805701380c63;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 a7470da768..523a62e75b 100644 --- a/resources/src/mediawiki.special/mediawiki.special.apisandbox.js +++ b/resources/src/mediawiki.special/mediawiki.special.apisandbox.js @@ -1,7 +1,7 @@ ( function ( $, mw, OO ) { 'use strict'; var ApiSandbox, Util, WidgetMethods, Validators, - $content, panel, booklet, oldhash, windowManager, fullscreenButton, + $content, panel, booklet, oldhash, windowManager, formatDropdown, api = new mw.Api(), bookletPages = [], @@ -774,8 +774,6 @@ init: function () { var $toolbar; - ApiSandbox.isFullscreen = false; - $content = $( '#mw-apisandbox' ); windowManager = new OO.ui.WindowManager(); @@ -784,15 +782,9 @@ errorAlert: new OO.ui.MessageDialog() } ); - fullscreenButton = new OO.ui.ButtonWidget( { - label: mw.message( 'apisandbox-fullscreen' ).text(), - title: mw.message( 'apisandbox-fullscreen-tooltip' ).text() - } ).on( 'click', ApiSandbox.toggleFullscreen ); - $toolbar = $( '
' ) .addClass( 'mw-apisandbox-toolbar' ) .append( - fullscreenButton.$element, new OO.ui.ButtonWidget( { label: mw.message( 'apisandbox-submit' ).text(), flags: [ 'primary', 'progressive' ] @@ -804,6 +796,7 @@ ); booklet = new OO.ui.BookletLayout( { + expanded: false, outlined: true, autoFocus: false } ); @@ -822,19 +815,7 @@ ApiSandbox.updateUI(); } - // If the hashchange event exists, use it. Otherwise, fake it. - // And, of course, IE has to be dumb. - if ( 'onhashchange' in window && - ( document.documentMode === undefined || document.documentMode >= 8 ) - ) { - $( window ).on( 'hashchange', ApiSandbox.loadFromHash ); - } else { - setInterval( function () { - if ( oldhash !== location.hash ) { - ApiSandbox.loadFromHash(); - } - }, 1000 ); - } + $( window ).on( 'hashchange', ApiSandbox.loadFromHash ); $content .empty() @@ -844,52 +825,6 @@ .append( $toolbar ) .append( panel.$element ) ); - - $( window ).on( 'resize', ApiSandbox.resizePanel ); - - ApiSandbox.resizePanel(); - }, - - /** - * Toggle "fullscreen" mode - */ - toggleFullscreen: function () { - var $body = $( document.body ), - $ui = $( '#mw-apisandbox-ui' ); - - 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() ); - OO.ui.getDefaultOverlay().prepend( $ui ); - } else { - fullscreenButton.setLabel( mw.message( 'apisandbox-fullscreen' ).text() ); - fullscreenButton.setTitle( mw.message( 'apisandbox-fullscreen-tooltip' ).text() ); - $content.append( $ui ); - } - ApiSandbox.resizePanel(); - }, - - /** - * Set the height of the panel based on the current viewport. - */ - resizePanel: function () { - var height = $( window ).height(), - contentTop = $content.offset().top; - - if ( ApiSandbox.isFullscreen ) { - height -= panel.$element.offset().top - $( '#mw-apisandbox-ui' ).offset().top; - panel.$element.height( height - 1 ); - } else { - // Subtract the height of the intro text - height -= panel.$element.offset().top - contentTop; - - panel.$element.height( height - 10 ); - $( window ).scrollTop( contentTop - 5 ); - } }, /** @@ -1015,6 +950,7 @@ deferreds = [], paramsAreForced = !!params, displayParams = {}, + tokenWidgets = [], checkPages = [ pages.main ]; // Blur any focused widget before submit, because @@ -1033,7 +969,10 @@ params = {}; while ( checkPages.length ) { page = checkPages.shift(); - deferreds.push( page.apiCheckValid() ); + if ( page.tokenWidget ) { + tokenWidgets.push( page.tokenWidget ); + } + deferreds = deferreds.concat( page.apiCheckValid() ); page.getQueryParams( params, displayParams ); subpages = page.getSubpages(); for ( i = 0; i < subpages.length; i++ ) { @@ -1049,19 +988,60 @@ } $.when.apply( $, deferreds ).done( function () { - var formatItems, menu, selectedLabel; + var formatItems, menu, selectedLabel, deferred, actions, errorCount; + + // Count how many times `value` occurs in `array`. + function countValues( value, array ) { + var count, i; + count = 0; + for ( i = 0; i < array.length; i++ ) { + if ( array[ i ] === value ) { + count++; + } + } + return count; + } - if ( $.inArray( false, arguments ) !== -1 ) { - windowManager.openWindow( 'errorAlert', { - title: Util.parseMsg( 'apisandbox-submit-invalid-fields-title' ), - message: Util.parseMsg( 'apisandbox-submit-invalid-fields-message' ), - actions: [ - { - action: 'accept', - label: OO.ui.msg( 'ooui-dialog-process-dismiss' ), - flags: 'primary' + errorCount = countValues( false, arguments ); + if ( errorCount > 0 ) { + actions = [ + { + action: 'accept', + label: OO.ui.msg( 'ooui-dialog-process-dismiss' ), + flags: 'primary' + } + ]; + if ( tokenWidgets.length ) { + // Check all token widgets' validity separately + deferred = $.when.apply( $, tokenWidgets.map( function ( w ) { + return w.apiCheckValid(); + } ) ); + + deferred.done( function () { + // If only the tokens are invalid, offer to fix them + var tokenErrorCount = countValues( false, arguments ); + if ( tokenErrorCount === errorCount ) { + delete actions[ 0 ].flags; + actions.push( { + action: 'fix', + label: mw.message( 'apisandbox-results-fixtoken' ).text(), + flags: 'primary' + } ); } - ] + } ); + } else { + deferred = $.Deferred().resolve(); + } + deferred.always( function () { + windowManager.openWindow( 'errorAlert', { + title: Util.parseMsg( 'apisandbox-submit-invalid-fields-title' ), + message: Util.parseMsg( 'apisandbox-submit-invalid-fields-message' ), + actions: actions + } ).closed.then( function ( data ) { + if ( data && data.action === 'fix' ) { + ApiSandbox.fixTokenAndResend(); + } + } ); } ); return; } @@ -1090,7 +1070,7 @@ $result = $( '
' ) .append( progress.$element ); - resultPage = page = new OO.ui.PageLayout( '|results|' ); + resultPage = page = new OO.ui.PageLayout( '|results|', { expanded: false } ); page.setupOutlineItem = function () { this.outlineItem.setLabel( mw.message( 'apisandbox-results' ).text() ); }; @@ -1342,7 +1322,7 @@ * @param {Object} [config] Configuration options */ ApiSandbox.PageLayout = function ( config ) { - config = $.extend( { prefix: '' }, config ); + config = $.extend( { prefix: '', expanded: false }, config ); this.displayText = config.key; this.apiModule = config.path; this.prefix = config.prefix; @@ -1795,17 +1775,18 @@ /** * Check that all widgets on the page are in a valid state. * - * @return {boolean} + * @return {jQuery.Promise[]} One promise for each widget, resolved with `false` if invalid */ ApiSandbox.PageLayout.prototype.apiCheckValid = function () { - var that = this; + var promises, that = this; if ( this.paramInfo === null ) { - return $.Deferred().resolve( false ).promise(); + return []; } else { - return $.when.apply( $, $.map( this.widgets, function ( widget ) { + promises = $.map( this.widgets, function ( widget ) { return widget.apiCheckValid(); - } ) ).then( function () { + } ); + $.when.apply( $, promises ).then( function () { that.apiIsValid = $.inArray( false, arguments ) === -1; if ( that.getOutlineItem() ) { that.getOutlineItem().setIcon( that.apiIsValid || suppressErrors ? null : 'alert' ); @@ -1813,8 +1794,8 @@ that.apiIsValid || suppressErrors ? '' : mw.message( 'apisandbox-alert-page' ).plain() ); } - return $.Deferred().resolve( that.apiIsValid ).promise(); } ); + return promises; } };