forked from sffc/placeholdr
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplaceholdr.js
192 lines (172 loc) · 6.4 KB
/
placeholdr.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
/*
* Copyright (c) 2013 Shane Carr and Sam Placette
*
* https://github.com/SamPlacette/placeholdr
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
(function($, ns, placeholderAttribute, origValFn, alternatePlaceholderAttribute){
// Utility functions
var findPlaceholderInputs = function () {
var $this = $(this);
return $this.find("["+placeholderAttribute+"]," +
"["+alternatePlaceholderAttribute+"]");
};
var getPlaceholderText = function () {
var $this = $(this);
return ($this.attr(placeholderAttribute) ||
$this.attr(alternatePlaceholderAttribute));
};
var setCursorPositionAtStart = function () {
if (typeof this.selectionStart == "number") {
this.selectionStart = 0;
this.selectionEnd = 0;
}
else if (typeof this.createTextRange != "undefined") {
var range = this.createTextRange();
range.collapse(true);
range.moveStart('character', 0);
range.moveEnd('character', 0);
range.select();
}
};
var putPlaceholder = function(){
var $this = $(this);
if ($this.hasClass(ns)) { return; } // idempotent
if (!$this[origValFn]()) {
$this.addClass(ns);
if ($this.attr("type") === "password") {
$this.attr("type", "text");
$this.data(ns+"-pwd", true);
}
$this[origValFn](getPlaceholderText.call(this));
// disable selecting range and caret position within the placeholder text
// TODO this little snippet needs more testing (only IE9 and IE10 so far)
$this.on('mouseup', setCursorPositionAtStart)
.on('selectstart', false);
if ($this.is(':focus')) {
setCursorPositionAtStart.call(this);
}
}
};
var clearPlaceholder = function(){
var $this = $(this);
$this.removeClass(ns);
if ($this.data(ns+"-pwd")) {
$this.attr("type", "password");
}
if ($this[origValFn]() === getPlaceholderText.call(this)){
$this[origValFn]("");
}
// re-enable selecting range and caret position within the input text
$this.off('mouseup', setCursorPositionAtStart)
.off('selectstart', false);
};
var clearPlaceholdersInForm = function(){
findPlaceholderInputs.call(this).each(function() {
if ($(this).data(ns)) clearPlaceholder.call(this);
});
};
// options: Object (optional)
// options.preserveOnFocus: Boolean. If truthy, preserve placeholder text
// on focus (hide on keypress instead)
// options.overrideNative: Boolean. If truthy, override native placeholder
$.fn.placeholdr = function(options){
// ensure options exists (avoid null reference errors)
options || (options = {});
// Don't evaluate the polyfill if the browser supports placeholders
// unless an option specifies to override the native implementation
var overrideNative = options.overrideNative || false;
if (placeholderAttribute in document.createElement("input") &&
! overrideNative
) {
return;
}
// Find and iterate through all inputs with a native placeholder attribute
$(this).find("["+placeholderAttribute+"]").each(function() {
var $this = $(this);
// leave now if we've polyfilled this element before (idempotent)
if ($this.data(ns)) return;
$this.data(ns, true);
if (overrideNative) {
// copy original placeholder text to alternate location
$this.attr(alternatePlaceholderAttribute,
$this.attr(placeholderAttribute));
// clear native placeholder attr to disable native implementation
$this.attr(placeholderAttribute, '');
}
// put the placeholder into the value
putPlaceholder.call(this);
var putPlaceholderHandler = function (evt) {
var target = this;
// set putPlaceholder to execute in near future in order to catch
// IE10 clear event
setTimeout(function () {
putPlaceholder.call(target, evt);
}, 25);
};
// just for consistency, define a handler for clear too
var clearPlaceholderHandler = clearPlaceholder;
if (options && options.preserveOnFocus) {
// clear placeholder when characters are entered
$this.on('keypress', clearPlaceholderHandler)
// restore placeholder when input is cleared
.on('blur change mouseup keyup', putPlaceholderHandler);
}
else {
// handle focus and blur events
$this.focus(clearPlaceholderHandler);
$this.blur(putPlaceholderHandler);
}
});
// Find and iterate through all form elements in order to prevent
// placeholders from being submitted in forms.
$(this).find("form").each(function(){
var $this = $(this);
// leave now if we've polyfilled this element before (idempotent)
if ($this.data(ns)) return;
$this.data(ns, true);
$this.submit(clearPlaceholdersInForm);
});
};
// A convenience method to provide a common interface for consumers to
// get placeholder text value, regardless of whether implementation is
// native or via placeholdr library:
$.fn.getPlaceholderText = function () {
return getPlaceholderText.call(this);
};
// Overwrite the existing jQuery val() function
$.fn[origValFn] = $.fn.val;
$.fn.val = function(txt){
var $this = $(this);
if ($.type(txt) === "undefined"
&& $this.data(ns)
&& $this[origValFn]() === getPlaceholderText.call(this)
){
return "";
}
if ($.type(txt) === "string") {
clearPlaceholder.call(this);
}
return $.fn[origValFn].apply(this, arguments);
};
// Add default CSS rule
document.write("<style>." + ns + "{color:#AAA;}</style>");
// $, ns, placeholderAttribute, origValFn, alternatePlaceholderAttribute
})(jQuery, "placeholdr", "placeholder", "placeholdrVal", "placeholdr");