;(function () {
'use strict';
var $form_task_add = $('.task-add'),
$complete_state,
$delete_buttons,
$detail_buttons,
$detail_task = $('.task-detail'),
$detail_task_mask = $('.task-detail-mask'),
task_list = {},
$remind_msg = $('.remind-msg'),
$remind_music = $('.remind-music');
init();
$form_task_add.find('button').on('click',listen_task_add);
$form_task_add.keydown(function (e) {
if (e.keyCode === 13) {
listen_task_add(e);
}
});
$detail_task_mask.click(listen_task_detail_off);
function init() {
task_list = store.get('task_list') || [];
if (task_list.length) {
render_task_list()
}
task_remind_time()
$remind_msg.find('button').click(remind_msg_hide);
}
* 自定义alert
*/
function todo_alert(argument) {
if (!argument) {
console.error('conf title is required')
}
var conf = {},
alert_box,
alert_mask,
alert_title,
alert_content,
alert_confirm,
alert_cancel,
defer,
is_confirm,
timer;
if (typeof argument == 'string') {
conf.title = argument
} else {
conf = $.extend(conf,argument)
}
alert_box = $('<div>' +
'<div><div class="alert-title">' + conf.title + '</div></div>' +
'<div><button class="alert-confirm">Confirm</button><button class="alert-cancel">Cancel</button></div>' +
'</div>').attr('class','alert-box')
alert_mask = $('<div>').attr('class','alert-mask')
alert_title = alert_box.find('.alert-title')
alert_content = alert_box.find('.alert-content')
alert_confirm = alert_box.find('.alert-confirm')
alert_cancel = alert_box.find('.alert-cancel')
defer = $.Deferred();
timer = setInterval(function () {
if (is_confirm !== undefined) {
defer.resolve(is_confirm)
clearInterval(timer)
dismiss_alert()
};
},50)
function dismiss_alert () {
alert_box.remove();
alert_mask.remove();
}
alert_confirm.click(function (e) {
is_confirm = true
})
alert_cancel.click(function (e) {
is_confirm = false
})
$(window).resize(function (e) {
adjust_position(alert_box)
});
$('body').append(alert_mask).append(alert_box);
$(window).resize();
* 防止执行状态被外部改变
* 返回promise对象,要想改变执行状态,只能操作原来的deferred对象
*/
return defer.promise();
}
function adjust_position (element) {
var $window = $(window),
window_w = $window.width(),
window_h = $window.height(),
element_w = element.width(),
element_h = element.height(),
left,
top;
left = (window_w - element_w) / 2;
top = ((window_h - element_h) / 2) - 20;
element.css({
left: left,
top: top
})
}
function task_remind_time() {
var current_time,
item = store.get('task_list'),
remind_time,
sub_time;
var check_timer = setInterval(function () {
for (var i = 0; i < task_list.length; i++) {
var _item = item[i];
if (!_item || !_item.date || _item.informed) {
continue
}
current_time = (new Date()).getTime();
remind_time = (new Date(_item.date)).getTime();
sub_time = current_time - remind_time;
if (sub_time >= 1) {
updateTask(i,{informed: true});
remind_msg_show(_item.content);
}
}
},1000)
}
function remind_msg_show(content) {
if (!content) {
return
}
$remind_msg.find('span').html(content)
$remind_msg.fadeIn(200)
$remind_music.get(0).play()
}
function remind_msg_hide() {
$remind_msg.hide()
window.location.reload()
}
function listen_task_add(e) {
e.preventDefault();
var new_task = {};
var $input = $form_task_add.find('input[name=input-content]');
new_task.content = $input.val();
if (!new_task.content) {
return
}
if (addTask(new_task)) {
$input.val(null);
}
}
function addTask(new_task) {
task_list.push(new_task);
refresh_task_list();
return true
}
function listen_task_delete() {
$delete_buttons.on('click',function (e) {
var $this = $(this),
$item = $this.parent().parent(),
index = $item.data('index');
todo_alert("Do you want to delete this item?").then(function (result) {
result ? deleteTask(index) : null;
})
})
}
function deleteTask(index) {
if (index === undefined || !task_list[index]) {
return
}
delete task_list[index];
refresh_task_list();
}
function listen_task_detail() {
var index;
$('.task-item').dblclick(function (e) {
index = $(this).data('index');
detailTask(index);
})
$detail_buttons.on('click',function (e) {
var $this = $(this),
$item = $this.parent().parent();
index = $item.data('index');
detailTask(index);
})
}
function detailTask(index) {
if (index === undefined || !task_list[index]) {
return
}
render_task_detail(index);
$detail_task.fadeIn(500);
$detail_task_mask.show();
}
function updateTask(index,data) {
if (index === undefined || !task_list[index]) {
return
}
task_list[index] = $.extend({},task_list[index],data);
refresh_task_list();
}
function listen_task_detail_off() {
$detail_task.fadeOut(500);
$detail_task_mask.hide();
}
function listen_task_completed() {
$complete_state.click(function (e) {
var $this = $(this),
index = $this.parent().parent().data('index'),
isCompleted = task_list[index].completed;
if (isCompleted) {
updateTask(index,{completed:false})
} else {
updateTask(index,{completed:true})
}
})
}
* 更新数据存储内容
*/
function refresh_task_list() {
store.set('task_list',task_list);
render_task_list();
}
* 根据数据渲染列表模板
*/
function render_task_list() {
var $task_list = $('.task-list');
$task_list.html(null);
for (var i = 0; i < task_list.length; i++) {
var $task = render_task_item(task_list[i],i),
item = task_list[i];
if (item && item.completed) {
$task.addClass('task-item-completed')
$task_list.append($task)
} else {
$task_list.prepend($task)
}
}
$delete_buttons = $('.action.delete');
listen_task_delete();
$detail_buttons = $('.action.detail');
listen_task_detail();
$complete_state = $task_list.find('.complete[type=checkbox]');
listen_task_completed();
}
* 取得单项模板结构
*/
function render_task_item(data,index) {
if (!data || !index) {
return
}
var task_item_tpl = "<div class='task-item' data-index='" + index + "'>" +
"<span>" +
"<input type='checkbox' class='complete' " + (data.completed ? 'checked' : '') + ">" +
"</span>" +
"<span class='task-content'>" + data.content +
"</span>" +
"<span class='flr'>" +
"<span class='action delete'> Delete</span>" +
"<span class='action detail'> Detail</span>" +
"</span>" +
"</div>";
return $(task_item_tpl)
}
* 渲染详情模板结构
*/
function render_task_detail(index) {
if (index === undefined || !task_list[index]) {
return
}
var item = task_list[index],
task_detail_tpl = "<form>" +
"<div class='content'>" + (item.content || "") +
"</div>" +
"<div>" +
"<input type='text' name='detail-content' value='" + (item.content || "") + "' style='display:none'></div>" +
"</div>" +
"<div>" +
"<div class='desc'>" +
"<textarea name='detail-desc'>" + (item.desc || "") + "</textarea>" +
"</div>" +
"</div>" +
"<div class='remind'>" +
"<label for='remind-date'>Remind time</label>" +
"<input type='text' name='remind-date' id='remind-date' value='" + (item.date ? item.date : '') + "'>" +
"</div>" +
"<div>" +
"<button type='submit'>Update</button>" +
"</div>" +
"</form>";
$detail_task.html(null);
$detail_task.html(task_detail_tpl);
var $update_form = $detail_task.find("form"),
$detail_task_content = $update_form.find('.content'),
$detail_task_content_input = $update_form.find('input[name=detail-content]'),
$detail_task_desc = $update_form.find('textarea[name=detail-desc]'),
$detail_task_date = $update_form.find('input[name=remind-date]');
$update_form.submit(function (e) {
e.preventDefault();
var data = {};
data.content = $detail_task_content_input.val();
data.desc = $detail_task_desc.val();
data.date = $detail_task_date.val();
updateTask(index,data);
listen_task_detail_off();
})
$detail_task_content.dblclick(function (e) {
$detail_task_content_input.show();
$(this).hide();
})
$detail_task_date.datetimepicker();
}
})()