Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
 * Allows to recalculate a single value on demand
18
 *
19
 * @module     customfield_number/recalculate
20
 * @author     2024 Marina Glancy
21
 * @copyright  2024 Moodle Pty Ltd <support@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
import Ajax from 'core/ajax';
26
import Notification from 'core/notification';
27
import {addIconToContainer} from 'core/loadingicon';
28
import Pending from 'core/pending';
29
 
30
const SELECTORS = {
31
    wrapper: '[data-fieldtype="wrapper"]',
32
    value: '[data-fieldtype="value"]',
33
    link: '[data-fieldtype="link"]',
34
};
35
 
36
let initialised = false;
37
 
38
/**
39
 * Init
40
 */
41
export function init() {
42
    if (initialised) {
43
        return;
44
    }
45
 
46
    document.addEventListener('click', (e) => {
47
        const target = e.target.closest(SELECTORS.wrapper + " " + SELECTORS.link);
48
        if (!target) {
49
            return;
50
        }
51
        const el = target.closest(SELECTORS.wrapper).querySelector(SELECTORS.value);
52
        if (!el) {
53
            return;
54
        }
55
        e.preventDefault();
56
 
57
        const {fieldid, instanceid} = target.dataset;
58
 
59
        const pendingPromise = new Pending('recalculate_customfield_number');
60
        addIconToContainer(el).then(() => {
61
            return Ajax.call([{
62
                methodname: 'customfield_number_recalculate_value',
63
                args: {fieldid, instanceid}
64
            }])[0];
65
        }).then((data) => {
66
            el.innerHTML = data.value;
67
            return pendingPromise.resolve();
68
        }).catch(Notification.exception);
69
    });
70
 
71
    initialised = true;
72
}