-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
177 lines (125 loc) · 4.12 KB
/
script.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
$(document).ready(function(){
/* The following code is executed once the DOM is loaded */
// $(function() {
// var todoList;
// $("#add").click(function(){
// todoList = prompt('New TODO List', 'Task List');
// });
// });
$(".todoList").sortable({
axis : 'y',
containment : 'window',
update : function(){
// The toArray method returns an array with the ids of the todos
var arr = $(".todoList").sortable('toArray');
// Striping the todo- prefix of the ids:
arr = $.map(arr,function(val,key){
return val.replace('todo-','');
});
// Saving with AJAX
$.get('ajax.php',{action:'rearrange',positions:arr});
},
/* Opera fix: */
stop: function(e,ui) {
ui.item.css({'top':'0','left':'0'});
}
});
// A global variable, holding a jQuery object
// containing the current todo item:
var currentTODO;
// Configuring the delete confirmation dialog
$("#dialog-confirm").dialog({
resizable: false,
height:130,
modal: true,
autoOpen:false,
buttons: {
'Delete item': function() {
$.get("ajax.php",{"action":"delete","id":currentTODO.data('id')},function(msg){
currentTODO.fadeOut('fast');
})
$(this).dialog('close');
},
Cancel: function() {
$(this).dialog('close');
}
}
});
// $('.todo a').hide();
// $('.todo').hover(
// function(){
// $('.todo a').index(this).show();
// }, function(){
// $('.todo a').index(this).hide();
// });
// $('.todo').bind('click', function(e) {
// var item=e.target;
// alert(this.className);
// });
// When a double click occurs, just simulate a click on the edit button:
$('.todo').live('dblclick',function(){
currentTODO.find.find('a.edit').click();
});
// If any link in the todo is clicked, assign
// the todo item to the currentTODO variable for later use.
$('.todo a').live('click',function(e){
currentTODO = $(this).closest('.todo');
currentTODO.data('id',currentTODO.attr('id').replace('todo-',''));
e.preventDefault();
});
// Listening for a click on a delete button:
$('.todo a.delete').live('click',function(){
$("#dialog-confirm").dialog('open');
});
// Listening for a click on a edit button
$('.todo a.edit').live('click',function(){
var container = currentTODO.find('.text');
if(!currentTODO.data('origText'))
{
// Saving the current value of the ToDo so we can
// restore it later if the user discards the changes:
currentTODO.data('origText',container.text());
}
else
{
// This will block the edit button if the edit box is already open:
return false;
}
$('<input type="text">').val(container.text()).appendTo(container.empty());
// Appending the save and cancel links:
container.append(
'<div class="editTodo">'+
'<a class="saveChanges" href="#">Save</a> | <a class="discardChanges" href="#">Cancel</a>'+
'</div>'
);
});
// The cancel edit link:
$('.todo a.discardChanges').live('click',function(){
currentTODO.find('.text')
.text(currentTODO.data('origText'))
.end()
.removeData('origText');
});
// The save changes link:
$('.todo a.saveChanges').live('click',function(){
var text = currentTODO.find("input[type=text]").val();
$.get("ajax.php",{'action':'edit','id':currentTODO.data('id'),'text':text});
currentTODO.removeData('origText')
.find(".text")
.text(text);
});
// The Add New ToDo button:
var timestamp=0;
$('#addButton').click(function(e){
// Only one todo per 0.5 seconds is allowed:
//if((new Date()).getTime() - timestamp<500) return false;
$.get("ajax.php",{'action':'new','text':$("#message").val(),'rand':Math.random()},function(msg){
// Appending the new todo and fading it into view:
$(msg).hide().appendTo('.todoList').fadeIn();
$("#message").val("");
});
// Updating the timestamp:
timestamp = (new Date()).getTime();
e.preventDefault();
});
}); // Closing $(document).ready()