-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact_manager.js
98 lines (89 loc) · 2.96 KB
/
contact_manager.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
/* global alert, $, document, setTimeout, clearTimeout */
class ContactManager {
constructor(customOptions) {
var defaultOptions = {};
Object.assign(customOptions, defaultOptions);
this.$username = customOptions.$username;
this.$email = customOptions.$email;
this.$addBtn = customOptions.$addBtn;
this.$userSearch = customOptions.$userSearch;
this.container = customOptions.$container;
this.emailRegex = customOptions.emailRegex;
}
init() {
this.$addBtn.on("click", ContactManager.validator.bind(this, this.$username, this.$email, this.emailRegex, this.container));
this.container.on("click", ".deleteBtn", (event) => {
ContactManager.removeElements(event);
});
this.$userSearch.on("keyup", ContactManager.debounce((event) => {
this.$newElements = $(".newElements");
this.inputValue = this.$userSearch.val();
ContactManager.suggestName(this.$newElements, this.inputValue);
}, 1000, this.$userSearch));
}
static debounce(func, delay, $userSearch) {
let interval;
return () => {
clearTimeout(interval);
if (this.inputValue != $userSearch.val()) {
interval = setTimeout(() => func.apply(this), delay);
}
};
}
static validator($username, $email, emailRegex, container) {
if ($username.val().trim()) {
if (this.constructor.checkEmail($email, emailRegex)) {
ContactManager.addElements($username.val(), $email.val(), container);
} else {
alert("Email is Invalid");
}
} else {
alert("Please enter name");
}
}
static addElements(name, email, container) {
var newDiv = $("<div>").appendTo(container).addClass("newElements").css("border", "2px");
newDiv.html("<br/>" + "NAME: " + name + "<br/>" + "EMAIL: " + email).attr("id", name);
$("<input type=button value=Delete>").appendTo(newDiv).addClass("deleteBtn");
}
static removeElements(event) {
$(event.currentTarget).closest("div").remove();
}
static checkEmail($email, emailRegex) {
var EmailRegex = new RegExp(emailRegex);
if ($email.val().trim()) {
return EmailRegex.test($email.val());
}
}
static suggestName($newElements, inputValue) {
var count = 0;
if (!inputValue) {
$newElements.show();
} else {
$newElements.hide();
for (var j = 0; j < $newElements.length; j++) {
count = 0;
for (var i = 0; i < inputValue.length; i++) {
if (inputValue[i] == $newElements[j].id[i] && count == i) {
$($newElements[j]).show();
count++;
} else {
$($newElements[j]).hide();
}
}
}
}
}
}
$(document).ready(() => {
var customOptions = {
$username: $("#username"),
$email: $("#useremail"),
$addBtn: $("#userbutton"),
$userSearch: $("#usersearch"),
$container: $("#container-div"),
emailRegex: "^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$"
};
var obj = new ContactManager(customOptions);
obj.init();
});