This repository has been archived by the owner on Jan 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverstrap.js
209 lines (173 loc) · 4.57 KB
/
overstrap.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
(function(factory){
if(typeof module === "object" && typeof module.exports === "object"){
module.exports = factory( require('jquery'), require('node-waves'), require('bootstrap') );
}
else if(typeof define === 'function' && define.amd){
define(["jquery","waves","bootstrap"], factory);
}
else{
factory($, Waves);
}
}(function($, Waves){
const input_selector = 'input[type!=checkbox][type!=radio], textarea';
const defaultOptions = {
animatedBar: true,
autoValidate: false,
isValid: function(input){
$(input).trigger('validate');
return $(input).is(':valid');
},
validateField: function(field){
return field.hasClass('validate');
},
};
let overstrap = {
};
overstrap.init = function(options){
let self = this;
this.options = $.extend(true,defaultOptions,options);
let observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node){
if(node.nodeType===Node.ELEMENT_NODE){
if($(node).hasClass('input-field')){
self.loadInputField(node);
}
$('.input-field', node).each(function(){
self.loadInputField(this);
});
}
});
});
});
observer.observe(document.body, {
subtree: true,
childList: true,
attributes: false,
characterData: false,
});
$('.input-field').each(function(){
self.loadInputField(this);
});
$(document.body).on('reset', 'form', function(){
$(this).find('.input-field').each(function(){
self.validateField(this, true);
});
});
//bootstrap 4 bugfix !
$(document.body).on('mousedown','.dropdown-menu *',function(e){
return false;
});
Waves.init({
duration: 500,
delay: 200
});
};
overstrap.inputFilled = function(el){
if(el.tagName.toLowerCase()=='input'&&( el.type=='checkbox'||el.type=='radio' )){
return $(el).prop('checked');
}
else{
return Boolean( $(el).val().length );
}
};
overstrap.validateField = function(field, isInit){
if(this.options.validateField(field, isInit)===false){
return;
}
this.validateFieldDefault(field, isInit);
};
overstrap.validateFieldDefault = function(field, isInit){
let self = this;
let isValid = true;
let isFilled = false;
field.find(':input[name]').each(function(){
let input = $(this);
if(isInit&&!isFilled&&self.inputFilled(this)){
isFilled = true;
}
if(!self.options.isValid(this)){
isValid = false;
input.removeClass('form-control-success').addClass('form-control-danger');
}
else{
input.removeClass('form-control-danger').addClass('form-control-success');
}
});
if(!isInit||isFilled){
if(isValid){
field.removeClass('has-danger invalid').addClass('has-success valid');
}
else{
field.removeClass('has-success valid').addClass('has-danger invalid');
}
}
};
overstrap.loadInputField = function(el){
let self = this;
let $el = $(el);
let autofocus = $el.find('input[autofocus]');
if(autofocus.length){
autofocus.siblings('label, i').addClass('active');
}
let inputs = $el.find('input, textarea');
if(self.options.autoValidate&&!$el.hasClass('no-validate')){
$el.addClass('validate');
}
if(inputs.length){
if(self.options.animatedBar){
let underlinableInputs = inputs.filter(input_selector);
if(underlinableInputs.length){
$el.addClass('animated-bar');
if(!$el.find('.bar').length){
underlinableInputs.each(function(){
$(this).next('label').after('<span class="bar" />');
});
}
}
}
self.validateField($el, true);
inputs.each(function(){
let input = $(this);
let val = self.inputFilled(this);
let isFirstFilling = !val;
if(this.type=='checkbox'){
if(!input.next().is('label')){
input.after('<label></label>');
}
}
if(val || input.is(':focus')){
$el.addClass('active');
}
let id = input.requiredId();
input.nextUntil('input','label').each(function(){
if(!this.hasAttribute('for')){
this.setAttribute('for',id);
}
});
input
.on('change blur reset', function(e){
if(self.inputFilled(this)){
$el.addClass('active');
}
else{
$el.removeClass('active');
}
isFirstFilling = false;
self.validateField($el);
})
.on('input', function(e){
if(!isFirstFilling&&$(this).is(input_selector)){
self.validateField($el);
}
})
.on('focus', function(){
$el.addClass('active');
})
;
});
}
};
$.overstrap = overstrap;
return overstrap;
}));