1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Add search filtering of available language packs
|
|
|
18 |
*
|
|
|
19 |
* @module tool_langimport/search
|
|
|
20 |
* @copyright 2021 Paul Holden <paulh@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
import Pending from 'core/pending';
|
|
|
25 |
import {debounce} from 'core/utils';
|
|
|
26 |
|
|
|
27 |
const SELECTORS = {
|
|
|
28 |
AVAILABLE_LANG_SELECT: 'select',
|
|
|
29 |
AVAILABLE_LANG_SEARCH: '[data-action="search"]',
|
|
|
30 |
};
|
|
|
31 |
|
|
|
32 |
const DEBOUNCE_TIMER = 250;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Initialize module
|
|
|
36 |
*
|
|
|
37 |
* @param {Element} form
|
|
|
38 |
*/
|
|
|
39 |
export const init = form => {
|
|
|
40 |
const availableLangsElement = form.querySelector(SELECTORS.AVAILABLE_LANG_SELECT);
|
|
|
41 |
|
|
|
42 |
const availableLangsFilter = (event) => {
|
|
|
43 |
const pendingPromise = new Pending('tool_langimport/search:filter');
|
|
|
44 |
|
|
|
45 |
// Remove existing options.
|
|
|
46 |
availableLangsElement.querySelectorAll('option').forEach((option) => {
|
|
|
47 |
option.remove();
|
|
|
48 |
});
|
|
|
49 |
|
|
|
50 |
// Filter for matching languages.
|
|
|
51 |
const searchTerm = event.target.value.toLowerCase();
|
|
|
52 |
const availableLanguages = JSON.parse(availableLangsElement.dataset.availableLanguages);
|
|
|
53 |
const filteredLanguages = Object.keys(availableLanguages).reduce((matches, langcode) => {
|
|
|
54 |
if (availableLanguages[langcode].toLowerCase().includes(searchTerm)) {
|
|
|
55 |
matches[langcode] = availableLanguages[langcode];
|
|
|
56 |
}
|
|
|
57 |
return matches;
|
|
|
58 |
}, []);
|
|
|
59 |
|
|
|
60 |
// Re-create filtered options.
|
|
|
61 |
Object.entries(filteredLanguages).forEach(([langcode, langname]) => {
|
|
|
62 |
const option = document.createElement('option');
|
|
|
63 |
option.value = langcode;
|
|
|
64 |
option.innerText = langname;
|
|
|
65 |
availableLangsElement.append(option);
|
|
|
66 |
});
|
|
|
67 |
|
|
|
68 |
pendingPromise.resolve();
|
|
|
69 |
};
|
|
|
70 |
|
|
|
71 |
// Cache initial available language options.
|
|
|
72 |
const availableLanguages = {};
|
|
|
73 |
availableLangsElement.querySelectorAll('option').forEach((option) => {
|
|
|
74 |
availableLanguages[option.value] = option.text;
|
|
|
75 |
});
|
|
|
76 |
availableLangsElement.dataset.availableLanguages = JSON.stringify(availableLanguages);
|
|
|
77 |
|
|
|
78 |
// Register event listeners on the search element.
|
|
|
79 |
const availableLangsSearch = form.querySelector(SELECTORS.AVAILABLE_LANG_SEARCH);
|
|
|
80 |
availableLangsSearch.addEventListener('keydown', (event) => {
|
|
|
81 |
if (event.key === 'Enter') {
|
|
|
82 |
event.preventDefault();
|
|
|
83 |
}
|
|
|
84 |
});
|
|
|
85 |
|
|
|
86 |
// Debounce the event listener to allow the user to finish typing.
|
|
|
87 |
const availableLangsSearchDebounce = debounce(availableLangsFilter, DEBOUNCE_TIMER);
|
|
|
88 |
availableLangsSearch.addEventListener('keyup', (event) => {
|
|
|
89 |
const pendingPromise = new Pending('tool_langimport/search:keyup');
|
|
|
90 |
|
|
|
91 |
availableLangsSearchDebounce(event);
|
|
|
92 |
setTimeout(() => {
|
|
|
93 |
pendingPromise.resolve();
|
|
|
94 |
}, DEBOUNCE_TIMER);
|
|
|
95 |
});
|
|
|
96 |
};
|