Scan inputs controller refactor (#975)

* Add public function to clear inputs

* Return the promise

* Pass input options directly

* Assign showAdvanced

* Add more menu options
This commit is contained in:
toasted-nutbread 2020-10-31 16:26:01 -04:00 committed by GitHub
parent ab98caf842
commit 21cae0e38e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 11 deletions

View File

@ -77,6 +77,10 @@ class KeyboardMouseInputField extends EventDispatcher {
this._penPointerIds.clear(); this._penPointerIds.clear();
} }
clearInputs() {
this._updateInputs([]);
}
// Private // Private
_splitValue(value) { _splitValue(value) {
@ -169,7 +173,7 @@ class KeyboardMouseInputField extends EventDispatcher {
switch (key) { switch (key) {
case 'Escape': case 'Escape':
case 'Backspace': case 'Backspace':
this._updateInputs([]); this.clearInputs();
break; break;
default: default:
this._addInputs(this._getModifierKeys(e)); this._addInputs(this._getModifierKeys(e));

View File

@ -65,7 +65,7 @@ class ScanInputsController {
setProperty(index, property, value) { setProperty(index, property, value) {
const path = `scanning.inputs[${index}].${property}`; const path = `scanning.inputs[${index}].${property}`;
this._settingsController.setProfileSetting(path, value); return this._settingsController.setProfileSetting(path, value);
} }
instantiateTemplate(name) { instantiateTemplate(name) {
@ -93,8 +93,7 @@ class ScanInputsController {
this._entries.length = 0; this._entries.length = 0;
for (let i = 0, ii = inputs.length; i < ii; ++i) { for (let i = 0, ii = inputs.length; i < ii; ++i) {
const {include, exclude} = inputs[i]; this._addOption(i, inputs[i]);
this._addOption(i, include, exclude);
} }
this._updateCounts(); this._updateCounts();
@ -104,23 +103,22 @@ class ScanInputsController {
e.preventDefault(); e.preventDefault();
const index = this._entries.length; const index = this._entries.length;
const include = ''; const scanningInput = ScanInputsController.createDefaultMouseInput('', '');
const exclude = ''; this._addOption(index, scanningInput);
this._addOption(index, include, exclude);
this._updateCounts(); this._updateCounts();
this._modifyProfileSettings([{ this._modifyProfileSettings([{
action: 'splice', action: 'splice',
path: 'scanning.inputs', path: 'scanning.inputs',
start: index, start: index,
deleteCount: 0, deleteCount: 0,
items: [ScanInputsController.createDefaultMouseInput(include, exclude)] items: [scanningInput]
}]); }]);
} }
_addOption(index, include, exclude) { _addOption(index, scanningInput) {
const field = new ScanInputField(this, index, this._os); const field = new ScanInputField(this, index, this._os);
this._entries.push(field); this._entries.push(field);
field.prepare(this._container, include, exclude); field.prepare(this._container, scanningInput);
} }
_updateCounts() { _updateCounts() {
@ -174,7 +172,9 @@ class ScanInputField {
this._updateDataSettingTargets(); this._updateDataSettingTargets();
} }
prepare(container, include, exclude) { prepare(container, scanningInput) {
const {include, exclude, options: {showAdvanced}} = scanningInput;
const node = this._parent.instantiateTemplate('scan-input'); const node = this._parent.instantiateTemplate('scan-input');
const includeInputNode = node.querySelector('.scan-input-field[data-property=include]'); const includeInputNode = node.querySelector('.scan-input-field[data-property=include]');
const includeMouseButton = node.querySelector('.mouse-button[data-property=include]'); const includeMouseButton = node.querySelector('.mouse-button[data-property=include]');
@ -183,6 +183,8 @@ class ScanInputField {
const removeButton = node.querySelector('.scan-input-remove'); const removeButton = node.querySelector('.scan-input-remove');
const menuButton = node.querySelector('.scanning-input-menu-button'); const menuButton = node.querySelector('.scanning-input-menu-button');
node.dataset.showAdvanced = `${showAdvanced}`;
this._node = node; this._node = node;
container.appendChild(node); container.appendChild(node);
@ -198,6 +200,7 @@ class ScanInputField {
this._eventListeners.addEventListener(removeButton, 'click', this._onRemoveClick.bind(this)); this._eventListeners.addEventListener(removeButton, 'click', this._onRemoveClick.bind(this));
} }
if (menuButton !== null) { if (menuButton !== null) {
this._eventListeners.addEventListener(menuButton, 'menuOpened', this._onMenuOpened.bind(this));
this._eventListeners.addEventListener(menuButton, 'menuClosed', this._onMenuClosed.bind(this)); this._eventListeners.addEventListener(menuButton, 'menuClosed', this._onMenuClosed.bind(this));
} }
@ -217,6 +220,8 @@ class ScanInputField {
} }
} }
// Private
_onIncludeValueChange({value}) { _onIncludeValueChange({value}) {
this._parent.setProperty(this._index, 'include', value); this._parent.setProperty(this._index, 'include', value);
} }
@ -230,11 +235,33 @@ class ScanInputField {
this._removeSelf(); this._removeSelf();
} }
_onMenuOpened({detail: {menu}}) {
const showAdvanced = menu.querySelector('.popup-menu-item[data-menu-action="showAdvanced"]');
const hideAdvanced = menu.querySelector('.popup-menu-item[data-menu-action="hideAdvanced"]');
const advancedVisible = (this._node.dataset.showAdvanced === 'true');
if (showAdvanced !== null) {
showAdvanced.hidden = advancedVisible;
}
if (hideAdvanced !== null) {
hideAdvanced.hidden = !advancedVisible;
}
}
_onMenuClosed({detail: {action}}) { _onMenuClosed({detail: {action}}) {
switch (action) { switch (action) {
case 'remove': case 'remove':
this._removeSelf(); this._removeSelf();
break; break;
case 'showAdvanced':
this._setAdvancedOptionsVisible(true);
break;
case 'hideAdvanced':
this._setAdvancedOptionsVisible(false);
break;
case 'clearInputs':
this._includeInputField.clearInputs();
this._excludeInputField.clearInputs();
break;
} }
} }
@ -255,4 +282,10 @@ class ScanInputField {
_removeSelf() { _removeSelf() {
this._parent.removeInput(this._index); this._parent.removeInput(this._index);
} }
_setAdvancedOptionsVisible(showAdvanced) {
showAdvanced = !!showAdvanced;
this._node.dataset.showAdvanced = `${showAdvanced}`;
this._parent.setProperty(this._index, 'options.showAdvanced', showAdvanced);
}
} }