Search refactoring (#725)

* Rename node fields to disambiguate

* Change order
This commit is contained in:
toasted-nutbread 2020-08-09 21:03:11 -04:00 committed by GitHub
parent 7b1838a282
commit f502dd4f21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,18 +26,18 @@
class DisplaySearch extends Display { class DisplaySearch extends Display {
constructor() { constructor() {
super(document.querySelector('#spinner'), document.querySelector('#content')); super(document.querySelector('#spinner'), document.querySelector('#content'));
this._searchButton = document.querySelector('#search');
this._queryInput = document.querySelector('#query');
this._introElement = document.querySelector('#intro');
this._clipboardMonitorEnableCheckbox = document.querySelector('#clipboard-monitor-enable');
this._wanakanaEnableCheckbox = document.querySelector('#wanakana-enable');
this._isPrepared = false; this._isPrepared = false;
this._search = document.querySelector('#search');
this._query = document.querySelector('#query');
this._intro = document.querySelector('#intro');
this._clipboardMonitorEnable = document.querySelector('#clipboard-monitor-enable');
this._wanakanaEnable = document.querySelector('#wanakana-enable');
this._introVisible = true; this._introVisible = true;
this._introAnimationTimer = null; this._introAnimationTimer = null;
this._clipboardMonitorEnabled = false;
this._clipboardMonitor = new ClipboardMonitor({ this._clipboardMonitor = new ClipboardMonitor({
getClipboard: api.clipboardGet.bind(api) getClipboard: api.clipboardGet.bind(api)
}); });
this._clipboardMonitorEnabled = false;
this._onKeyDownIgnoreKeys = new Map([ this._onKeyDownIgnoreKeys = new Map([
['ANY_MOD', new Set([ ['ANY_MOD', new Set([
'Tab', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'PageDown', 'PageUp', 'Home', 'End', 'Tab', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'PageDown', 'PageUp', 'Home', 'End',
@ -71,18 +71,18 @@ class DisplaySearch extends Display {
const options = this.getOptions(); const options = this.getOptions();
if (options.general.enableWanakana === true) { if (options.general.enableWanakana === true) {
this._wanakanaEnable.checked = true; this._wanakanaEnableCheckbox.checked = true;
wanakana.bind(this._query); wanakana.bind(this._queryInput);
} else { } else {
this._wanakanaEnable.checked = false; this._wanakanaEnableCheckbox.checked = false;
} }
this._search.addEventListener('click', this._onSearch.bind(this), false); this._searchButton.addEventListener('click', this._onSearch.bind(this), false);
this._query.addEventListener('input', this._onSearchInput.bind(this), false); this._queryInput.addEventListener('input', this._onSearchInput.bind(this), false);
this._wanakanaEnable.addEventListener('change', this._onWanakanaEnableChange.bind(this)); this._wanakanaEnableCheckbox.addEventListener('change', this._onWanakanaEnableChange.bind(this));
window.addEventListener('copy', this._onCopy.bind(this)); window.addEventListener('copy', this._onCopy.bind(this));
this._clipboardMonitor.on('change', this._onExternalSearchUpdate.bind(this)); this._clipboardMonitor.on('change', this._onExternalSearchUpdate.bind(this));
this._clipboardMonitorEnable.addEventListener('change', this._onClipboardMonitorEnableChange.bind(this)); this._clipboardMonitorEnableCheckbox.addEventListener('change', this._onClipboardMonitorEnableChange.bind(this));
this._updateSearchButton(); this._updateSearchButton();
this._onModeChange(); this._onModeChange();
@ -95,12 +95,12 @@ class DisplaySearch extends Display {
} }
onEscape() { onEscape() {
if (this._query === null) { if (this._queryInput === null) {
return; return;
} }
this._query.focus(); this._queryInput.focus();
this._query.select(); this._queryInput.select();
} }
onKeyDown(e) { onKeyDown(e) {
@ -124,15 +124,15 @@ class DisplaySearch extends Display {
} }
} }
if (!super.onKeyDown(e) && !preventFocus && document.activeElement !== this._query) { if (!super.onKeyDown(e) && !preventFocus && document.activeElement !== this._queryInput) {
this._query.focus({preventScroll: true}); this._queryInput.focus({preventScroll: true});
} }
} }
async updateOptions() { async updateOptions() {
await super.updateOptions(); await super.updateOptions();
if (!this._isPrepared) { return; } if (!this._isPrepared) { return; }
const query = this._query.value; const query = this._queryInput.value;
if (query) { if (query) {
this._onSearchQueryUpdated(query, false); this._onSearchQueryUpdated(query, false);
} }
@ -159,7 +159,7 @@ class DisplaySearch extends Display {
case 'kanji': case 'kanji':
animate = content.animate; animate = content.animate;
valid = content.definitions.length > 0; valid = content.definitions.length > 0;
this._query.blur(); this._queryInput.blur();
break; break;
case 'clear': case 'clear':
valid = false; valid = false;
@ -170,7 +170,7 @@ class DisplaySearch extends Display {
if (typeof source !== 'string') { source = ''; } if (typeof source !== 'string') { source = ''; }
this._query.value = source; this._queryInput.value = source;
this._setIntroVisible(!valid, animate); this._setIntroVisible(!valid, animate);
this._updateSearchButton(); this._updateSearchButton();
} }
@ -178,20 +178,20 @@ class DisplaySearch extends Display {
_onSearchInput() { _onSearchInput() {
this._updateSearchButton(); this._updateSearchButton();
const queryElementRect = this._query.getBoundingClientRect(); const queryElementRect = this._queryInput.getBoundingClientRect();
if (queryElementRect.top < 0 || queryElementRect.bottom > window.innerHeight) { if (queryElementRect.top < 0 || queryElementRect.bottom > window.innerHeight) {
this._query.scrollIntoView(); this._queryInput.scrollIntoView();
} }
} }
_onSearch(e) { _onSearch(e) {
if (this._query === null) { if (this._queryInput === null) {
return; return;
} }
e.preventDefault(); e.preventDefault();
const query = this._query.value; const query = this._queryInput.value;
this._onSearchQueryUpdated(query, true); this._onSearchQueryUpdated(query, true);
} }
@ -227,9 +227,9 @@ class DisplaySearch extends Display {
_onWanakanaEnableChange(e) { _onWanakanaEnableChange(e) {
const value = e.target.checked; const value = e.target.checked;
if (value) { if (value) {
wanakana.bind(this._query); wanakana.bind(this._queryInput);
} else { } else {
wanakana.unbind(this._query); wanakana.unbind(this._queryInput);
} }
api.modifySettings([{ api.modifySettings([{
action: 'set', action: 'set',
@ -253,7 +253,7 @@ class DisplaySearch extends Display {
} }
_isWanakanaEnabled() { _isWanakanaEnabled() {
return this._wanakanaEnable !== null && this._wanakanaEnable.checked; return this._wanakanaEnableCheckbox !== null && this._wanakanaEnableCheckbox.checked;
} }
_setIntroVisible(visible, animate) { _setIntroVisible(visible, animate) {
@ -263,7 +263,7 @@ class DisplaySearch extends Display {
this._introVisible = visible; this._introVisible = visible;
if (this._intro === null) { if (this._introElement === null) {
return; return;
} }
@ -282,38 +282,38 @@ class DisplaySearch extends Display {
_showIntro(animate) { _showIntro(animate) {
if (animate) { if (animate) {
const duration = 0.4; const duration = 0.4;
this._intro.style.transition = ''; this._introElement.style.transition = '';
this._intro.style.height = ''; this._introElement.style.height = '';
const size = this._intro.getBoundingClientRect(); const size = this._introElement.getBoundingClientRect();
this._intro.style.height = '0px'; this._introElement.style.height = '0px';
this._intro.style.transition = `height ${duration}s ease-in-out 0s`; this._introElement.style.transition = `height ${duration}s ease-in-out 0s`;
window.getComputedStyle(this._intro).getPropertyValue('height'); // Commits height so next line can start animation window.getComputedStyle(this._introElement).getPropertyValue('height'); // Commits height so next line can start animation
this._intro.style.height = `${size.height}px`; this._introElement.style.height = `${size.height}px`;
this._introAnimationTimer = setTimeout(() => { this._introAnimationTimer = setTimeout(() => {
this._intro.style.height = ''; this._introElement.style.height = '';
this._introAnimationTimer = null; this._introAnimationTimer = null;
}, duration * 1000); }, duration * 1000);
} else { } else {
this._intro.style.transition = ''; this._introElement.style.transition = '';
this._intro.style.height = ''; this._introElement.style.height = '';
} }
} }
_hideIntro(animate) { _hideIntro(animate) {
if (animate) { if (animate) {
const duration = 0.4; const duration = 0.4;
const size = this._intro.getBoundingClientRect(); const size = this._introElement.getBoundingClientRect();
this._intro.style.height = `${size.height}px`; this._introElement.style.height = `${size.height}px`;
this._intro.style.transition = `height ${duration}s ease-in-out 0s`; this._introElement.style.transition = `height ${duration}s ease-in-out 0s`;
window.getComputedStyle(this._intro).getPropertyValue('height'); // Commits height so next line can start animation window.getComputedStyle(this._introElement).getPropertyValue('height'); // Commits height so next line can start animation
} else { } else {
this._intro.style.transition = ''; this._introElement.style.transition = '';
} }
this._intro.style.height = '0'; this._introElement.style.height = '0';
} }
_updateSearchButton() { _updateSearchButton() {
this._search.disabled = this._introVisible && (this._query === null || this._query.value.length === 0); this._searchButton.disabled = this._introVisible && (this._queryInput === null || this._queryInput.value.length === 0);
} }
async _prepareNestedPopups() { async _prepareNestedPopups() {
@ -367,7 +367,7 @@ class DisplaySearch extends Display {
_updateClipboardMonitorEnabled() { _updateClipboardMonitorEnabled() {
const mode = this.mode; const mode = this.mode;
const enabled = this._clipboardMonitorEnabled; const enabled = this._clipboardMonitorEnabled;
this._clipboardMonitorEnable.checked = enabled; this._clipboardMonitorEnableCheckbox.checked = enabled;
if (enabled && mode !== 'popup') { if (enabled && mode !== 'popup') {
this._clipboardMonitor.start(); this._clipboardMonitor.start();
} else { } else {