-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
104 lines (84 loc) · 2.93 KB
/
app.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
var myBaseRef = new Firebase("https://askqueue.firebaseio.com");
var myQRef = myBaseRef.child('-Jg4e7XT_OaCuXONRNM1/questions');
var createQuestionTags = function(qData) {
var wrapper = document.createElement('div'),
question = document.createElement('div'),
votes = document.createElement('div'),
upvote = document.createElement('div'),
delq = document.createElement('div'),
qText = document.createElement('p');
question.className = 'question';
question.setAttribute('data-key', qData.key());
votes.className = 'votes';
votes.textContent = qData.val().votes.toString();
upvote.className = 'upvote';
upvote.textContent = '+';
delq.className = 'delete';
delq.textContent = 'x';
qText.textContent = qData.val().question;
votes.appendChild(upvote);
votes.appendChild(delq);
question.appendChild(votes);
question.appendChild(qText);
wrapper.appendChild(question);
return wrapper.innerHTML;
};
var updateQuestion = function(qData) {
var wrapper = document.createElement('div'),
votes = document.createElement('div'),
upvote = document.createElement('div'),
delq = document.createElement('div'),
qText = document.createElement('p');
votes.className = 'votes';
votes.textContent = qData.val().votes.toString();
upvote.className = 'upvote';
upvote.textContent = '+';
delq.className = 'delete';
delq.textContent = 'x';
qText.textContent = qData.val().question;
votes.appendChild(upvote);
votes.appendChild(delq);
wrapper.appendChild(votes);
wrapper.appendChild(qText);
return wrapper.innerHTML;
};
var LoadQuestion = function(qData, updated) {
var el = createQuestionTags(qData);
document.getElementsByClassName('questions')[0].innerHTML += el;
};
myQRef.on('child_added', function(snap){
LoadQuestion(snap);
});
myQRef.on('child_changed', function(snap){
$(".questions").find("[data-key='" + snap.key() + "']")[0].innerHTML = updateQuestion(snap);
});
myQRef.on('child_removed', function(snap){
$(".questions").find("[data-key='" + snap.key() + "']")[0].remove();
});
var submitQ = function() {
myQRef.push({
question: document.getElementsByClassName('input')[0].value,
votes: 0
});
$('.input').val("");
};
document.getElementsByClassName('input')[0].addEventListener('keydown', function(e) {
if(e.keyCode == 13) { submitQ(); }
}, false);
document.getElementsByClassName('submit')[0].addEventListener('click', function() {
submitQ();
});
$('.questions').on('click', '.upvote', function(e) {
var myKey = $(e.target).parents('.question')[0].getAttribute('data-key');
myQRef.child(myKey + '/votes').transaction(function(votes) { return votes + 1; });
});
$('.questions').on('click', '.delete', function(e) {
var myKey = $(e.target).parents('.question')[0].getAttribute('data-key');
myQRef.child(myKey).remove();
});
$('header a').on('click', function() {
$('.halp').show();
});
$('.halp').on('click', function() {
$('.halp').hide();
});