-
Notifications
You must be signed in to change notification settings - Fork 57
/
jquery.ah-placeholder.js
167 lines (156 loc) · 5.16 KB
/
jquery.ah-placeholder.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
/*
* jQuery ah-placeholder plugin 1.2
*
* https://github.com/ahomu/jquery.ah-placeholder
* http://havelog.ayumusato.com/develop/javascript/e189-jquery-plugin-placeholder.html
*
* Copyright (c) 2011 Ayumu Sato ( http://havelog.ayumusato.com )
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
(function($) {
$.fn.ahPlaceholder = function(options)
{
// property
var defaults = {
placeholderColor : 'silver',
placeholderAttr : 'title',
likeApple : false
},
settings = $.extend({}, defaults, options);
var ngCode = [
' ', // --------------
'0', // ???
'9', // tab
'16', // shift
'17', // ctrl
'32', // space
'27', // esc
'37', // left
'38', // up
'39', // right
'40', // down
'91', // Cmd(L), Win(L)
'92', // Win(R)
'93', // Cmd(R)
'112',// F1
'113',// F2
'114',// F3
'115',// F4
'116',// F5
'117',// F6
'118',// F7
'119',// F8
'120',// F9
'121',// F10
'122',// F11
'123',// F12
' ' // ---------------
].join('@'),
keyCatch = (function(){
if ( document.all ) {
return function(e){return e.keyCode;};
} else if ( document.getElementById ) {
return function(e){return (e.keyCode)? e.keyCode: e.charCode;};
} else if ( document.layers ) {
return function(e){return e.which;};
}
})();
// method
var init = function()
{
// placeholderが有効なら処理を必要としないので終了
if ( settings.placeholderAttr === 'placeholder' && ('placeholder' in document.createElement('input')) ) {
return;
}
$.data(this, 'placeholder-string', $(this).attr(settings.placeholderAttr));
$.data(this, 'placeholder-color', $(this).css('color'));
var phString = $.data(this, 'placeholder-string'),
self = this,
$self = $(this);
if ( self.value === '' ) {
self.value = phString;
$self.css('color', settings.placeholderColor);
}
if ( settings.likeApple === true ) {
$self.bind('mousedown', moveCursorToHead);
$self.bind('keydown', onKeydown);
$self.bind('keyup', resetPlaceholder);
} else {
$self.bind('focus', onFocus);
$self.bind('blur', resetPlaceholder);
}
$self.closest('form').submit(function() {
if ( self.value === $.data(self, 'placeholder-string')
&& $self.css('color') === $('<div/>').css('color', settings.placeholderColor).css('color')) {
self.value = '';
}
return true;
});
},
onKeydown = function(e)
{
if ( this.value === $.data(this, 'placeholder-string') ) {
var key = keyCatch(e);
if ( ngCode.indexOf('@'+key+'@') !== -1 ) {
// tabの入力は認める
return ( key === 9 );
} else {
_clearPlaceholder(this);
}
}
},
onFocus = function()
{
if ( this.value === $.data(this, 'placeholder-string') ) {
_clearPlaceholder(this);
}
},
resetPlaceholder = function(e)
{
if ( this.value === '' ) {
_setPlaceholder(this);
if ( e.type === 'keyup' ) {
moveCursorToHead.apply(this);
}
}
},
moveCursorToHead = function()
{
if ( this.value === $.data(this, 'placeholder-string') ) {
$(this).focus();
if ( this.createTextRange ) {
var range = this.createTextRange();
range.collapse();
range.moveEnd('character', 0);
range.moveStart('character', 0);
setTimeout(function() {
range.select();
}, 17);
}
else if ( this.setSelectionRange ) {
this.setSelectionRange(0, 0);
}
return false;
}
},
_setPlaceholder = function(self)
{
self.value = $.data(self, 'placeholder-string');
$(self).css('color', settings.placeholderColor);
},
_clearPlaceholder = function(self)
{
self.value = '';
$(self).css('color', $.data(self, 'placeholder-color'));
};
// construct
this.each(function()
{
init.apply(this);
});
return this;
};
})(jQuery);