/*! * VisualEditor MediaWiki Initialization ViewPageTarget class. * * @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt * @license The MIT License (MIT); see LICENSE.txt */ /*global mw, confirm, alert */ /** * Initialization MediaWiki view page target. * * @class * @extends ve.init.mw.Target * * @constructor */ ve.init.mw.ViewPageTarget = function VeInitMwViewPageTarget() { var browserWhitelisted, currentUri = new mw.Uri(); // Parent constructor ve.init.mw.Target.call( this, $( '#content' ), mw.config.get( 'wgRelevantPageName' ), currentUri.query.oldid ); // Properties this.$document = null; this.$spinner = $( '
' ); this.$toolbarTracker = $( '' ); this.toolbarTrackerFloating = null; this.toolbarOffset = null; this.toolbarCancelButton = null; this.toolbarSaveButton = null; this.saveDialog = null; this.onBeforeUnloadFallback = null; this.onBeforeUnloadHandler = null; this.timings = {}; this.active = false; this.edited = false; this.sanityCheckFinished = false; this.sanityCheckVerified = false; this.activating = false; this.deactivating = false; // If this is true then #transformPage / #restorePage will not call pushState // This is to avoid adding a new history entry for the url we just got from onpopstate // (which would mess up with the expected order of Back/Forwards browsing) this.actFromPopState = false; this.scrollTop = null; this.currentUri = currentUri; this.section = currentUri.query.vesection || null; this.sectionPositionRestored = false; this.sectionTitleRestored = false; this.namespaceName = mw.config.get( 'wgCanonicalNamespace' ); this.viewUri = new mw.Uri( mw.util.wikiGetlink( this.pageName ) ); this.veEditUri = this.viewUri.clone().extend( { 'veaction': 'edit' } ); this.isViewPage = ( mw.config.get( 'wgAction' ) === 'view' && currentUri.query.diff === undefined ); this.originalDocumentTitle = document.title; this.editSummaryByteLimit = 255; this.tabLayout = mw.config.get( 'wgVisualEditorConfig' ).tabLayout; /** * @property {jQuery.Promise|null} */ this.sanityCheckPromise = null; browserWhitelisted = ( 'vewhitelist' in currentUri.query || $.client.test( ve.init.mw.ViewPageTarget.compatibility.whitelist, null, true ) ); // Events this.connect( this, { 'load': 'onLoad', 'save': 'onSave', 'loadError': 'onLoadError', 'tokenError': 'onTokenError', 'saveError': 'onSaveError', 'editConflict': 'onEditConflict', 'showChanges': 'onShowChanges', 'showChangesError': 'onShowChangesError', 'noChanges': 'onNoChanges', 'serializeError': 'onSerializeError' } ); if ( !browserWhitelisted ) { // Show warning in unknown browsers that pass the support test // Continue at own risk. this.localNoticeMessages.push( 'visualeditor-browserwarning' ); } if ( currentUri.query.venotify ) { // The following messages can be used here: // visualeditor-notification-saved // visualeditor-notification-created // visualeditor-notification-restored mw.hook( 'postEdit' ).fire( { 'message': ve.msg( 'visualeditor-notification-' + currentUri.query.venotify, new mw.Title( this.pageName ).toText() ) } ); if ( window.history.replaceState ) { delete currentUri.query.venotify; window.history.replaceState( null, document.title, currentUri ); } } this.setupSkinTabs(); window.addEventListener( 'popstate', ve.bind( this.onWindowPopState, this ) ) ; }; /* Inheritance */ OO.inheritClass( ve.init.mw.ViewPageTarget, ve.init.mw.Target ); /* Static Properties */ /** * Compatibility map used with jQuery.client to black-list incompatible browsers. * * @static * @property */ ve.init.mw.ViewPageTarget.compatibility = { // The key is the browser name returned by jQuery.client // The value is either null (match all versions) or a list of tuples // containing an inequality (<,>,<=,>=) and a version number 'whitelist': { 'firefox': [['>=', 15]], 'iceweasel': [['>=', 10]], 'safari': [['>=', 5]], 'chrome': [['>=', 19]] } }; /* Methods */ /** * Switch to edit mode. * * @method */ ve.init.mw.ViewPageTarget.prototype.activate = function () { if ( !this.active && !this.activating ) { this.activating = true; this.timings.activationStart = ve.now(); // User interface changes this.transformPage(); this.showSpinner(); this.hideTableOfContents(); this.mutePageContent(); this.mutePageTitle(); this.saveScrollPosition(); this.load(); } }; /** * Switch to view mode. * * @method */ ve.init.mw.ViewPageTarget.prototype.deactivate = function ( override ) { if ( override || ( this.active && !this.deactivating ) ) { if ( override || !this.edited || confirm( ve.msg( 'visualeditor-viewpage-savewarning' ) ) ) { this.deactivating = true; // User interface changes this.restorePage(); this.hideSpinner(); this.showTableOfContents(); if ( this.toolbarCancelButton ) { // If deactivate is called before a successful load, then // setupToolbarButtons has not been called yet and as such tearDownToolbarButtons // would throw an error when trying call methods on the button property (bug 46456) this.tearDownToolbarButtons(); this.detachToolbarButtons(); } if ( this.saveDialog ) { // If we got as far as setting up the save dialog, tear it down this.saveDialog.reset(); this.saveDialog.close(); } // Check we got as far as setting up the surface if ( this.active ) { // If we got as far as setting up the surface, tear that down this.tearDownSurface(); } // Show/restore components that are otherwise handled by tearDownSurface this.showPageContent(); this.restorePageTitle(); // If there is a load in progress, abort it if ( this.loading ) { this.loading.abort(); } this.deactivating = false; mw.hook( 've.deactivationComplete' ).fire(); } } }; /** * Handle successful DOM load event. * * @method * @param {HTMLDocument} doc Parsed DOM from server */ ve.init.mw.ViewPageTarget.prototype.onLoad = function ( doc ) { if ( this.activating ) { this.edited = false; this.doc = doc; this.setUpSurface( doc, ve.bind( function() { this.startSanityCheck(); this.setupToolbarButtons(); this.setupSaveDialog(); this.attachToolbarButtons(); this.restoreScrollPosition(); this.restoreEditSection(); this.setupBeforeUnloadHandler(); this.$document[0].focus(); this.activating = false; if ( mw.config.get( 'wgVisualEditorConfig' ).showBetaWelcome ) { this.showBetaWelcome(); } ve.track( 'performance.system.activation', { 'duration': ve.now() - this.timings.activationStart } ); mw.hook( 've.activationComplete' ).fire(); }, this ) ); } }; /** * Handle failed DOM load event. * * @method * @param {Object} response HTTP Response object * @param {string} status Text status message * @param {Mixed} error Thrown exception or HTTP error string */ ve.init.mw.ViewPageTarget.prototype.onLoadError = function ( response, status ) { // Don't show an error if the load was manually aborted if ( status !== 'abort' && confirm( ve.msg( 'visualeditor-loadwarning', status ) ) ) { this.load(); } else { this.activating = false; // User interface changes this.deactivate( true ); } }; /** * Handle failed token refresh event. * * @method * @param {Object} response Response object * @param {string} status Text status message * @param {Mixed} error Thrown exception or HTTP error string */ ve.init.mw.ViewPageTarget.prototype.onTokenError = function ( response, status ) { if ( confirm( ve.msg( 'visualeditor-loadwarning-token', status ) ) ) { this.load(); } else { this.activating = false; // User interface changes this.deactivate( true ); } }; /** * Handle successful DOM save event. * * @method * @param {HTMLElement} html Rendered HTML from server * @param {number} [newid] New revision id, undefined if unchanged */ ve.init.mw.ViewPageTarget.prototype.onSave = function ( html, newid ) { ve.track( 'performance.user.saveComplete', { 'duration': ve.now() - this.timings.saveDialogSave } ); if ( !this.pageExists || this.restoring ) { // This is a page creation or restoration, refresh the page this.tearDownBeforeUnloadHandler(); window.location.href = this.viewUri.extend( { 'venotify': this.restoring ? 'restored' : 'created' } ); } else { // Update watch link to match 'watch checkbox' in save dialog. // User logged in if module loaded. // Just checking for mw.page.watch is not enough because in Firefox // there is Object.prototype.watch... if ( mw.page.watch && mw.page.watch.updateWatchLink ) { var watchChecked = this.saveDialog.$saveOptions .find( '.ve-ui-mwSaveDialog-checkboxes' ) .find( '#wpWatchthis' ) .prop( 'checked' ); mw.page.watch.updateWatchLink( $( '#ca-watch a, #ca-unwatch a' ), watchChecked ? 'unwatch': 'watch' ); } // If we were explicitly editing an older version, make sure we won't // load the same old version again, now that we've saved the next edit // will be against the latest version. // TODO: What about oldid in the url? this.restoring = false; if ( newid !== undefined ) { mw.config.set( 'wgCurRevisionId', newid ); this.revid = newid; } this.saveDialog.close(); this.saveDialog.reset(); this.replacePageContent( html ); this.setupSectionEditLinks(); this.tearDownBeforeUnloadHandler(); this.deactivate( true ); mw.hook( 'postEdit' ).fire( { 'message': ve.msg( 'visualeditor-notification-saved', new mw.Title( this.pageName ).toText() ) } ); } }; /** * Handle failed DOM save event. * * @method * @param {Object} jqXHR * @param {string} status Text status message * @param {Object|null} data API response data */ ve.init.mw.ViewPageTarget.prototype.onSaveError = function ( jqXHR, status, data ) { var api, editApi, trackData = { 'duration': ve.now() - this.timings.saveDialogSave, 'retries': this.timings.saveRetries }, viewPage = this; this.saveDialog.saveButton.setDisabled( false ); this.saveDialog.$loadingIcon.hide(); this.saveDialog.clearMessage( 'api-save-error' ); // Handle empty response if ( !data ) { ve.track( 'performance.user.saveError.empty', trackData ); this.saveDialog.showMessage( 'api-save-error', ve.msg( 'visualeditor-saveerror', 'Empty server response' ), { wrap: 'error' } ); this.saveDialog.saveButton.setDisabled( true ); return; } editApi = data && data.visualeditoredit && data.visualeditoredit.edit; // Handle spam blacklist error (either from core or from Extension:SpamBlacklist) if ( editApi && editApi.spamblacklist ) { ve.track( 'performance.user.saveError.spamblacklist', trackData ); this.saveDialog.showMessage( 'api-save-error', // TODO: Use mediawiki.language equivalant of Language.php::listToText once it exists ve.msg( 'spamprotectiontext' ) + ' ' + ve.msg( 'spamprotectionmatch', editApi.spamblacklist.split( '|' ).join( ', ' ) ), { wrap: 'error' } ); this.saveDialog.saveButton.setDisabled( true ); return; } // Handle warnings/errors from Extension:AbuseFilter // TODO: Move this to a plugin if ( editApi && editApi.info && editApi.info.indexOf( 'Hit AbuseFilter:' ) === 0 && editApi.warning ) { ve.track( 'performance.user.saveError.abusefilter', trackData ); this.saveDialog.showMessage( 'api-save-error', $.parseHTML( editApi.warning ), { wrap: false } ); // Don't disable the save button. If the action is not disallowed the user may save the // edit by pressing Save again. The AbuseFilter API currently has no way to distinguish // between filter triggers that are and aren't disallowing the action. return; } // Handle token errors if ( data.error && data.error.code === 'badtoken' ) { api = new mw.Api(); viewPage.saveDialog.saveButton.setDisabled( true ); viewPage.saveDialog.$loadingIcon.show(); api.get( { // action=query&meta=userinfo and action=tokens&type=edit can't be combined // but action=query&meta=userinfo and action=query&prop=info can, however // that means we have to give it titles and deal with page ids. 'action': 'query', 'meta': 'userinfo', 'prop': 'info', // Try to send the normalised form so that it is less likely we get extra data like // data.normalised back that we don't need. 'titles': new mw.Title( viewPage.pageName ).toText(), 'indexpageids': '', 'intoken': 'edit' } ) .always( function () { viewPage.saveDialog.$loadingIcon.hide(); } ) .done( function ( data ) { var badTokenText, userMsg, userInfo = data.query && data.query.userinfo, pageInfo = data.query && data.query.pages && data.query.pageids && data.query.pageids[0] && data.query.pages[ data.query.pageids[0] ], editToken = pageInfo && pageInfo.edittoken; if ( userInfo && editToken ) { viewPage.editToken = editToken; if ( ( mw.user.isAnon() && userInfo.anon !== undefined ) || // Comparing id instead of name to pretect against possible // normalisation and against case where the user got renamed. mw.config.get( 'wgUserId' ) === userInfo.id ) { // New session is the same user still this.timings.saveRetries++; viewPage.saveDocument(); } else { // The now current session is a different user ve.track( 'performance.user.saveError.badtoken', trackData ); viewPage.saveDialog.saveButton.setDisabled( false ); // Trailing space is to separate from the other message. badTokenText = document.createTextNode( mw.msg( 'visualeditor-savedialog-error-badtoken' ) + ' ' ); if ( userInfo.anon !== undefined ) { // New session is an anonymous user mw.config.set( { // wgUserId is unset for anonymous users, not set to null 'wgUserId': undefined, // wgUserName is explicitly set to null for anonymous users, // functions like mw.user.isAnon rely on this. 'wgUserName': null } ); viewPage.saveDialog.showMessage( 'api-save-error', $( badTokenText ).add( $.parseHTML( mw.message( 'visualeditor-savedialog-identify-anon' ).parse() ) ), { wrap: 'warning' } ); } else { // New session is a different user mw.config.set( { 'wgUserId': userInfo.id, 'wgUserName': userInfo.name } ); // mediawiki.jqueryMsg has a bug with [[User:$1|$1]] (bug 51388) userMsg = 'visualeditor-savedialog-identify-user---' + userInfo.name; mw.messages.set( userMsg, mw.messages.get( 'visualeditor-savedialog-identify-user' ) .replace( /\$1/g, userInfo.name ) ); viewPage.saveDialog.showMessage( 'api-save-error', $( badTokenText ).add( $.parseHTML( mw.message( userMsg ).parse() ) ), { wrap: 'warning' } ); } } } } ); return; } // Handle captcha // Captcha "errors" usually aren't errors. We simply don't know about them ahead of time, // so we save once, then (if required) we get an error with a captcha back and try again after // the user solved the captcha. // TODO: ConfirmEdit API is horrible, there is no reliable way to know whether it is a "math", // "question" or "fancy" type of captcha. They all expose differently named properties in the // API for different things in the UI. At this point we only support the FancyCaptha which we // very intuitively detect by the presence of a "url" property. if ( editApi && editApi.captcha && editApi.captcha.url ) { ve.track( 'performance.user.saveError.captcha', trackData ); this.captcha = { input: new OO.ui.TextInputWidget(), id: editApi.captcha.id }; this.saveDialog.showMessage( 'api-save-error', $( '' ).append(
$( '' ).text( mw.msg( 'captcha-label' ) ),
document.createTextNode( mw.msg( 'colon-separator' ) ),
$( $.parseHTML( mw.message( 'fancycaptcha-edit' ).parse() ) )
.filter( 'a' ).attr( 'target', '_blank' ).end()
),
$( '' ).attr( 'src', editApi.captcha.url ),
this.captcha.input.$element
),
{
wrap: false
}
);
return;
}
// Handle (other) unknown and/or unrecoverable errors
ve.track( 'performance.user.saveError.unknown', trackData );
this.saveDialog.showMessage(
'api-save-error',
document.createTextNode(
( editApi && editApi.info ) ||
( data.error && data.error.info ) ||
( editApi && editApi.code ) ||
( data.error && data.error.code ) ||
'Unknown error'
),
{
wrap: 'error'
}
);
this.saveDialog.saveButton.setDisabled( true );
};
/**
* Handle Show changes event.
*
* @method
* @param {string} diffHtml
*/
ve.init.mw.ViewPageTarget.prototype.onShowChanges = function ( diffHtml ) {
ve.track( 'performance.user.reviewComplete', { 'duration': ve.now() - this.timings.saveDialogReview } );
// Invalidate the viewer diff on next change
this.surface.getModel().getDocument().connect( this, { 'transact': 'clearSaveDialogDiff' } );
this.saveDialog.setDiffAndReview( diffHtml );
};
/**
* Handle Serialize event.
*
* @method
* @param {string} wikitext
*/
ve.init.mw.ViewPageTarget.prototype.onSerialize = function ( wikitext ) {
ve.track( 'performance.user.reviewComplete', { 'duration': ve.now() - this.timings.saveDialogReview } );
// Invalidate the viewer wikitext on next change
this.surface.getModel().getDocument().connect( this, { 'transact': 'clearSaveDialogDiff' } );
this.saveDialog.setDiffAndReview( $( '' ).text( wikitext ) );
};
/**
* Handle failed show changes event.
*
* @method
* @param {Object} jqXHR
* @param {string} status Text status message
*/
ve.init.mw.ViewPageTarget.prototype.onShowChangesError = function ( jqXHR, status ) {
ve.track( 'performance.user.reviewError', { 'duration': ve.now() - this.timings.saveDialogReview } );
alert( ve.msg( 'visualeditor-differror', status ) );
this.saveDialog.$loadingIcon.hide();
};
/**
* Called if a call to target.serialize() failed.
*
* @method
* @param {jqXHR|null} jqXHR
* @param {string} status Text status message
*/
ve.init.mw.ViewPageTarget.prototype.onSerializeError = function ( jqXHR, status ) {
if ( this.timings.saveDialogOpen ) {
// This function can be called by the switch to wikitext button as well, so only log
// reviewError if we actually got here from the save dialog
ve.track( 'performance.user.reviewError', { 'duration': ve.now() - this.timings.saveDialogReview } );
}
alert( ve.msg( 'visualeditor-serializeerror', status ) );
this.saveDialog.$loadingIcon.hide();
};
/**
* Handle edit conflict event.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.onEditConflict = function () {
ve.track( 'performance.user.saveError.editconflict', {
'duration': ve.now() - this.timings.saveDialogSave,
'retries': this.timings.saveRetries
} );
this.saveDialog.$loadingIcon.hide();
this.saveDialog.swapPanel( 'conflict' );
};
/**
* Handle failed show changes event.
*
* @method
*/
ve.init.mw.ViewPageTarget.prototype.onNoChanges = function () {
ve.track( 'performance.user.reviewComplete', { 'duration': ve.now() - this.timings.saveDialogReview } );
this.saveDialog.$loadingIcon.hide();
this.saveDialog.swapPanel( 'nochanges' );
this.saveDialog.reviewGoodButton.setDisabled( false );
};
/**
* Handle clicks on the view tab.
*
* @method
* @param {jQuery.Event} e Mouse click event
*/
ve.init.mw.ViewPageTarget.prototype.onViewTabClick = function ( e ) {
if ( ( e.which && e.which !== 1 ) || e.shiftKey || e.altKey || e.ctrlKey || e.metaKey ) {
return;
}
if ( this.active ) {
this.deactivate();
// Prevent the edit tab's normal behavior
e.preventDefault();
} else if ( this.activating ) {
this.deactivate( true );
this.activating = false;
e.preventDefault();
}
};
/**
* Handle clicks on the save button in the toolbar.
*
* @method
* @param {jQuery.Event} e Mouse click event
*/
ve.init.mw.ViewPageTarget.prototype.onToolbarSaveButtonClick = function () {
if ( this.edited || this.restoring ) {
this.showSaveDialog();
}
};
/**
* Handle clicks on the save button in the toolbar.
*
* @method
* @param {jQuery.Event} e Mouse click event
*/
ve.init.mw.ViewPageTarget.prototype.onToolbarCancelButtonClick = function () {
this.deactivate();
};
/**
* Handle clicks on the MwMeta button in the toolbar.
*
* @method
* @param {jQuery.Event} e Mouse click event
*/
ve.init.mw.ViewPageTarget.prototype.onToolbarMetaButtonClick = function () {
this.surface.getDialogs().getWindow( 'meta' ).open();
};
/**
* Clear the diff in the save dialog.
*
* This method is bound to the 'transact' event on the document model, and unbinds itself the first
* time it runs. It's bound when the surface is set up and rebound every time a diff is loaded into
* the save dialog.
*
* @method
* @param {ve.dm.Transaction} tx Processed transaction
*/
ve.init.mw.ViewPageTarget.prototype.clearSaveDialogDiff = function () {
// Clear the diff
this.saveDialog.$reviewViewer.empty();
this.surface.getModel().getDocument().disconnect( this, { 'transact': 'clearSaveDialogDiff' } );
};
/**
* Record the time of the last transaction in response to a 'transact' event on the document.
*/
ve.init.mw.ViewPageTarget.prototype.recordLastTransactionTime = function () {
this.timings.lastTransaction = ve.now();
};
/**
* Check if the user is entering wikitext, and show a notification if they are.
*
* This check is fairly simplistic: it checks whether the content branch node the selection is in
* looks like wikitext, so it can trigger if the user types in a paragraph that has pre-existing
* wikitext-like content.
*
* This method is bound to the 'documentUpdate' event on the surface model, and unbinds itself when
* the wikitext notification is displayed.
*
* @param {ve.dm.Transaction} transaction
*/
ve.init.mw.ViewPageTarget.prototype.checkForWikitextWarning = function () {
var text, doc = this.surface.getView().getDocument(),
selection = this.surface.getModel().getSelection(),
node = doc.getNodeFromOffset( selection.start );
if ( !( node instanceof ve.ce.ContentBranchNode ) ) {
return;
}
text = ve.ce.getDomText( node.$element[0] );
if ( text.match( /\[\[|\{\{|''|