Merge "mediawiki.action.edit, mediawiki.page.watch.ajax: Move var declarations inline"

This commit is contained in:
jenkins-bot 2021-10-25 17:23:58 +00:00 committed by Gerrit Code Review
commit f6f6c748a6
4 changed files with 34 additions and 45 deletions

View file

@ -16,12 +16,11 @@
*/ */
$( function () { $( function () {
var wpSummary, editBox, scrollTop, $editForm, var $wpSummary = $( '#wpSummaryWidget' );
$wpSummary = $( '#wpSummaryWidget' );
// The summary field might not be there, e.g. when extensions replace it // The summary field might not be there, e.g. when extensions replace it
if ( $wpSummary.length ) { if ( $wpSummary.length ) {
wpSummary = OO.ui.infuse( $wpSummary ); var wpSummary = OO.ui.infuse( $wpSummary );
// Show a byte-counter to users with how many bytes are left for their edit summary. // Show a byte-counter to users with how many bytes are left for their edit summary.
mw.widgets.visibleCodePointLimit( wpSummary, mw.config.get( 'wgCommentCodePointLimit' ) ); mw.widgets.visibleCodePointLimit( wpSummary, mw.config.get( 'wgCommentCodePointLimit' ) );
@ -29,9 +28,9 @@ $( function () {
// Restore the edit box scroll state following a preview operation, // Restore the edit box scroll state following a preview operation,
// and set up a form submission handler to remember this state. // and set up a form submission handler to remember this state.
editBox = document.getElementById( 'wpTextbox1' ); var editBox = document.getElementById( 'wpTextbox1' );
scrollTop = document.getElementById( 'wpScrolltop' ); var scrollTop = document.getElementById( 'wpScrolltop' );
$editForm = $( '#editform' ); var $editForm = $( '#editform' );
mw.hook( 'wikipage.editform' ).fire( $editForm ); mw.hook( 'wikipage.editform' ).fire( $editForm );
if ( $editForm.length && editBox && scrollTop ) { if ( $editForm.length && editBox && scrollTop ) {
if ( scrollTop.value ) { if ( scrollTop.value ) {

View file

@ -40,8 +40,7 @@ $( function () {
// If a request is in progress, abort it since its payload is stale and the API // If a request is in progress, abort it since its payload is stale and the API
// may limit concurrent stash parses. // may limit concurrent stash parses.
function stashEdit() { function stashEdit() {
var req, params, var textChanged = isTextChanged(),
textChanged = isTextChanged(),
priority = textChanged ? PRIORITY_HIGH : PRIORITY_LOW; priority = textChanged ? PRIORITY_HIGH : PRIORITY_LOW;
if ( stashReq ) { if ( stashReq ) {
@ -63,7 +62,7 @@ $( function () {
lastTextHash = null; lastTextHash = null;
} }
params = { var params = {
formatversion: 2, formatversion: 2,
action: 'stashedit', action: 'stashedit',
title: mw.config.get( 'wgPageName' ), title: mw.config.get( 'wgPageName' ),
@ -80,7 +79,7 @@ $( function () {
params.text = lastText; params.text = lastText;
} }
req = api.postWithToken( 'csrf', params ); var req = api.postWithToken( 'csrf', params );
stashReq = req; stashReq = req;
req.then( function ( data ) { req.then( function ( data ) {
if ( req === stashReq ) { if ( req === stashReq ) {

View file

@ -6,14 +6,13 @@
// Toggle the watchlist-expiry dropdown's disabled state according to the // Toggle the watchlist-expiry dropdown's disabled state according to the
// selected state of the watchthis checkbox. // selected state of the watchthis checkbox.
$( function () { $( function () {
var watchThisWidget, expiryWidget, // The 'wpWatchthis' and 'wpWatchlistExpiry' fields come from EditPage.php.
// The 'wpWatchthis' and 'wpWatchlistExpiry' fields come from EditPage.php. var watchThisNode = document.getElementById( 'wpWatchthisWidget' ),
watchThisNode = document.getElementById( 'wpWatchthisWidget' ),
expiryNode = document.getElementById( 'wpWatchlistExpiryWidget' ); expiryNode = document.getElementById( 'wpWatchlistExpiryWidget' );
if ( watchThisNode && expiryNode ) { if ( watchThisNode && expiryNode ) {
watchThisWidget = OO.ui.infuse( watchThisNode ); var watchThisWidget = OO.ui.infuse( watchThisNode );
expiryWidget = OO.ui.infuse( expiryNode ); var expiryWidget = OO.ui.infuse( expiryNode );
// Set initial state to match the watchthis checkbox. // Set initial state to match the watchthis checkbox.
expiryWidget.setDisabled( !watchThisWidget.isSelected() ); expiryWidget.setDisabled( !watchThisWidget.isSelected() );

View file

@ -31,11 +31,6 @@
* Default is a null expiry * Default is a null expiry
*/ */
function updateWatchLink( $link, action, state, expiry ) { function updateWatchLink( $link, action, state, expiry ) {
var msgKey, $li, otherAction, expiryDate,
tooltipAction = action,
daysLeftExpiry = null,
currentDate = new Date();
// A valid but empty jQuery object shouldn't throw a TypeError // A valid but empty jQuery object shouldn't throw a TypeError
if ( !$link.length ) { if ( !$link.length ) {
return; return;
@ -50,9 +45,9 @@
throw new Error( 'Invalid action' ); throw new Error( 'Invalid action' );
} }
msgKey = state === 'loading' ? action + 'ing' : action; var msgKey = state === 'loading' ? action + 'ing' : action;
otherAction = action === 'watch' ? 'unwatch' : 'watch'; var otherAction = action === 'watch' ? 'unwatch' : 'watch';
$li = $link.closest( 'li' ); var $li = $link.closest( 'li' );
// Trigger a 'watchpage' event for this List item. // Trigger a 'watchpage' event for this List item.
// Announce the otherAction value and expiry as params. // Announce the otherAction value and expiry as params.
@ -62,13 +57,16 @@
$li.trigger( 'watchpage.mw', [ otherAction, expiry === 'infinity' ? null : expiry ] ); $li.trigger( 'watchpage.mw', [ otherAction, expiry === 'infinity' ? null : expiry ] );
} }
var tooltipAction = action;
var daysLeftExpiry = null;
// Checking to see what if the expiry is set or indefinite to display the correct message // Checking to see what if the expiry is set or indefinite to display the correct message
if ( isWatchlistExpiryEnabled && action === 'unwatch' ) { if ( isWatchlistExpiryEnabled && action === 'unwatch' ) {
if ( expiry === null || expiry === 'infinity' ) { if ( expiry === null || expiry === 'infinity' ) {
// Resolves to tooltip-ca-unwatch message // Resolves to tooltip-ca-unwatch message
tooltipAction = 'unwatch'; tooltipAction = 'unwatch';
} else { } else {
expiryDate = new Date( expiry ); var expiryDate = new Date( expiry );
var currentDate = new Date();
// Using the Math.ceil function instead of floor so when, for example, a user selects one week // Using the Math.ceil function instead of floor so when, for example, a user selects one week
// the tooltip shows 7 days instead of 6 days (see Phab ticket T253936) // the tooltip shows 7 days instead of 6 days (see Phab ticket T253936)
daysLeftExpiry = Math.ceil( ( expiryDate - currentDate ) / ( 1000 * 60 * 60 * 24 ) ); daysLeftExpiry = Math.ceil( ( expiryDate - currentDate ) / ( 1000 * 60 * 60 * 24 ) );
@ -124,20 +122,18 @@
* @return {string} The extracted action, defaults to 'view' * @return {string} The extracted action, defaults to 'view'
*/ */
function mwUriGetAction( url ) { function mwUriGetAction( url ) {
var action, actionPaths, key, m, parts;
// TODO: Does MediaWiki give action path or query param // TODO: Does MediaWiki give action path or query param
// precedence? If the former, move this to the bottom // precedence? If the former, move this to the bottom
action = mw.util.getParamValue( 'action', url ); var action = mw.util.getParamValue( 'action', url );
if ( action !== null ) { if ( action !== null ) {
return action; return action;
} }
actionPaths = mw.config.get( 'wgActionPaths' ); var actionPaths = mw.config.get( 'wgActionPaths' );
for ( key in actionPaths ) { for ( var key in actionPaths ) {
parts = actionPaths[ key ].split( '$1' ); var parts = actionPaths[ key ].split( '$1' );
parts = parts.map( mw.util.escapeRegExp ); parts = parts.map( mw.util.escapeRegExp );
m = new RegExp( parts.join( '(.+)' ) ).exec( url ); var m = new RegExp( parts.join( '(.+)' ) ).exec( url );
if ( m && m[ 1 ] ) { if ( m && m[ 1 ] ) {
return key; return key;
} }
@ -201,10 +197,8 @@
// Add click handler. // Add click handler.
$links.on( 'click', function ( e ) { $links.on( 'click', function ( e ) {
var mwTitle, action, api, $link, modulesToLoad; var mwTitle = mw.Title.newFromText( title );
var action = mwUriGetAction( this.href );
mwTitle = mw.Title.newFromText( title );
action = mwUriGetAction( this.href );
if ( !mwTitle || ( action !== 'watch' && action !== 'unwatch' ) ) { if ( !mwTitle || ( action !== 'watch' && action !== 'unwatch' ) ) {
// Let native browsing handle the link // Let native browsing handle the link
@ -213,7 +207,7 @@
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
$link = $( this ); var $link = $( this );
// eslint-disable-next-line no-jquery/no-class-state // eslint-disable-next-line no-jquery/no-class-state
if ( $link.hasClass( 'loading' ) ) { if ( $link.hasClass( 'loading' ) ) {
@ -223,7 +217,7 @@
updateWatchLink( $link, action, 'loading', null ); updateWatchLink( $link, action, 'loading', null );
// Preload the notification module for mw.notify // Preload the notification module for mw.notify
modulesToLoad = [ 'mediawiki.notification' ]; var modulesToLoad = [ 'mediawiki.notification' ];
// Preload watchlist expiry widget so it runs in parallel with the api call // Preload watchlist expiry widget so it runs in parallel with the api call
if ( isWatchlistExpiryEnabled ) { if ( isWatchlistExpiryEnabled ) {
@ -232,20 +226,19 @@
mw.loader.load( modulesToLoad ); mw.loader.load( modulesToLoad );
api = new mw.Api(); var api = new mw.Api();
api[ action ]( title ) api[ action ]( title )
.done( function ( watchResponse ) { .done( function ( watchResponse ) {
var message,
watchlistPopup = null,
otherAction = action === 'watch' ? 'unwatch' : 'watch',
notifyPromise;
var message;
if ( mwTitle.isTalkPage() ) { if ( mwTitle.isTalkPage() ) {
message = action === 'watch' ? 'addedwatchtext-talk' : 'removedwatchtext-talk'; message = action === 'watch' ? 'addedwatchtext-talk' : 'removedwatchtext-talk';
} else { } else {
message = action === 'watch' ? 'addedwatchtext' : 'removedwatchtext'; message = action === 'watch' ? 'addedwatchtext' : 'removedwatchtext';
} }
var notifyPromise;
var watchlistPopup;
// @since 1.35 - pop up notification will be loaded with OOUI // @since 1.35 - pop up notification will be loaded with OOUI
// only if Watchlist Expiry is enabled // only if Watchlist Expiry is enabled
if ( isWatchlistExpiryEnabled ) { if ( isWatchlistExpiryEnabled ) {
@ -293,6 +286,7 @@
); );
} }
var otherAction = action === 'watch' ? 'unwatch' : 'watch';
// The notifications are stored as a promise and the watch link is only updated // The notifications are stored as a promise and the watch link is only updated
// once it is resolved. Otherwise, if $wgWatchlistExpiry set, the loading of // once it is resolved. Otherwise, if $wgWatchlistExpiry set, the loading of
// OOUI could cause a race condition and the link is updated before the popup // OOUI could cause a race condition and the link is updated before the popup
@ -303,13 +297,11 @@
} ); } );
} ) } )
.fail( function ( code, data ) { .fail( function ( code, data ) {
var $msg;
// Reset link to non-loading mode // Reset link to non-loading mode
updateWatchLink( $link, action ); updateWatchLink( $link, action );
// Format error message // Format error message
$msg = api.getErrorMessage( data ); var $msg = api.getErrorMessage( data );
// Report to user about the error // Report to user about the error
mw.notify( $msg, { mw.notify( $msg, {