Alex Monk has uploaded a new change for review.
https://gerrit.wikimedia.org/r/190144
Change subject: Send abort event on unload
......................................................................
Send abort event on unload
Be careful, I had issues testing this.
Change-Id: I0ce721e24e69c31318064c6b443c1bfe01077546
---
M modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.js
M modules/ve-mw/init/ve.init.mw.trackSubscriber.js
2 files changed, 73 insertions(+), 12 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor
refs/changes/44/190144/1
diff --git a/modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.js
b/modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.js
index 926ed57..273804d 100644
--- a/modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.js
+++ b/modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.js
@@ -36,6 +36,8 @@
this.saveDialog = null;
this.onBeforeUnloadFallback = null;
this.onBeforeUnloadHandler = null;
+ this.onUnloadFallback = null;
+ this.onUnloadHandler = null;
this.active = false;
this.activating = false;
this.deactivating = false;
@@ -378,7 +380,7 @@
// Check we got as far as setting up the surface
if ( this.active ) {
- this.tearDownBeforeUnloadHandler();
+ this.tearDownUnloadHandlers();
// If we got as far as setting up the surface, tear that down
promises.push( this.tearDownSurface() );
}
@@ -479,7 +481,7 @@
this.attachToolbarSaveButton();
this.restoreScrollPosition();
this.restoreEditSection();
- this.setupBeforeUnloadHandler();
+ this.setupUnloadHandlers();
this.maybeShowDialogs();
this.activatingDeferred.resolve();
mw.hook( 've.activationComplete' ).fire();
@@ -541,7 +543,7 @@
this.saveDeferred.resolve();
if ( !this.pageExists || this.restoring ) {
// This is a page creation or restoration, refresh the page
- this.tearDownBeforeUnloadHandler();
+ this.tearDownUnloadHandlers();
newUrlParams = newid === undefined ? {} : { venotify:
this.restoring ? 'restored' : 'created' };
if ( isRedirect ) {
@@ -1502,15 +1504,18 @@
};
/**
- * Add onbeforeunload handler.
+ * Add onunload and unbeforeunload handlesr.
*
* @method
*/
-ve.init.mw.ViewPageTarget.prototype.setupBeforeUnloadHandler = function () {
- // Remember any already set on before unload handler
+ve.init.mw.ViewPageTarget.prototype.setupUnloadHandlers = function () {
+ // Remember any already set on handlers
this.onBeforeUnloadFallback = window.onbeforeunload;
- // Attach before unload handler
+ this.onUnloadFallback = window.onunload;
+ // Attach our handlers
window.onbeforeunload = this.onBeforeUnloadHandler =
this.onBeforeUnload.bind( this );
+ window.onunload = this.onUnloadHandler = this.onUnload.bind( this );
+
// Attach page show handlers
if ( window.addEventListener ) {
window.addEventListener( 'pageshow', this.onPageShow.bind( this
), false );
@@ -1518,15 +1523,15 @@
window.attachEvent( 'pageshow', this.onPageShow.bind( this ) );
}
};
-
/**
- * Remove onbeforunload handler.
+ * Remove onunload and onbeforunload handlers.
*
* @method
*/
-ve.init.mw.ViewPageTarget.prototype.tearDownBeforeUnloadHandler = function () {
- // Restore whatever previous onbeforeload hook existed
+ve.init.mw.ViewPageTarget.prototype.tearDownUnloadHandlers = function () {
+ // Restore whatever previous onunload/onbeforeunload hooks existed
window.onbeforeunload = this.onBeforeUnloadFallback;
+ window.onunload = this.onUnloadFallback;
};
/**
@@ -1603,8 +1608,9 @@
* @method
*/
ve.init.mw.ViewPageTarget.prototype.onPageShow = function () {
- // Re-add onbeforeunload handler
+ // Re-add onunload and onbeforeunload handlers
window.onbeforeunload = this.onBeforeUnloadHandler;
+ window.onunload = this.onUnloadHandler;
};
/**
@@ -1648,6 +1654,46 @@
};
/**
+ * Handle unload event.
+ *
+ * @method
+ */
+ve.init.mw.ViewPageTarget.prototype.onUnload = function () {
+ var fallbackResult,
+ message,
+ onUnloadHandler = this.onUnloadHandler;
+ // Check if someone already set on onunload hook
+ if ( this.onUnloadFallback ) {
+ // Get the result of their onunload hook
+ fallbackResult = this.onBeforeUnloadFallback();
+ }
+ // Check if their onunload hook returned something
+ if ( fallbackResult !== undefined ) {
+ // Exit here, returning their message
+ message = fallbackResult;
+ } else {
+ // Override if submitting
+ if ( this.submitting ) {
+ return undefined;
+ }
+ if ( this.edited ) {
+ ve.track( 'mwedit.abort', { mechanism: 'navigate' } );
+ } else {
+ ve.track( 'mwedit.abort', { type: 'nochange',
mechanism: 'navigate' } );
+ }
+ }
+ // Unset the onunload handler so we don't break page caching in Firefox
+ window.onunload = null;
+ if ( message !== undefined ) {
+ // ...but if the user chooses not to leave the page, we need to
rebind it
+ setTimeout( function () {
+ window.onunload = onUnloadHandler;
+ } );
+ return message;
+ }
+};
+
+/**
* Switches to the wikitext editor, either keeping (default) or discarding
changes.
*
* @param {boolean} [discardChanges] Whether to discard changes or not.
diff --git a/modules/ve-mw/init/ve.init.mw.trackSubscriber.js
b/modules/ve-mw/init/ve.init.mw.trackSubscriber.js
index d73a43c..4f13949 100644
--- a/modules/ve-mw/init/ve.init.mw.trackSubscriber.js
+++ b/modules/ve-mw/init/ve.init.mw.trackSubscriber.js
@@ -71,6 +71,21 @@
if ( action === 'init' ) {
// Regenerate editingSessionId
editingSessionId =
mw.user.generateRandomSessionId();
+ } else if ( action === 'abort' && data.type ===
undefined ) {
+ if (
+ lastEventWithAction.saveAttempt &&
+ lastEventWithAction.saveSuccess ===
undefined &&
+ lastEventWithAction.saveFailure ===
undefined
+ ) {
+ data.type = 'abandonMidsave';
+ } else if (
+ lastEventWithAction.init &&
+ lastEventWithAction.ready === undefined
+ ) {
+ data.type = 'preinit';
+ } else {
+ data.type = 'abandon';
+ }
}
newData = $.extend( {
version: 1,
--
To view, visit https://gerrit.wikimedia.org/r/190144
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I0ce721e24e69c31318064c6b443c1bfe01077546
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Alex Monk <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits