-
Notifications
You must be signed in to change notification settings - Fork 0
/
todos.js
100 lines (90 loc) · 4.13 KB
/
todos.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*Version 2.0*/
//get or create todos array
if (localStorage.getItem("todos") != null) {
var todos = JSON.parse(localStorage.getItem("todos"));
} else {
var todos = [];
}
//check off specific todos by clicking
$("ul").on("click", "li.canComplete", function(){
$(this).toggleClass("completed");
});
//click on trash can to delete todo
$("ul").on("click", "span.spanDelete", function(event){
$(this).parent().parent().fadeOut(function(){
$(this).remove();
});
event.stopPropagation();
});
//click on arrows to add heaven and hell lines
$("ul").on("click", "span.spanExpand", function(event){
$(this).children().toggleClass("fa-angle-double-down fa-angle-double-up");
$(this).toggleClass("spanShow");
$(this).parent().siblings().fadeToggle();
event.stopPropagation();
});
//click plus/minus sign to toggle input box
$("#container").on("click", "#collapse", function(event){
$(this).parent().siblings("input[type = 'text']").fadeToggle();
$(this).toggleClass("fa-minus fa-plus");
event.stopPropagation();
});
//on enter add text input as new todo
$("#inputTodo").keypress(function(event){
if(event.which === 13) {
//grabbing new todo text from input
var todoText = escapeHtml($(this).val());
$(this).val("");
//create a new li and add to ul
//very ugly but it works?
$("#mainList").append("<ul><li class='canComplete'><span class='spanExpand'><i class='fa fa-angle-double-down'></i></span>" + todoText + "<span class='spanDelete'><i class='fas fa-trash'></i></span></li><li class='hidden'><i class='fas fa-times hellIcon'></i><input class='hellInput' type='text' placeholder='If you don't complete it...'></li><li class='hidden'><i class='fas fa-check heavenIcon'></i><input class='heavenInput' type='text' placeholder='If you do complete it...'></li></ul>");
}
});
$(".fa-plus").on("click", function(){
$("input[type = 'text']").fadeToggle();
});
$(document).ready(function() {
for (var i = 0; i < todos.length; i++) {
var todoText = escapeHtml(todos[i].todo);
var hellText = escapeHtml(todos[i].hell);
var heavenText = escapeHtml(todos[i].heaven);
var compClass = (todos[i].completed) ? " completed" : "";
console.log(compClass);
console.log(todos[i].completed);
$("#mainList").append("<ul><li class='canComplete" + compClass + "'><span class='spanExpand'><i class='fa fa-angle-double-down'></i></span>" + todoText + "<span class='spanDelete'><i class='fas fa-trash'></i></span></li><li class='hidden'><i class='fas fa-times hellIcon'></i><input class='hellInput' type='text' placeholder='If you don't complete it...' value='" + hellText + "'></li><li class='hidden'><i class='fas fa-check heavenIcon'></i><input class='heavenInput' type='text' placeholder='If you do complete it...' value='" + heavenText + "'></li></ul>");
console.log("<ul><li class='canComplete" + compClass + "'><span class='spanExpand'><i class='fa fa-angle-double-down'></i></span>" + todoText + "<span class='spanDelete'><i class='fas fa-trash'></i></span></li><li class='hidden'><i class='fas fa-times hellIcon'></i><input class='hellInput' type='text' placeholder='If you don't complete it...' value='" + hellText + "'></li><li class='hidden'><i class='fas fa-check heavenIcon'></i><input class='heavenInput' type='text' placeholder='If you do complete it...' value='" + heavenText + "'></li></ul>");
}
setInterval(function(){
if (localStorage.getItem("todos") || todos) {
todos = [];
$("#mainList > ul").each(function() {
todos.push({
todo : $(this).children("li:first").text(),
hell : $(this).children().children("input:first").val(),
heaven : $(this).children().children("input:last").val(),
completed : $(this).children("li:first").hasClass("completed")
});
localStorage.setItem("todos", JSON.stringify(todos));
});
}
if (todos.length === 0) {
localStorage.clear();
}
/* console.log(todos[0].hell, todos[0].heaven, todos[0].todo);*/
}, 500);
});
var entityMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/',
'`': '`',
'=': '='
};
function escapeHtml (string) {
return String(string).replace(/[&<>"'`=\/]/g, function (s) {
return entityMap[s];
});
}