-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjquery.formLabels.js
More file actions
executable file
·243 lines (222 loc) · 10.1 KB
/
jquery.formLabels.js
File metadata and controls
executable file
·243 lines (222 loc) · 10.1 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*global jQuery, window*/
/*
* @File: jquery.formLabels.js
* @Version: 1.1
* @Author: Andrei Zharov (www.o2v.net) - Senior Software Engineer at Nokia
*
* @Requires: jQuery v1.9 & jQueryUI v1.9.1
* @Usage: $.fn.formLabels()
* @Options: excludeElts - Excludes certain elements from being 'labelized'. Default: ''. Example: $.fn.formLabels(excludeElts:'#email, .nolabel')
* refreshOnResize - whether or not refresh labels on window resize. Default: true
* safemode - if enabled the plugin runs in the safemode without using spans and animation. Default: false
* labelParent - parentContainer for your 'labels'. Default: 'form'
* semantic - puts label before input element. Default: true
*
* Link to the demo: <http://o2v.net/demo/formLabels/index.html>
*
* Released under the MIT license
*/
(function ($) {
"use strict";
var formLabels = $.fn.formLabels = function (opts) {
opts = $.extend({
animationDuration: 300,
excludeElts: '', //elements to be excluded
refreshOnResize: false, //whether or not refresh labels on 'resize' event for window
safemode: false, //enable safe mode without DOM modifications
labelParent: 'form', //specifies a block that will store the form labels (body or form)
semantic: true //if true, then plugin renders label before input box (works only if labelParent:'form')
}, opts);
var inputs = ["textarea", "input[type='email']", "input[type='text']", "input[type='password']"],
getPropertyValue = function (prop) {
return parseInt(this.css(prop), 10);
},
enabled = true,
spanID = 0,
context = this.length ? this : "body",
elts = $(inputs.join(", "), context).not(opts.excludeElts).filter(":visible[title]"),
refreshLabels = function () {
elts.each(function () {
var $this = $(this),
assocSpan = $($this.data("spanID"));
assocSpan.position({
my: "left top",
at: "left top",
of: $this,
using: function (pos) {
var paddings = JSON.parse($this.data("paddings"));
$(this).css({
top: pos.top + paddings.top,
left: pos.left + paddings.left
});
},
collision: 'none'
});
});
};
if (elts.length === 0) {
return null;
}
if (opts.safemode === false) {
elts.each(function () {
var paddings, formLabel, label, labelParent, tagName,
$this = $(this);
if (this.value === '' && (label = this.title) !== "") {
if (opts.labelParent === "form") {
labelParent = $this.closest("form");
tagName = "<label/>";
} else {
labelParent = $(opts.labelParent);
tagName = "<span/>";
}
if (labelParent.css("position") === "static" && !labelParent.data("processed")) {
labelParent.css({
position: "relative"
}).data("processed", true);
}
paddings = {
top: getPropertyValue.call($this, "padding-top") + 1,
left: Math.max(2, getPropertyValue.call($this, "padding-left") + 1)
};
formLabel = $(tagName, {
css: {
'font-family' : $this.css("font-family"),
'font-size' : $this.css("font-size"),
'font-style' : $this.css("font-style"),
'font-weight' : $this.css("font-weight"),
'text-shadow' : ($.support.opacity) ? $this.css("text-shadow") : "",
'line-height' : $this.css("line-height"),
'background-color' : $this.css("background-color"),
'color' : $this.css("color"),
'position' : 'absolute',
'top' : 0,
'left' : 0,
'-moz-user-select' : "none",
'-webkit-user-select' : "none",
'user-select' : "none",
'cursor' : "text",
'z-index' : "999"
},
id: "spanLabel" + spanID,
"for": ($this.attr("id") === "") ? "" : $this.attr("id"),
"class" : "fLabel",
html : label,
click : function () {
$this.trigger('focus');
return false;
}
});
if (opts.semantic && opts.labelParent === 'form') {
$this.before(formLabel);
} else {
formLabel.appendTo(labelParent);
}
formLabel.position({
my: "left top",
at: "left top",
of: $this,
using: function (pos) {
$(this).css({
top: pos.top + paddings.top,
left: pos.left + paddings.left
});
},
collision: 'none'
});
$this.data({
"spanID" : "#spanLabel" + spanID,
"paddings" : JSON.stringify(paddings)
});
}
spanID = spanID + 1; //Increasing spanID by one
})
.on('focus.formLabels blur.formLabels change.formLabels cut.formLabels paste.formLabels input.formLabels keyup.formLabels', function (e) {
if (enabled === false) {
return;
}
var LabelSpanID = $($.data(this, "spanID"));
if (e.type === 'focus') {
if (this.title !== "" && this.value === "") {
LabelSpanID.stop().fadeTo(opts.animationDuration, 0.2);
}
} else if (e.type === "blur") {
if (this.title !== "") {
if (this.value === "") {
LabelSpanID.stop().fadeTo(opts.animationDuration, 1, function () {
if (!$.support.opacity) {
this.style.removeAttribute("filter");
}
});
} else {
LabelSpanID.stop().fadeOut(opts.animationDuration);
}
}
} else if (e.type === "change" || e.type === "cut" || e.type === "paste" || e.type === "input" || e.type === "keyup") {
this.title !== "" && LabelSpanID.stop().fadeOut(opts.animationDuration);
this.value === "" && LabelSpanID.stop().fadeTo(100, 0.2);
}
});
if (opts.refreshOnResize) {
$(window).on("resize.formLabels", function () {
refreshLabels();
});
}
} else {
elts.val(function (i, v) {
return v === "" ? this.title : v;
});
elts.on('focus.formLabels blur.formLabels', function (e) {
if (enabled === false) {
return;
}
if (e.type === 'focus') {
(this.value === this.title) && (this.value = "");
} else {
(this.value === "") && (this.value = this.title);
}
});
$("input:image, button, input:submit", context).on("click.formLabels", function () {
if (enabled === false) {
return;
}
elts.each(function () {
if (this.type === "email" || this.type === "text" || this.type === "textarea" || this.type === "password") {
if (this.value === this.title && this.title !== "") {
this.value = "";
}
}
});
});
}
return {
refreshLabels: refreshLabels,
destroy: function () {
elts.each(function () {
var label = $.data(this, "spanID");
if (label) {
// remove from the dom
$(label).remove();
$.data(this, "spanID", null);
}
}).off(".formLabels");
$(window).off(".formLabels");
},
disable: function () {
if (enabled === true) {
enabled = false;
elts.each(function () {
$($.data(this, "spanID")).hide();
});
}
},
enable: function () {
if (enabled === false) {
enabled = true;
elts.each(function () {
$($.data(this, "spanID")).show();
});
}
}
};
};
})(jQuery);