-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjquery.validate.tooltips.js
121 lines (108 loc) · 3.3 KB
/
jquery.validate.tooltips.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
/**
* jQuery Validation Tooltips Plugin v1.0
* http://envysphere.com/jquery-validation-tooltips-14/
*
* Copyright 2011, Shaun Simmons
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
;(function($) {
$.extend($.validator.defaults, {
errorPlacement: function($error, $element) {
$error.css('visibility', 'hidden').appendTo($element[0].form);
},
showErrors: function(errorMap, errorList) {
this.defaultShowErrors();
this.reflow(errorList);
},
errorPosition: 'r',
// Extra spacing is applied only in the direction of the error's side closest to it's element.
errorSpacing: {
top: 2,
right: 3,
bottom: 2,
left: 3
}
});
$.validator.prototype.reflow = function(errorList) {
// If an errorList is not provided and the form has already been submitted once,
// reflow all errors.
if ( ! errorList) {
if (typeof this.lastElement !== 'undefined') {
this.reset();
this.form();
}
return;
}
var i = errorList.length;
while (i--) {
var $element = $(errorList[i].element),
$error = this.errorsFor(errorList[i].element),
error_position = [$element.data('errorposition') || this.settings.errorPosition][0].toLowerCase(),
offset, top = 0, left = 0;
switch (errorList[i].element.type) {
// Make an assumption that radios and checkboxes are wrapped by a label, or
// they have labels that are directly linked using the `for` attribute.
case 'checkbox':
case 'radio':
var $parentLabel = $element.parent('label');
if ($parentLabel.length) {
$element = $parentLabel;
top = -3;
}
else {
var $linkedLabel = $(this.currentForm).find("label[for='"+ $element.attr('id') +"']");
if ($linkedLabel.length) {
$element = $linkedLabel;
top = -3;
}
}
break;
}
offset = $element.position();
switch (error_position) {
case 't':
top += offset.top - this.settings.errorSpacing.top - $element.outerHeight();
left += offset.left;
break;
case 'b':
top += offset.top + this.settings.errorSpacing.bottom + $element.outerHeight();
left += offset.left;
break;
case 'l':
top += offset.top;
left += offset.left - this.settings.errorSpacing.right - $error.outerWidth();
break;
case 'tl':
top += offset.top - this.settings.errorSpacing.top - $element.outerHeight();
left += offset.left - this.settings.errorSpacing.right - $error.outerWidth();
break;
case 'tr':
top += offset.top - this.settings.errorSpacing.top - $element.outerHeight();
left += offset.left + this.settings.errorSpacing.left + $element.outerWidth();
break;
case 'bl':
top += offset.top + this.settings.errorSpacing.bottom + $element.outerHeight();
left += offset.left - this.settings.errorSpacing.right - $error.outerWidth();
break;
case 'br':
top += offset.top + this.settings.errorSpacing.bottom + $element.outerHeight();
left += offset.left + this.settings.errorSpacing.left + $element.outerWidth();
break;
case 'r':
default:
top += offset.top;
left += offset.left + this.settings.errorSpacing.left + $element.outerWidth();
break;
}
$error.css({
'left': left + 'px',
'top': top + 'px',
'position': 'absolute',
'visibility': 'visible'
});
}
};
})( jQuery );