aboutsummaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-05-07 19:38:09 -0400
committerGitHub <noreply@github.com>2020-05-07 19:38:09 -0400
commitedb86d9ec32d3d8719ad69b23cf32b139f1df40e (patch)
tree52fa4fa686b92c51f890cc095e9e2025ead866bb /ext
parentcaca9869c4c43dc1c9b286d512468e1802de6e73 (diff)
Organize frontend functions (#513)
Diffstat (limited to 'ext')
-rw-r--r--ext/fg/js/frontend.js170
1 files changed, 86 insertions, 84 deletions
diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js
index f6cce123..9de6597e 100644
--- a/ext/fg/js/frontend.js
+++ b/ext/fg/js/frontend.js
@@ -95,6 +95,58 @@ class Frontend {
}
}
+ async setPopup(popup) {
+ this._textScanner.clearSelection(true);
+ this._popup = popup;
+ await popup.setOptionsContext(await this.getOptionsContext(), this._id);
+ }
+
+ setDisabledOverride(disabled) {
+ this._disabledOverride = disabled;
+ this._updateTextScannerEnabled();
+ }
+
+ async setTextSource(textSource) {
+ await this._onSearchSource(textSource, 'script');
+ this._textScanner.setCurrentTextSource(textSource);
+ }
+
+ async getOptionsContext() {
+ const url = this._getUrl !== null ? await this._getUrl() : window.location.href;
+ const depth = this._popup.depth;
+ const modifierKeys = [...this._activeModifiers];
+ return {depth, url, modifierKeys};
+ }
+
+ async updateOptions() {
+ const optionsContext = await this.getOptionsContext();
+ this._options = await apiOptionsGet(optionsContext);
+ this._textScanner.setOptions(this._options);
+ this._updateTextScannerEnabled();
+
+ const ignoreNodes = ['.scan-disable', '.scan-disable *'];
+ if (!this._options.scanning.enableOnPopupExpressions) {
+ ignoreNodes.push('.source-text', '.source-text *');
+ }
+ this._textScanner.ignoreNodes = ignoreNodes.join(',');
+
+ await this._popup.setOptionsContext(optionsContext, this._id);
+
+ this._updateContentScale();
+
+ const textSourceCurrent = this._textScanner.getCurrentTextSource();
+ const causeCurrent = this._textScanner.causeCurrent;
+ if (textSourceCurrent !== null && causeCurrent !== null) {
+ await this._onSearchSource(textSourceCurrent, causeCurrent);
+ }
+ }
+
+ showContentCompleted() {
+ return this._lastShowPromise;
+ }
+
+ // Private
+
_onResize() {
this._updatePopupPosition();
}
@@ -136,50 +188,20 @@ class Frontend {
this._updateContentScale();
}
- setDisabledOverride(disabled) {
- this._disabledOverride = disabled;
- this._updateTextScannerEnabled();
- }
-
- async setPopup(popup) {
- this._textScanner.clearSelection(true);
- this._popup = popup;
- await popup.setOptionsContext(await this.getOptionsContext(), this._id);
- }
-
- async updateOptions() {
- const optionsContext = await this.getOptionsContext();
- this._options = await apiOptionsGet(optionsContext);
- this._textScanner.setOptions(this._options);
- this._updateTextScannerEnabled();
-
- const ignoreNodes = ['.scan-disable', '.scan-disable *'];
- if (!this._options.scanning.enableOnPopupExpressions) {
- ignoreNodes.push('.source-text', '.source-text *');
- }
- this._textScanner.ignoreNodes = ignoreNodes.join(',');
-
- await this._popup.setOptionsContext(optionsContext, this._id);
-
- this._updateContentScale();
-
- const textSourceCurrent = this._textScanner.getCurrentTextSource();
- const causeCurrent = this._textScanner.causeCurrent;
- if (textSourceCurrent !== null && causeCurrent !== null) {
- await this._onSearchSource(textSourceCurrent, causeCurrent);
- }
+ _onClearSelection({passive}) {
+ this._popup.hide(!passive);
+ this._popup.clearAutoPlayTimer();
+ this._updatePendingOptions();
}
- async _updatePendingOptions() {
- if (this._optionsUpdatePending) {
- this._optionsUpdatePending = false;
- await this.updateOptions();
+ async _onActiveModifiersChanged({modifiers}) {
+ if (areSetsEqual(modifiers, this._activeModifiers)) { return; }
+ this._activeModifiers = modifiers;
+ if (await this._popup.isVisible()) {
+ this._optionsUpdatePending = true;
+ return;
}
- }
-
- async setTextSource(textSource) {
- await this._onSearchSource(textSource, 'script');
- this._textScanner.setCurrentTextSource(textSource);
+ await this.updateOptions();
}
async _onSearchSource(textSource, cause) {
@@ -216,21 +238,6 @@ class Frontend {
return results;
}
- _showContent(textSource, focus, definitions, type, optionsContext) {
- const {url} = optionsContext;
- const sentence = docSentenceExtract(textSource, this._options.anki.sentenceExt);
- this._showPopupContent(
- textSource,
- optionsContext,
- type,
- {definitions, context: {sentence, url, focus, disableHistory: true}}
- );
- }
-
- showContentCompleted() {
- return this._lastShowPromise;
- }
-
async _findTerms(textSource, optionsContext) {
const searchText = this._textScanner.getTextSourceContent(textSource, this._options.scanning.length);
if (searchText.length === 0) { return null; }
@@ -255,27 +262,15 @@ class Frontend {
return {definitions, type: 'kanji'};
}
- _onClearSelection({passive}) {
- this._popup.hide(!passive);
- this._popup.clearAutoPlayTimer();
- this._updatePendingOptions();
- }
-
- async _onActiveModifiersChanged({modifiers}) {
- if (areSetsEqual(modifiers, this._activeModifiers)) { return; }
- this._activeModifiers = modifiers;
- if (await this._popup.isVisible()) {
- this._optionsUpdatePending = true;
- return;
- }
- await this.updateOptions();
- }
-
- async getOptionsContext() {
- const url = this._getUrl !== null ? await this._getUrl() : window.location.href;
- const depth = this._popup.depth;
- const modifierKeys = [...this._activeModifiers];
- return {depth, url, modifierKeys};
+ _showContent(textSource, focus, definitions, type, optionsContext) {
+ const {url} = optionsContext;
+ const sentence = docSentenceExtract(textSource, this._options.anki.sentenceExt);
+ this._showPopupContent(
+ textSource,
+ optionsContext,
+ type,
+ {definitions, context: {sentence, url, focus, disableHistory: true}}
+ );
}
_showPopupContent(textSource, optionsContext, type=null, details=null) {
@@ -290,6 +285,13 @@ class Frontend {
return this._lastShowPromise;
}
+ async _updatePendingOptions() {
+ if (this._optionsUpdatePending) {
+ this._optionsUpdatePending = false;
+ await this.updateOptions();
+ }
+ }
+
_updateTextScannerEnabled() {
const enabled = (
this._options.general.enable &&
@@ -321,6 +323,13 @@ class Frontend {
this._updatePopupPosition();
}
+ async _updatePopupPosition() {
+ const textSource = this._textScanner.getCurrentTextSource();
+ if (textSource !== null && await this._popup.isVisible()) {
+ this._showPopupContent(textSource, await this.getOptionsContext());
+ }
+ }
+
_broadcastRootPopupInformation() {
if (!this._popup.isProxy() && this._popup.depth === 0 && this._popup.frameId === 0) {
apiBroadcastTab('rootPopupInformation', {popupId: this._popup.id, frameId: this._popup.frameId});
@@ -334,11 +343,4 @@ class Frontend {
title: document.title
});
}
-
- async _updatePopupPosition() {
- const textSource = this._textScanner.getCurrentTextSource();
- if (textSource !== null && await this._popup.isVisible()) {
- this._showPopupContent(textSource, await this.getOptionsContext());
- }
- }
}