-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatermark.js
111 lines (101 loc) · 2.63 KB
/
watermark.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
/*
* http://github.com/jwestbrook/Prototype.Watermark
*/
var Watermark = Class.create({
initialize: function(el, text, inForm)
{
this.el = $(el);
this.watermarkEl = new Element('div', {'class': 'watermarked'}).update(text);
if(inForm)
{
this.el.observe('focus', function(){
this.watermarkEl.setStyle({'textAlign': 'right'});
}.bind(this));
this.el.observe('keydown', this.hide.bind(this));
this.watermarkEl.setStyle({width: (this.el.getWidth() - (this.el.paddingLeft() * 2) - (this.el.marginLeft() * 2)) + 'px'});
}
else
{
this.el.observe('focus', this.hide.bind(this));
}
this.el.observe('blur', this.show.bind(this));
this.el.observe('change', function(){
if(this.canShow())
{
this.show();
}
else
{
this.hide();
}
}.bind(this));
this.watermarkEl.setStyle({
'display': 'none',
'fontSize': this.el.getStyle('fontSize'),
'fontFamily': this.el.getStyle('fontFamily'),
'lineHeight': this.el.getStyle('lineHeight'),
'paddingLeft': this.el.getStyle('paddingLeft'),
'paddingTop': this.el.getStyle('paddingTop'),
'marginLeft': this.el.getStyle('marginLeft'),
'marginTop': this.el.getStyle('marginTop')
});
this.watermarkEl.observe('click', function(){
this.el.focus();
}.bind(this));
// Wrap relative container around watermark element.
var wrapperEl = new Element('div', { 'style': 'position:relative;' });
wrapperEl.insert(this.watermarkEl);
Element.wrap(this.el, wrapperEl);
this.el.disabled = false;
var form = this.el.up('form');
if(Object.isElement(form))
{
form.observe('reset', function(){
this.show.bind(this).defer();
}.bind(this));
}
// Show watermark if text is not empty.
this.show();
},
show: function()
{
this.watermarkEl.setStyle({'textAlign': 'left'});
if(this.canShow())
{
this.el.setValue('');
this.watermarkEl.show();
}
},
hide: function()
{
if(this.isActivated())
{
this.watermarkEl.hide();
}
},
canShow: function()
{
return !this.isActivated() && this.el.getValue().blank();
},
isActivated: function()
{
return this.watermarkEl.visible();
}
});
Element.addMethods(['INPUT', 'TEXTAREA'], {
watermark: function(element, text, forTabs)
{
var waterMark = element.retrieve('watermark');
if(!waterMark)
{
waterMark = new Watermark(element, text || element.readAttribute('title'), forTabs);
element.store('watermark', waterMark);
}
return waterMark;
}
});
document.observe('dom:loaded', function() {
$$('.watermark').each(function(el) {
el.watermark();
});
});