-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
135 lines (125 loc) · 5.49 KB
/
index.html
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
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="/css/base.css">
<style>
input.toggle:checked ~ div.view label {
color: #d9d9d9;
text-decoration: line-through;
}
li.slip-reordering {
box-shadow: 0 2px 10px rgba(0,0,0,0.45);
background-color: white;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/slipjs/2.0.0/slip.min.js"></script>
</head>
<body>
<section class="todoapp">
<div>
<header class="header">
<h1>todos</h1>
<form id="form" data-simply-command="addTodo">
<input class="new-todo" type="text" name="newItem" placeholder="What needs to be done?">
</form>
</header>
<section class="main">
<ul class="todo-list" data-simply-list="todo">
<template>
<li>
<input type="checkbox" class="toggle" data-simply-field="completed" value="1">
<div class="view">
<label data-simply-field="item">Item</label>
<button class="edit" data-simply-command="editTodo"></button>
<button class="destroy" data-simply-command="delTodo"></button>
</div>
<input class="edit" data-simply-field="item" type="text" data-simply-command="showTodo">
</li>
</template>
</ul>
</section>
<footer class="footer">
<span class="todo-count" data-simply-field="count" data-simply-content="template" data-simply-default-template="other">
<template data-simply-template="0">Nothing left todo!</template>
<template data-simply-template="1"><strong>1</strong> item left</template>
<template data-simply-template="other"><strong data-simply-field="count">n</strong> items left</template>
</span>
</footer>
</div>
</section>
<p class="refer-back">This example is part of <a href="https://simplyedit.io/examples/" target="_blank">simplyedit.io/examples/</a>
<script src="/simplyview/dist/simply.everything.js"></script>
<script src="https://cdn.simplyedit.io/1/simply-edit.js"></script>
<script>
var todoApp = simply.app({
commands: {
addTodo: function(form, values) {
if (values.newItem.trim()) {
todoApp.view.todo.push({
item : values.newItem.trim(),
completed : 0
});
}
form.newItem.value = '';
},
delTodo: function(el, value) {
el.closest('ul').removeChild( el.closest('li') );
},
editTodo: function(el, value) {
var item = el.closest('li');
var input = item.querySelector('input[type="text"]');
item.classList.add('editing');
input.focus();
input.addEventListener('blur', function() {
input.removeEventListener('keyup', keyEscape);
todoApp.commands.call('showTodo', input);
}, { once: true });
var keyEscape = function(evt) {
if (["Escape","Cancel"].indexOf(evt.key)!=-1) {
input.value = item.querySelector('label[data-simply-field="item"]').innerHTML;
}
if (["Escape","Cancel","Enter"].indexOf(evt.key)!=-1) {
todoApp.commands.call('showTodo', input);
}
};
input.addEventListener('keyup', keyEscape);
},
showTodo: function(el, value) {
el.closest('li').classList.remove('editing');
el.blur();
}
},
view: (function() {
var data = localStorage.getItem('simplyTodo');
if (!data) {
data = '[]';
}
var items = JSON.parse(data) || [];
return {
todo: items,
count: items.length
};
})()
});
// 3: update the counter whenever the list changes
document.addEventListener('simply-data-changed', function(evt) {
if (evt.data.arguments[0]=='todo') {
todoApp.view.count = todoApp.view.todo.length;
}
});
document.addEventListener('simply-data-changed', function(evt) {
localStorage.setItem('simplyTodo', JSON.stringify(todoApp.view.todo));
});
// 6: drag and drop items to reorder them
var todoList = document.querySelector('ul.todo-list');
todoList.addEventListener('slip:reorder', function(e){
e.target.parentNode.insertBefore(e.target, e.detail.insertBefore);
});
// 7: swipe to remove an item
todoList.addEventListener('slip:swipe', function(e) {
e.target.parentNode.removeChild(e.target);
});
new Slip(todoList);
</script>
</body>
</html>