This repository was archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (88 loc) · 1.76 KB
/
index.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
/**
* dependencies
*/
var classes = require('classes')
, events = require('events');
/**
* Export `placeholder`
*/
module.exports = Placeholder;
/**
* Initialize a new `Placeholder`.
*
* @param {Element} el
*/
function Placeholder(el){
if (!(this instanceof Placeholder)) return new Placeholder(el);
if (!el) throw new TypeError('expected an input / textarea element');
this.events = events(el, this);
this.classes = classes(el);
this.type = el.type;
this.el = el;
this.bind();
}
/**
* Bind internal events.
*
* @return {Placeholder}
*/
Placeholder.prototype.bind = function(){
this.events.bind('focus');
this.events.bind('blur');
return this;
};
/**
* Unbind internal events.
*
* @return {Placeholder}
*/
Placeholder.prototype.unbind = function(){
this.events.unbind();
return this;
};
/**
* Set the placeholder to `val`.
*
* @param {String} val
* @return {Placeholder}
*/
Placeholder.prototype.set = function(val){
this.val = val;
if (this.classes.has('placeholder')) return this;
if (this.el.value) return this;
this.classes.add('placeholder');
this.el.value = val;
return this;
};
/**
* Clear placeholder.
*
* @return {Placeholder}
*/
Placeholder.prototype.clear = function(){
if (this.val != this.el.value) return this;
if (!this.classes.has('placeholder')) return this;
this.el.value = '';
this.classes.remove('placeholder');
return this;
};
/**
* Remove the placeholder on focus.
*
* @param {Event} e
* @api private
*/
Placeholder.prototype.onfocus = function(e){
this.clear();
if (this.el != document.activeElement) return;
this.el.select();
};
/**
* Set the placeholder on focus.
*
* @param {Event} e
* @api private
*/
Placeholder.prototype.onblur = function(e){
this.set(this.val);
};