-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforms-free.js
173 lines (144 loc) · 5.26 KB
/
forms-free.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"use strict";
var _this = void 0;
(function ($) {
var inputSelector = "".concat(['text', 'password', 'email', 'url', 'tel', 'number', 'search', 'search-md'].map(function (selector) {
return "input[type=".concat(selector, "]");
}).join(', '), ", textarea");
var textAreaSelector = '.materialize-textarea';
var updateTextFields = function updateTextFields($input) {
var $labelAndIcon = $input.siblings('label, i');
var hasValue = $input.val().length;
var hasPlaceholder = $input.attr('placeholder');
var addOrRemove = "".concat(hasValue || hasPlaceholder ? 'add' : 'remove', "Class");
$labelAndIcon[addOrRemove]('active');
};
var validateField = function validateField($input) {
if ($input.hasClass('validate')) {
var value = $input.val();
var noValue = !value.length;
var isValid = !$input[0].validity.badInput;
if (noValue && isValid) {
$input.removeClass('valid').removeClass('invalid');
} else {
var valid = $input.is(':valid');
var length = Number($input.attr('length')) || 0;
if (valid && (!length || length > value.length)) {
$input.removeClass('invalid').addClass('valid');
} else {
$input.removeClass('valid').addClass('invalid');
}
}
}
};
var textAreaAutoResize = function textAreaAutoResize() {
var $textarea = $(_this);
if ($textarea.val().length) {
var $hiddenDiv = $('.hiddendiv');
var fontFamily = $textarea.css('font-family');
var fontSize = $textarea.css('font-size');
if (fontSize) {
$hiddenDiv.css('font-size', fontSize);
}
if (fontFamily) {
$hiddenDiv.css('font-family', fontFamily);
}
if ($textarea.attr('wrap') === 'off') {
$hiddenDiv.css('overflow-wrap', 'normal').css('white-space', 'pre');
}
$hiddenDiv.text("".concat($textarea.val(), "\n"));
var content = $hiddenDiv.html().replace(/\n/g, '<br>');
$hiddenDiv.html(content); // When textarea is hidden, width goes crazy.
// Approximate with half of window size
$hiddenDiv.css('width', $textarea.is(':visible') ? $textarea.width() : $(window).width() / 2);
$textarea.css('height', $hiddenDiv.height());
}
};
$(inputSelector).each(function (index, input) {
var $this = $(input);
var $labelAndIcon = $this.siblings('label, i');
updateTextFields($this);
var isValid = input.validity.badInput;
if (isValid) {
$labelAndIcon.addClass('active');
}
});
$(document).on('focus', inputSelector, function (e) {
$(e.target).siblings('label, i').addClass('active');
});
$(document).on('blur', inputSelector, function (e) {
var $this = $(e.target);
var noValue = !$this.val();
var invalid = !e.target.validity.badInput;
var noPlaceholder = $this.attr('placeholder') === undefined;
if (noValue && invalid && noPlaceholder) {
$this.siblings('label, i').removeClass('active');
}
validateField($this);
});
$(document).on('change', inputSelector, function (e) {
var $this = $(e.target);
updateTextFields($this);
validateField($this);
});
$('input[autofocus]').siblings('label, i').addClass('active');
$(document).on('reset', function (e) {
var $formReset = $(e.target);
if ($formReset.is('form')) {
var $formInputs = $formReset.find(inputSelector);
$formInputs.removeClass('valid').removeClass('invalid').each(function (index, input) {
var $this = $(input);
var noDefaultValue = !$this.val();
var noPlaceholder = !$this.attr('placeholder');
if (noDefaultValue && noPlaceholder) {
$this.siblings('label, i').removeClass('active');
}
});
$formReset.find('select.initialized').each(function (index, select) {
var $select = $(select);
var $visibleInput = $select.siblings('input.select-dropdown');
var defaultValue = $select.children('[selected]').val();
$select.val(defaultValue);
$visibleInput.val(defaultValue);
});
}
});
function init() {
var $text = $('.md-textarea-auto');
if ($text.length) {
var observe;
if (window.attachEvent) {
observe = function observe(element, event, handler) {
element.attachEvent("on".concat(event), handler);
};
} else {
observe = function observe(element, event, handler) {
element.addEventListener(event, handler, false);
};
}
$text.each(function () {
var self = this;
function resize() {
self.style.height = 'auto';
self.style.height = "".concat(self.scrollHeight, "px");
}
function delayedResize() {
window.setTimeout(resize, 0);
}
observe(self, 'change', resize);
observe(self, 'cut', delayedResize);
observe(self, 'paste', delayedResize);
observe(self, 'drop', delayedResize);
observe(self, 'keydown', delayedResize);
resize();
});
}
}
init();
var $body = $('body');
if (!$('.hiddendiv').first().length) {
var $hiddenDiv = $('<div class="hiddendiv common"></div>');
$body.append($hiddenDiv);
}
$(textAreaSelector).each(textAreaAutoResize);
$body.on('keyup keydown', textAreaSelector, textAreaAutoResize);
})(jQuery);