mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/WikiEditor
synced 2024-11-25 08:48:18 +00:00
d1270ed4b6
Using document.activeElement at the time of the unload event as a proxy for whether the user is switching to VE. Switching to VE is weighed more heavily than whether or not the user made changes: switching to VE causes abort.type to be 'switchwithout' regardless of whether changes were made; only if the user isn't switching to VE do we look at whether changes were made and use 'abort' or 'nochange' as appropriate. When wgAction === 'submit' (i.e. we're on a preview / show changes view), it is assumed that the user has made changes. Bug: T95938 Change-Id: Ic91b87c4fc5b601e1fd98b237100d808e97400bd
109 lines
3.2 KiB
JavaScript
109 lines
3.2 KiB
JavaScript
/*
|
|
* JavaScript for WikiEditor
|
|
*/
|
|
|
|
( function ( $, mw ) {
|
|
var editingSessionId;
|
|
|
|
function logEditEvent( action, data ) {
|
|
if ( mw.loader.getState( 'schema.Edit' ) === null ) {
|
|
return;
|
|
}
|
|
|
|
// Sample 25% (via hex digit)
|
|
// We have to do this on the client too because the unload handler
|
|
// can cause an editingSessionId to be generated on the client
|
|
if ( editingSessionId.charAt( 0 ) > '3' ) {
|
|
return;
|
|
}
|
|
|
|
mw.loader.using( 'schema.Edit' ).done( function () {
|
|
data = $.extend( {
|
|
version: 1,
|
|
action: action,
|
|
editor: 'wikitext',
|
|
platform: 'desktop', // FIXME
|
|
integration: 'page',
|
|
'page.id': mw.config.get( 'wgArticleId' ),
|
|
'page.title': mw.config.get( 'wgPageName' ),
|
|
'page.ns': mw.config.get( 'wgNamespaceNumber' ),
|
|
'page.revid': mw.config.get( 'wgRevisionId' ),
|
|
'page.length': -1, // FIXME
|
|
'user.id': mw.user.getId(),
|
|
'user.editCount': mw.config.get( 'wgUserEditCount', 0 ),
|
|
'mediawiki.version': mw.config.get( 'wgVersion' )
|
|
}, data );
|
|
|
|
if ( mw.user.isAnon() ) {
|
|
data['user.class'] = 'IP';
|
|
}
|
|
|
|
data['action.' + action + '.type'] = data.type;
|
|
data['action.' + action + '.mechanism'] = data.mechanism;
|
|
data['action.' + action + '.timing'] = data.timing === undefined ?
|
|
0 : Math.floor( data.timing );
|
|
// Remove renamed properties
|
|
delete data.type;
|
|
delete data.mechanism;
|
|
delete data.timing;
|
|
|
|
mw.eventLog.logEvent( 'Edit', data );
|
|
} );
|
|
}
|
|
|
|
$( function () {
|
|
var $textarea = $( '#wpTextbox1' ),
|
|
$editingSessionIdInput = $( '#editingStatsId' ),
|
|
origText = $textarea.val(),
|
|
submitting, onUnloadFallback;
|
|
|
|
// Initialize wikiEditor
|
|
$textarea.wikiEditor();
|
|
|
|
if ( $editingSessionIdInput.length ) {
|
|
editingSessionId = $editingSessionIdInput.val();
|
|
logEditEvent( 'ready', {
|
|
editingSessionId: editingSessionId
|
|
} );
|
|
$textarea.closest( 'form' ).submit( function () {
|
|
submitting = true;
|
|
} );
|
|
onUnloadFallback = window.onunload;
|
|
window.onunload = function () {
|
|
var fallbackResult,
|
|
caVeEdit = $( '#ca-ve-edit' )[0],
|
|
switchingToVE = caVeEdit && (
|
|
document.activeElement === caVeEdit ||
|
|
$.contains( caVeEdit, document.activeElement )
|
|
);
|
|
|
|
if ( onUnloadFallback ) {
|
|
fallbackResult = onUnloadFallback();
|
|
}
|
|
|
|
if ( !submitting ) {
|
|
logEditEvent( 'abort', {
|
|
editingSessionId: editingSessionId,
|
|
type: switchingToVE ? 'switchwithout' :
|
|
( mw.config.get( 'wgAction' ) !== 'submit' && origText === $textarea.val() ?
|
|
'nochange' :
|
|
'abandon'
|
|
)
|
|
} );
|
|
}
|
|
|
|
// If/when the user uses the back button to go back to the edit form
|
|
// and the browser serves this from bfcache, regenerate the session ID
|
|
// so we don't use the same ID twice. Ideally we'd do this by listening to the pageshow
|
|
// event and checking e.originalEvent.persisted, but that doesn't work in Chrome:
|
|
// https://code.google.com/p/chromium/issues/detail?id=344507
|
|
// So instead we modify the DOM here, after sending the abort event.
|
|
editingSessionId = mw.user.generateRandomSessionId();
|
|
$editingSessionIdInput.val( editingSessionId );
|
|
|
|
return fallbackResult;
|
|
};
|
|
}
|
|
} );
|
|
}( jQuery, mediaWiki ) );
|