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 index section component.
18
 *
19
 * This component is used to control specific course section interactions like drag and drop.
20
 *
21
 * @module     core_courseformat/local/courseindex/section
22
 * @class      core_courseformat/local/courseindex/section
23
 * @copyright  2021 Ferran Recio <ferran@moodle.com>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
 
27
import SectionTitle from 'core_courseformat/local/courseindex/sectiontitle';
28
import DndSection from 'core_courseformat/local/courseeditor/dndsection';
29
 
30
export default class Component extends DndSection {
31
 
32
    /**
33
     * Constructor hook.
34
     */
35
    create() {
36
        // Optional component name for debugging.
37
        this.name = 'courseindex_section';
38
        // Default query selectors.
39
        this.selectors = {
40
            SECTION_ITEM: `[data-for='section_item']`,
41
            SECTION_TITLE: `[data-for='section_title']`,
42
            CM_LAST: `[data-for="cm"]:last-child`,
43
        };
44
        // Default classes to toggle on refresh.
45
        this.classes = {
46
            SECTIONHIDDEN: 'dimmed',
47
            SECTIONCURRENT: 'current',
48
            LOCKED: 'editinprogress',
49
            RESTRICTIONS: 'restrictions',
50
            PAGEITEM: 'pageitem',
51
            OVERLAYBORDERS: 'overlay-preview-borders',
52
        };
53
 
54
        // We need our id to watch specific events.
55
        this.id = this.element.dataset.id;
56
        this.isPageItem = false;
57
    }
58
 
59
    /**
60
     * Static method to create a component instance form the mustahce template.
61
     *
62
     * @param {string} target the DOM main element or its ID
63
     * @param {object} selectors optional css selector overrides
64
     * @return {Component}
65
     */
66
    static init(target, selectors) {
67
        return new this({
68
            element: document.getElementById(target),
69
            selectors,
70
        });
71
    }
72
 
73
    /**
74
     * Initial state ready method.
75
     *
76
     * @param {Object} state the initial state
77
     */
78
    stateReady(state) {
79
        this.configState(state);
80
        const sectionItem = this.getElement(this.selectors.SECTION_ITEM);
81
        // Drag and drop is only available for components compatible course formats.
82
        if (this.reactive.isEditing && this.reactive.supportComponents) {
83
            // Init the inner dragable element passing the full section as affected region.
84
            const titleitem = new SectionTitle({
85
                ...this,
86
                element: sectionItem,
87
                fullregion: this.element,
88
            });
89
            this.configDragDrop(titleitem);
90
        }
91
        // Check if the current url is the section url.
92
        const section = state.section.get(this.id);
93
        if (window.location.href == section.sectionurl.replace(/&amp;/g, "&")) {
94
            this.reactive.dispatch('setPageItem', 'section', this.id);
95
            sectionItem.scrollIntoView();
96
        }
97
    }
98
 
99
    /**
100
     * Component watchers.
101
     *
102
     * @returns {Array} of watchers
103
     */
104
    getWatchers() {
105
        return [
106
            {watch: `section[${this.id}]:deleted`, handler: this.remove},
107
            {watch: `section[${this.id}]:updated`, handler: this._refreshSection},
108
            {watch: `course.pageItem:updated`, handler: this._refreshPageItem},
109
        ];
110
    }
111
 
112
    /**
113
     * Get the last CM element of that section.
114
     *
115
     * @returns {element|null}
116
     */
117
    getLastCm() {
118
        return this.getElement(this.selectors.CM_LAST);
119
    }
120
 
121
    /**
122
     * Update a course index section using the state information.
123
     *
124
     * @param {Object} param details the update details.
125
     * @param {Object} param.element the section element
126
     */
127
    _refreshSection({element}) {
128
        // Update classes.
129
        const sectionItem = this.getElement(this.selectors.SECTION_ITEM);
130
        sectionItem.classList.toggle(this.classes.SECTIONHIDDEN, !element.visible);
131
        sectionItem.classList.toggle(this.classes.RESTRICTIONS, element.hasrestrictions ?? false);
132
        this.element.classList.toggle(this.classes.SECTIONCURRENT, element.current);
133
        this.element.classList.toggle(this.classes.DRAGGING, element.dragging ?? false);
134
        this.element.classList.toggle(this.classes.LOCKED, element.locked ?? false);
135
        this.locked = element.locked;
136
        // Update title.
137
        this.getElement(this.selectors.SECTION_TITLE).innerHTML = element.title;
138
    }
139
 
140
    /**
141
     * Handle a page item update.
142
     *
143
     * @param {Object} details the update details
144
     * @param {Object} details.state the state data.
145
     * @param {Object} details.element the course state data.
146
     */
147
    _refreshPageItem({element, state}) {
148
        if (!element.pageItem) {
149
            return;
150
        }
11 efrain 151
        if (element.pageItem.sectionId !== this.id && this.isPageItem || element.pageItem.type !== 'section') {
1 efrain 152
            this.pageItem = false;
153
            this.getElement(this.selectors.SECTION_ITEM).classList.remove(this.classes.PAGEITEM);
154
            return;
155
        }
156
        const section = state.section.get(this.id);
157
        if (section.indexcollapsed && !element.pageItem?.isStatic) {
158
            this.pageItem = (element.pageItem?.sectionId == this.id);
159
        } else {
160
            this.pageItem = (element.pageItem.type == 'section' && element.pageItem.id == this.id);
161
        }
162
        const sectionItem = this.getElement(this.selectors.SECTION_ITEM);
163
        sectionItem.classList.toggle(this.classes.PAGEITEM, this.pageItem ?? false);
164
        if (this.pageItem && !this.reactive.isEditing) {
165
            this.element.scrollIntoView({block: "nearest"});
166
        }
167
    }
168
 
169
    /**
170
     * Overridden version of the component addOverlay async method.
171
     *
172
     * The course index is not compatible with overlay elements.
173
     */
174
    async addOverlay() {
175
        this.element.classList.add(this.classes.OVERLAYBORDERS);
176
    }
177
 
178
    /**
179
     * Overridden version of the component removeOverlay.
180
     *
181
     * The course index is not compatible with overlay elements.
182
     */
183
    removeOverlay() {
184
        this.element.classList.remove(this.classes.OVERLAYBORDERS);
185
    }
186
}