-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent-evaluationJSFile.js
196 lines (164 loc) · 7.35 KB
/
event-evaluationJSFile.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//I am limiting the list size to 100 events.
var EVENTSBOARD_SIZE = 100;
//Here's our Firebase reference for 1) events on display...
var eventScore = new Firebase("https://eventeval.firebaseio.com/events");
//...and 2) archived events
var archiveRef = new Firebase("https://eventeval.firebaseio.com/archives");
//I had no idea what this supposed to do at first. Now I know it's creating a dictionary.
//Later on, it's going to tell us what's being added to the HTML
var htmlForEachEvent = {}
//This tells us how the new info will be appended in the HTML. I had to look up ".append" and ".val"
//Added "prevEvent" as part of testing the remove event feature. Works without it!
//This section got major help from Anant, Alex and Adam because I couldn't figure out how to
//make the buttons increase the scores. Initially the buttons were placeholders and score
//was inputted manually just like the Leaderboard example on Firebase.com.
//.attr() lets me add CSS around the buttons
function addEventToTable(newEvent, prevEvent) {
var newEventRow = $("<tr/>").attr({'class':'row'});
newEventRow.append($("<td/>").text(newEvent.val().name));
var upvoteButton = $('<button/>', {
text: "Yes!",
click: function () { upvoteEvent(newEvent.name()); }
}).attr({'class':'btn button button-flat-action'});
var downvoteButton = $('<button/>', {
text: "No!",
click: function () { downvoteEvent(newEvent.name()); }
}).attr({'class':'btn button button-flat-caution'});
var archiveButton = $('<button/>', {
text: "Archive",
click: function () { archiveEvent(newEvent.name()); }
}).attr({'class':'btn button button-flat-royal archive-btn'});
newEventRow.append($("<td/>").text(newEvent.val().upvotes));
newEventRow.append(upvoteButton);
newEventRow.append($("<td/>").text(newEvent.val().downvotes));
newEventRow.append(downvoteButton);
newEventRow.append(archiveButton);
console.log(newEvent.val().name + ": " + newEventRow.html());
//This tells me what's being added in the HTML
htmlForEachEvent[newEvent.name()] = newEventRow;
// Insert the new score in the appropriate place in the table.
//Should "newEvent" be what I'm calling here?
if (prevEvent === null) {
//Adds HTML to the table. Could remove the rest of the If statement and the app will work fine.
$("#eventTable").append(newEventRow);
}
else {
var lowerUpvoteScore = htmlForEachEvent[prevEvent];
//Just adding this section got me an error becuase "before" is undefined. Remove if statement to restore app.
lowerUpvoteScore.before(newEventRow);
}
}
// Arhive an event. Anant helped add this.
function archiveEvent(name) {
var eventRef = eventScore.child(name);
eventRef.once('value', function(snap) {
eventRef.remove();
archiveRef.child(name).set(snap.val());
});
}
// Helper function to handle a score object being removed; just removes the corresponding table row.
function removeEvent(newEvent) {
var removeEventRow = htmlForEachEvent[newEvent.name()];
removeEventRow.remove();
delete htmlForEachEvent[newEvent.name()];
}
//Updating one updates the other because of "eventScore". How do I separate? Solution: fixed
//addEventToTable function.
function upvoteEvent(name) {
console.log("In the upvote function")
var eventRef = eventScore.child(name);
// Note, this does NOT reset priority
eventRef.transaction(function(current_value) {
var newValue = current_value;
newValue.upvotes += 1;
console.log("Updating upvotes to " + (newValue.upvotes));
return {".priority": newValue.upvotes, ".value": newValue};
}, function(error, committed, snapshot) {
if (error) {
alert('Did we commit the transaction? ' + committed);
alert('The final value is: ' + snapshot.val());
}
});
}
function downvoteEvent(name) {
console.log("In the downvote function")
var eventRef = eventScore.child(name + "/downvotes");
// Note, this does NOT reset priority
eventRef.transaction(function(current_value) {
console.log("Updating downvotes to " + (current_value + 1));
return current_value + 1;
}, function(error, committed, snapshot) {
if (
error) {
alert('Did we commit the transaction? ' + committed);
alert('The final value is: ' + snapshot.val());
}
});
}
//Viewing the last events. If I comment out ".limit(EVENTSBOARD_SIZE);" the limit will be removed!
//How do I get this list in the reverse order?! I tried .reverse... And then once I added "prevEvent"
//and "remove" stuff... the order worked fine! WTF. - Solved later with other code.
var eventListView = eventScore.limit(EVENTSBOARD_SIZE);
//"When Firebase tells you a new item was added, do this". Works great. No need to comment out.
eventListView.on('child_added', function(newEvent, prevEvent) {
addEventToTable(newEvent, prevEvent);
});
//New code up until the "enter key" section. Comment out if buggy.
//"When firebase tells me an item has been modified, update it"
// Add a callback to handle when a score is removed
eventListView.on('child_removed', function (oldEvent) {
removeEvent(oldEvent);
});
// Add a callback to handle when a score changes or moves positions.
var changedCallback = function (newEvent, prevEvent) {
removeEvent(newEvent);
addEventToTable(newEvent, prevEvent);
};
eventListView.on('child_moved', changedCallback);
eventListView.on('child_changed', changedCallback);
// When the user presses enter on eventName, add the event and its up/down votes
$("#eventName").keypress(function (e) {
if (e.keyCode == 13) {
console.log("Why won't you work?!")
//I tried commenting the following two var out as well as removing the var newEventUpvotes and newEventDownvotes from the eventRef.setWithPriority.
//Also commented out the HTML for the input boxes.
var newEventUpvotes = 0;
//Number($("#eventUpvotes").val());
var newEventDownvotes = 0;
//Number($("#eventDownvotes").val());
var name = $("#eventName").val();
// Reset values
$("#eventName").val("");
$("#eventUpvotes").val("0");
$("#eventDownvotes").val("0");
if (name.length === 0)
return;
var eventRef = eventScore.child(eventScore.push().name());
// Use setWithPriority to put the name / votes in Firebase, and set the priority to be the score.
eventRef.setWithPriority({ name:name, upvotes: newEventUpvotes, downvotes:newEventDownvotes }, newEventUpvotes);
}
});
//Thanks to Sara for helping make the submit button work.
$("#submit-btn").click(function (e) {
console.log("Why won't you work?!")
//I tried commenting the following two var out as well as removing the var newEventUpvotes and newEventDownvotes from the eventRef.setWithPriority.
//Also commented out the HTML for the input boxes.
var newEventUpvotes = 0;
//Number($("#eventUpvotes").val());
var newEventDownvotes = 0;
//Number($("#eventDownvotes").val());
var name = $("#eventName").val();
// Reset values
$("#eventName").val("");
$("#eventUpvotes").val("0");
$("#eventDownvotes").val("0");
if (name.length === 0)
return;
var eventRef = eventScore.child(eventScore.push().name());
// Use setWithPriority to put the name / votes in Firebase, and set the priority to be the score.
eventRef.setWithPriority({ name:name, upvotes: newEventUpvotes, downvotes:newEventDownvotes }, newEventUpvotes);
});
// setTimeout(function() {
// upvoteEvent("Kaitlin");
//}, 1000);
//downvoteEvent("Kaitlin");