aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/settings/scan-inputs-controller.js
blob: 5895cceac26f3d3d89bcfaf18c16dcce5a1eb3bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * Copyright (C) 2020  Yomichan Authors
 *
 * 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
 * api
 */

class ScanInputsController {
    constructor(settingsController) {
        this._settingsController = settingsController;
        this._os = null;
        this._container = null;
        this._addButton = null;
        this._entries = [];
    }

    async prepare() {
        const {platform: {os}} = await api.getEnvironmentInfo();
        this._os = os;

        this._container = document.querySelector('#scan-input-list');
        this._addButton = document.querySelector('#scan-input-add');

        this._addButton.addEventListener('click', this._onAddButtonClick.bind(this), false);
        this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));

        const options = await this._settingsController.getOptions();
        this._onOptionsChanged({options});
    }

    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;
        }
        this._settingsController.modifyProfileSettings([{
            action: 'splice',
            path: 'scanning.inputs',
            start: index,
            deleteCount: 1,
            items: []
        }]);
    }

    setProperty(index, property, value) {
        const path = `scanning.inputs[${index}].${property}`;
        this._settingsController.setProfileSetting(path, value);
    }

    // Private

    _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) {
            const {include, exclude} = inputs[i];
            this._addOption(i, include, exclude);
        }
    }

    _onAddButtonClick(e) {
        e.preventDefault();

        const index = this._entries.length;
        const include = '';
        const exclude = '';
        this._addOption(index, include, exclude);
        this._settingsController.modifyProfileSettings([{
            action: 'splice',
            path: 'scanning.inputs',
            start: index,
            deleteCount: 0,
            items: [{
                include,
                exclude,
                types: {mouse: true, touch: false, pen: false}
            }]
        }]);
    }

    _addOption(index, include, exclude) {
        const field = new ScanInputField(this, index, this._os);
        this._entries.push(field);
        field.prepare(this._container, include, exclude);
    }
}

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;
    }

    prepare(container, include, exclude) {
        const node = this._instantiateTemplate('#scan-input-template');
        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]');
        const removeButton = node.querySelector('.scan-input-remove');

        this._node = node;
        container.appendChild(node);

        const isPointerTypeSupported = this._isPointerTypeSupported.bind(this);
        this._includeInputField = new KeyboardMouseInputField(includeInputNode, includeMouseButton, this._os, isPointerTypeSupported);
        this._excludeInputField = new KeyboardMouseInputField(excludeInputNode, excludeMouseButton, this._os, isPointerTypeSupported);
        this._includeInputField.prepare(include, 'modifierInputs');
        this._excludeInputField.prepare(exclude, 'modifierInputs');

        this._eventListeners.on(this._includeInputField, 'change', this._onIncludeValueChange.bind(this));
        this._eventListeners.on(this._excludeInputField, 'change', this._onExcludeValueChange.bind(this));
        this._eventListeners.addEventListener(removeButton, 'click', this._onRemoveClick.bind(this));

        for (const typeCheckbox of node.querySelectorAll('.scan-input-type-checkbox')) {
            const {property} = typeCheckbox.dataset;
            typeCheckbox.dataset.setting = `scanning.inputs[${this._index}].types.${property}`;
        }
    }

    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;
        }
    }

    _onIncludeValueChange({value}) {
        this._parent.setProperty(this._index, 'include', value);
    }

    _onExcludeValueChange({value}) {
        this._parent.setProperty(this._index, 'exclude', value);
    }

    _onRemoveClick(e) {
        e.preventDefault();
        this._parent.removeInput(this._index);
    }

    _instantiateTemplate(templateSelector) {
        const template = document.querySelector(templateSelector);
        const content = document.importNode(template.content, true);
        return content.firstChild;
    }

    _isPointerTypeSupported(pointerType) {
        if (this._node === null) { return false; }
        const node = this._node.querySelector(`input.scan-input-type-checkbox[data-type=${pointerType}]`);
        return node !== null && node.checked;
    }
}