-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.js
327 lines (298 loc) · 8.57 KB
/
todo.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
var todo = {};
todo.txtNew = $('#txtNew');
todo.list = $("#lists");
todo.oldEntry = ''; // storage of old entry
todo.options = "<div class='ud'><a class='update'>Update</a><a class='delete'>Delete</a></div>";
todo.form = "<form id='updateTextForm' action='' onsubmit='return false;'><input id='updateTextInput' type='text'/></form>";
// View
todo.deleteView = function (item){
// remove the entry
$(item).remove();
// if the list has nothing, update info
if($(todo.list).children().length == 0){
todo.updateInfo("Weeee! Nothing on the list!");
};
// hide the massive weapon when there is only one entry
if($(todo.list).children().length == 1){
$('#massiveWeapon').css('display', 'none');
};
}
todo.addView = function (res){
// set text area as null;
$('input').attr('value','');
// de-focus the text area
$('input').blur();
// add new entry to the top of the list
$("<li/>",{"id": res.id, "text": res.content}).prependTo(todo.list);
// Extend the width if msg is too long
if(res.content.length >= 35){
$('#'+res.id).css("height","50px");
}
// if the list has more than two entries, grants the permission to use massive weapon/delete all
if($(todo.list).children().length >= 2){
$('#massiveWeapon').css('display', 'block');
};
}
// Render to-do list view
todo.getView = function (res){
// if the list has more than two entries, grants the permission to use massive weapon/delete all
if(res.length >= 2){
$('#massiveWeapon').css('display', 'block');
}
// update the list view
for (var i=0; i< res.length; i++){
$("<li/>", {"id": res[i].id, "text": res[i].content}).appendTo(todo.list);
// Extend the width if msg is too long
if(res[i].content.length >= 35){
$('#'+res[i].id).css("height","50px");
}
}
}
todo.updateView = function (id, content){
var newLiElement = $('#'+id);
// update the entry
var newElement = $("<li/>", {"id": id, "text": content});
// remove the text area
$("#updateTextForm").parent().replaceWith(newElement);
// enable option view
$(newLiElement).removeClass("activeIsDisabled");
// Extend the width if msg is too long
if(content.length >= 35){
$(newLiElement).css("height","50px");
}
}
todo.deleteAllView = function (){
// remove every entry in the window
$(todo.list).children().remove();
// hide the massive weapon
$('#massiveWeapon').css('display', 'none');
// update happy info
todo.updateInfo("Weeee! Nothing on the list!");
}
todo.formView = function (id){
// replace the old entry with form
$(todo.form).hide().appendTo('#'+id).slideDown().css("text-decoration","none");
// Remove the options view and disable adding options view
$("#updateTextForm").parent().removeClass("active").addClass("activeIsDisabled");
// set focus on text area
$('#updateTextForm input').focus();
}
todo.formCancellView = function (e){
// enable option view
$(e).parent().removeClass("activeIsDisabled");
// remove the form
$(e).remove();
}
// Update Information Area on the top right of the window
todo.updateInfo = function (text){
$("#info").html(text).fadeIn("slow");
// Wait 1.5s to fade out the information
setTimeout(function(){
$('#info').fadeOut('slow');
}, 2000);
}
// Handlers / Model
// Handler for mouse enter and leave an entry
$('li:not(.activeIsDisabled)').live('mouseover mouseleave', function(event){
if (event.type == 'mouseover'){
todo.addOptions($(this));
}
else{
todo.removeOptions($(this));
}
});
// Handler for click event of the delete button
$(".delete").live("click", function(){
var item = $(this).parent().parent();
todo.removeList(item);
});
// Handler for click event of the update button
$(".update").live("click", function(){
var item = $(this).parent().parent();
var id = $(item).attr('id');
// keep the old entry in case user cancells the update
todo.oldEntry = item;
// remove the delete and update div
$(item).children().filter("div").remove();
// Update the view--- add form
todo.formView(id);
// bind the submit event to form
todo.bindSubmitEvent(id);
});
// Hander for blur event and keydown event of the textbox.
// Old entry will be restored when textbox loses focus or ESC is pressed
/*
$("#updateTextForm").live('keydown', function(e){
if (e.keyCode == 27){
console.log("The error message below might be a bug of jQuery");
$(this).replaceWith(todo.oldEntry);
}
});
*/
$("#updateTextForm").live('blur', function(){
todo.formCancellView(this);
});
// Hander for focus event for both textboxs
$('#mainTextInput').focus(function(){
$(this).css("color", "#000000");
$(this).css("text-align", "left");
$(this).css("font-size", "22px");
if($(this).attr("value") == " I have to ...."){
$(this).attr("value", "");
}
});
// Handler for keypress event of the main textbox
// when key is pressed, clear button shows up
$('#mainTextInput').keypress(function(){
$('#clearButton').css('display', 'block');
});
// Handler for click event of the clear button
$("#clearButton").live("click",function(){
$('#mainTextInput').attr("value","");
$('#clearButton').css('display', 'none');
});
// Handler for submit event of the main textbox
$("#mainTextForm").live("submit", function(){
// trim off the white space and empty line
var content = $(this).children().filter("#mainTextInput").attr("value");
var text = $.trim(content);
// do not add if text is null
if (text == ''){
todo.updateInfo("It's an empty entry");
}
else if ( text.length > 58){
todo.updateInfo("message's too long");
}
else{
todo.updateInfo("Adding....");
todo.addList(text);
content = null;
}
});
// Handler for submit event of the new update textbox
todo.bindSubmitEvent = function(itemId){
$("#updateTextForm").live("submit", function(){
// trim off the white space and empty line
var content = $(this).children().filter("#updateTextInput").attr("value");
var newContent = $.trim(content);
// do not add if text is null
if (newContent == ''){
todo.updateInfo("It's an empty entry");
}
else if (newContent.length > 58){
todo.updateInfo("message's too long");
}
else{
todo.updateInfo("Updating....");
todo.updateList(itemId,newContent);
}
});
}
// Handler for massiveWeapon/ DeleteAll
$('#massiveWeapon').bind('click', function(){
if (confirm("Are you sure to delete everything?")){
todo.removeAllList();
}
});
// add the "Delete" and "Update" options to an entry
todo.addOptions = function(item){
$(item).addClass("active");
if($(item).children().hasClass("ud") == false){
$(item).append(todo.options);
}
};
//remove the "Delete" and "Update" options to an entry
todo.removeOptions = function(item){
$(item).removeClass("active");
if($(item).children().hasClass("ud")){
$(item).children(".ud").remove();
}
};
// Four methods for to-do lists
// "op" stand for the five operations of the database:
// Get the list, update the list, add an entry to the list, delete an entry from the list and delete all
todo.getList = function (){
$.ajax({
url: 'server.php',
method: 'GET',
data: 'op=getList',
dataType: "json", // response as json
error: function(res){
todo.updateInfo("Failed to get the list");
},
success: function(res){
if (res.length == 0){
todo.updateInfo("Weeee! Nothing on the list!");
}
else if (res.length >= 10){
todo.updateInfo("Crap! Swamped!");
}
else{
// nothing for now
};
todo.getView(res);
}
});
};
todo.addList = function (text){
$.ajax({
url: 'server.php',
method: 'GET',
data: 'op=addList&content='+text,
dataType: "json", // response as json
error: function(res){
todo.updateInfo("Failed to add new entry");
},
success: function(res){
todo.updateInfo("New Entry added");
todo.addView(res);
}
});
}
todo.updateList = function (id, newContent){
$.ajax({
url: 'server.php',
method: 'GET',
data: 'op=updateList&id='+id+"&content="+newContent,
error: function(){
todo.updateInfo("Failed to update new entry");
},
success: function() {
todo.updateInfo("Entry updated");
todo.updateView(id, newContent);
}
});
}
todo.removeList = function (item){
var itemId = $(item).attr('id');
$.ajax({
url: 'server.php',
method: 'GET',
data: 'op=removeList&id='+itemId ,
error: function(){
todo.updateInfo("Failed to delete new entry");
},
success: function() {
todo.updateInfo("Entry deleted");
todo.deleteView(item);
}
});
}
todo.removeAllList = function (){
$.ajax({
url: 'server.php',
method: 'GET',
data: 'op=removeAllList' ,
error: function(){
todo.updateInfo("Failed to delete all the entries");
},
success: function() {
todo.updateInfo("Weeee! Nothing on the list!");
todo.deleteAllView();
}
});
}
$(document).ready(function(){
// fetch list from database
todo.getList();
});