-
Notifications
You must be signed in to change notification settings - Fork 0
/
example4-react.js
186 lines (176 loc) · 4.62 KB
/
example4-react.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
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
var redux = require('../')
var react = require('react')
var toString = require('react-dom/server').renderToString;
var reactdom = require('react-dom');
var hyperx = require('hyperx')
var hx = hyperx(react.createElement)
var nextTodoId = 0;
var myinput;
var todo = function(state,action) {
switch (action.type) {
case 'ADD_TODO' :
return {
id: action.id,
text: action.text,
completed:false
}
break;
case 'TOGGLE_TODO' :
if (state.id !== action.id) {
return state
}
return {
type: state.type,
id : state.id,
text: state.text,
completed: !state.completed
}
break;
default:
return state
break;
}
}
var todos = function(state, action) {
if (state === undefined)
state = []
switch (action.type) {
case 'ADD_TODO':
return state.concat(todo(undefined,action))
break;
case 'TOGGLE_TODO' :
return state.map(function(t) {
return todo(t,action)
})
break;
default:
return state;
}
}
var visibilityFilter = function(state, action) {
if (state === undefined)
state = 'SHOW_ALL'
switch (action.type) {
case 'SET_VISIBILITY_FILTER':
return action.filter;
break;
default:
return state
}
}
var combineReducers = redux.combineReducers;
var todoApp = combineReducers({
todos:todos,
visibilityFilter:visibilityFilter
});
var createStore = redux.createStore;
var store = createStore(todoApp)
var getVisibleTodos = function(todos,filter) {
switch (filter) {
case 'SHOW_ALL' :
return todos
case 'SHOW_COMPLETED' :
return todos.filter(function(t) { return t.completed })
case 'SHOW_ACTIVE' :
return todos.filter(function(t) { return !t.completed })
default:
break;
}
}
var FilterLink = react.createClass({
render: function() {
var that = this;
if (this.props.filter === this.props.currentFilter) {
return hx`<span>${this.props.children}</span>`
}
return hx`<a href='#' onClick=${ function(e) {
e.preventDefault();
that.props.onClick(that.props.filter);
}}>${this.props.children}</a>`;
}
})
var Todo = react.createClass({
render: function() {
var that = this;
return hx`<li onClick=${that.props.onClick}
style=${{textDecoration:that.props.completed ? 'line-through':'none'}}>${this.props.text}</li>`
}
})
var TodoList = react.createClass({
render: function() {
var that = this;
return hx`<ul>
${this.props.todos.map(function(todo) {
return react.createElement(Todo,{
onClick:function() {
that.props.onTodoClick(todo.id);
},
completed:todo.completed,
text:todo.text
})
})}
</ul>`
}
})
var AddTodo = react.createClass({
render: function() {
var that = this;
return hx`<div>
<input ref=${function(node) {
myinput = node
}} />
<button onClick=${function() {
that.props.onAddClick(myinput.value);
}}> Add Todo</button>
</div>`
}
})
var Footer = react.createClass({
render: function() {
return hx`<div>${react.createElement(FilterLink,{filter:'SHOW_ALL',children:'All',currentFilter:this.props.visibilityFilter,onClick:this.props.onFilterClick})}
${react.createElement(FilterLink,{filter:'SHOW_ACTIVE',children:'Active',currentFilter:this.props.visibilityFilter,onClick:this.props.onFilterClick})}
${react.createElement(FilterLink,{filter:'SHOW_COMPLETED',children:'Completed',currentFilter:this.props.visibilityFilter,onClick:this.props.onFilterClick})}</div>`
}
})
var TodoApp = react.createClass({
render : function() {
return hx`<div>
${react.createElement(AddTodo,{
onAddClick:function(text) {
store.dispatch({
type:'ADD_TODO',
id:nextTodoId++,
text:text
})
myinput.value='';
}
})}
${react.createElement(TodoList,{
onTodoClick:function(id) {
store.dispatch({
type:'TOGGLE_TODO',
id: id
})
},
todos:getVisibleTodos(this.props.todos,this.props.visibilityFilter)
})}
Show ${' '}
${react.createElement(Footer,{
onFilterClick: function(filter) {
store.dispatch({
type:'SET_VISIBILITY_FILTER',
filter:filter
})
},
visibilityFilter:this.props.visibilityFilter})}
</div>`
}
});
var render = function() {
reactdom.render(react.createElement(TodoApp, {
todos:store.getState().todos,
visibilityFilter:store.getState().visibilityFilter
}),document.querySelector('#content'))
}
store.subscribe(render)
render()