-
Notifications
You must be signed in to change notification settings - Fork 0
/
Todo1.js
123 lines (114 loc) · 3.24 KB
/
Todo1.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
function init() {
var max = getMax(),
selectedId = 0,
divInput = $('div.divInput'),
divTodo = $('div.divTodo'),
btninputData = $(document.getElementById('btninputData')),
input = $('#input');
//document.body.style.width = $(window).width();
divTodo.append( Handlebars.compile( $('#template').html() )( createList(max)) );
divInput.hide();
$('div.divBtns').hide();
$('div.divTaskContainer').on('mouseenter', function() {
$(document.getElementById('buttons'+$(this).data('id'))).show().slideDown('fast');
})
$('div.divTaskContainer').on('mouseleave', function() {
$('div.divBtns').slideUp('fast',function() {
$('div.divBtns').hide();
});
})
$('button.btnAddTasks').on('click', function() {
divInput.fadeIn('fast');
selectedId = -1;
setInput();
})
$('button.btnDeleteTasks').on('click', function() {
for (var i = 1; i <= max; i++) {
//$.cookie(""+i, '');
localStorage.setItem(""+i, '');
}
location.reload();
})
$('button.btnEdit').on('click', function() {
selectedId = $(this).data('id');
setInput();
})
$('button.btnDelete').on('click', function() {
//$.cookie(""+$(this).data('id'), '');
localStorage.setItem(""+$(this).data('id'), '');
location.reload();
})
$(document.getElementById('btnX')).on('click', function() {
divInput.fadeOut('fast', function() {
divInput.hide();
divTodo.show().fadeIn('fast');
});
})
btninputData.on('click', function() {
var data = input.val().toString();
if(data.length == 0)
{
alert('Please write something');
}
else
{
if(selectedId == -1)
{
max++;
//$.cookie("max", max);
//$.cookie(""+max,input.val());
localStorage.setItem("max", max);
localStorage.setItem(""+max,input.val());
}
else {
//$.cookie(""+selectedId,input.val());
localStorage.setItem(""+selectedId,input.val());
}
location.reload();
}
})
function setInput()
{
divInput.show().fadeIn();
divTodo.hide();
if(selectedId == -1)
{
input.val('').focus();
btninputData.html( 'Save Task');
}
else {
//input.val($.cookie(""+selectedId)).focus();
input.val(localStorage.getItem(""+selectedId)).focus();
btninputData.html( 'Edit Task');
}
}
}
init();
function getMax() {
//var num = $.cookie('max');
var num = localStorage.getItem('max');
if(num !== undefined)
{
return num;
}
else {
//$.cookie('max','0');
localStorage.setItem('max','0');
return 0;
}
}
function createList(max) {
var array = [];
for (var i = 1; i <= max; i++) {
//var Task = $.cookie(i+'');
var Task = localStorage.getItem(i+'');
if(Task.length != 0)
{
array.push({
id: i,
task: Task,
});
}
}
return array;
}