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
 * Compare a given form's values and its previously set data attributes.
18
 *
19
 * @module     core_grades/grades/grader/gradingpanel/comparison
20
 * @copyright  2019 Mathew May <mathew.solutions>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
export const fillInitialValues = (form) => {
25
    Array.prototype.forEach.call(form.elements, (input) => {
26
        if (input.type === 'submit' || input.type === 'button') {
27
            return;
28
        } else if (input.type === 'radio' || input.type === 'checkbox') {
29
            input.dataset.initialValue = JSON.stringify(input.checked);
30
        } else if (typeof input.value !== 'undefined') {
31
            input.dataset.initialValue = JSON.stringify(input.value);
32
        } else if (input.type === 'select-one') {
33
            Array.prototype.forEach.call(input.options, (option) => {
34
                option.dataset.initialValue = JSON.stringify(option.selected);
35
            });
36
        }
37
   });
38
};
39
 
40
 
41
/**
42
 * Compare the form data with the initial form data from when the form was set up.
43
 *
44
 * If values have changed, return a truthy value.
45
 *
46
 * @param {HTMLElement} form
47
 * @return {Boolean}
48
 */
49
export const compareData = (form) => {
50
    const result = Array.prototype.some.call(form.elements, (input) => {
51
        if (input.type === 'submit' || input.type === 'button') {
52
            return false;
53
        } else if (input.type === 'radio' || input.type === 'checkbox') {
54
            if (typeof input.dataset.initialValue !== 'undefined') {
55
                return input.dataset.initialValue !== JSON.stringify(input.checked);
56
            }
57
        } else if (typeof input.value !== 'undefined') {
58
            if (typeof input.dataset.initialValue !== 'undefined') {
59
                return input.dataset.initialValue !== JSON.stringify(input.value);
60
            }
61
        } else if (input.type === 'select-one') {
62
            return Array.prototype.some.call(input.options, (option) => {
63
                if (typeof option.dataset.initialValue !== 'undefined') {
64
                    return option.dataset.initialValue !== JSON.stringify(option.selected);
65
                }
66
 
67
                return false;
68
            });
69
        }
70
 
71
        // No value found to check. Assume that there were changes.
72
        return true;
73
    });
74
 
75
    // Fill the initial values again as the form may not be reloaded.
76
    fillInitialValues(form);
77
 
78
    return result;
79
};