| 1 |
// $Id: todolist.js,v 1.4 2007/10/28 23:52:51 mikesmullin Exp $
|
| 2 |
|
| 3 |
$(function() {
|
| 4 |
TodoList.add_incomplete_list();
|
| 5 |
|
| 6 |
$.ajaxSetup({
|
| 7 |
error: function(xml, status, e) {
|
| 8 |
if (status == 'error' && e) {
|
| 9 |
alert('An exception occurred in the script. Error name: ' + e.name + '. Error message: ' + e.message);
|
| 10 |
} else if (xml.status == 0) {
|
| 11 |
// retry
|
| 12 |
$.ajax({
|
| 13 |
type: this.type,
|
| 14 |
url: this.url,
|
| 15 |
data: this.data,
|
| 16 |
dataType: this.dataType,
|
| 17 |
});
|
| 18 |
} else
|
| 19 |
alert('Unknown AJAX Error: ' + status + ' Status:' + xml.status + ' ' + xml.statusText);
|
| 20 |
},
|
| 21 |
});
|
| 22 |
|
| 23 |
$('#todolist-add-task-form').submit(function() {
|
| 24 |
$.ajax({
|
| 25 |
type: 'POST',
|
| 26 |
url: getBaseUrl() + 'index.php?q=todolist/create_task',
|
| 27 |
data: $('input, select', this).serialize() + '&sort='+($('.todolist.incomplete .task').length+1),
|
| 28 |
dataType: 'json',
|
| 29 |
});
|
| 30 |
this.reset();
|
| 31 |
return false;
|
| 32 |
});
|
| 33 |
|
| 34 |
// Bind event hooks to complete/uncomplete a task.
|
| 35 |
$('.todolist .task').each(TodoList.bind_task);
|
| 36 |
});
|
| 37 |
|
| 38 |
/**
|
| 39 |
* Todolist class.
|
| 40 |
*/
|
| 41 |
var TodoList = {
|
| 42 |
add_incomplete_list: function() {
|
| 43 |
if (!$('.todolist.incomplete').length) {
|
| 44 |
$('<ul class="todolist incomplete"></ul>').insertBefore('#todolist-add-task-form');
|
| 45 |
}
|
| 46 |
|
| 47 |
// create one nub for each incomplete list item
|
| 48 |
$('.todolist.incomplete .task').each(TodoList.add_nub);
|
| 49 |
|
| 50 |
if (!$('.todolist.incomplete').isSortable) {
|
| 51 |
// make every incomplete todolist sortable
|
| 52 |
$('.todolist.incomplete').Sortable({
|
| 53 |
accept: 'task',
|
| 54 |
axis: 'vertically',
|
| 55 |
revert: true,
|
| 56 |
handle: '.reorder',
|
| 57 |
onchange: TodoList.incomplete_list_reorder,
|
| 58 |
// containment: 'parent', // having trouble
|
| 59 |
});
|
| 60 |
}
|
| 61 |
},
|
| 62 |
|
| 63 |
incomplete_list_reorder: function(obj) {
|
| 64 |
serial = $.SortSerialize(obj[0].id);
|
| 65 |
$.ajax({
|
| 66 |
type: 'POST',
|
| 67 |
url: getBaseUrl() + 'index.php?q=todolist/reorder_task',
|
| 68 |
data: serial.hash,
|
| 69 |
});
|
| 70 |
},
|
| 71 |
|
| 72 |
/**
|
| 73 |
* Bind event hooks to complete/uncomplete a task.
|
| 74 |
*/
|
| 75 |
bind_task: function() {
|
| 76 |
$('input[@type=checkbox]', this).click(function() {
|
| 77 |
var element = $(this).parent();
|
| 78 |
$.ajax({
|
| 79 |
type: 'POST',
|
| 80 |
url: getBaseUrl() + 'index.php?q=todolist/toggle_task',
|
| 81 |
data: 'id='+this.name+'&checked='+(this.checked? '1' : '0')+'&sort='+(this.checked? -1 : $('.todolist.incomplete .task').length+1),
|
| 82 |
dataType: 'json',
|
| 83 |
beforeSend: function() { $(element).addClass('loading'); },
|
| 84 |
complete: function() { $(element).removeClass('loading'); },
|
| 85 |
});
|
| 86 |
});
|
| 87 |
},
|
| 88 |
|
| 89 |
delete_task: function(id) {
|
| 90 |
var element = $('#'+id);
|
| 91 |
$.ajax({
|
| 92 |
type: 'POST',
|
| 93 |
url: getBaseUrl() + 'index.php?q=todolist/delete_task',
|
| 94 |
data: 'id='+id,
|
| 95 |
dataType: 'json',
|
| 96 |
beforeSend: function() { $(element).addClass('loading'); },
|
| 97 |
complete: function() { $(element).removeClass('loading'); },
|
| 98 |
});
|
| 99 |
return false;
|
| 100 |
},
|
| 101 |
|
| 102 |
edit_task: function(id) {
|
| 103 |
if (id instanceof Object) {
|
| 104 |
var element = $(this).parent();
|
| 105 |
$.ajax({
|
| 106 |
type: 'POST',
|
| 107 |
url: getBaseUrl() + 'index.php?q=todolist/edit_task',
|
| 108 |
data: $('input, textarea, select', this).serialize(),
|
| 109 |
dataType: 'json',
|
| 110 |
beforeSend: function() { $(element).addClass('loading'); },
|
| 111 |
complete: function() { $(element).removeClass('loading'); },
|
| 112 |
});
|
| 113 |
} else {
|
| 114 |
var element = $('#'+id);
|
| 115 |
$.ajax({
|
| 116 |
type: 'GET',
|
| 117 |
url: getBaseUrl() + 'index.php?q=todolist/edit_task&id='+id,
|
| 118 |
dataType: 'json',
|
| 119 |
beforeSend: function() { $(element).addClass('loading'); },
|
| 120 |
complete: function() { $(element).removeClass('loading'); },
|
| 121 |
});
|
| 122 |
}
|
| 123 |
return false;
|
| 124 |
},
|
| 125 |
|
| 126 |
/**
|
| 127 |
* Create one nub for each incomplete list item.
|
| 128 |
*/
|
| 129 |
add_nub: function() {
|
| 130 |
if (!$('.nub', this).length) {
|
| 131 |
$(this).prepend(
|
| 132 |
'<div class="nub" style="display:none">' +
|
| 133 |
' <a href="javascript:void(0)" title="Delete this task" class="delete" onclick="return TodoList.delete_task(\''+ $('input[@type=checkbox]', this).attr('name') +'\')">Delete</a>' +
|
| 134 |
' <a href="javascript:void(0)" title="Edit this task" class="edit" onclick="return TodoList.edit_task(\''+ $('input[@type=checkbox]', this).attr('name') +'\')">Edit</a>' +
|
| 135 |
' <a href="javascript:void(0)" title="Reorder this task" class="reorder">Reorder</a>' +
|
| 136 |
'</div>'
|
| 137 |
).each(TodoList.bind_nub);
|
| 138 |
}
|
| 139 |
},
|
| 140 |
|
| 141 |
/**
|
| 142 |
* Bind event hooks to hide/show the nub.
|
| 143 |
*/
|
| 144 |
bind_nub: function() {
|
| 145 |
if (!$('.nub', this).length) {
|
| 146 |
return $(this).each(TodoList.add_nub);
|
| 147 |
}
|
| 148 |
|
| 149 |
$(this).mouseover(function() {
|
| 150 |
with ($('.nub', this)) {
|
| 151 |
TodoList.NubTimer.exec(); // hide all other nubs
|
| 152 |
css('display', 'block'); // must render before offsetWidth can be calculated
|
| 153 |
css('left', (get(0).offsetWidth*-1)+'px');
|
| 154 |
css('top', '-4px');
|
| 155 |
}
|
| 156 |
|
| 157 |
TodoList.NubTimer.stop();
|
| 158 |
});
|
| 159 |
$(this).mouseout(function() { TodoList.NubTimer.start(); });
|
| 160 |
$('.nub', this).mouseover(function() { TodoList.NubTimer.stop(); });
|
| 161 |
$('.nub', this).mouseout(function() { TodoList.NubTimer.start(); });
|
| 162 |
},
|
| 163 |
|
| 164 |
/**
|
| 165 |
* Unbind event hooks to hide/show the nub.
|
| 166 |
*/
|
| 167 |
unbind_nub: function() {
|
| 168 |
$(this).unbind('mouseover');
|
| 169 |
$(this).unbind('mouseout');
|
| 170 |
$('.nub', this).unbind('mouseover');
|
| 171 |
$('.nub', this).unbind('mouseout');
|
| 172 |
TodoList.NubTimer.exec(); // hide all nubs
|
| 173 |
},
|
| 174 |
|
| 175 |
/**
|
| 176 |
* Nub Timer class.
|
| 177 |
*/
|
| 178 |
NubTimer: {
|
| 179 |
tid: 0, // Timer ID
|
| 180 |
|
| 181 |
/**
|
| 182 |
* Start the Nub Timer.
|
| 183 |
*/
|
| 184 |
start: function() {
|
| 185 |
this.stop();
|
| 186 |
this.tid = setTimeout(this.exec, 300); // start timer
|
| 187 |
},
|
| 188 |
|
| 189 |
/**
|
| 190 |
* Stop the Nub Timer.
|
| 191 |
*/
|
| 192 |
stop: function() {
|
| 193 |
if (this.tid) {
|
| 194 |
clearTimeout(this.tid); // stop timer
|
| 195 |
this.tid = 0;
|
| 196 |
}
|
| 197 |
},
|
| 198 |
|
| 199 |
/**
|
| 200 |
* Execute the Nub Timer.
|
| 201 |
*/
|
| 202 |
exec: function() {
|
| 203 |
$('.todolist .nub').css('display', 'none');
|
| 204 |
this.stop();
|
| 205 |
}
|
| 206 |
}
|
| 207 |
};
|
| 208 |
|
| 209 |
|
| 210 |
function getBaseUrl() {
|
| 211 |
var url = '';
|
| 212 |
var script = $.grep($('head > script'), function(i) {
|
| 213 |
return $(i).attr('src').indexOf('todolist.js') > -1
|
| 214 |
});
|
| 215 |
url = $(script).attr('src');
|
| 216 |
url = url.substring(0, url.indexOf('modules/'));
|
| 217 |
if (url.indexOf('sites/') > -1)
|
| 218 |
url = url.substring(0, url.indexOf('sites/'));
|
| 219 |
return url;
|
| 220 |
}
|