Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
913 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
 * Course index cm component.
18
 *
19
 * This component is used to control specific course modules interactions like drag and drop.
20
 *
21
 * @module     theme_universe/courseindex/cm
22
 * @class      theme_universe/courseindex/cm
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 DndCmItem from 'theme_universe_child/courseeditor/dndcmitem';
28
import Templates from 'core/templates';
29
import Prefetch from 'core/prefetch';
30
import Config from 'core/config';
31
import Pending from "core/pending";
32
 
33
// Prefetch the completion icons template.
34
const completionTemplate = 'theme_universe/courseindex/cmcompletion';
35
Prefetch.prefetchTemplate(completionTemplate);
36
 
37
export default class Component extends DndCmItem {
38
 
39
    /**
40
     * Constructor hook.
41
     */
42
    create() {
43
        // Optional component name for debugging.
44
        this.name = 'courseindex_cm';
45
        // Default query selectors.
46
        this.selectors = {
47
            CM_NAME: `[data-for='cm_name']`,
48
            CM_COMPLETION: `[data-for='cm_completion']`,
49
        };
50
        // Default classes to toggle on refresh.
51
        this.classes = {
52
            CMHIDDEN: 'dimmed',
53
            LOCKED: 'editinprogress',
54
            RESTRICTIONS: 'restrictions',
55
            PAGEITEM: 'pageitem',
56
            INDENTED: 'indented',
57
        };
58
        // We need our id to watch specific events.
59
        this.id = this.element.dataset.id;
60
    }
61
 
62
    /**
63
     * Static method to create a component instance form the mustache template.
64
     *
65
     * @param {element|string} target the DOM main element or its ID
66
     * @param {object} selectors optional css selector overrides
67
     * @return {Component}
68
     */
69
    static init(target, selectors) {
70
        return new this({
71
            element: document.getElementById(target),
72
            selectors,
73
        });
74
    }
75
 
76
    /**
77
     * Initial state ready method.
78
     *
79
     * @param {Object} state the course state.
80
     */
81
    stateReady(state) {
82
        this.configDragDrop(this.id);
83
        const cm = state.cm.get(this.id);
84
        const course = state.course;
85
        // Refresh completion icon.
86
        this._refreshCompletion({
87
            state,
88
            element: cm,
89
        });
90
        const url = new URL(window.location.href);
91
        const anchor = url.hash.replace('#', '');
92
        // Check if the current url is the cm url.
93
        if (window.location.href == cm.url
94
            || (window.location.href.includes(course.baseurl) && anchor == cm.anchor)
95
        ) {
96
            this.element.scrollIntoView({block: "center"});
97
        }
98
        // Check if this we are displaying this activity page.
99
        if (Config.contextid != Config.courseContextId && Config.contextInstanceId == this.id) {
100
            this.reactive.dispatch('setPageItem', 'cm', this.id, true);
101
            this.element.scrollIntoView({block: "center"});
102
        }
103
        // Add anchor logic if the element is not user visible or the element hasn't URL.
104
        if (!cm.uservisible || !cm.url) {
105
            this.addEventListener(
106
                this.getElement(this.selectors.CM_NAME),
107
                'click',
108
                this._activityAnchor,
109
            );
110
        }
111
    }
112
 
113
    /**
114
     * Component watchers.
115
     *
116
     * @returns {Array} of watchers
117
     */
118
    getWatchers() {
119
        return [
120
            {watch: `cm[${this.id}]:deleted`, handler: this.remove},
121
            {watch: `cm[${this.id}]:updated`, handler: this._refreshCm},
122
            {watch: `cm[${this.id}].completionstate:updated`, handler: this._refreshCompletion},
123
            {watch: `course.pageItem:updated`, handler: this._refreshPageItem},
124
        ];
125
    }
126
 
127
    /**
128
     * Update a course index cm using the state information.
129
     *
130
     * @param {object} param
131
     * @param {Object} param.element details the update details.
132
     */
133
    _refreshCm({element}) {
134
        // Update classes.
135
        this.element.classList.toggle(this.classes.CMHIDDEN, !element.visible);
136
        this.getElement(this.selectors.CM_NAME).innerHTML = element.name;
137
        this.element.classList.toggle(this.classes.DRAGGING, element.dragging ?? false);
138
        this.element.classList.toggle(this.classes.LOCKED, element.locked ?? false);
139
        this.element.classList.toggle(this.classes.RESTRICTIONS, element.hascmrestrictions ?? false);
140
        this.element.classList.toggle(this.classes.INDENTED, element.indent);
141
        this.locked = element.locked;
142
    }
143
 
144
    /**
145
     * Handle a page item update.
146
     *
147
     * @param {Object} details the update details
148
     * @param {Object} details.element the course state data.
149
     */
150
    _refreshPageItem({element}) {
151
        if (!element.pageItem) {
152
            return;
153
        }
154
        const isPageId = (element.pageItem.type == 'cm' && element.pageItem.id == this.id);
155
        this.element.classList.toggle(this.classes.PAGEITEM, isPageId);
156
        if (isPageId && !this.reactive.isEditing) {
157
            this.element.scrollIntoView({block: "nearest"});
158
        }
159
    }
160
 
161
    /**
162
     * Update the activity completion icon.
163
     *
164
     * @param {Object} details the update details
165
     * @param {Object} details.state the state data
166
     * @param {Object} details.element the element data
167
     */
168
    async _refreshCompletion({state, element}) {
169
        // No completion icons are displayed in edit mode.
170
        if (this.reactive.isEditing || !element.istrackeduser) {
171
            return;
172
        }
173
        // Check if the completion value has changed.
174
        const completionElement = this.getElement(this.selectors.CM_COMPLETION);
175
        if (completionElement.dataset.value == element.completionstate) {
176
            return;
177
        }
178
 
179
        // Collect section information from the state.
180
        const exporter = this.reactive.getExporter();
181
        const data = exporter.cmCompletion(state, element);
182
 
183
        const {html, js} = await Templates.renderForPromise(completionTemplate, data);
184
        Templates.replaceNode(completionElement, html, js);
185
    }
186
 
187
    /**
188
     * The activity anchor event.
189
     *
190
     * @param {Event} event
191
     */
192
    _activityAnchor(event) {
193
        const cm = this.reactive.get('cm', this.id);
194
        // If the user cannot access the element but the element is present in the page
195
        // the new url should be an anchor link.
196
        const element = document.getElementById(cm.anchor);
197
        if (element) {
198
            // Make sure the section is expanded.
199
            this.reactive.dispatch('sectionContentCollapsed', [cm.sectionid], false);
200
            // Marc the element as page item once the event is handled.
201
            const pendingAnchor = new Pending(`courseformat/activity:openAnchor`);
202
            setTimeout(() => {
203
                this.reactive.dispatch('setPageItem', 'cm', cm.id);
204
                pendingAnchor.resolve();
205
            }, 50);
206
            return;
207
        }
208
        // If the element is not present in the page we need to go to the specific section.
209
        const course = this.reactive.get('course');
210
        const section = this.reactive.get('section', cm.sectionid);
211
        if (!section) {
212
            return;
213
        }
214
        const url = `${course.baseurl}&section=${section.number}#${cm.anchor}`;
215
        event.preventDefault();
216
        window.location = url;
217
    }
218
}