jquery.formalize.legacy.js
5.53 KB
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
/*
Formalize - version 1.2
Note: This file depends on the jQuery library.
This version of Formalize, "jquery.formalize.legacy.js"
is intended for use with jQuery version 1.4, and exists
because Drupal 7 ships with jQuery 1.4.4. When you are
starting a new project with a current version of jQuery,
you should simply use the "jquery.formalize.js" instead.
*/
// Module pattern:
// http://yuiblog.com/blog/2007/06/12/module-pattern
var FORMALIZE = (function($, window, document, undefined) {
// Internet Explorer detection.
function IE(version) {
var b = document.createElement('b');
b.innerHTML = '<!--[if IE ' + version + ']><br><![endif]-->';
return !!b.getElementsByTagName('br').length;
}
// Private constants.
var PLACEHOLDER_SUPPORTED = 'placeholder' in document.createElement('input');
var AUTOFOCUS_SUPPORTED = 'autofocus' in document.createElement('input');
var IE6 = IE(6);
var IE7 = IE(7);
// Expose innards of FORMALIZE.
return {
// FORMALIZE.go
go: function() {
var i, j = this.init;
for (i in j) {
j.hasOwnProperty(i) && j[i]();
}
},
// FORMALIZE.init
init: {
// FORMALIZE.init.disable_link_button
disable_link_button: function() {
$(document.documentElement).delegate('a.button_disabled', 'click', function() {
return false;
});
},
// FORMALIZE.init.full_input_size
full_input_size: function() {
if (!IE7 || !$('textarea, input.input_full').length) {
return;
}
// This fixes width: 100% on <textarea> and class="input_full".
// It ensures that form elements don't go wider than container.
$('textarea, input.input_full').wrap('<span class="input_full_wrap"></span>');
},
// FORMALIZE.init.ie6_skin_inputs
ie6_skin_inputs: function() {
// Test for Internet Explorer 6.
if (!IE6 || !$('input, select, textarea').length) {
// Exit if the browser is not IE6,
// or if no form elements exist.
return;
}
// For <input type="submit" />, etc.
var button_regex = /button|submit|reset/;
// For <input type="text" />, etc.
var type_regex = /date|datetime|datetime-local|email|month|number|password|range|search|tel|text|time|url|week/;
$('input').each(function() {
var el = $(this);
// Is it a button?
if (this.getAttribute('type').match(button_regex)) {
el.addClass('ie6_button');
/* Is it disabled? */
if (this.disabled) {
el.addClass('ie6_button_disabled');
}
}
// Or is it a textual input?
else if (this.getAttribute('type').match(type_regex)) {
el.addClass('ie6_input');
/* Is it disabled? */
if (this.disabled) {
el.addClass('ie6_input_disabled');
}
}
});
$('textarea, select').each(function() {
/* Is it disabled? */
if (this.disabled) {
$(this).addClass('ie6_input_disabled');
}
});
},
// FORMALIZE.init.autofocus
autofocus: function() {
if (AUTOFOCUS_SUPPORTED || !$(':input[autofocus]').length) {
return;
}
var el = $('[autofocus]')[0];
if (!el.disabled) {
el.focus();
}
},
// FORMALIZE.init.placeholder
placeholder: function() {
if (PLACEHOLDER_SUPPORTED || !$(':input[placeholder]').length) {
// Exit if placeholder is supported natively,
// or if page does not have any placeholder.
return;
}
FORMALIZE.misc.add_placeholder();
$(':input[placeholder]').each(function() {
// Placeholder obscured in older browsers,
// so there's no point adding to password.
if (this.type === 'password') {
return;
}
var el = $(this);
var text = el.attr('placeholder');
el.focus(function() {
if (el.val() === text) {
el.val('').removeClass('placeholder_text');
}
}).blur(function() {
FORMALIZE.misc.add_placeholder();
});
// Prevent <form> from accidentally
// submitting the placeholder text.
el.closest('form').submit(function() {
if (el.val() === text) {
el.val('').removeClass('placeholder_text');
}
}).bind('reset', function() {
setTimeout(FORMALIZE.misc.add_placeholder, 50);
});
});
}
},
// FORMALIZE.misc
misc: {
// FORMALIZE.misc.add_placeholder
add_placeholder: function() {
if (PLACEHOLDER_SUPPORTED || !$(':input[placeholder]').length) {
// Exit if placeholder is supported natively,
// or if page does not have any placeholder.
return;
}
$(':input[placeholder]').each(function() {
// Placeholder obscured in older browsers,
// so there's no point adding to password.
if (this.type === 'password') {
return;
}
var el = $(this);
var text = el.attr('placeholder');
if (!el.val() || el.val() === text) {
el.val(text).addClass('placeholder_text');
}
});
}
}
};
// Alias jQuery, window, document.
})(jQuery, this, this.document);
// Automatically calls all functions in FORMALIZE.init
jQuery(document).ready(function() {
FORMALIZE.go();
});