diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-07-03 11:56:26 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-03 11:56:26 -0400 |
commit | c13160d784caf5ca2803081171d1c01eb91f49c6 (patch) | |
tree | 88526d4c338cea194cc3d5a998d6cab17d12e23d /ext/bg/js/settings | |
parent | 1d02013642df825f3539b03c325dc51d9fd00e83 (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')
-rw-r--r-- | ext/bg/js/settings/dictionaries.js | 21 | ||||
-rw-r--r-- | ext/bg/js/settings/settings-controller.js | 29 |
2 files changed, 42 insertions, 8 deletions
diff --git a/ext/bg/js/settings/dictionaries.js b/ext/bg/js/settings/dictionaries.js index 94a71233..1e780909 100644 --- a/ext/bg/js/settings/dictionaries.js +++ b/ext/bg/js/settings/dictionaries.js @@ -16,7 +16,6 @@ */ /* global - * PageExitPrevention * api * utilBackgroundIsolate */ @@ -156,6 +155,11 @@ class SettingsDictionaryListUI extends EventDispatcher { // Overwrite } + preventPageExit() { + // Overwrite + return {end: () => {}}; + } + onDictionaryConfirmDelete(e) { e.preventDefault(); const n = document.querySelector('#dict-delete-modal'); @@ -286,10 +290,8 @@ class SettingsDictionaryEntryUI { const progressBar = this.content.querySelector('.progress-bar'); this.isDeleting = true; - const prevention = new PageExitPrevention(); + const prevention = this.parent.preventPageExit(); try { - prevention.start(); - const onProgress = ({processed, count, storeCount, storesProcesed}) => { let percent = 0.0; if (count > 0 && storesProcesed > 0) { @@ -409,6 +411,7 @@ class DictionaryController { document.querySelector('#dict-extra-template') ); this._dictionaryUI.save = () => this._settingsController.save(); + this._dictionaryUI.preventPageExit = this._preventPageExit.bind(this); this._dictionaryUI.on('databaseUpdated', this._onDatabaseUpdated.bind(this)); document.querySelector('#dict-purge-button').addEventListener('click', this._onPurgeButtonClick.bind(this), false); @@ -610,10 +613,9 @@ class DictionaryController { const dictProgress = document.querySelector('#dict-purge'); dictProgress.hidden = false; - const prevention = new PageExitPrevention(); + const prevention = this._preventPageExit(); try { - prevention.start(); this._dictionaryErrorsShow(null); this._dictionarySpinnerShow(true); @@ -649,10 +651,9 @@ class DictionaryController { const dictProgress = $('#dict-import-progress').show(); const dictImportInfo = document.querySelector('#dict-import-info'); - const prevention = new PageExitPrevention(); + const prevention = this._preventPageExit(); try { - prevention.start(); this._dictionaryErrorsShow(null); this._dictionarySpinnerShow(true); @@ -718,4 +719,8 @@ class DictionaryController { optionsFull.global.database.prefixWildcardsSupported = !!e.target.checked; await this._settingsController.save(); } + + _preventPageExit() { + return this.settingsController.preventPageExit(); + } } 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(); + } + } } |