2010-09-15 22:40:50 +00:00
|
|
|
/*
|
|
|
|
* JavaScript for WikiEditor
|
|
|
|
*/
|
2015-03-24 21:14:16 +00:00
|
|
|
|
2018-11-12 22:28:40 +00:00
|
|
|
( function () {
|
2019-06-27 21:48:25 +00:00
|
|
|
var editingSessionId, logEditEvent, logEditFeature,
|
2018-10-26 20:48:55 +00:00
|
|
|
actionPrefixMap = {
|
|
|
|
saveIntent: 'save_intent',
|
|
|
|
saveAttempt: 'save_attempt',
|
|
|
|
saveSuccess: 'save_success',
|
|
|
|
saveFailure: 'save_failure'
|
2019-06-27 21:48:25 +00:00
|
|
|
},
|
2019-07-17 10:45:10 +00:00
|
|
|
trackdebug = !!mw.util.getParamValue( 'trackdebug' );
|
2019-06-27 21:48:25 +00:00
|
|
|
|
2019-07-20 08:09:04 +00:00
|
|
|
// This sets $.wikiEditor and $.fn.wikiEditor
|
|
|
|
require( './jquery.wikiEditor.js' );
|
|
|
|
|
2019-06-27 21:48:25 +00:00
|
|
|
function log() {
|
|
|
|
// mw.log is a no-op unless resource loader is in debug mode, so
|
|
|
|
// this allows trackdebug to work independently (T211698)
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log.apply( console, arguments );
|
|
|
|
}
|
|
|
|
|
|
|
|
function sampledLogger( schema, callback ) {
|
|
|
|
return function () {
|
|
|
|
var args;
|
|
|
|
if ( mw.loader.getState( 'ext.eventLogging' ) === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
args = Array.prototype.slice.call( arguments );
|
|
|
|
|
|
|
|
mw.loader.using( [ 'ext.eventLogging' ] ).done( function () {
|
|
|
|
// Sampling
|
|
|
|
// We have to do this on the client too because the unload handler
|
|
|
|
// can cause an editingSessionId to be generated on the client
|
|
|
|
// Not using mw.eventLog.inSample() because we need to be able to pass our own editingSessionId
|
|
|
|
var data,
|
|
|
|
inSample = mw.eventLog.randomTokenMatch(
|
|
|
|
1 / mw.config.get( 'wgWMESchemaEditAttemptStepSamplingRate' ),
|
|
|
|
editingSessionId
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( !inSample && !mw.config.get( 'wgWMESchemaEditAttemptStepOversample' ) && !trackdebug ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = callback.apply( this, [ inSample ].concat( args ) );
|
|
|
|
|
|
|
|
if ( trackdebug ) {
|
|
|
|
log( schema, data );
|
|
|
|
} else {
|
|
|
|
mw.eventLog.logEvent( schema, data );
|
|
|
|
}
|
|
|
|
} );
|
2018-10-26 20:48:55 +00:00
|
|
|
};
|
2019-06-27 21:48:25 +00:00
|
|
|
}
|
2015-04-19 23:47:37 +00:00
|
|
|
|
2019-06-27 21:48:25 +00:00
|
|
|
logEditEvent = sampledLogger( 'EditAttemptStep', function ( inSample, action, data ) {
|
|
|
|
var actionPrefix = actionPrefixMap[ action ] || action;
|
|
|
|
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
data = $.extend( {
|
|
|
|
version: 1,
|
|
|
|
action: action,
|
|
|
|
is_oversample: !inSample,
|
|
|
|
editing_session_id: editingSessionId,
|
|
|
|
page_token: mw.user.getPageviewToken(),
|
|
|
|
session_token: mw.user.sessionId(),
|
|
|
|
editor_interface: 'wikitext',
|
|
|
|
platform: 'desktop', // FIXME
|
|
|
|
integration: 'page',
|
|
|
|
page_id: mw.config.get( 'wgArticleId' ),
|
|
|
|
page_title: mw.config.get( 'wgPageName' ),
|
|
|
|
page_ns: mw.config.get( 'wgNamespaceNumber' ),
|
|
|
|
revision_id: mw.config.get( 'wgRevisionId' ),
|
|
|
|
user_id: mw.user.getId(),
|
|
|
|
user_editcount: mw.config.get( 'wgUserEditCount', 0 ),
|
|
|
|
mw_version: mw.config.get( 'wgVersion' )
|
|
|
|
}, data );
|
|
|
|
|
|
|
|
if ( mw.user.isAnon() ) {
|
|
|
|
data.user_class = 'IP';
|
2015-03-24 21:14:16 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 21:48:25 +00:00
|
|
|
data[ actionPrefix + '_type' ] = data.type;
|
|
|
|
data[ actionPrefix + '_mechanism' ] = data.mechanism;
|
|
|
|
data[ actionPrefix + '_timing' ] = data.timing === undefined ? 0 : Math.floor( data.timing );
|
|
|
|
/* eslint-enable camelcase */
|
2015-04-28 21:55:54 +00:00
|
|
|
|
2019-06-27 21:48:25 +00:00
|
|
|
// Remove renamed properties
|
|
|
|
delete data.type;
|
|
|
|
delete data.mechanism;
|
|
|
|
delete data.timing;
|
2015-03-24 21:14:16 +00:00
|
|
|
|
2019-06-27 21:48:25 +00:00
|
|
|
return data;
|
|
|
|
} );
|
2018-10-26 20:48:55 +00:00
|
|
|
|
2019-06-27 21:48:25 +00:00
|
|
|
logEditFeature = sampledLogger( 'VisualEditorFeatureUse', function ( inSample, feature, action ) {
|
|
|
|
return {
|
|
|
|
feature: feature,
|
|
|
|
action: action,
|
|
|
|
editingSessionId: editingSessionId
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
function logAbort( switchingToVE, unmodified ) {
|
|
|
|
var abortType;
|
|
|
|
|
|
|
|
if ( switchingToVE ) {
|
|
|
|
logEditFeature( 'editor-switch', 'visual-desktop' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( switchingToVE && unmodified ) {
|
|
|
|
abortType = 'switchnochange';
|
|
|
|
} else if ( switchingToVE ) {
|
|
|
|
abortType = 'switchwithout';
|
|
|
|
} else if ( unmodified ) {
|
|
|
|
abortType = 'nochange';
|
|
|
|
} else {
|
|
|
|
abortType = 'abandon';
|
|
|
|
}
|
2015-03-24 21:14:16 +00:00
|
|
|
|
2019-06-27 21:48:25 +00:00
|
|
|
logEditEvent( 'abort', {
|
|
|
|
type: abortType
|
2015-03-24 21:14:16 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
$( function () {
|
|
|
|
var $textarea = $( '#wpTextbox1' ),
|
2015-04-19 23:47:37 +00:00
|
|
|
$editingSessionIdInput = $( '#editingStatsId' ),
|
2015-04-20 01:13:50 +00:00
|
|
|
origText = $textarea.val(),
|
2019-07-20 08:09:04 +00:00
|
|
|
submitting, onUnloadFallback, dialogsConfig;
|
2015-03-24 21:14:16 +00:00
|
|
|
|
2015-04-19 23:47:37 +00:00
|
|
|
if ( $editingSessionIdInput.length ) {
|
|
|
|
editingSessionId = $editingSessionIdInput.val();
|
2017-12-11 17:46:47 +00:00
|
|
|
if ( window.performance && window.performance.timing ) {
|
|
|
|
// We want to track from the time the user started to try to
|
|
|
|
// launch the editor which navigationStart approximates. All
|
|
|
|
// of our supported browsers *should* allow this. Rather than
|
|
|
|
// fall back to the timestamp when the page loaded for those
|
|
|
|
// that don't, we just ignore them, so as to not skew the
|
|
|
|
// results towards better-performance in those cases.
|
|
|
|
logEditEvent( 'ready', {
|
|
|
|
timing: Date.now() - window.performance.timing.navigationStart
|
2017-12-06 16:31:52 +00:00
|
|
|
} );
|
2017-12-11 17:46:47 +00:00
|
|
|
$textarea.on( 'wikiEditor-toolbar-doneInitialSections', function () {
|
|
|
|
logEditEvent( 'loaded', {
|
|
|
|
timing: Date.now() - window.performance.timing.navigationStart
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
}
|
2019-01-08 23:32:56 +00:00
|
|
|
$textarea.closest( 'form' ).on( 'submit', function () {
|
2015-03-24 21:14:16 +00:00
|
|
|
submitting = true;
|
|
|
|
} );
|
|
|
|
onUnloadFallback = window.onunload;
|
2019-06-27 21:48:25 +00:00
|
|
|
|
2015-03-24 21:14:16 +00:00
|
|
|
window.onunload = function () {
|
2019-06-27 21:48:25 +00:00
|
|
|
var fallbackResult,
|
|
|
|
unmodified = mw.config.get( 'wgAction' ) !== 'submit' && origText === $textarea.val(),
|
2015-11-15 14:30:18 +00:00
|
|
|
caVeEdit = $( '#ca-ve-edit' )[ 0 ],
|
2015-04-20 01:13:50 +00:00
|
|
|
switchingToVE = caVeEdit && (
|
|
|
|
document.activeElement === caVeEdit ||
|
|
|
|
$.contains( caVeEdit, document.activeElement )
|
2019-06-27 21:48:25 +00:00
|
|
|
);
|
2015-03-24 21:14:16 +00:00
|
|
|
|
|
|
|
if ( onUnloadFallback ) {
|
|
|
|
fallbackResult = onUnloadFallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !submitting ) {
|
2019-06-27 21:48:25 +00:00
|
|
|
logAbort( switchingToVE, unmodified );
|
2015-03-24 21:14:16 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 23:47:37 +00:00
|
|
|
// 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 );
|
|
|
|
|
2015-03-24 21:14:16 +00:00
|
|
|
return fallbackResult;
|
|
|
|
};
|
2019-06-27 21:48:25 +00:00
|
|
|
$textarea.on( 'wikiEditor-switching-visualeditor', function () {
|
|
|
|
var unmodified = mw.config.get( 'wgAction' ) !== 'submit' && origText === $textarea.val();
|
|
|
|
// A non-navigation switch to VE has occurred. As such, avoid eventually
|
|
|
|
// double-logging an abort when VE is done.
|
|
|
|
window.onunload = onUnloadFallback;
|
|
|
|
|
|
|
|
logAbort( true, unmodified );
|
|
|
|
} );
|
2015-03-24 21:14:16 +00:00
|
|
|
}
|
2019-07-20 08:09:04 +00:00
|
|
|
|
|
|
|
// The old toolbar is still in place and needs to be removed so there aren't two toolbars
|
|
|
|
$( '#toolbar' ).remove();
|
|
|
|
// Add toolbar module
|
|
|
|
// TODO: Implement .wikiEditor( 'remove' )
|
|
|
|
$textarea.wikiEditor(
|
|
|
|
'addModule', require( './jquery.wikiEditor.toolbar.config.js' )
|
|
|
|
);
|
|
|
|
|
|
|
|
dialogsConfig = require( './jquery.wikiEditor.dialogs.config.js' );
|
|
|
|
// Replace icons
|
|
|
|
dialogsConfig.replaceIcons( $textarea );
|
|
|
|
// Add dialogs module
|
|
|
|
$textarea.wikiEditor( 'addModule', dialogsConfig.getDefaultConfig() );
|
2015-03-24 21:14:16 +00:00
|
|
|
} );
|
2018-11-12 22:28:40 +00:00
|
|
|
}() );
|