-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoteClient.js
67 lines (57 loc) · 2.19 KB
/
voteClient.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
var socket = io();
socket.on("displayVotingWords", function(dataFromServer) {
// Fills votingWordsTable for client
$("#votingWordsTable").empty();
var votingTable = "";
votingTable += "<tr id='submitRow'><td><img src='img/add.png' id='add' class='no-selection'></td><td> </td>";
votingTable += "<td id='submissiontd'><input id='wordBox' type='text' placeholder='Type your word submission here...'><button type='button' id='addButton'>Add It!</button></td></tr>";
for (let i = 0; i < dataFromServer.length; i++) {
votingTable += "<tr>";
votingTable += ("<td><img src='img/upvote.png' alt='upvote' class='voteClick' id='upvote" + i + "' draggable='false'><img src='img/downvote.png' alt='downvote' class='voteClick' id='downvote" + i + "' draggable='false'></td>");
votingTable += ("<td>" + dataFromServer[i].points + "</td>");
votingTable += ("<td>" + dataFromServer[i].word + "</td>");
votingTable += "</tr>";
$("#votingWordsTable").append(votingTable);
votingTable = "";
var currentWord = dataFromServer[i].word;
//click handlers for upvote/downvote
createButtons(currentWord, i);
}
createSubmissionHandlers();
});
function createSubmissionHandlers() {
//Adds handlers for submitting a new word
console.log("sub handlers created");
$("#addButton").click(submitWord);
$("#wordBox").keypress(function(event){
if (event.which == 13) {
submitWord();
event.preventDefault();
}
});
}
function createButtons(currentWord, index) {
//Creates button handlers for upvote/downvote
$("#upvote" + index).click(function() {
console.log(currentWord);
socket.emit("voteWord", currentWord, true);
});
$("#downvote" + index).click(function() {
console.log(currentWord);
socket.emit("voteWord", currentWord, false);
});
}
function submitWord() {
//Submits word in #wordBox input
socket.emit("submitWord", $("#wordBox").val());
$("#wordBox").val("");
};
socket.on("submissionFeedback", function(message){
//Displays feedback from server to user on whether or not submitted word was accepted
$("#wordBox").attr("placeholder", message);
});
function startup() {
console.log("startup!");
socket.emit("getVotingWords");
}
$(startup);