summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2018-07-10 21:22:48 +0200
committerTom Smeding <tom.smeding@gmail.com>2018-07-10 21:22:48 +0200
commitba048a22078bfc7a88c79020447e8cda47cd7f40 (patch)
treeeb08ccb89b7ace0342859a6b226ab34f48d03e81
parent3845bf7c3218a644b0804e9080b5a5c9ef1489f1 (diff)
Add module 'lijst'
-rw-r--r--modules/lijst/lijst.html46
-rw-r--r--modules/lijst/lijst.js46
2 files changed, 92 insertions, 0 deletions
diff --git a/modules/lijst/lijst.html b/modules/lijst/lijst.html
new file mode 100644
index 0000000..0c1a3dd
--- /dev/null
+++ b/modules/lijst/lijst.html
@@ -0,0 +1,46 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="utf-8">
+<title>Lijst</title>
+<script>
+function addItem() {
+ var text = document.getElementById("text").value;
+
+ var xhr = new XMLHttpRequest();
+ xhr.open("POST", location.href + "/add");
+ xhr.responseType = "text";
+ xhr.onreadystatechange = function() {
+ if (xhr.readyState == 4 && xhr.status == 200) {
+ location.href = location.href;
+ }
+ };
+ xhr.send(text);
+}
+
+function removeItem() {
+ var xhr = new XMLHttpRequest();
+ xhr.open("POST", location.href + "/remove");
+ xhr.responseType = "text";
+ xhr.onreadystatechange = function() {
+ if (xhr.readyState == 4 && xhr.status == 200) {
+ location.href = location.href;
+ }
+ };
+ xhr.send();
+}
+</script>
+<style>
+
+</style>
+</head>
+<body>
+<ol id="lijst">
+<!--[[LIJST_ITEMS]]-->
+</ol>
+<input type="text" id="text" placeholder="New item text">
+<input type="button" onclick="addItem()" value="Add item"><br>
+<br>
+<input type="button" onclick="removeItem()" value="Remove top item">
+</body>
+</html>
diff --git a/modules/lijst/lijst.js b/modules/lijst/lijst.js
new file mode 100644
index 0000000..fb1c70c
--- /dev/null
+++ b/modules/lijst/lijst.js
@@ -0,0 +1,46 @@
+var cmn=require("../$common.js"),
+ fs=require("fs"),
+ persist=require("node-persist"),
+ Naampje=require("naampje").name;
+
+persist=persist.create({
+ dir:"persist/lijst",
+ continuous:false,
+ interval:false
+});
+persist.initSync();
+
+var lijst=persist.getItemSync("lijst");
+if(!lijst){
+ lijst=[];
+ persist.setItemSync("lijst",lijst);
+}
+
+var moddir;
+
+function render(res){
+ var html=fs.readFileSync(moddir+"/lijst.html")+"";
+ var s="";
+ for(var i=0;i<lijst.length;i++){
+ s+="<li>"+escape(lijst[i])+"</li>\n";
+ }
+ html=html.replace("<!--[[LIJST_ITEMS]]-->",s);
+ res.send(html);
+}
+
+module.exports=function(app,io,_moddir){
+ moddir=_moddir;
+ app.get("/lijst",function(req,res){
+ render(res);
+ });
+ app.post("/lijst/add",function(req,res){
+ lijst.push(req.body.trim());
+ persist.setItemSync("lijst",lijst);
+ res.status(200).end();
+ });
+ app.post("/lijst/remove",function(req,res){
+ if(lijst.length>0)lijst.shift();
+ persist.setItemSync("lijst",lijst);
+ res.status(200).end();
+ });
+};