-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
237 lines (216 loc) · 7.93 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<!DOCTYPE html>
<title>TempalteView</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="todomvc.css">
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/morphdom-umd.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/nunjucks/3.0.1/nunjucks.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.1.16/backbone.localStorage-min.js"></script>
<script type="text/javascript" src="templateview.js"></script>
<script type="text/template" id="mainView" data-prepend-to="body">
<div class="container">
<!-- ^ Like React you always need a single container ^ -->
<section class="todoapp">
<header class="header">
<h1>todos</h1>
</header>
<section class="main" {% if allItems == 0 %}style="display: none;"{% endif %}>
<input class="toggle-all" id="toggle-all" type="checkbox" {% if activeItems == 0 and allItems != 0 %}checked{% endif %}>
<ul class="todo-list"></ul>
</section>
<footer class="footer" {% if allItems == 0 %}style="display: none;"{% endif %}>
<span class="todo-count"><strong>{{ activeItems }}</strong> item{% if activeItems > 1 %}s{% endif %} left</span>
<ul class="filters">
<li>
<a {% if filter == "all" %}class="selected"{% endif %} href="#/">All</a>
</li>
<li>
<a {% if filter == "active" %}class="selected"{% endif %} href="#/active">Active</a>
</li>
<li>
<a {% if filter == "completed" %}class="selected"{% endif %} href="#/completed">Completed</a>
</li>
</ul>
<button class="clear-completed" {% if activeItems >= allItems %}style="display: none;"{% endif %}>Clear completed</button>
</footer>
</section>
<footer class="info">
<p>Double-click to edit a todo</p>
<p>Built with <a href="https://github.com/howardroark/TemplateView">TemplateView</a> for <a href="http://todomvc.com">TodoMVC</a></p>
</footer>
</div>
</script>
<script type="text/template" id="formView" data-insert-after="& header h1">
<form>
<input class="new-todo" placeholder="What needs to be done?" autofocus="" />
</form>
</script>
<script type="text/template" id="itemView" data-append-to="& .todo-list">
{% if status == filter or filter == 'all' %}
{% set display = 'block' %}
{% else %}
{% set display = 'none' %}
{% endif %}
<li class="{{ status }}" style="display: {{ display }};">
<div class="view">
<input class="toggle" type="checkbox" {% if status == "completed" %}checked{% endif %}>
<label>{{ label }}</label>
<button class="destroy"></button>
</div>
<form class="editForm">
<input class="edit" value="{{ label }}">
</form>
</li>
</script>
<script type="text/javascript">
$(function () {
Backbone.history.start();
state.fetch();
todos.fetch();
main.render();
});
// Domain
var State = Backbone.Model.extend({
localStorage: new Backbone.LocalStorage('state'),
defaults: {
filter: 'all'
}
});
var state = new State({id: 'todos'});
var Todo = Backbone.Model.extend({
defaults: {
status: 'active'
}
});
var Todos = Backbone.Collection.extend({
Model: Todo,
localStorage: new Backbone.LocalStorage('todos')
});
var todos = new Todos();
var Router = Backbone.Router.extend({
routes: {
'*filter':'setFilter'
},
setFilter: function (filter) {
if (filter === null) {
filter = 'all';
}
state.save({
filter: filter
});
}
});
new Router();
// UI
var FormView = TemplateView.extend({
template: '#formView',
events: {
submit:'submit'
},
submit: function (e) {
var todo = new Todo({
status: 'active',
label: e.target[0].value
});
this.collection.add(todo);
todo.save();
return false;
}
});
var ItemView = TemplateView.extend({
template: '#itemView',
events: {
'dblclick label':'edit',
'keydown input': 'escape',
'submit .editForm':'submit',
'blur .edit':'blur',
'click .toggle':'toggle',
'click .destroy':'destroy'
},
edit: function (e) {
var $listItem = $(e.target.parentNode.parentNode);
var $input = $listItem.find('.edit');
$listItem.addClass('editing');
// http://stackoverflow.com/a/1056406/4241697
$input.focus().val($input.val());
},
escape: function (e) {
if (e.which === 27) {
var $listItem = $(e.target.parentNode.parentNode);
var $input = $listItem.find('.edit');
$listItem.removeClass('editing');
$input.val(this.model.get('label'));
}
},
submit: function (e) {
this.update(e.target[0].value);
return false;
},
blur: function (e) {
this.update(e.target.value);
return false;
},
update: function (label) {
if (label === '') {
this.model.destroy();
} else {
this.model.save({
label: label
});
}
},
toggle: function (e) {
var status = 'active';
if (e.target.checked) {
status = 'completed';
}
this.model.save({
status: status
});
},
destroy: function (e) {
this.model.destroy();
}
});
var MainView = TemplateView.extend({
ChildView: ItemView,
SubViews: [FormView],
state: state,
collection: todos,
template: '#mainView',
templateContext: function () {
return {
allItems: this.collection.length,
activeItems: this.collection.where({ status: 'active' }).length
};
},
events: {
'click .toggle-all':'toggleAll',
'click .clear-completed':'clearCompleted'
},
toggleAll: function () {
var activeItems = this.collection.where({ status: 'active' });
var status = 'completed';
if (activeItems.length === 0) {
status = 'active';
}
this.collection.where({ status: status }).forEach(function (model) {
model.save({ status: status }, { silent: true });
});
this.trigger('change');
},
clearCompleted: function () {
this.collection.where({ status: 'completed' }).forEach(function (model) {
model.destroy();
});
return false;
}
});
var main = new MainView();
// Polyfill type stuff not directly related
if ('ontouchstart' in document.documentElement === false) {
$('html').addClass('no-touch');
}
</script>