Created
November 16, 2017 11:51
-
-
Save anonymous/b827d13d0ae2a054a14503578700eb7c to your computer and use it in GitHub Desktop.
Todolist that doesn't work (yet)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--This file is the template--> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Ma todolist</title> | |
<style> | |
a {text-decoration: none; color: black;} | |
#date-dd {position: fixed;} | |
#month-dd{ | |
position:fixed; | |
left: 470px;} | |
.submitButton { | |
position:relative; | |
left : 12.5%; | |
} | |
/* Links inside the dropdown */ | |
.dropdown option { | |
color: black; | |
padding: 7px 10px; | |
text-decoration: none; | |
display: block; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Ma todolist</h1> | |
<script | |
src="http://code.jquery.com/jquery-3.2.1.js" | |
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" | |
crossorigin="anonymous"></script> | |
<script type="text/javascript"> | |
//showhide the modifying text | |
function showHideForm(formid) { | |
var _form = document.getElementById(formid); | |
_form.style.display = (_form.style.display != "block") ? "block" : "none"; | |
return false; | |
} | |
</script> | |
<ul> | |
<% todolist.forEach(function(todo, index) { %> | |
<li> | |
<a href="/todo/supprimer/<%= index %>"> ✘</a> | |
<a onclick="return showHideForm('hiddenform<%= index %>');" href="/todo/modifier/<%= index %>">✎</a> | |
<%= todo.action %> <%=todo.date%> (priorité: <%=todo.priority%>/5) | |
<form action="/todo/modifier/<%= index %>" id="hiddenform<%= index %>" style="display: none;" method="get"> | |
<label for="item"></label> | |
<input type="text" name="item" id="item" autofocus/> | |
<input type="submit" value="OK" /> | |
</form> | |
</li> | |
<% }); %> | |
</ul> | |
<form action="/todo/ajouter/" method="post"> | |
<label for="newtodo">Que dois-je faire ?</label> | |
<input type="text" name="newtodo" id="newtodo" size="10" autofocus /> | |
Priorité | |
<select class="dropdown" name="priorite"> | |
<option value=5>Très haute</option> | |
<option value=4>Haute</option> | |
<option value=3>Moyenne</option> | |
<option value=2>Basse</option> | |
<option value=1>Très basse</option> | |
</select> | |
Date | |
<select class="dropdown" name = "day" id="date-dd" onmousedown="if(this.options.length>5){this.size=5;}" onchange="this.blur()" onblur="this.size=0;"> | |
<script language="javascript" type="text/javascript"> | |
for(var d=1;d<=31;d++) | |
{ | |
document.write("<option>"+d+"</option>"); | |
} | |
</script> | |
</select> | |
<select class="dropdown" name = "month" id= "month-dd" onmousedown="if(this.options.length>5){this.size=5;}" onchange="this.blur()" onblur="this.size=0;"> | |
<script language="javascript" type="text/javascript"> | |
months=["Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre"]; | |
for(var m=0;m<=months.length;m++) | |
{ | |
document.write("<option>"+months[m]+"</option>"); | |
} | |
</script> | |
</select> | |
<input type="submit" name="envoyer" class="submitButton" value="Ajouter"/> | |
</form> | |
<form action="/order/"> | |
<input type="submit" value="Sort the list" /> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*This file is the module*/ | |
/*the print method defined line 9 is generating an error when called on line 26 by Todolist.js*/ | |
function Todo(act, pri, day, month){ | |
this.action=act; | |
this.priority=pri; | |
this.date= " --- Le "+day+" "+month; | |
} | |
Todo.prototype.print = function(){ | |
return(this.action + " " + this.date + " (priorité: " +this.priority+"/5)"); | |
} | |
module.exports = { | |
Todo: Todo | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*This file is the main app*/ | |
var todos = require('Todo'); | |
var express = require('express'); | |
var session = require('cookie-session'); // Charge le middleware de sessions | |
var bodyParser = require('body-parser'); // Charge le middleware de gestion des paramètres | |
var urlencodedParser = bodyParser.urlencoded({ extended: false }); | |
var app = express(); | |
var Todo = todos.Todo; | |
/* On utilise les sessions */ | |
app.use(session({secret: 'sess1'})) | |
/* S'il n'y a pas de todolist dans la session, | |
on en crée une vide sous forme d'array avant la suite */ | |
.use(function(req, res, next){ | |
if (typeof(req.session.todolist) == 'undefined') { | |
req.session.todolist = []; | |
} | |
next(); | |
}) | |
/* On affiche la todolist et le formulaire */ | |
.get('/todo', function(req, res) { | |
req.session.todolist.forEach(function(todo){console.log(todo.print());});/*ERROR: todo.print() is not a function*/ | |
res.render('todo.ejs', {todolist: req.session.todolist}); | |
}) | |
/* On ajoute un élément à la todolist */ | |
.post('/todo/ajouter/', urlencodedParser, function(req, res) { | |
if (req.body.newtodo != '') { | |
var ntodo = new Todo(req.body.newtodo, req.body.priorite, req.body.day, req.body.month); | |
req.session.todolist.push(ntodo); | |
} | |
res.redirect('/todo'); | |
}) | |
/*On modifie un élément de la todolist */ | |
.get('/todo/modifier/:id', urlencodedParser,function(req, res){ | |
let i0 = parseInt(req.params.id); | |
if(req.params.id != '' && Number.isInteger(i0)) | |
{ | |
console.log(i0); | |
console.log("item = "+req.body.item); | |
console.log("url= "+req.query.item); | |
req.session.todolist[i0]=req.query.item; | |
} | |
res.redirect('/todo'); | |
}) | |
/* Supprime un élément de la todolist */ | |
.get('/todo/supprimer/:id', function(req, res) { | |
if (req.params.id != '') { | |
req.session.todolist.splice(req.params.id, 1); | |
} | |
res.redirect('/todo'); | |
}) | |
/*tri des taches*/ | |
.get('/order/',function(req,res){ | |
req.session.todolist.sort(); | |
res.redirect('/todo'); | |
}) | |
/* On redirige vers la todolist si la page demandée n'est pas trouvée */ | |
.use(function(req, res, next){ | |
res.redirect('/todo'); | |
}) | |
.listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment