Proyectos de Subversion Moodle

Rev

| 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
 * Generic reactive module used in the course editor.
18
 *
19
 * @module     core_courseformat/courseeditor
20
 * @copyright  2021 Ferran Recio <ferran@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import DefaultMutations from 'core_courseformat/local/courseeditor/mutations';
25
import CourseEditor from 'core_courseformat/local/courseeditor/courseeditor';
26
import events from 'core_course/events';
27
 
28
// A map with all the course editor instances.
29
const courseEditorMap = new Map();
30
 
31
// Map with all the state keys the backend send us to know if the frontend cache is valid or not.
32
const courseStateKeyMap = new Map();
33
 
34
/**
35
 * Trigger a state changed event.
36
 *
37
 * This function will be moved to core_course/events module
38
 * when the file is migrated to the new JS events structure proposed in MDL-70990.
39
 *
40
 * @method dispatchStateChangedEvent
41
 * @param {object} detail the full state
42
 * @param {object} target the custom event target (document if none provided)
43
 */
44
function dispatchStateChangedEvent(detail, target) {
45
    if (target === undefined) {
46
        target = document;
47
    }
48
    target.dispatchEvent(new CustomEvent(events.stateChanged, {
49
        bubbles: true,
50
        detail: detail,
51
    }));
52
}
53
 
54
/**
55
 * Setup the current view settings
56
 *
57
 * The backend cache state revision is a combination of the course->cacherev, the
58
 * user course preferences and completion state. The backend updates that number
59
 * everytime some change in the course affects the user course state.
60
 *
61
 * @param {number} courseId the course id
62
 * @param {setup} setup format, page and course settings
63
 * @param {boolean} setup.editing if the page is in edit mode
64
 * @param {boolean} setup.supportscomponents if the format supports components for content
65
 * @param {String} setup.statekey the backend cached state revision
66
 * @param {Array} setup.overriddenStrings optional overridden strings
67
 */
68
export const setViewFormat = (courseId, setup) => {
69
    courseId = parseInt(courseId);
70
    // Caches are ignored in edit mode.
71
    if (!setup.editing) {
72
        courseStateKeyMap.set(courseId, setup.statekey);
73
    }
74
    const editor = getCourseEditor(courseId);
75
    editor.setViewFormat(setup);
76
};
77
 
78
/**
79
 * Get a specific course editor reactive instance.
80
 *
81
 * @param {number} courseId the course id
82
 * @returns {CourseEditor}
83
 */
84
export const getCourseEditor = (courseId) => {
85
    courseId = parseInt(courseId);
86
 
87
    if (!courseEditorMap.has(courseId)) {
88
        courseEditorMap.set(
89
            courseId,
90
            new CourseEditor({
91
                name: `CourseEditor${courseId}`,
92
                eventName: events.stateChanged,
93
                eventDispatch: dispatchStateChangedEvent,
94
                // Mutations can be overridden by the format plugin using setMutations
95
                // but we need the default one at least.
96
                mutations: new DefaultMutations(),
97
            })
98
        );
99
        courseEditorMap.get(courseId).loadCourse(courseId, courseStateKeyMap.get(courseId));
100
    }
101
    return courseEditorMap.get(courseId);
102
};
103
 
104
/**
105
 * Get the current course reactive instance.
106
 *
107
 * @returns {CourseEditor}
108
 */
109
export const getCurrentCourseEditor = () => getCourseEditor(M.cfg.courseId);