Skip to content

Instantly share code, notes, and snippets.

@mikedoubintchik
Created May 10, 2017 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikedoubintchik/564a9b6afd01e6f25ba65ab9388f48fa to your computer and use it in GitHub Desktop.
Save mikedoubintchik/564a9b6afd01e6f25ba65ab9388f48fa to your computer and use it in GitHub Desktop.
const ManageWaitingList = {
init() {
this.waitingList = [];
this.buttons = document.querySelectorAll("button");
this.input = document.querySelector("input");
this.waitingListContainer = document.getElementById("waiting-list");
this.bindEvents();
this.onLoad();
},
addPeople(people) {
if (Array.isArray(people)) {
people.forEach(function(person, index) {
waitingList.push(person);
});
} else {
waitingList.push(people);
}
},
removePeople(people) {
if (Array.isArray(people)) {
people.forEach(function(person, index) {
index = waitingList.indexOf(person);
waitingList.splice(index, 1);
});
} else {
index = waitingList.indexOf(people);
waitingList.splice(index, 1);
}
},
updateWaitingList() {
var waitingListHtml = "";
waitingList.forEach(function(person, index) {
waitingListHtml += "<li>" + person + "</li>";
});
this.waitingListContainer.innerHTML = waitingListHtml;
},
buttonClicked(e) {
var people = this.input.value;
if (people.includes(",")) {
people = people.split(",");
}
if (people !== "") {
if (e.target.className === "add") {
this.addPeople(people);
this.updateWaitingList();
}
if (e.target.className === "remove") {
this.removePeople(people);
this.updateWaitingList();
}
} else {
alert("Please enter a name");
}
},
bindEvents() {
for (var i = 0; i < this.buttons.length; i++) {
this.buttons[i].addEventListener("click", this.buttonClicked.bind(this));
}
},
onLoad() {
window.onload = function() {};
}
};
ManageWaitingList.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment