-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkanban.js
143 lines (106 loc) · 2.96 KB
/
kanban.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
$(document).ready(function(){
initializeFirebase();
checkPage();
setEventListeners();
})
var initializeFirebase = function(){
myDataRef = new Firebase('https://blazing-fire-1516.firebaseio.com/');
}
var checkPage = function(){
if(location.search === ""){
board = myDataRef.push({init: " "})
boardId = board.name()
loadNewPage(location.href, boardId)
}else{
boardId = location.search.split("")
delete boardId[0]
boardId = boardId.join("")
// console.log(boardId)
doesBoardExist(boardId)
}
}
var doesBoardExist = function(boardId){
myDataRef.child(boardId).once('value', function(snapshot) {
if (snapshot.val() === null){
createNewBoard(boardId)
}
});
}
var createNewBoard = function(boardId){
board = myDataRef.child(boardId).push({init: " "})
boardId = board.name()
}
var loadNewPage = function(current_url, board_id){
window.open( current_url + "?"+board_id, "_self")
}
var setEventListeners = function(){
submitNewMessage();
getNewMessages();
clickOnBacklog();
clickOnInProgress();
updateTask();
}
var clickOnBacklog = function(){
$(document).on('click', '#backlog-items li', function(e){
var uniq_key = $(e.target).attr("id")
myDataRef.child(boardId).child(uniq_key).update({status: "in-progress"})
})
}
var clickOnInProgress = function(){
$(document).on('click', '#in-progress-items li', function(e){
var uniq_key = $(e.target).attr("id")
myDataRef.child(boardId).child(uniq_key).update({status: "done"})
})
}
var updateTask = function(){
myDataRef.child(boardId).on('child_changed', function(snapshot) {
var uniq_key = snapshot.name()
var data = snapshot.val()
$('#' + uniq_key).remove()
displayChatMessage(data.task, data.status, uniq_key)
});
}
var getNewMessages = function(){
myDataRef.child(boardId).on('child_added', function(snapshot) {
var message = snapshot.val();
displayChatMessage(message.task, message.status, snapshot.name());
});
}
var displayChatMessage = function(task, status, uniq_key) {
var list_item = document.createElement('li')
list_item.innerHTML = task
$(list_item).attr('id', uniq_key)
$('#' + status + '-items').append(list_item)
};
var submitNewMessage = function(){
$('#taskInput').keypress(function (e) {
if (e.keyCode == 13) {
addMessageToFb()
}
});
}
var addMessageToFb = function(){
if (taskIsValid()){
myDataRef.child(boardId).push({task: getTask(), status: "backlog"});
}
clearTextBox()
}
var clearTextBox = function(){
$('#taskInput').val('');
}
var getTask = function(){
var text = $('#taskInput').val();
return escapeString(text)
}
var taskIsValid = function(){
return true
}
var escapeString = function(text){
sanitizedText = text.replace(/&/g, '&');
sanitizedText =sanitizedText.replace(/&/g, '&')
sanitizedText =sanitizedText.replace(/</g, '<')
sanitizedText =sanitizedText.replace(/>/g, '>')
sanitizedText =sanitizedText.replace(/\"/g, '"')
sanitizedText =sanitizedText.replace(/\'/g, ''');
return sanitizedText
}