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 placeholder replacer.
18
 *
19
 * @module     core_courseformat/local/courseindex/placeholder
20
 * @class      core_courseformat/local/courseindex/placeholder
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 {BaseComponent} from 'core/reactive';
26
import Templates from 'core/templates';
27
import {getCurrentCourseEditor} from 'core_courseformat/courseeditor';
28
import Pending from 'core/pending';
1441 ariadna 29
import log from "core/log";
1 efrain 30
 
31
export default class Component extends BaseComponent {
32
 
33
    /**
34
     * Static method to create a component instance form the mustache template.
35
     *
36
     * @param {element|string} target the DOM main element or its ID
37
     * @param {object} selectors optional css selector overrides
38
     * @return {Component}
39
     */
40
    static init(target, selectors) {
1441 ariadna 41
        let element = document.querySelector(target);
42
        // TODO Remove this if condition as part of MDL-83851.
43
        if (!element) {
44
            log.debug('Init component with id is deprecated, use a query selector instead.');
45
            element = document.getElementById(target);
46
        }
1 efrain 47
        return new this({
1441 ariadna 48
            element,
1 efrain 49
            reactive: getCurrentCourseEditor(),
50
            selectors,
51
        });
52
    }
53
 
54
    /**
55
     * Component creation hook.
56
     */
57
    create() {
58
        // Add a pending operation waiting for the initial content.
59
        this.pendingContent = new Pending(`core_courseformat/placeholder:loadcourseindex`);
60
    }
61
 
62
    /**
63
     * Initial state ready method.
64
     *
65
     * This stateReady to be async because it loads the real courseindex.
66
     *
67
     * @param {object} state the initial state
68
     */
69
    async stateReady(state) {
70
 
71
        // Check if we have a static course index already loded from a previous page.
72
        if (!this.loadStaticContent()) {
73
            await this.loadTemplateContent(state);
74
        }
75
    }
76
 
77
    /**
78
     * Load the course index from the session storage if any.
79
     *
80
     * @return {boolean} true if the static version is loaded form the session
81
     */
82
    loadStaticContent() {
83
        // Load the previous static course index from the session cache.
84
        const index = this.reactive.getStorageValue(`courseIndex`);
85
        if (index.html && index.js) {
86
            Templates.replaceNode(this.element, index.html, index.js);
87
            this.pendingContent.resolve();
88
            return true;
89
        }
90
        return false;
91
    }
92
 
93
    /**
94
     * Load the course index template.
95
     *
96
     * @param {Object} state the initial state
97
     */
98
    async loadTemplateContent(state) {
99
        // Collect section information from the state.
100
        const exporter = this.reactive.getExporter();
101
        const data = exporter.course(state);
102
        try {
103
            // To render an HTML into our component we just use the regular Templates module.
104
            const {html, js} = await Templates.renderForPromise(
105
                'core_courseformat/local/courseindex/courseindex',
106
                data,
107
            );
108
            Templates.replaceNode(this.element, html, js);
109
            this.pendingContent.resolve();
110
 
111
            // Save the rendered template into the session cache.
112
            this.reactive.setStorageValue(`courseIndex`, {html, js});
113
        } catch (error) {
114
            this.pendingContent.resolve(error);
115
            throw error;
116
        }
117
    }
118
}