forked from bahmutov/test-todomvc-using-app-actions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.jsx
178 lines (150 loc) · 4.19 KB
/
app.jsx
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
/* jshint quotmark:false */
/* jshint white:false */
/* jshint trailing:false */
/* jshint newcap:false */
/* global React, Router */
var app = app || {}
;(function () {
'use strict'
app.ALL_TODOS = 'all'
app.ACTIVE_TODOS = 'active'
app.COMPLETED_TODOS = 'completed'
var TodoFooter = app.TodoFooter
var TodoItem = app.TodoItem
var ENTER_KEY = 13
var TodoApp = React.createClass({
getInitialState: function () {
return {
nowShowing: app.ALL_TODOS,
editing: null
}
},
componentDidMount: function () {
var setState = this.setState
var router = Router({
'/': setState.bind(this, { nowShowing: app.ALL_TODOS }),
'/active': setState.bind(this, { nowShowing: app.ACTIVE_TODOS }),
'/completed': setState.bind(this, { nowShowing: app.COMPLETED_TODOS })
})
router.init('/')
},
handleNewTodoKeyDown: function (event) {
if (event.keyCode !== ENTER_KEY) {
return
}
event.preventDefault()
var val = React.findDOMNode(this.refs.newField).value.trim()
if (val) {
this.props.model.addTodo(val)
React.findDOMNode(this.refs.newField).value = ''
}
},
toggleAll: function (event) {
var checked = event.target.checked
this.props.model.toggleAll(checked)
},
toggle: function (todoToToggle) {
this.props.model.toggle(todoToToggle)
},
destroy: function (todo) {
this.props.model.destroy(todo)
},
edit: function (todo) {
this.setState({ editing: todo.id })
},
save: function (todoToSave, text) {
this.props.model.save(todoToSave, text)
this.setState({ editing: null })
},
cancel: function () {
this.setState({ editing: null })
},
clearCompleted: function () {
this.props.model.clearCompleted()
},
render: function () {
var footer
var main
var todos = this.props.model.todos
var shownTodos = todos.filter(function (todo) {
switch (this.state.nowShowing) {
case app.ACTIVE_TODOS:
return !todo.completed
case app.COMPLETED_TODOS:
return todo.completed
default:
return true
}
}, this)
var todoItems = shownTodos.map(function (todo) {
return (
<TodoItem
key={todo.id}
todo={todo}
onToggle={this.toggle.bind(this, todo)}
onDestroy={this.destroy.bind(this, todo)}
onEdit={this.edit.bind(this, todo)}
editing={this.state.editing === todo.id}
onSave={this.save.bind(this, todo)}
onCancel={this.cancel}
/>
)
}, this)
var activeTodoCount = todos.reduce(function (accum, todo) {
return todo.completed ? accum : accum + 1
}, 0)
var completedCount = todos.length - activeTodoCount
if (activeTodoCount || completedCount) {
footer = (
<TodoFooter
count={activeTodoCount}
completedCount={completedCount}
nowShowing={this.state.nowShowing}
onClearCompleted={this.clearCompleted}
/>
)
}
if (todos.length) {
main = (
<section className='main'>
<input
className='toggle-all'
type='checkbox'
onChange={this.toggleAll}
checked={activeTodoCount === 0}
/>
<ul className='todo-list'>{todoItems}</ul>
</section>
)
}
return (
<div>
<header className='header'>
<h1>todos</h1>
<input
ref='newField'
className='new-todo'
placeholder='What needs to be done?'
onKeyDown={this.handleNewTodoKeyDown}
autoFocus
/>
</header>
{main}
{footer}
</div>
)
}
})
var model = new app.TodoModel('react-todos')
if (window.Cypress) {
window.model = model
}
function render () {
React.render(
<TodoApp model={model} />,
document.getElementsByClassName('todoapp')[0]
)
}
model.subscribe(render)
render()
})()