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
 * JavaScript required by the question engine.
18
 *
19
 * @module     core_question/question_engine
20
 * @copyright  2021 The Open University
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import * as scrollManager from 'core/scroll_manager';
25
import * as formSubmit from 'core_form/submit';
26
 
27
/**
28
 * Initialise a question submit button. This saves the scroll position and
29
 * sets the fragment on the form submit URL so the page reloads in the right place.
30
 *
31
 * @param {string} button the id of the button in the HTML.
32
 */
33
export const initSubmitButton = button => {
34
    formSubmit.init(button);
35
    scrollManager.watchScrollButtonSaves();
36
};
37
 
38
/**
39
 * Initialise a form that contains questions printed using print_question.
40
 * This has the effect of:
41
 * 1. Turning off browser autocomlete.
42
 * 2. Stopping enter from submitting the form (or toggling the next flag) unless
43
 *    keyboard focus is on the submit button or the flag.
44
 * 3. Removes any '.questionflagsavebutton's, since we have JavaScript to toggle
45
 *    the flags using ajax.
46
 * 4. Scroll to the position indicated by scrollpos= in the URL, if it is there.
47
 * 5. Prevent the user from repeatedly submitting the form.
48
 *
49
 * @param {string} formSelector Selector to identify the form.
50
 */
51
export const initForm = (formSelector) => {
52
    const form = document.querySelector(formSelector);
53
    form.setAttribute('autocomplete', 'off');
54
 
55
    form.addEventListener('submit', preventRepeatSubmission);
56
 
57
    form.addEventListener('key', (event) => {
58
        if (event.keyCode !== 13) {
59
            return;
60
        }
61
 
62
        if (event.target.matches('a')) {
63
            return;
64
        }
65
 
66
        if (event.target.matches('input[type="submit"]')) {
67
            return;
68
        }
69
 
70
        if (event.target.matches('input[type=img]')) {
71
            return;
72
        }
73
 
74
        if (event.target.matches('textarea') || event.target.matches('[contenteditable=true]')) {
75
            return;
76
        }
77
 
78
        event.preventDefault();
79
    });
80
 
81
    const questionFlagSaveButtons = form.querySelectorAll('.questionflagsavebutton');
82
    [...questionFlagSaveButtons].forEach((node) => node.remove());
83
 
84
    // Note: The scrollToSavedPosition function tries to wait until the content has loaded before firing.
85
    scrollManager.scrollToSavedPosition();
86
};
87
 
88
/**
89
 * Event handler to stop a question form being submitted more than once.
90
 *
91
 * @param {object} event the form submit event.
92
 */
93
export const preventRepeatSubmission = (event) => {
94
    const form = event.target.closest('form');
95
    if (form.dataset.formSubmitted === '1') {
96
        event.preventDefault();
97
        return;
98
    }
99
 
100
    setTimeout(() => {
101
        [...form.querySelectorAll('input[type=submit]')].forEach((input) => input.setAttribute('disabled', true));
102
    });
103
    form.dataset.formSubmitted = '1';
104
};