Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * Format topics section extra logic component.
18
 *
19
 * @module     format_topics/section
20
 * @copyright  2022 Ferran Recio <ferran@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import {BaseComponent} from 'core/reactive';
25
import {getCurrentCourseEditor} from 'core_courseformat/courseeditor';
26
import Templates from 'core/templates';
27
 
28
class HighlightSection extends BaseComponent {
29
 
30
    /**
31
     * Constructor hook.
32
     */
33
    create() {
34
        // Optional component name for debugging.
35
        this.name = 'format_topics_section';
36
        // Default query selectors.
37
        this.selectors = {
38
            SECTION: `[data-for='section']`,
39
            SETMARKER: `[data-action="sectionHighlight"]`,
40
            REMOVEMARKER: `[data-action="sectionUnhighlight"]`,
41
            ACTIONTEXT: `.menu-action-text`,
42
            ICON: `.icon`,
43
        };
44
        // Default classes to toggle on refresh.
45
        this.classes = {
46
            HIDE: 'd-none',
47
        };
48
        // The topics format section specific actions.
49
        this.formatActions = {
50
            HIGHLIGHT: 'sectionHighlight',
51
            UNHIGHLIGHT: 'sectionUnhighlight',
52
        };
53
    }
54
 
55
    /**
56
     * Component watchers.
57
     *
58
     * @returns {Array} of watchers
59
     */
60
    getWatchers() {
61
        return [
62
            {watch: `section.current:updated`, handler: this._refreshHighlight},
63
        ];
64
    }
65
 
66
    /**
67
     * Update a content section using the state information.
68
     *
69
     * @param {object} param
70
     * @param {Object} param.element details the update details.
71
     */
72
    async _refreshHighlight({element}) {
73
        let selector;
74
        let newAction;
75
        if (element.current) {
76
            selector = this.selectors.SETMARKER;
77
            newAction = this.formatActions.UNHIGHLIGHT;
78
        } else {
79
            selector = this.selectors.REMOVEMARKER;
80
            newAction = this.formatActions.HIGHLIGHT;
81
        }
82
        // Find the affected action.
83
        const affectedAction = this.getElement(`${this.selectors.SECTION} ${selector}`, element.id);
84
        if (!affectedAction) {
85
            return;
86
        }
87
        // Change action, text and icon.
88
        affectedAction.dataset.action = newAction;
89
        const actionText = affectedAction.querySelector(this.selectors.ACTIONTEXT);
90
        if (affectedAction.dataset?.swapname && actionText) {
91
            const oldText = actionText?.innerText;
92
            actionText.innerText = affectedAction.dataset.swapname;
93
            affectedAction.dataset.swapname = oldText;
94
        }
95
        const icon = affectedAction.querySelector(this.selectors.ICON);
96
        if (affectedAction.dataset?.swapicon && icon) {
97
            const newIcon = affectedAction.dataset.swapicon;
98
            if (newIcon) {
99
                const pixHtml = await Templates.renderPix(newIcon, 'core');
100
                Templates.replaceNode(icon, pixHtml, '');
11 efrain 101
                affectedAction.dataset.swapicon = affectedAction.dataset.icon;
102
                affectedAction.dataset.icon = newIcon;
1 efrain 103
            }
104
        }
105
    }
106
}
107
 
108
export const init = () => {
109
    // Add component to the section.
110
    const courseEditor = getCurrentCourseEditor();
111
    if (courseEditor.supportComponents && courseEditor.isEditing) {
112
        new HighlightSection({
113
            element: document.getElementById('region-main'),
114
            reactive: courseEditor,
115
        });
116
    }
117
};