1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
/* ------------------------------------------------------------------------
* Forms
* ------------------------------------------------------------------------ */
const Forms = {
initialized: false,
initialize: function(scope) {
if (scope === undefined) {
scope = document;
}
$('input[required],textarea[required]', scope).attr('aria-required', true);
$('input[pattern][title],textarea[pattern][title]', scope).each(function() {
$(this).data('message', $(this).attr('title'));
});
if (!Forms.initialized) {
// add invalid-handler to every input and textarea on the page
$(document).on('invalid', 'input, textarea', function() {
$(this)
.attr('aria-invalid', 'true')
.change(function() {
$(this).removeAttr('aria-invalid');
});
// get the fieldset that contains the invalid input
var fieldset = $(this).closest('fieldset');
// toggle the collapsed class if the fieldset is currently collapsed
if (fieldset.hasClass('collapsed')) {
fieldset.toggleClass('collapsed');
}
});
$(document).on('change', 'form.default label.file-upload input[type=file]', function(ev) {
var selected_file = ev.target.files[0],
filename;
if (
$(this)
.closest('label')
.find('.filename').length
) {
filename = $(this)
.closest('label')
.find('.filename');
} else {
filename = $('<span class="filename"/>');
$(this)
.closest('label')
.append(filename);
}
filename.text(selected_file.name + ' ' + Math.ceil(selected_file.size / 1024) + 'KB');
});
}
Forms.initialized = true;
}
};
export default Forms;
|