Commit 12f2888a authored by Bady's avatar Bady
Browse files

Update leaves using server-side data

parent 8f76c834
......@@ -481,30 +481,30 @@
//set up the tree with the data of all leaves
});
socket.on('leaf data', (leafdata) => {
//update the tree with the new leaf data
});
function addKnowledgeItem() {
leaftext = document.getElementById('knowledge-input').value;
var leafdata = { "leaftext": leaftext }
socket.emit("leaf text", leafdata);
}
function growLeaf() {
let inactiveLeaves = document.getElementsByClassName("leaf-inactive");
let currentLeaf = inactiveLeaves[0];
currentLeaf.classList.add("leaf-active");
currentLeaf.classList.remove("leaf-inactive");
currentLeaf.addEventListener('click', popupLeaf, false);
socket.on('leaf data', (leafdata) => {
leafID = leafdata.leafid;
activeLeaf = document.getElementById(leafID);
activeLeaf.classList.add("leaf-active");
activeLeaf.classList.remove("leaf-inactive");
activeLeaf.addEventListener("click", popupLeaf, false);
let knowledgeItem = document.createElement("p");
knowledgeItem.setAttribute("class", "knowledge-item");
let knowledgeId = currentLeaf.id.replace("path", "knowledge");
knowledgeItem.setAttribute("id", knowledgeId);
knowledgeItem.innerHTML = document.getElementById('knowledge-input').value;
document.getElementById("knowledge").appendChild(knowledgeItem);
}
let knowledgeId = activeLeaf.id.replace("path", "knowledge");
leafData = document.getElementById(knowledgeId);
if(!leafData) {
knowledgeItem.setAttribute("id", knowledgeId);
knowledgeItem.innerHTML = leafdata.leaftext;
document.getElementById("knowledge").appendChild(knowledgeItem);
}
leafData.innerHTML = leafdata.leaftext;
});
function popupLeaf() {
let knowledgeId = this.id.replace("path", "knowledge");
......
......@@ -13,7 +13,13 @@ server.listen(port, () => {
//assign static path to public folder
app.use(express.static(path.join(__dirname, '/')));
var leafids = []; //ids of leaves [TODO] to be filled
var leafids = [];
for (let i=7; i<332; i++) {
leafID = "path" + i.toString();
i += 3;
leafids.push(leafID);
}
var leavesdata = {}; //object of structure {socketid1: {leaftext:"text",leafid:"leafid"}, ...}
var leafno = 0;
var socketids = [];
......@@ -21,10 +27,10 @@ var socketids = [];
io.on('connection', (socket) => {
let id = socket.id;
io.to(id).emit('leaves data', leavesdata);
socketids.push(id);
// when the client emits 'leaf data'
socket.on('leaf text', (leafdata) => {
if (!socketids.includes(id)) { //checks if socketid is already assigned with leafid
if(!socketids.includes(id)) { //checks if socketid is already assigned with leafid
socketids.push(id);
leafdata["leafid"] = leafids[leafno]; //if not assign leafid
leafno = leafno+1;
leavesdata[id] = leafdata;
......@@ -32,6 +38,6 @@ io.on('connection', (socket) => {
leavesdata[id]["leaftext"] = leafdata["leaftext"]; //if yes update the message
leafdata = leavesdata[id];
};
socket.broadcast.emit('leaf data', leafdata);
socket.emit('leaf data', leafdata);
});
});
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment