Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
 * Allows to filter the plugin list on plugins overview page
18
 *
19
 * @module     core_admin/plugins_overview
20
 * @copyright  2024 Marina Glancy
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
const SELECTORS = {
25
    PLUGIN_FILTERS: '#plugins-overview-panel [data-filterby]',
26
    PLUGIN_ROWS: 'table#plugins-control-panel tbody tr:not(.plugintypeheader)',
27
    PLUGIN_TYPE_ROWS: 'table#plugins-control-panel tbody tr.plugintypeheader',
28
};
29
 
30
/**
31
 * Initialise filters for the "Plugins overview" page
32
 */
33
export function init() {
34
    const filters = document.querySelectorAll(SELECTORS.PLUGIN_FILTERS);
35
    const pluginRows = document.querySelectorAll(SELECTORS.PLUGIN_ROWS);
36
    const pluginTypeRows = document.querySelectorAll(SELECTORS.PLUGIN_TYPE_ROWS);
37
 
38
    const filterPlugins = (target) => {
39
        const filterBy = target.getAttribute('data-filterby');
40
        const headerVisibility = {};
41
 
42
        // Hide all plugin rows in the plugin table that do not match the filter and show all others.
43
        for (const row of pluginRows) {
44
            const type = [...row.classList].find(s => s.startsWith('type-'));
45
            const visible = filterBy === 'all' ? true : row.classList.contains(filterBy);
46
            row.style.display = visible ? null : 'none';
47
            if (visible && type) {
48
                headerVisibility[type] = true;
49
            }
50
        }
51
 
52
        // Hide all the plugin type headers that do not have any visible plugins and show all others.
53
        for (const row of pluginTypeRows) {
54
            const type = [...row.classList].find(s => s.startsWith('type-'));
55
            if (type) {
56
                const visible = filterBy === 'all' || headerVisibility[type];
57
                row.style.display = visible ? null : 'none';
58
            }
59
        }
60
 
61
        // Toggle 'active' class for the selected filter.
62
        filters.forEach(el => el.classList.remove('active'));
63
        target.classList.add('active');
64
    };
65
 
66
    // Add event listeners for the links changing plugins filters.
67
    filters
68
    .forEach(target => target.addEventListener('click', (e) => {
69
        e.preventDefault();
70
        window.history.replaceState({}, null, e.target.href);
71
        filterPlugins(target);
72
    }));
73
 
74
    // Pre-filter plugins based on the current url anchor.
75
    if (window.location.hash.length > 1) {
76
        const anchor = window.location.hash.substring(1);
77
        const target = [...filters].find(t => t.getAttribute('data-filterby') === anchor);
78
        if (target) {
79
            filterPlugins(target);
80
        }
81
    }
82
}