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
 * Password Unmask functionality.
18
 *
19
 * @module     core_form/passwordunmask
20
 * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @since      3.2
23
 */
24
define(['jquery', 'core/templates'], function($, Template) {
25
 
26
    /**
27
     * Constructor for PasswordUnmask.
28
     *
29
     * @class core_form/passwordunmask
30
     * @param   {String}    elementid   The element to apply the PasswordUnmask to
31
     */
32
    var PasswordUnmask = function(elementid) {
33
        // Setup variables.
34
        this.wrapperSelector = '[data-passwordunmask="wrapper"][data-passwordunmaskid="' + elementid + '"]';
35
        this.wrapper = $(this.wrapperSelector);
36
        this.editorSpace = this.wrapper.find('[data-passwordunmask="editor"]');
37
        this.editLink = this.wrapper.find('a[data-passwordunmask="edit"]');
38
        this.editInstructions = this.wrapper.find('[data-passwordunmask="instructions"]');
39
        this.displayValue = this.wrapper.find('[data-passwordunmask="displayvalue"]');
40
        this.inputFieldLabel = $('label[for="' + elementid + '"]');
41
 
42
        this.inputField = this.editorSpace.find(document.getElementById(elementid));
43
        // Hide the field.
44
        this.inputField.addClass('d-none');
45
        this.inputField.removeClass('hiddenifjs');
46
 
47
        if (!this.editInstructions.attr('id')) {
48
            this.editInstructions.attr('id', elementid + '_instructions');
49
        }
50
        this.editInstructions.hide();
51
 
52
        this.setDisplayValue();
53
 
54
        // Add the listeners.
55
        this.addListeners();
56
    };
57
 
58
    /**
59
     * Add the event listeners required for PasswordUnmask.
60
     *
61
     * @method  addListeners
62
     * @return  {PasswordUnmask}
63
     * @chainable
64
     */
65
    PasswordUnmask.prototype.addListeners = function() {
66
        this.wrapper.on('click keypress', '[data-passwordunmask="edit"]', $.proxy(function(e) {
67
            if (e.type === 'keypress' && e.keyCode !== 13) {
68
                return;
69
            }
70
            e.stopImmediatePropagation();
71
            e.preventDefault();
72
 
73
            if (this.isEditing()) {
74
                // Only focus on the edit link if the event was not a click, and the new target is not an input field.
75
                if (e.type !== 'click' && !$(e.relatedTarget).is(':input')) {
76
                    this.turnEditingOff(true);
77
                } else {
78
                    this.turnEditingOff(false);
79
                }
80
            } else {
81
                this.turnEditingOn();
82
            }
83
        }, this));
84
 
85
        this.wrapper.on('click keypress', '[data-passwordunmask="unmask"]', $.proxy(function(e) {
86
            if (e.type === 'keypress' && e.keyCode !== 13) {
87
                return;
88
            }
89
            e.stopImmediatePropagation();
90
            e.preventDefault();
91
 
92
            // Toggle the data attribute.
93
            this.wrapper.data('unmasked', !this.wrapper.data('unmasked'));
94
 
95
            this.setDisplayValue();
96
        }, this));
97
 
98
        this.wrapper.on('keydown', 'input', $.proxy(function(e) {
99
            if (e.type === 'keydown' && e.keyCode !== 13) {
100
                return;
101
            }
102
 
103
            e.stopImmediatePropagation();
104
            e.preventDefault();
105
 
106
            this.turnEditingOff(true);
107
        }, this));
108
 
109
        this.inputFieldLabel.on('click', $.proxy(function(e) {
110
            e.preventDefault();
111
 
112
            this.turnEditingOn();
113
        }, this));
114
 
115
        return this;
116
    };
117
 
118
    /**
119
     * Check whether focus was lost from the PasswordUnmask and turn editing off if required.
120
     *
121
     * @method  checkFocusOut
122
     * @param   {EventFacade}   e       The EventFacade generating the suspsected Focus Out
123
     */
124
    PasswordUnmask.prototype.checkFocusOut = function(e) {
125
        if (!this.isEditing()) {
126
            // Ignore - not editing.
127
            return;
128
        }
129
 
130
        window.setTimeout($.proxy(function() {
131
            // Firefox does not have the focusout event. Instead jQuery falls back to the 'blur' event.
132
            // The blur event does not have a relatedTarget, so instead we use a timeout and the new activeElement.
133
            var relatedTarget = e.relatedTarget || document.activeElement;
134
            if (this.wrapper.has($(relatedTarget)).length) {
135
                // Ignore, some part of the element is still active.
136
                return;
137
            }
138
 
139
            // Only focus on the edit link if the new related target is not an input field or anchor.
140
            this.turnEditingOff(!$(relatedTarget).is(':input,a'));
141
        }, this), 100);
142
    };
143
 
144
    /**
145
     * Whether the password is currently visible (unmasked).
146
     *
147
     * @method  passwordVisible
148
     * @return  {Boolean}            True if the password is unmasked
149
     */
150
    PasswordUnmask.prototype.passwordVisible = function() {
151
        return !!this.wrapper.data('unmasked');
152
    };
153
 
154
    /**
155
     * Whether the user is currently editing the field.
156
     *
157
     * @method  isEditing
158
     * @return  {Boolean}            True if edit mode is enabled
159
     */
160
    PasswordUnmask.prototype.isEditing = function() {
161
        return this.inputField.hasClass('d-inline-block');
162
    };
163
 
164
    /**
165
     * Enable the editing functionality.
166
     *
167
     * @method  turnEditingOn
168
     * @return  {PasswordUnmask}
169
     * @chainable
170
     */
171
    PasswordUnmask.prototype.turnEditingOn = function() {
172
        var value = this.getDisplayValue();
173
        if (this.passwordVisible()) {
174
            this.inputField.attr('type', 'text');
175
        } else {
176
            this.inputField.attr('type', 'password');
177
        }
178
        this.inputField.val(value);
179
        this.inputField.attr('size', this.inputField.attr('data-size'));
180
        // Show the field.
181
        this.inputField.addClass('d-inline-block');
182
 
183
        if (this.editInstructions.length) {
184
            this.inputField.attr('aria-describedby', this.editInstructions.attr('id'));
185
            this.editInstructions.show();
186
        }
187
 
188
        this.wrapper.attr('data-passwordunmask-visible', 1);
189
 
190
        this.editLink.hide();
191
        this.inputField
192
            .focus()
193
            .select();
194
 
195
        // Note, this cannot be added as a delegated listener on init because Firefox does not support the FocusOut
196
        // event (https://bugzilla.mozilla.org/show_bug.cgi?id=687787) and the blur event does not identify the
197
        // relatedTarget.
198
        // The act of focusing the this.inputField means that in Firefox the focusout will be triggered on blur of the edit
199
        // link anchor.
200
        $('body').on('focusout', this.wrapperSelector, $.proxy(this.checkFocusOut, this));
201
 
202
        return this;
203
    };
204
 
205
    /**
206
     * Disable the editing functionality, optionally focusing on the edit link.
207
     *
208
     * @method  turnEditingOff
209
     * @param   {Boolean}       focusOnEditLink     Whether to focus on the edit link after disabling the editor
210
     * @return  {PasswordUnmask}
211
     * @chainable
212
     */
213
    PasswordUnmask.prototype.turnEditingOff = function(focusOnEditLink) {
214
        $('body').off('focusout', this.wrapperSelector, this.checkFocusOut);
215
        var value = this.getDisplayValue();
216
        this.inputField
217
            // Ensure that the aria-describedby is removed.
218
            .attr('aria-describedby', null);
219
        this.inputField.val(value);
220
        // Hide the field again.
221
        this.inputField.removeClass('d-inline-block');
222
 
223
        this.editInstructions.hide();
224
 
225
        // Remove the visible attr.
226
        this.wrapper.removeAttr('data-passwordunmask-visible');
227
 
228
        // Remove the size attr.
229
        this.inputField.removeAttr('size');
230
 
231
        this.editLink.show();
232
        this.setDisplayValue();
233
 
234
        if (focusOnEditLink) {
235
            this.editLink.focus();
236
        }
237
 
238
        return this;
239
    };
240
 
241
    /**
242
     * Get the currently value.
243
     *
244
     * @method  getDisplayValue
245
     * @return  {String}
246
     */
247
    PasswordUnmask.prototype.getDisplayValue = function() {
248
        return this.inputField.val();
249
    };
250
 
251
    /**
252
     * Set the currently value in the display, taking into account the current settings.
253
     *
254
     * @method  setDisplayValue
255
     * @return  {PasswordUnmask}
256
     * @chainable
257
     */
258
    PasswordUnmask.prototype.setDisplayValue = function() {
259
        var value = this.getDisplayValue();
260
        if (this.isEditing()) {
261
            if (this.wrapper.data('unmasked')) {
262
                this.inputField.attr('type', 'text');
263
            } else {
264
                this.inputField.attr('type', 'password');
265
            }
266
            this.inputField.val(value);
267
        }
268
 
269
        // Update the display value.
270
        // Note: This must always be updated.
271
        // The unmask value can be changed whilst editing and the editing can then be disabled.
272
        if (value && this.wrapper.data('unmasked')) {
273
            // There is a value, and we will show it.
274
            this.displayValue.text(value);
275
        } else {
276
            if (!value) {
277
                value = "";
278
            }
279
            // There is a value, but it will be disguised.
280
            // We use the passwordunmask-fill to allow modification of the fill and to ensure that the display does not
281
            // change as the page loads the JS.
282
            Template.render('core_form/element-passwordunmask-fill', {
283
                element: {
284
                    frozen:     this.inputField.is('[readonly]'),
285
                    value:      value,
286
                    valuechars: value.split(''),
287
                },
288
            }).done($.proxy(function(html, js) {
289
                this.displayValue.html(html);
290
 
291
                Template.runTemplateJS(js);
292
            }, this));
293
        }
294
 
295
        return this;
296
    };
297
 
298
    return PasswordUnmask;
299
});