aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/settings/settings-controller.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-07-03 11:56:26 -0400
committerGitHub <noreply@github.com>2020-07-03 11:56:26 -0400
commitc13160d784caf5ca2803081171d1c01eb91f49c6 (patch)
tree88526d4c338cea194cc3d5a998d6cab17d12e23d /ext/bg/js/settings/settings-controller.js
parent1d02013642df825f3539b03c325dc51d9fd00e83 (diff)
Page exit prevention refactor (#637)
* Add page exit prevention functionality to SettingsController * Update dictionary controller to use new page exit prevention system * Remove page-exit-prevention.js
Diffstat (limited to 'ext/bg/js/settings/settings-controller.js')
-rw-r--r--ext/bg/js/settings/settings-controller.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/ext/bg/js/settings/settings-controller.js b/ext/bg/js/settings/settings-controller.js
index 87dea408..6479714d 100644
--- a/ext/bg/js/settings/settings-controller.js
+++ b/ext/bg/js/settings/settings-controller.js
@@ -26,6 +26,8 @@ class SettingsController extends EventDispatcher {
super();
this._profileIndex = profileIndex;
this._source = yomichan.generateId(16);
+ this._pageExitPreventions = new Set();
+ this._pageExitPreventionEventListeners = new EventListenerCollection();
}
get source() {
@@ -109,6 +111,16 @@ class SettingsController extends EventDispatcher {
return {index: this._profileIndex};
}
+ preventPageExit() {
+ const obj = {end: null};
+ obj.end = this._endPreventPageExit.bind(this, obj);
+ if (this._pageExitPreventionEventListeners.size === 0) {
+ this._pageExitPreventionEventListeners.addEventListener(window, 'beforeunload', this._onBeforeUnload.bind(this), false);
+ }
+ this._pageExitPreventions.add(obj);
+ return obj;
+ }
+
// Private
_setProfileIndex(value) {
@@ -147,4 +159,21 @@ class SettingsController extends EventDispatcher {
targets = this._setupTargets(targets, extraFields);
return await api.modifySettings(targets, this._source);
}
+
+ _onBeforeUnload(e) {
+ if (this._pageExitPreventions.size === 0) {
+ return;
+ }
+
+ e.preventDefault();
+ e.returnValue = '';
+ return '';
+ }
+
+ _endPreventPageExit(obj) {
+ this._pageExitPreventions.delete(obj);
+ if (this._pageExitPreventions.size === 0) {
+ this._pageExitPreventionEventListeners.removeAllEventListeners();
+ }
+ }
}