Commit 6789e0db authored by Kannan V M's avatar Kannan V M
Browse files

add moderation controls

parent 0589e953
......@@ -48,10 +48,22 @@ const notifyEveryone = function notifyUserStateChangeToEveryone() {
};
const updateLeavesData = function udateLeavesDataAndNotify(socketId, leafData) {
if (!(leavesData[socketId].leafId)) {
leavesData[socketId].leafId = leafIds.shift();
}
leavesData[socketId] = { ...leavesData[socketId], ...leafData };
notifyEveryone();
};
const deleteLeaf = function deleteLeafAndNotifyEveryone(socketId) {
const { leafId } = leavesData[socketId];
delete leavesData[socketId].leafText;
delete leavesData[socketId].leafId;
leafIds.push(leafId);
enUserNamespace.emit('leaf delete', leafId);
enModNamespace.emit('leaves data', leavesData);
};
enUserNamespace.on('connection', (socket) => {
const socketId = socket.id;
leavesData[socketId] = { userName: socket.handshake.query.userName }; // user
......@@ -61,7 +73,7 @@ enUserNamespace.on('connection', (socket) => {
const leafData = {};
leafData.leafText = leafText;
if (!(leavesData[socketId].leafText)) {
leafData.leafNo = leafIds.shift();
leafData.leafId = leafIds.shift();
}
updateLeavesData(socketId, leafData);
......@@ -75,17 +87,14 @@ enUserNamespace.on('connection', (socket) => {
enModNamespace.on('connection', (socket) => {
enModNamespace.emit('leaves data', leavesData);
socket.on('leaf delete', (socketId) => {
leafIds.push(leavesData[socketId].leafId);
delete leavesData[socketId].leafText;
delete leavesData[socketId].leafId;
notifyEveryone();
deleteLeaf(socketId);
});
socket.on('leaf update', (leafData) => updateLeavesData(socket.id, leafData));
socket.on('leaf update', (data) => updateLeavesData(data.socketId, data.leafData));
socket.on('user delete', (socketId) => {
leafIds.push(leavesData[socketId].leafId);
deleteLeaf(socketId);
enUserNamespace.sockets.get(socketId).disconnect();
delete leavesData[socketId];
notifyEveryone();
enModNamespace.emit('leaves data', leavesData);
});
socket.on('save exit', () => { // save data into file
enUserNamespace.disconnectSockets();
......
table {
width: 100%;
background-color: #ffffff;
border-collapse: collapse;
border-width: 2px;
border-color: #ffcc00;
border-style: solid;
color: #000000;
}
table td, table th {
border-width: 2px;
border-color: #ffcc00;
border-style: solid;
padding: 3px;
}
table thead {
background-color: #3584e4;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Document</title>
<script src="https://cdn.socket.io/4.3.2/socket.io.min.js" integrity="sha384-KAZ4DtjNhLChOB/hxXuKqhMLYvx3b5MlT55xPEiNmREKRzeEm+RVPlTnAn0ajQNs" crossorigin="anonymous"></script>
<script src="mod.js"></script>
</head>
<body>
<ul id="listid">
</ul>
<br/>
<br/>
<br/>
<br/>
<button onclick="savenExit()">Save and Reset</button>
</body>
<head>
<meta charset="UTF-8"/>
<title>Document</title>
<link rel="stylesheet" href="mod.css">
<script src="https://cdn.socket.io/4.3.2/socket.io.min.js" integrity="sha384-KAZ4DtjNhLChOB/hxXuKqhMLYvx3b5MlT55xPEiNmREKRzeEm+RVPlTnAn0ajQNs" crossorigin="anonymous"></script>
</head>
<body>
<table>
<thead>
<tr>
<th>User name</th>
<th>Leaf Text</th>
<th>Buttons</th>
</tr>
</thead>
<tbody id="tableId">
<tr><td colspan=3>No users</td></tr>
</tbody>
</table>
<button onclick="savenExit()">Save and Reset</button>
<script src="mod.js"></script>
</body>
</html>
const mod = io.connect('/mod_en');
mod.on('leaves data', (leavesdata) => {
var text = {};
var x;
document.getElementById("listid").innerHTML = '';
for (x in leavesdata) {
document.getElementById("listid").innerHTML += '<li>'+leavesdata[x]["leaftext"]+'<button onclick="contentRemove('+"'"+x+"'"+')">Remove</button></li>';
};
const tBody = document.querySelector('tbody');
mod.on('leaves data', (leavesData) => {
console.log(leavesData);
var text = {};
tBody.innerHTML = '';
for (socketId in leavesData) {
const rowData =
`
<tr>
<td>${leavesData[socketId]["userName"]}</td>
<td><input type="text" id=${socketId} value=${leavesData[socketId]["leafText"]}></td>
<td>
<input type="button" onclick="contentUpdate(this.name)" name=${socketId} value="update text">
<input type="button" onclick="contentRemove(this.name)" name=${socketId} value="delete text">
<input type="button" onclick="userRemove(this.name)" name=${socketId} value="delete user">
</td>
</tr>
`;
tBody.innerHTML += rowData;
};
});
function contentRemove(socketid){
mod.emit('message delete', socketid);
function contentRemove(socketId){
mod.emit('leaf delete', socketId);
};
function contentUpdate(socketId){
const leafData = { leafText: document.getElementById(socketId).value };
console.log('32', { socketId, leafData });
mod.emit('leaf update', { socketId, leafData });
};
function userRemove(socketId) {
mod.emit('user delete', socketId);
};
function savenExit(){
......
......@@ -15,9 +15,9 @@ function popupLeaf() {
document.getElementById('close-button').style.display = 'block';
}
function growLeaf(leafData) {
const { leafNo } = leafData;
const activeLeaf = document.getElementById(leafNo);
function growLeaf(socketId, leafData) {
const { leafId } = leafData;
const activeLeaf = document.getElementById(leafId);
activeLeaf.classList.add('leaf-active');
activeLeaf.classList.remove('leaf-inactive');
activeLeaf.addEventListener('click', popupLeaf, false);
......@@ -28,6 +28,12 @@ function growLeaf(leafData) {
const leafDataInput = document.getElementById(knowledgeId);
if (leafDataInput) {
leafDataInput.innerHTML = leafData.leafText;
console.log(socket.id, socketId);
if (socketId == socket.id) {
const knowledgeInput = document.getElementById('knowledge-input');
console.log('32',knowledgeInput);
knowledgeInput.value = leafData.leafText;
}
} else {
knowledgeItem.setAttribute('id', knowledgeId);
knowledgeItem.innerHTML = leafData.leafText;
......@@ -35,8 +41,8 @@ function growLeaf(leafData) {
}
}
function growLeaves(i, leafData) {
setTimeout(() => growLeaf(leafData), 300 * i);
function growLeaves(i, socketId, leafData) {
setTimeout(() => growLeaf(socketId, leafData), 300 * i);
}
socket.on('leaves data', (leavesData) => {
......@@ -45,13 +51,23 @@ socket.on('leaves data', (leavesData) => {
for(socketId in leavesData) {
leafData = leavesData[socketId];
if (leafData.leafText) {
growLeaves(i, leafData);
growLeaves(i, socketId, leafData);
i++;
}
}
}
});
socket.on('leaf delete', (leafId) => {
const activeLeaf = document.getElementById(leafId);
activeLeaf.classList.add('leaf-inactive');
activeLeaf.classList.remove('leaf-active');
activeLeaf.removeEventListener('click', popupLeaf, false);
const knowledgeId = activeLeaf.id.replace('path', 'knowledge');
const leafDataInput = document.getElementById(knowledgeId);
leafDataInput.outerHTML = "";
});
function addKnowledgeItem() {
leafText = document.getElementById('knowledge-input').value;
socket.emit("leaf text", leafText);
......
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