Proyectos de Subversion Moodle

Rev

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