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
/**
18
 * @author     Andreas Grabs <moodle@grabs-edv.de>
19
 * @copyright  2024 Andreas Grabs
20
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21
 */
22
 
23
import ContentLoader from 'mod_unilabel/contentloader';
24
import Templates from 'core/templates';
25
import notification from 'core/notification';
26
import log from 'core/log';
27
 
28
let _formid;
29
let _type;
30
let _elements;
31
 
32
/**
33
 * Register the del button and get the html from mustache.
34
 *
35
 * @param {Element} headerelement The draggable header element
36
 * @param {Integer} index The index of the headerelement
37
 * @returns {Promise}
38
 */
39
const registerActionButtons = (headerelement, index) => {
40
    const context = {
41
        type: _type,
42
        repeatindex: index,
43
        repeatnr: (index + 1),
44
    };
45
    return Templates.renderForPromise('mod_unilabel/element_action_buttons', context)
46
    .then(({html, js}) => {
47
        headerelement.querySelector('div.d-flex').insertAdjacentHTML(
48
            'beforeend', html
49
        );
50
        Templates.runTemplateJS(js);
51
        return;
52
    }).catch((error) => notification.exception(error));
53
};
54
 
55
/**
56
 * Delete an element and set dummy hidden elements with "0" value, what is needed by the mform.
57
 *
58
 * @param {Integer} index The index of the deleted element
59
 */
60
const delElement = (index) => {
61
    var headerelement = document.querySelector('#id_singleelementheader_' + index);
62
    if (headerelement) {
63
        headerelement.remove();
64
    }
65
    var thisform = document.querySelector('#' + _formid);
66
    var myparent = document.querySelector('#id_unilabelcontenthdr');
67
    if (myparent) {
68
        var newelement;
69
        _elements.forEach((element) => {
70
            let name = 'unilabeltype_' + _type + '_' + element + '[' + index + ']';
71
            log.debug('Set dummy element ' + name);
72
            newelement = document.createElement('input');
73
            newelement.type = 'hidden';
74
            newelement.name = name;
75
            newelement.value = '';
76
            myparent.insertAdjacentElement('afterbegin', newelement);
77
        });
78
        const myevent = new CustomEvent('itemremoved', {detail: index});
79
        thisform.dispatchEvent(myevent);
80
    }
81
};
82
 
83
/**
84
 * Export our init method.
85
 *
86
 * @param {string} type The type of unilabeltype e.g.: grid
87
 * @param {string} formid The id of the mform the draggable elements are related to
88
 * @param {Integer} contextid
89
 * @param {string} prefix
90
 * @param {array} elements The dummy fields we need if we want to delete an element
91
 * @param {boolean} useDragdrop The same as element but for editor which has subelements like "text", "format" and "itemid"
92
 */
93
export const init = async(type, formid, contextid, prefix, elements, useDragdrop) => {
94
    // Import the dragdrop module asynchron.
95
    const dragDrop = await import('mod_unilabel/dragdrop');
96
    dragDrop.init(type, formid, useDragdrop);
97
 
98
    _type = type;
99
    _formid = formid;
100
    _elements = elements;
101
 
102
    // Register a click for the whole form but only applying to the delButtons.
103
    var thisform = document.querySelector('#' + formid);
104
    thisform.addEventListener('click', (e) => {
105
        if (e.target.dataset.action == 'deleteelement') {
106
            var index = e.target.dataset.id;
107
            log.debug('Deleting element: ' + index);
108
            delElement(index);
109
        }
110
    });
111
 
112
    // Look for the header elements and add and register a delete button.
113
    var headerelements = document.querySelectorAll('fieldset[id^="id_singleelementheader"]');
114
    for (var i = 0; i < headerelements.length; i++) {
115
        var headerelement = headerelements[i];
116
        log.debug('looking for: ' + headerelement.id);
117
        registerActionButtons(headerelement, i);
118
    }
119
 
120
    var button = document.querySelector('#button-' + formid);
121
    if (button) {
122
        var repeatbutton = document.querySelector('#fitem_id_' + prefix + 'add_more_elements_btn');
123
        if (repeatbutton) {
124
            repeatbutton.remove();
125
        }
126
        button.addEventListener('click', (e) => {
127
            var contentcontainerselector = '#addcontent-' + formid;
128
            var repeatindex = parseInt(e.target.form.multiple_chosen_elements_count.value);
129
            var fragmentcall = 'get_edit_element';
130
 
131
            var serviceparams = {
132
                'contextid': contextid,
133
                'formid': formid,
134
                'repeatindex': repeatindex
135
            };
136
            log.debug(serviceparams);
137
 
138
            e.target.form.multiple_chosen_elements_count.value = repeatindex + 1;
139
 
140
            // To make the moodle form aware of the change, we set the data-initial-value to its original value.
141
            e.target.form.dataset.formDirty = true;
142
 
143
            var contentLoader = new ContentLoader(contentcontainerselector, fragmentcall, serviceparams, contextid);
144
            contentLoader.loadContent('beforebegin').then(() => {
145
                const myevent = new CustomEvent('itemadded', {detail: repeatindex});
146
                thisform.dispatchEvent(myevent);
147
                return true;
148
            }).catch((error) => notification.exception(error));
149
        });
150
    }
151
};