2020-09-09 20:59:03 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2020-2022 Yomichan Authors
|
2020-09-09 20:59:03 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* global
|
|
|
|
* KeyboardMouseInputField
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ScanInputsController {
|
|
|
|
constructor(settingsController) {
|
|
|
|
this._settingsController = settingsController;
|
|
|
|
this._os = null;
|
|
|
|
this._container = null;
|
|
|
|
this._addButton = null;
|
2020-10-31 17:40:10 +00:00
|
|
|
this._scanningInputCountNodes = null;
|
2020-09-09 20:59:03 +00:00
|
|
|
this._entries = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
async prepare() {
|
2021-02-14 20:53:35 +00:00
|
|
|
const {platform: {os}} = await yomichan.api.getEnvironmentInfo();
|
2020-09-09 20:59:03 +00:00
|
|
|
this._os = os;
|
|
|
|
|
|
|
|
this._container = document.querySelector('#scan-input-list');
|
|
|
|
this._addButton = document.querySelector('#scan-input-add');
|
2020-10-31 17:40:10 +00:00
|
|
|
this._scanningInputCountNodes = document.querySelectorAll('.scanning-input-count');
|
2020-09-09 20:59:03 +00:00
|
|
|
|
|
|
|
this._addButton.addEventListener('click', this._onAddButtonClick.bind(this), false);
|
2020-10-14 23:37:46 +00:00
|
|
|
this._settingsController.on('scanInputsChanged', this._onScanInputsChanged.bind(this));
|
2020-09-09 20:59:03 +00:00
|
|
|
this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
|
|
|
|
|
2020-10-14 23:37:46 +00:00
|
|
|
this.refresh();
|
2020-09-09 20:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
removeInput(index) {
|
|
|
|
if (index < 0 || index >= this._entries.length) { return false; }
|
|
|
|
const input = this._entries[index];
|
|
|
|
input.cleanup();
|
|
|
|
this._entries.splice(index, 1);
|
|
|
|
for (let i = index, ii = this._entries.length; i < ii; ++i) {
|
|
|
|
this._entries[i].index = i;
|
|
|
|
}
|
2020-10-31 20:38:48 +00:00
|
|
|
this._updateCounts();
|
2020-10-14 23:37:46 +00:00
|
|
|
this._modifyProfileSettings([{
|
2020-09-09 20:59:03 +00:00
|
|
|
action: 'splice',
|
|
|
|
path: 'scanning.inputs',
|
|
|
|
start: index,
|
|
|
|
deleteCount: 1,
|
|
|
|
items: []
|
|
|
|
}]);
|
2020-10-14 23:37:46 +00:00
|
|
|
return true;
|
2020-09-09 20:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 21:13:24 +00:00
|
|
|
async setProperty(index, property, value, event) {
|
2020-09-09 20:59:03 +00:00
|
|
|
const path = `scanning.inputs[${index}].${property}`;
|
2020-10-31 21:13:24 +00:00
|
|
|
await this._settingsController.setProfileSetting(path, value);
|
|
|
|
if (event) {
|
|
|
|
this._triggerScanInputsChanged();
|
|
|
|
}
|
2020-09-09 20:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 01:23:42 +00:00
|
|
|
instantiateTemplate(name) {
|
|
|
|
return this._settingsController.instantiateTemplate(name);
|
|
|
|
}
|
|
|
|
|
2020-10-14 23:37:46 +00:00
|
|
|
async refresh() {
|
|
|
|
const options = await this._settingsController.getOptions();
|
|
|
|
this._onOptionsChanged({options});
|
|
|
|
}
|
|
|
|
|
2020-09-09 20:59:03 +00:00
|
|
|
// Private
|
|
|
|
|
2020-10-14 23:37:46 +00:00
|
|
|
_onScanInputsChanged({source}) {
|
|
|
|
if (source === this) { return; }
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
2020-09-09 20:59:03 +00:00
|
|
|
_onOptionsChanged({options}) {
|
|
|
|
const {inputs} = options.scanning;
|
|
|
|
|
|
|
|
for (let i = this._entries.length - 1; i >= 0; --i) {
|
|
|
|
this._entries[i].cleanup();
|
|
|
|
}
|
|
|
|
this._entries.length = 0;
|
|
|
|
|
|
|
|
for (let i = 0, ii = inputs.length; i < ii; ++i) {
|
2020-10-31 20:26:01 +00:00
|
|
|
this._addOption(i, inputs[i]);
|
2020-09-09 20:59:03 +00:00
|
|
|
}
|
2020-10-31 17:40:10 +00:00
|
|
|
|
|
|
|
this._updateCounts();
|
2020-09-09 20:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onAddButtonClick(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const index = this._entries.length;
|
2020-10-31 20:26:01 +00:00
|
|
|
const scanningInput = ScanInputsController.createDefaultMouseInput('', '');
|
|
|
|
this._addOption(index, scanningInput);
|
2020-10-31 17:40:10 +00:00
|
|
|
this._updateCounts();
|
2020-10-14 23:37:46 +00:00
|
|
|
this._modifyProfileSettings([{
|
2020-09-09 20:59:03 +00:00
|
|
|
action: 'splice',
|
|
|
|
path: 'scanning.inputs',
|
|
|
|
start: index,
|
|
|
|
deleteCount: 0,
|
2020-10-31 20:26:01 +00:00
|
|
|
items: [scanningInput]
|
2020-09-09 20:59:03 +00:00
|
|
|
}]);
|
2022-06-03 22:19:40 +00:00
|
|
|
|
|
|
|
// Scroll to bottom
|
|
|
|
const button = e.currentTarget;
|
|
|
|
const modalContainer = button.closest('.modal');
|
|
|
|
const scrollContainer = modalContainer.querySelector('.modal-body');
|
|
|
|
scrollContainer.scrollTop = scrollContainer.scrollHeight;
|
2020-09-09 20:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 20:26:01 +00:00
|
|
|
_addOption(index, scanningInput) {
|
2020-09-09 20:59:03 +00:00
|
|
|
const field = new ScanInputField(this, index, this._os);
|
|
|
|
this._entries.push(field);
|
2020-10-31 20:26:01 +00:00
|
|
|
field.prepare(this._container, scanningInput);
|
2020-09-09 20:59:03 +00:00
|
|
|
}
|
2020-10-14 23:37:46 +00:00
|
|
|
|
2020-10-31 17:40:10 +00:00
|
|
|
_updateCounts() {
|
|
|
|
const stringValue = `${this._entries.length}`;
|
|
|
|
for (const node of this._scanningInputCountNodes) {
|
|
|
|
node.textContent = stringValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-14 23:37:46 +00:00
|
|
|
async _modifyProfileSettings(targets) {
|
|
|
|
await this._settingsController.modifyProfileSettings(targets);
|
2020-10-31 21:13:24 +00:00
|
|
|
this._triggerScanInputsChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
_triggerScanInputsChanged() {
|
2020-10-14 23:37:46 +00:00
|
|
|
this._settingsController.trigger('scanInputsChanged', {source: this});
|
|
|
|
}
|
|
|
|
|
|
|
|
static createDefaultMouseInput(include, exclude) {
|
|
|
|
return {
|
|
|
|
include,
|
|
|
|
exclude,
|
|
|
|
types: {mouse: true, touch: false, pen: false},
|
|
|
|
options: {
|
|
|
|
showAdvanced: false,
|
|
|
|
searchTerms: true,
|
|
|
|
searchKanji: true,
|
|
|
|
scanOnTouchMove: true,
|
2022-06-03 21:11:32 +00:00
|
|
|
scanOnTouchPress: true,
|
|
|
|
scanOnTouchRelease: false,
|
|
|
|
scanOnPenMove: true,
|
2020-10-14 23:37:46 +00:00
|
|
|
scanOnPenHover: true,
|
2022-06-03 21:11:32 +00:00
|
|
|
scanOnPenReleaseHover: false,
|
2020-10-14 23:37:46 +00:00
|
|
|
scanOnPenPress: true,
|
|
|
|
scanOnPenRelease: false,
|
2022-06-03 21:11:32 +00:00
|
|
|
preventTouchScrolling: true,
|
|
|
|
preventPenScrolling: true
|
2020-10-14 23:37:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-09-09 20:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ScanInputField {
|
|
|
|
constructor(parent, index, os) {
|
|
|
|
this._parent = parent;
|
|
|
|
this._index = index;
|
|
|
|
this._os = os;
|
|
|
|
this._node = null;
|
|
|
|
this._includeInputField = null;
|
|
|
|
this._excludeInputField = null;
|
|
|
|
this._eventListeners = new EventListenerCollection();
|
|
|
|
}
|
|
|
|
|
|
|
|
get index() {
|
|
|
|
return this._index;
|
|
|
|
}
|
|
|
|
|
|
|
|
set index(value) {
|
|
|
|
this._index = value;
|
2020-09-27 15:47:56 +00:00
|
|
|
this._updateDataSettingTargets();
|
2020-09-09 20:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 20:26:01 +00:00
|
|
|
prepare(container, scanningInput) {
|
|
|
|
const {include, exclude, options: {showAdvanced}} = scanningInput;
|
|
|
|
|
2020-10-08 01:23:42 +00:00
|
|
|
const node = this._parent.instantiateTemplate('scan-input');
|
2020-09-12 15:22:17 +00:00
|
|
|
const includeInputNode = node.querySelector('.scan-input-field[data-property=include]');
|
|
|
|
const includeMouseButton = node.querySelector('.mouse-button[data-property=include]');
|
|
|
|
const excludeInputNode = node.querySelector('.scan-input-field[data-property=exclude]');
|
|
|
|
const excludeMouseButton = node.querySelector('.mouse-button[data-property=exclude]');
|
2020-09-09 20:59:03 +00:00
|
|
|
const removeButton = node.querySelector('.scan-input-remove');
|
2020-10-31 17:40:10 +00:00
|
|
|
const menuButton = node.querySelector('.scanning-input-menu-button');
|
2020-09-09 20:59:03 +00:00
|
|
|
|
2020-10-31 20:26:01 +00:00
|
|
|
node.dataset.showAdvanced = `${showAdvanced}`;
|
|
|
|
|
2020-09-09 20:59:03 +00:00
|
|
|
this._node = node;
|
|
|
|
container.appendChild(node);
|
|
|
|
|
2020-09-11 20:57:57 +00:00
|
|
|
const isPointerTypeSupported = this._isPointerTypeSupported.bind(this);
|
|
|
|
this._includeInputField = new KeyboardMouseInputField(includeInputNode, includeMouseButton, this._os, isPointerTypeSupported);
|
|
|
|
this._excludeInputField = new KeyboardMouseInputField(excludeInputNode, excludeMouseButton, this._os, isPointerTypeSupported);
|
2021-01-14 22:54:09 +00:00
|
|
|
this._includeInputField.prepare(null, this._splitModifiers(include), true, false);
|
|
|
|
this._excludeInputField.prepare(null, this._splitModifiers(exclude), true, false);
|
2020-09-09 20:59:03 +00:00
|
|
|
|
|
|
|
this._eventListeners.on(this._includeInputField, 'change', this._onIncludeValueChange.bind(this));
|
|
|
|
this._eventListeners.on(this._excludeInputField, 'change', this._onExcludeValueChange.bind(this));
|
2020-10-31 17:40:10 +00:00
|
|
|
if (removeButton !== null) {
|
|
|
|
this._eventListeners.addEventListener(removeButton, 'click', this._onRemoveClick.bind(this));
|
|
|
|
}
|
|
|
|
if (menuButton !== null) {
|
2021-01-20 01:52:57 +00:00
|
|
|
this._eventListeners.addEventListener(menuButton, 'menuOpen', this._onMenuOpen.bind(this));
|
|
|
|
this._eventListeners.addEventListener(menuButton, 'menuClose', this._onMenuClose.bind(this));
|
2020-10-31 17:40:10 +00:00
|
|
|
}
|
2020-09-11 18:13:52 +00:00
|
|
|
|
2020-09-27 15:47:56 +00:00
|
|
|
this._updateDataSettingTargets();
|
2020-09-09 20:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup() {
|
|
|
|
this._eventListeners.removeAllEventListeners();
|
|
|
|
if (this._includeInputField !== null) {
|
|
|
|
this._includeInputField.cleanup();
|
|
|
|
this._includeInputField = null;
|
|
|
|
}
|
|
|
|
if (this._node !== null) {
|
|
|
|
const parent = this._node.parentNode;
|
|
|
|
if (parent !== null) { parent.removeChild(this._node); }
|
|
|
|
this._node = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 20:26:01 +00:00
|
|
|
// Private
|
|
|
|
|
2021-01-14 22:54:09 +00:00
|
|
|
_onIncludeValueChange({modifiers}) {
|
|
|
|
modifiers = this._joinModifiers(modifiers);
|
|
|
|
this._parent.setProperty(this._index, 'include', modifiers, true);
|
2020-09-09 20:59:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 22:54:09 +00:00
|
|
|
_onExcludeValueChange({modifiers}) {
|
|
|
|
modifiers = this._joinModifiers(modifiers);
|
|
|
|
this._parent.setProperty(this._index, 'exclude', modifiers, true);
|
2020-09-09 20:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onRemoveClick(e) {
|
|
|
|
e.preventDefault();
|
2020-10-31 17:40:10 +00:00
|
|
|
this._removeSelf();
|
|
|
|
}
|
|
|
|
|
2021-01-20 01:52:57 +00:00
|
|
|
_onMenuOpen(e) {
|
2021-01-24 02:07:45 +00:00
|
|
|
const bodyNode = e.detail.menu.bodyNode;
|
|
|
|
const showAdvanced = bodyNode.querySelector('.popup-menu-item[data-menu-action="showAdvanced"]');
|
|
|
|
const hideAdvanced = bodyNode.querySelector('.popup-menu-item[data-menu-action="hideAdvanced"]');
|
2020-10-31 20:26:01 +00:00
|
|
|
const advancedVisible = (this._node.dataset.showAdvanced === 'true');
|
|
|
|
if (showAdvanced !== null) {
|
|
|
|
showAdvanced.hidden = advancedVisible;
|
|
|
|
}
|
|
|
|
if (hideAdvanced !== null) {
|
|
|
|
hideAdvanced.hidden = !advancedVisible;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 01:52:57 +00:00
|
|
|
_onMenuClose(e) {
|
|
|
|
switch (e.detail.action) {
|
2020-10-31 17:40:10 +00:00
|
|
|
case 'remove':
|
|
|
|
this._removeSelf();
|
|
|
|
break;
|
2020-10-31 20:26:01 +00:00
|
|
|
case 'showAdvanced':
|
|
|
|
this._setAdvancedOptionsVisible(true);
|
|
|
|
break;
|
|
|
|
case 'hideAdvanced':
|
|
|
|
this._setAdvancedOptionsVisible(false);
|
|
|
|
break;
|
|
|
|
case 'clearInputs':
|
|
|
|
this._includeInputField.clearInputs();
|
|
|
|
this._excludeInputField.clearInputs();
|
|
|
|
break;
|
2020-10-31 17:40:10 +00:00
|
|
|
}
|
2020-09-09 20:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 20:57:57 +00:00
|
|
|
_isPointerTypeSupported(pointerType) {
|
|
|
|
if (this._node === null) { return false; }
|
2020-09-12 17:20:02 +00:00
|
|
|
const node = this._node.querySelector(`input.scan-input-settings-checkbox[data-property="types.${pointerType}"]`);
|
2020-09-11 20:57:57 +00:00
|
|
|
return node !== null && node.checked;
|
|
|
|
}
|
2020-09-27 15:47:56 +00:00
|
|
|
|
|
|
|
_updateDataSettingTargets() {
|
|
|
|
const index = this._index;
|
|
|
|
for (const typeCheckbox of this._node.querySelectorAll('.scan-input-settings-checkbox')) {
|
|
|
|
const {property} = typeCheckbox.dataset;
|
|
|
|
typeCheckbox.dataset.setting = `scanning.inputs[${index}].${property}`;
|
|
|
|
}
|
|
|
|
}
|
2020-10-31 17:40:10 +00:00
|
|
|
|
|
|
|
_removeSelf() {
|
|
|
|
this._parent.removeInput(this._index);
|
|
|
|
}
|
2020-10-31 20:26:01 +00:00
|
|
|
|
|
|
|
_setAdvancedOptionsVisible(showAdvanced) {
|
|
|
|
showAdvanced = !!showAdvanced;
|
|
|
|
this._node.dataset.showAdvanced = `${showAdvanced}`;
|
2020-10-31 21:13:24 +00:00
|
|
|
this._parent.setProperty(this._index, 'options.showAdvanced', showAdvanced, false);
|
2020-10-31 20:26:01 +00:00
|
|
|
}
|
2021-01-14 22:54:09 +00:00
|
|
|
|
|
|
|
_splitModifiers(modifiersString) {
|
|
|
|
return modifiersString.split(/[,;\s]+/).map((v) => v.trim().toLowerCase()).filter((v) => v.length > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
_joinModifiers(modifiersArray) {
|
|
|
|
return modifiersArray.join(', ');
|
|
|
|
}
|
2020-09-09 20:59:03 +00:00
|
|
|
}
|