aboutsummaryrefslogtreecommitdiff
path: root/ext/js/pages/settings/sentence-termination-characters-controller.js
blob: c28fd2783bf7209a3695ece8941360b18c2389cd (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * Copyright (C) 2023       Yomitan Authors
 * Copyright (C) 2021-2022  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/>.
 */

class SentenceTerminationCharactersController {
    constructor(settingsController) {
        this._settingsController = settingsController;
        this._entries = [];
        this._addButton = null;
        this._resetButton = null;
        this._listTable = null;
        this._listContainer = null;
        this._emptyIndicator = null;
    }

    get settingsController() {
        return this._settingsController;
    }

    async prepare() {
        this._addButton = document.querySelector('#sentence-termination-character-list-add');
        this._resetButton = document.querySelector('#sentence-termination-character-list-reset');
        this._listTable = document.querySelector('#sentence-termination-character-list-table');
        this._listContainer = document.querySelector('#sentence-termination-character-list');
        this._emptyIndicator = document.querySelector('#sentence-termination-character-list-empty');

        this._addButton.addEventListener('click', this._onAddClick.bind(this));
        this._resetButton.addEventListener('click', this._onResetClick.bind(this));
        this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));

        await this._updateOptions();
    }

    async addEntry(terminationCharacterEntry) {
        const options = await this._settingsController.getOptions();
        const {sentenceParsing: {terminationCharacters}} = options;

        await this._settingsController.modifyProfileSettings([{
            action: 'splice',
            path: 'sentenceParsing.terminationCharacters',
            start: terminationCharacters.length,
            deleteCount: 0,
            items: [terminationCharacterEntry]
        }]);

        await this._updateOptions();
    }

    async deleteEntry(index) {
        const options = await this._settingsController.getOptions();
        const {sentenceParsing: {terminationCharacters}} = options;

        if (index < 0 || index >= terminationCharacters.length) { return false; }

        await this._settingsController.modifyProfileSettings([{
            action: 'splice',
            path: 'sentenceParsing.terminationCharacters',
            start: index,
            deleteCount: 1,
            items: []
        }]);

        await this._updateOptions();
        return true;
    }

    async modifyProfileSettings(targets) {
        return await this._settingsController.modifyProfileSettings(targets);
    }

    // Private

    _onOptionsChanged({options}) {
        for (const entry of this._entries) {
            entry.cleanup();
        }

        this._entries = [];
        const {sentenceParsing: {terminationCharacters}} = options;

        for (let i = 0, ii = terminationCharacters.length; i < ii; ++i) {
            const terminationCharacterEntry = terminationCharacters[i];
            const node = this._settingsController.instantiateTemplate('sentence-termination-character-entry');
            this._listContainer.appendChild(node);
            const entry = new SentenceTerminationCharacterEntry(this, terminationCharacterEntry, i, node);
            this._entries.push(entry);
            entry.prepare();
        }

        this._listTable.hidden = (terminationCharacters.length === 0);
        this._emptyIndicator.hidden = (terminationCharacters.length !== 0);
    }

    _onAddClick(e) {
        e.preventDefault();
        this._addNewEntry();
    }

    _onResetClick(e) {
        e.preventDefault();
        this._reset();
    }

    async _addNewEntry() {
        const newEntry = {
            enabled: true,
            character1: '"',
            character2: '"',
            includeCharacterAtStart: false,
            includeCharacterAtEnd: false
        };
        return await this.addEntry(newEntry);
    }

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

    async _reset() {
        const defaultOptions = await this._settingsController.getDefaultOptions();
        const value = defaultOptions.profiles[0].options.sentenceParsing.terminationCharacters;
        await this._settingsController.setProfileSetting('sentenceParsing.terminationCharacters', value);
        await this._updateOptions();
    }
}

class SentenceTerminationCharacterEntry {
    constructor(parent, data, index, node) {
        this._parent = parent;
        this._data = data;
        this._index = index;
        this._node = node;
        this._eventListeners = new EventListenerCollection();
        this._character1Input = null;
        this._character2Input = null;
        this._basePath = `sentenceParsing.terminationCharacters[${this._index}]`;
    }

    prepare() {
        const {enabled, character1, character2, includeCharacterAtStart, includeCharacterAtEnd} = this._data;
        const node = this._node;

        const enabledToggle = node.querySelector('.sentence-termination-character-enabled');
        const typeSelect = node.querySelector('.sentence-termination-character-type');
        const character1Input = node.querySelector('.sentence-termination-character-input1');
        const character2Input = node.querySelector('.sentence-termination-character-input2');
        const includeAtStartCheckbox = node.querySelector('.sentence-termination-character-include-at-start');
        const includeAtEndheckbox = node.querySelector('.sentence-termination-character-include-at-end');
        const menuButton = node.querySelector('.sentence-termination-character-entry-button');

        this._character1Input = character1Input;
        this._character2Input = character2Input;

        const type = (character2 === null ? 'terminator' : 'quote');
        node.dataset.type = type;

        enabledToggle.checked = enabled;
        typeSelect.value = type;
        character1Input.value = character1;
        character2Input.value = (character2 !== null ? character2 : '');
        includeAtStartCheckbox.checked = includeCharacterAtStart;
        includeAtEndheckbox.checked = includeCharacterAtEnd;

        enabledToggle.dataset.setting = `${this._basePath}.enabled`;
        includeAtStartCheckbox.dataset.setting = `${this._basePath}.includeCharacterAtStart`;
        includeAtEndheckbox.dataset.setting = `${this._basePath}.includeCharacterAtEnd`;

        this._eventListeners.addEventListener(typeSelect, 'change', this._onTypeSelectChange.bind(this), false);
        this._eventListeners.addEventListener(character1Input, 'change', this._onCharacterChange.bind(this, 1), false);
        this._eventListeners.addEventListener(character2Input, 'change', this._onCharacterChange.bind(this, 2), false);
        this._eventListeners.addEventListener(menuButton, 'menuClose', this._onMenuClose.bind(this), false);
    }

    cleanup() {
        this._eventListeners.removeAllEventListeners();
        if (this._node.parentNode !== null) {
            this._node.parentNode.removeChild(this._node);
        }
    }

    // Private

    _onTypeSelectChange(e) {
        this._setHasCharacter2(e.currentTarget.value === 'quote');
    }

    _onCharacterChange(characterNumber, e) {
        const node = e.currentTarget;
        if (characterNumber === 2 && this._data.character2 === null) {
            node.value = '';
        }

        const value = node.value.substring(0, 1);
        this._setCharacterValue(node, characterNumber, value);
    }

    _onMenuClose(e) {
        switch (e.detail.action) {
            case 'delete':
                this._delete();
                break;
        }
    }

    async _delete() {
        this._parent.deleteEntry(this._index);
    }

    async _setHasCharacter2(has) {
        const okay = await this._setCharacterValue(this._character2Input, 2, has ? this._data.character1 : null);
        if (okay) {
            const type = (!has ? 'terminator' : 'quote');
            this._node.dataset.type = type;
        }
    }

    async _setCharacterValue(inputNode, characterNumber, value) {
        const pathEnd = `character${characterNumber}`;
        const r = await this._parent.settingsController.setProfileSetting(`${this._basePath}.${pathEnd}`, value);
        const okay = !r[0].error;
        if (okay) {
            this._data[pathEnd] = value;
        } else {
            value = this._data[pathEnd];
        }
        inputNode.value = (value !== null ? value : '');
        return okay;
    }
}