-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ios.js
executable file
·277 lines (262 loc) · 6.15 KB
/
index.ios.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
'use strict';
var React = require('react-native');
var ReactPropTypes = React.PropTypes;
var assign = require('object-assign');
var keyMirror = require('key-mirror');
var EventEmitter = require('events').EventEmitter;
// AppDispatcher.jsx
var Dispatcher = require('flux').Dispatcher;
var AppDispatcher = new Dispatcher();
// TodoConstants.jsx
var TodoConstants = keyMirror({
TODO_CREATE: null,
TODO_COMPLETE: null,
TODO_DESTROY: null,
TODO_UNDO_COMPLETE: null
});
// TodoStore.jsx
var CHANGE_EVENT = 'change';
var _todos = {};
function create(text) {
var id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36);
_todos[id] = {
id: id,
complete: false,
text: text
};
}
function update(id, updates) {
_todos[id] = assign({}, _todos[id], updates);
}
function destroy(id) {
delete _todos[id];
}
var TodoStore = assign({}, EventEmitter.prototype, {
getAll: function() {
/* ObjectをArrayにしてから返す。ウェブのReactでは必要ない。
ListViewにした時にkeyで見ている可能性高い。
Objectのプロパティを変更してもTodoItemのrenderが動かなかった */
var todos = [];
for(var key in _todos) {
todos.push(_todos[key]);
}
return todos;
},
emitChange: function() {
this.emit(CHANGE_EVENT);
},
addChangeListener: function(callback) {
this.on(CHANGE_EVENT, callback);
},
removeChangeListener: function(callback) {
this.removeListener(CHANGE_EVENT, callback);
}
});
AppDispatcher.register(function(action) {
var text;
switch(action.actionType) {
case TodoConstants.TODO_CREATE:
text = action.text.trim();
if (text !== '') {
create(text);
TodoStore.emitChange();
}
break;
case TodoConstants.TODO_UNDO_COMPLETE:
update(action.id, {complete: false});
TodoStore.emitChange();
break;
case TodoConstants.TODO_COMPLETE:
update(action.id, {complete: true});
TodoStore.emitChange();
break;
case TodoConstants.TODO_DESTROY:
destroy(action.id);
TodoStore.emitChange();
break;
default:
}
});
// TodoActions.jsx
var TodoActions = {
create: function(text) {
AppDispatcher.dispatch({
actionType: TodoConstants.TODO_CREATE,
text: text
});
},
toggleComplete: function(todo) {
var id = todo.id;
if (todo.complete) {
AppDispatcher.dispatch({
actionType: TodoConstants.TODO_UNDO_COMPLETE,
id: id
});
} else {
AppDispatcher.dispatch({
actionType: TodoConstants.TODO_COMPLETE,
id: id
});
}
},
destroy: function(id) {
AppDispatcher.dispatch({
actionType: TodoConstants.TODO_DESTROY,
id: id
});
}
};
// components ////////////////////////////////////////////
var {
AppRegistry,
StyleSheet,
Text,
TextInput,
View,
ListView
} = React;
// TodoApp.react.jsx
var TodoApp = React.createClass({
getInitialState: function() {
return {
todos: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
})
};
},
componentDidMount: function() {
this.setState({
todos: this.state.todos.cloneWithRows(TodoStore.getAll())
});
TodoStore.addChangeListener(this._onChange);
},
componentWillUnmount: function() {
TodoStore.removeChangeListener(this._onChange);
},
render: function() {
return (
<View style={styles.TodoApp}>
<Header />
<MainSection todos={this.state.todos} />
</View>
);
},
_onChange: function() {
this.setState({
todos: this.state.todos.cloneWithRows(TodoStore.getAll())
});
}
});
// MainSection.react.jsx
var MainSection = React.createClass({
propTypes: {
todos: ReactPropTypes.object.isRequired
},
render: function() {
return (
<View>
<ListView dataSource={this.props.todos} renderRow={this.renderItem} />
</View>
);
},
renderItem: function(todo) {
return <TodoItem todo={todo} />;
}
});
// TodoItem.react.jsx
var TodoItem = React.createClass({
render: function() {
var todo = this.props.todo;
var todoItemStyle;
todoItemStyle = (todo.complete) ? styles.TodoItemDone : styles.TodoItem;
return (
<View style={todoItemStyle}>
<Text style={styles.text}>{todo.text}</Text>
<Text onPress={() => this._onToggleComplete(todo)}>[完了]</Text>
<Text onPress={() => this._onDestroy(todo)}>[削除]</Text>
</View>
);
},
_onToggleComplete: function(todo) {
TodoActions.toggleComplete(todo);
},
_onDestroy: function(todo) {
TodoActions.destroy(todo.id);
}
});
// Header.react.jsx
var Header = React.createClass({
render: function() {
return (
<View>
<TodoTextInput />
</View>
);
}
});
// TodoTextInput.react.jsx
var TodoTextInput = React.createClass({
getInitialState: function() {
return {
value: ''
}
},
render: function() {
return (
<View>
<TextInput
style={styles.TodoTextInput}
onChangeText={(text) => this.setState({value: text})}
onBlur={this._save}
placeholder={'What needs to be done?'}
value={this.state.value}
/>
</View>
);
},
_save: function() {
var text = this.state.value;
if(text) TodoActions.create(text);
this.setState({
value: ''
});
}
});
/////////////////////////////////////////////////////////////////////// style
var styles = StyleSheet.create({
TodoApp: {
padding: 20,
paddingTop: 40
},
TodoItem: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#FFFFFF',
height: 58
},
TodoItemDone: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#FFFFFF',
height: 58,
opacity: .3
},
text: {
flex: 1,
textAlign: 'left',
fontSize: 16
},
TodoTextInput: {
height: 40,
backgroundColor: '#EEEEEE',
padding: 10,
fontSize: 16
}
});
////////////////////////////////////////////////////////////////////// Registry
// app.jsxに当たる部分
AppRegistry.registerComponent('TodoProject', () => TodoApp);