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 |
* Render mustache template examples within the component library.
|
|
|
18 |
*
|
|
|
19 |
* @module tool_componentlibrary/mustache
|
|
|
20 |
* @copyright 2021 Bas Brands <bas@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
import selectors from 'tool_componentlibrary/selectors';
|
|
|
25 |
import Ajax from 'core/ajax';
|
|
|
26 |
import Config from 'core/config';
|
|
|
27 |
import Templates from 'core/templates';
|
|
|
28 |
import Log from 'core/log';
|
|
|
29 |
import Notification from 'core/notification';
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Handle a template loaded response.
|
|
|
33 |
*
|
|
|
34 |
* @method
|
|
|
35 |
* @private
|
|
|
36 |
* @param {String} container The template container
|
|
|
37 |
* @param {String} templateName The template name
|
|
|
38 |
* @param {String} context Data for the template.
|
|
|
39 |
*/
|
|
|
40 |
const renderTemplate = async(container, templateName, context) => {
|
|
|
41 |
try {
|
|
|
42 |
context = JSON.parse(context);
|
|
|
43 |
} catch (e) {
|
|
|
44 |
Log.debug('Could not parse json example context for template.');
|
|
|
45 |
Log.debug(e);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
const {html, js} = await Templates.renderForPromise(templateName, context);
|
|
|
49 |
|
|
|
50 |
const rendercontainer = container.querySelector(selectors.mustacherendered);
|
|
|
51 |
|
|
|
52 |
// Load the rendered content in the renderer tab.
|
|
|
53 |
await Templates.replaceNodeContents(rendercontainer, html, js);
|
|
|
54 |
};
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Load the a template source from Moodle.
|
|
|
58 |
*
|
|
|
59 |
* @method
|
|
|
60 |
* @private
|
|
|
61 |
* @param {String} container The template container
|
|
|
62 |
*/
|
|
|
63 |
const loadTemplate = container => {
|
|
|
64 |
const sourcecontainer = container.querySelector(selectors.mustachesource);
|
|
|
65 |
const contextcontainer = container.querySelector(selectors.mustachecontext);
|
|
|
66 |
const templateName = container.dataset.template;
|
|
|
67 |
let context = container.querySelector(selectors.mustacherawcontext).textContent;
|
|
|
68 |
|
|
|
69 |
const parts = templateName.split('/');
|
|
|
70 |
const component = parts.shift();
|
|
|
71 |
const name = parts.join('/');
|
|
|
72 |
|
|
|
73 |
const request = {
|
|
|
74 |
methodname: 'core_output_load_template',
|
|
|
75 |
args: {
|
|
|
76 |
component: component,
|
|
|
77 |
template: name,
|
|
|
78 |
themename: Config.theme,
|
|
|
79 |
includecomments: true
|
|
|
80 |
}
|
|
|
81 |
};
|
|
|
82 |
|
|
|
83 |
Ajax.call([request])[0]
|
|
|
84 |
.done((source) => {
|
|
|
85 |
// Load the source template in Template tab.
|
|
|
86 |
sourcecontainer.textContent = source;
|
|
|
87 |
if (!context) {
|
|
|
88 |
const example = source.match(/Example context \(json\):([\s\S]+?)(}})/);
|
|
|
89 |
context = example[1];
|
|
|
90 |
// Load the variables in the Variables tab.
|
|
|
91 |
const precontainer = document.createElement("pre");
|
|
|
92 |
precontainer.innerHTML = JSON.stringify(JSON.parse(context), null, 4);
|
|
|
93 |
contextcontainer.parentNode.appendChild(precontainer);
|
|
|
94 |
contextcontainer.classList.add('d-none');
|
|
|
95 |
}
|
|
|
96 |
renderTemplate(container, templateName, context);
|
|
|
97 |
})
|
|
|
98 |
.fail(Notification.exception);
|
|
|
99 |
};
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Initialize the module.
|
|
|
103 |
*
|
|
|
104 |
* @method
|
|
|
105 |
*/
|
|
|
106 |
export const mustache = () => {
|
|
|
107 |
document.querySelectorAll(selectors.mustachecode).forEach((container) => {
|
|
|
108 |
loadTemplate(container);
|
|
|
109 |
});
|
|
|
110 |
};
|