-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemailHint.js
77 lines (67 loc) · 2.33 KB
/
emailHint.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
var EmailHint = function() {
var domains;
var measureText = function(el) {
var span = document.createElement("span");
span.innerHTML = el.value;
span.className = "emailHint-measure";
document.body.appendChild(span);
var width = span.offsetWidth;
document.body.removeChild(span);
return width;
};
var addHint = function(el) {
el.parentNode.getElementsByTagName("input")[0].value += el.innerHTML;
el.innerHTML = "";
};
var findHint = function(hint, el) {
var domainsLength = domains.length;
for (var i = 0; i < domainsLength; i++) {
var hintElement = el.parentNode.getElementsByTagName("span")[0];
if (hint.length > 0 && domains[i].indexOf(hint) === 0) {
hintElement.innerHTML = domains[i].replace(hint, "");
hintElement.style.left = measureText(el) + "px";
break;
}
else {
hintElement.innerHTML = "";
}
}
};
var removeHint = function(el) {
var hintElement = el.parentNode.getElementsByTagName("span")[0];
hintElement.innerHTML = "";
};
var showHint = function(el) {
var at = el.value.indexOf("@");
removeHint(el);
if (at !== -1) {
var hint = (el.value.substr(at + 1, el.value.length));
if (hint) {
findHint(hint, el);
}
}
};
var init = function(className, hints) {
domains = hints;
var emailElements = document.querySelectorAll("." + className);
for (var i = emailElements.length - 1; i >= 0; i--) {
var input = emailElements[i].getElementsByTagName("input")[0];
input.addEventListener("keyup", function() {
showHint(this);
}, false);
input.addEventListener("blur", function() {
addHint(this.nextElementSibling);
}, false);
var span = emailElements[i].getElementsByTagName("span")[0];
span.addEventListener("click", function() {
addHint(this);
}, false);
input.form.addEventListener("submit", function() {
addHint(span);
}, false);
};
};
return {
init: init
}
}();