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
import Ajax from 'core/ajax';
17
import Templates from 'core/templates';
18
import PendingJS from 'core/pending';
19
 
20
/**
21
 * Enhancements for the step definitions page.
22
 *
23
 * @module tool_behat/steps
24
 * @copyright 2022 Catalyst IT EU
25
 * @author Mark Johnson <mark.johnson@catalyst-eu.net>
26
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
 
29
/**
30
 * Call the get_entity_generator web service function
31
 *
32
 * Takes the name of an entity generator and returns an object containing a list of the required fields.
33
 *
34
 * @param {String} entityType
35
 * @returns {Promise}
36
 */
37
const getGeneratorEntities = (entityType) => Ajax.call([{
38
    methodname: 'tool_behat_get_entity_generator',
39
    args: {entitytype: entityType}
40
}])[0];
41
 
42
/**
43
 * Render HTML for required fields
44
 *
45
 * Takes the entity data returned from getGeneratorEntities and renders the HTML to display the required fields.
46
 *
47
 * @param {String} entityData
48
 * @return {Promise}
49
 */
50
const getRequiredFieldsContent = (entityData) => {
51
    if (!entityData.required?.length) {
52
        return Promise.resolve({
53
            html: '',
54
            js: ''
55
        });
56
    }
57
    return Templates.renderForPromise('tool_behat/steprequiredfields', {fields: entityData.required});
58
};
59
 
60
export const init = () => {
61
    // When an entity is selected in the "the following exist" step, fetch and display the required fields.
62
    document.addEventListener('change', async(e) => {
63
        const entityElement = e.target.closest('.entities');
64
        const stepElement = e.target.closest('.stepcontent');
65
        if (!entityElement || !stepElement) {
66
            return;
67
        }
68
 
69
        const pendingPromise = new PendingJS('tool_behat/steps:change');
70
 
71
        const entityData = await getGeneratorEntities(e.target.value);
72
        const {html, js} = await getRequiredFieldsContent(entityData);
73
 
74
        const stepRequiredFields = stepElement.querySelector('.steprequiredfields');
75
        if (stepRequiredFields) {
76
            await Templates.replaceNode(stepRequiredFields, html, js);
77
        } else {
78
            await Templates.appendNodeContents(stepElement, html, js);
79
        }
80
        pendingPromise.resolve();
81
    });
82
};