-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
76 lines (67 loc) · 1.82 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
$(function() {
var get_tasks = function() {
var data = localStorage['tasks'];
if (!data) {
return {};
} else {
return JSON.parse(data);
}
};
var save_tasks = function(data) {
localStorage['tasks'] = JSON.stringify(data);
};
var append_task = function(task) {
var $task = $('<li/>').attr({id: task.id});
var $input = $('<input/>').attr({type: 'checkbox', class: 'done'});
if (task.done) {
$task.css('text-decoration', 'line-through');
$input.attr({checked: true});
}
$input.change(function() {
var tasks = get_tasks();
if ($(this).attr('checked')) {
$task.css('text-decoration', 'line-through');
tasks[task.id].done = true;
} else {
$task.css('text-decoration', 'none');
tasks[task.id].done = false;
}
save_tasks(tasks);
});
var $span = $('<span/>').text(task.text);
var $delete_button = $('<input/>').attr(
{type: 'button', class: 'delete', value: '削除'}
);
$delete_button.click(function() {
var tasks = get_tasks();
delete tasks[task.id];
save_tasks(tasks);
$task.fadeOut();
});
$task.
append($input).
append($span).
append($delete_button).
appendTo('#tasks');
};
var load_tasks = function() {
var tasks = get_tasks();
$.each(tasks, function(key, task) {
append_task(task);
});
};
$('#new_task_button').click(function() {
var $task_textbox = $('#new_task');
var task_id = (new Date()).toJSON();
var task_done = false;
var task_text = $task_textbox.val();
// DBへ保存
tasks = get_tasks();
tasks[task_id] = {id: task_id, text: task_text, done: false}
save_tasks(tasks);
// 表示
append_task(tasks[task_id]);
$task_textbox.val('');
});
load_tasks();
});