Commit 34c11748 authored by Bady's avatar Bady
Browse files

Merge branch 'devel' into 'master'

Back-end

See merge request !2
parents e8cbefa1 4c225dbb
# Created by https://www.gitignore.io/api/node
# Edit at https://www.gitignore.io/?templates=node
### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# next.js build output
.next
# nuxt.js build output
.nuxt
# rollup.js default build output
dist/
# Uncomment the public line if your project uses Gatsby
# https://nextjs.org/blog/next-9-1#public-directory-support
# https://create-react-app.dev/docs/using-the-public-folder/#docsNav
# public
# Storybook build outputs
.out
.storybook-out
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# Temporary folders
tmp/
temp/
# End of https://www.gitignore.io/api/node
# Created by https://www.gitignore.io/api/emacs
# Edit at https://www.gitignore.io/?templates=emacs
### Emacs ###
# -*- mode: gitignore; -*-
*~
\#*\#
/.emacs.desktop
/.emacs.desktop.lock
*.elc
auto-save-list
tramp
.\#*
# Org-mode
.org-id-locations
*_archive
# flymake-mode
*_flymake.*
# eshell files
/eshell/history
/eshell/lastdir
# elpa packages
/elpa/
# reftex files
*.rel
# AUCTeX auto folder
/auto/
# cask packages
.cask/
dist/
# Flycheck
flycheck_*.el
# server auth directory
/server/
# projectiles files
.projectile
# directory configuration
.dir-locals.el
# network security
/network-security.data
# End of https://www.gitignore.io/api/emacs
# Created by https://www.gitignore.io/api/vim
# Edit at https://www.gitignore.io/?templates=vim
### Vim ###
# Swap
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]
# Session
Session.vim
Sessionx.vim
# Temporary
.netrwhist
*~
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~
# Coc configuration directory
.vim
# End of https://www.gitignore.io/api/vim
# config file
config.js
\ No newline at end of file
module.exports = {
port: 3000,
filename: "knowledge.json",
username: "treehouse",
password: "fsc2kMod!"
}
......@@ -460,7 +460,7 @@
<form id="knowledge-form">
<input type="text" id="knowledge-input">
<input type="submit" value="Submit" onclick="event.preventDefault(); growLeaf();">
<input type="submit" value="Submit" onclick="event.preventDefault(); addKnowledgeItem();">
</form>
<div id="knowledge"></div>
......@@ -468,22 +468,55 @@
<button onclick="closeLeaf()">Close</button>
</div>
<script src="socket.io.js"></script>
<script>
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0-or-later
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);
var socket = io();
socket.on('leaves data', (leavesdata) => {
let i = 0;
for(leaf in leavesdata) {
leafdata = leavesdata[leaf];
growLeaves(i, leafdata);
i++;
}
function growLeaves(i, leafdata) {
setTimeout(function() {
growLeaf(leafdata);
}, 300 * i);
}
});
function addKnowledgeItem() {
leaftext = document.getElementById('knowledge-input').value;
var leafdata = { "leaftext": leaftext }
socket.emit("leaf text", leafdata);
}
socket.on('leaf data', (leafdata) => {
growLeaf(leafdata);
});
function growLeaf(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) {
leafData.innerHTML = leafdata.leaftext;
} else {
knowledgeItem.setAttribute("id", knowledgeId);
knowledgeItem.innerHTML = leafdata.leaftext;
document.getElementById("knowledge").appendChild(knowledgeItem);
}
}
function popupLeaf() {
......@@ -518,6 +551,16 @@
activeLeaves[i].addEventListener('click', popupLeaf, false);
}
}
socket.on('delete text', (leafid) => {
activeLeaf = document.getElementById(leafid);
activeLeaf.classList.remove("leaf-active");
activeLeaf.classList.add("leaf-inactive");
activeLeaf.removeEventListener("click", popupLeaf, false);
let knowledgeId = leafid.replace("path", "knowledge");
document.getElementById(knowledgeId).innerHTML = "";
});
// @license-end
</script>
</body>
......
var config = require('./config');
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var path = require('path');
var fs = require('fs')
var port = config.port || 3000;
var filename = config.filename || 'knowledge.json';
var filepath = path.join('/tmp', filename);
//asking server to listen to 3000
server.listen(port, () => {
console.log('Server listening at port %d', port);
});
//assign static path to public folder
app.use(express.static(path.join(__dirname, '/')));
app.get('/mod', (req, res) => {
res.sendFile(__dirname + '/mod.html');
});
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
const user = io.of('/');
const mod = io.of('/mod');
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 = [];
user.on('connection', (socket) => {
let id = socket.id;
socket.emit('leaves data', leavesdata);
socket.on('leaf text', (leafdata) => {
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;
} else {
leavesdata[id]["leaftext"] = leafdata["leaftext"]; //if yes update the message
leafdata = leavesdata[id];
};
io.emit('leaf data', leafdata);
io.of('mod').emit('leaves data', leavesdata);
fs.writeFileSync(filepath, JSON.stringify(leavesdata), function(err) {
if(err) return console.error(err);
})
});
});
mod.on('connection', (socket) => {
io.of('mod').emit('leaves data', leavesdata);
socket.on('message delete', id => {
io.emit('delete text', leavesdata[id]["leafid"]);
delete leavesdata[id];
const index = socketids.indexOf(id);
if (index > -1) {
socketids.splice(index, 1);
}
io.of('mod').emit('leaves data', leavesdata);
});
});
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Document</title>
</head>
<body>
<ul id="listid">
</ul>
<script src="socket.io.js"></script>
<script>
const mod = io.connect('/mod');
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>';
};
});
function contentRemove(socketid){
mod.emit('message delete', socketid);
};
</script>
</body>
</html>
{
"name": "knowledge-tree",
"version": "0.0.8",
"description": "A tree to show use inputs",
"license": "GPLv3",
"dependencies": {
"express.js": "^1.0.0",
"socket.io": "^2.3.0"
}
}
This diff is collapsed.
This diff is collapsed.
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