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