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
 * Course section format component.
18
 *
19
 * @module     core_courseformat/local/content/section
20
 * @class      core_courseformat/local/content/section
21
 * @copyright  2021 Ferran Recio <ferran@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
import Header from 'core_courseformat/local/content/section/header';
26
import DndSection from 'core_courseformat/local/courseeditor/dndsection';
27
import Templates from 'core/templates';
11 efrain 28
import Pending from "core/pending";
1 efrain 29
 
30
export default class extends DndSection {
31
 
32
    /**
33
     * Constructor hook.
34
     */
35
    create() {
36
        // Optional component name for debugging.
37
        this.name = 'content_section';
38
        // Default query selectors.
39
        this.selectors = {
40
            SECTION_ITEM: `[data-for='section_title']`,
41
            CM: `[data-for="cmitem"]`,
42
            SECTIONINFO: `[data-for="sectioninfo"]`,
43
            SECTIONBADGES: `[data-region="sectionbadges"]`,
44
            SHOWSECTION: `[data-action="sectionShow"]`,
45
            HIDESECTION: `[data-action="sectionHide"]`,
46
            ACTIONTEXT: `.menu-action-text`,
47
            ICON: `.icon`,
48
        };
49
        // Most classes will be loaded later by DndCmItem.
50
        this.classes = {
51
            LOCKED: 'editinprogress',
52
            HASDESCRIPTION: 'description',
53
            HIDE: 'd-none',
54
            HIDDEN: 'hidden',
55
            CURRENT: 'current',
56
        };
57
 
58
        // We need our id to watch specific events.
59
        this.id = this.element.dataset.id;
60
    }
61
 
62
    /**
63
     * Initial state ready method.
64
     *
65
     * @param {Object} state the initial state
66
     */
67
    stateReady(state) {
68
        this.configState(state);
69
        // Drag and drop is only available for components compatible course formats.
70
        if (this.reactive.isEditing && this.reactive.supportComponents) {
71
            // Section zero and other formats sections may not have a title to drag.
72
            const sectionItem = this.getElement(this.selectors.SECTION_ITEM);
73
            if (sectionItem) {
74
                // Init the inner dragable element.
75
                const headerComponent = new Header({
76
                    ...this,
77
                    element: sectionItem,
78
                    fullregion: this.element,
79
                });
80
                this.configDragDrop(headerComponent);
81
            }
82
        }
11 efrain 83
        this._openSectionIfNecessary();
1 efrain 84
    }
85
 
86
    /**
11 efrain 87
     * Open the section if the anchored activity is inside.
88
     */
89
    async _openSectionIfNecessary() {
90
        const pageCmInfo = this.reactive.getPageAnchorCmInfo();
91
        if (!pageCmInfo || pageCmInfo.sectionid !== this.id) {
92
            return;
93
        }
94
        await this.reactive.dispatch('sectionContentCollapsed', [this.id], false);
95
        const pendingOpen = new Pending(`courseformat/section:openSectionIfNecessary`);
96
        this.element.scrollIntoView({block: "center"});
97
        setTimeout(() => {
98
            this.reactive.dispatch('setPageItem', 'cm', pageCmInfo.id);
99
            pendingOpen.resolve();
100
        }, 250);
101
    }
102
 
103
    /**
1 efrain 104
     * Component watchers.
105
     *
106
     * @returns {Array} of watchers
107
     */
108
    getWatchers() {
109
        return [
110
            {watch: `section[${this.id}]:updated`, handler: this._refreshSection},
111
        ];
112
    }
113
 
114
    /**
115
     * Validate if the drop data can be dropped over the component.
116
     *
117
     * @param {Object} dropdata the exported drop data.
118
     * @returns {boolean}
119
     */
120
    validateDropData(dropdata) {
121
        // If the format uses one section per page sections dropping in the content is ignored.
122
       if (dropdata?.type === 'section' && this.reactive.sectionReturn !== null) {
123
            return false;
124
        }
125
        return super.validateDropData(dropdata);
126
    }
127
 
128
    /**
129
     * Get the last CM element of that section.
130
     *
131
     * @returns {element|null}
132
     */
133
    getLastCm() {
134
        const cms = this.getElements(this.selectors.CM);
135
        // DndUpload may add extra elements so :last-child selector cannot be used.
136
        if (!cms || cms.length === 0) {
137
            return null;
138
        }
139
        return cms[cms.length - 1];
140
    }
141
 
142
    /**
143
     * Update a content section using the state information.
144
     *
145
     * @param {object} param
146
     * @param {Object} param.element details the update details.
147
     */
148
    _refreshSection({element}) {
149
        // Update classes.
150
        this.element.classList.toggle(this.classes.DRAGGING, element.dragging ?? false);
151
        this.element.classList.toggle(this.classes.LOCKED, element.locked ?? false);
152
        this.element.classList.toggle(this.classes.HIDDEN, !element.visible ?? false);
153
        this.element.classList.toggle(this.classes.CURRENT, element.current ?? false);
154
        this.locked = element.locked;
155
        // The description box classes depends on the section state.
156
        const sectioninfo = this.getElement(this.selectors.SECTIONINFO);
157
        if (sectioninfo) {
158
            sectioninfo.classList.toggle(this.classes.HASDESCRIPTION, element.hasrestrictions);
159
        }
160
        // Update section badges and menus.
161
        this._updateBadges(element);
162
        this._updateActionsMenu(element);
163
    }
164
 
165
    /**
166
     * Update a section badges using the state information.
167
     *
168
     * @param {object} section the section state.
169
     */
170
    _updateBadges(section) {
171
        const current = this.getElement(`${this.selectors.SECTIONBADGES} [data-type='iscurrent']`);
172
        current?.classList.toggle(this.classes.HIDE, !section.current);
173
 
174
        const hiddenFromStudents = this.getElement(`${this.selectors.SECTIONBADGES} [data-type='hiddenfromstudents']`);
175
        hiddenFromStudents?.classList.toggle(this.classes.HIDE, section.visible);
176
    }
177
 
178
    /**
179
     * Update a section action menus.
180
     *
181
     * @param {object} section the section state.
182
     */
183
    async _updateActionsMenu(section) {
184
        let selector;
185
        let newAction;
186
        if (section.visible) {
187
            selector = this.selectors.SHOWSECTION;
188
            newAction = 'sectionHide';
189
        } else {
190
            selector = this.selectors.HIDESECTION;
191
            newAction = 'sectionShow';
192
        }
193
        // Find the affected action.
194
        const affectedAction = this.getElement(selector);
195
        if (!affectedAction) {
196
            return;
197
        }
198
        // Change action.
199
        affectedAction.dataset.action = newAction;
200
        // Change text.
201
        const actionText = affectedAction.querySelector(this.selectors.ACTIONTEXT);
202
        if (affectedAction.dataset?.swapname && actionText) {
203
            const oldText = actionText?.innerText;
204
            actionText.innerText = affectedAction.dataset.swapname;
205
            affectedAction.dataset.swapname = oldText;
206
        }
207
        // Change icon.
208
        const icon = affectedAction.querySelector(this.selectors.ICON);
209
        if (affectedAction.dataset?.swapicon && icon) {
210
            const newIcon = affectedAction.dataset.swapicon;
11 efrain 211
            affectedAction.dataset.swapicon = affectedAction.dataset.icon;
212
            affectedAction.dataset.icon = newIcon;
1 efrain 213
            if (newIcon) {
214
                const pixHtml = await Templates.renderPix(newIcon, 'core');
215
                Templates.replaceNode(icon, pixHtml, '');
216
            }
217
        }
218
    }
219
}