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
 * Encrypted password functionality.
18
 *
19
 * @module core_form/encryptedpassword
20
 * @copyright 2019 The Open University
21
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
/**
25
 * Constructor for EncryptedPassword.
26
 *
27
 * @class core_form/encryptedpassword
28
 * @param {String} elementId The element to apply the encrypted password JS to
29
 */
30
export const EncryptedPassword = function(elementId) {
31
    const wrapper = document.querySelector('div[data-encryptedpasswordid="' + elementId + '"]');
32
    this.spanOrLink = wrapper.querySelector('span, a');
33
    this.input = wrapper.querySelector('input');
34
    this.editButtonOrLink = wrapper.querySelector('button[data-editbutton], a');
35
    this.cancelButton = wrapper.querySelector('button[data-cancelbutton]');
36
 
37
    // Edit button action.
38
    var editHandler = (e) => {
39
        e.stopImmediatePropagation();
40
        e.preventDefault();
41
        this.startEditing(true);
42
    };
43
    this.editButtonOrLink.addEventListener('click', editHandler);
44
 
45
    // When it's a link, do some magic to make the label work as well.
46
    if (this.editButtonOrLink.nodeName === 'A') {
47
        wrapper.parentElement.previousElementSibling.querySelector('label').addEventListener('click', editHandler);
48
    }
49
 
50
    // Cancel button action.
51
    this.cancelButton.addEventListener('click', (e) => {
52
        e.stopImmediatePropagation();
53
        e.preventDefault();
54
        this.cancelEditing();
55
    });
56
 
57
    // If the value is not set yet, start editing and remove the cancel option - so that
58
    // it saves something in the config table and doesn't keep repeat showing it as a new
59
    // admin setting...
60
    if (wrapper.dataset.novalue === 'y') {
61
        this.startEditing(false);
62
        this.cancelButton.style.display = 'none';
63
    }
64
};
65
 
66
/**
67
 * Starts editing.
68
 *
69
 * @param {Boolean} moveFocus If true, sets focus to the edit box
70
 */
71
EncryptedPassword.prototype.startEditing = function(moveFocus) {
72
    this.input.style.display = 'inline';
73
    this.input.disabled = false;
74
    this.spanOrLink.style.display = 'none';
75
    this.editButtonOrLink.style.display = 'none';
76
    this.cancelButton.style.display = 'inline';
77
 
78
    // Move the id around, which changes what happens when you click the label.
79
    const id = this.editButtonOrLink.id;
80
    this.editButtonOrLink.removeAttribute('id');
81
    this.input.id = id;
82
 
83
    if (moveFocus) {
84
        this.input.focus();
85
    }
86
};
87
 
88
/**
89
 * Cancels editing.
90
 */
91
EncryptedPassword.prototype.cancelEditing = function() {
92
    this.input.style.display = 'none';
93
    this.input.value = '';
94
    this.input.disabled = true;
95
    this.spanOrLink.style.display = 'inline';
96
    this.editButtonOrLink.style.display = 'inline';
97
    this.cancelButton.style.display = 'none';
98
 
99
    // Move the id around, which changes what happens when you click the label.
100
    const id = this.input.id;
101
    this.input.removeAttribute('id');
102
    this.editButtonOrLink.id = id;
103
};