forked from RubyLouvre/anu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex2.html
More file actions
250 lines (216 loc) · 7.15 KB
/
index2.html
File metadata and controls
250 lines (216 loc) · 7.15 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>树组件</title>
<script type='text/javascript' src="./dist/React.js"></script>
<style>
.aaa {
width: 200px;
height: 200px;
background: red;
}
</style>
<!--<script src="./test/react.js"></script>
<script src="./test/react-dom.js"></script>-->
<script src="./test/redux.js"></script>
<script src="./test/react-redux.js"></script>
<script src="./lib/babel.js"></script>
<script type='text/babel'>
var s
window.onload = function(){
var combineReducers = Redux.combineReducers
var Provider = ReactRedux.Provider
var connect = ReactRedux.connect
var createStore = Redux.createStore
function generateTree() {
let tree = {
0: {
id: 0,
counter: 0,
childIds: []
}
}
for (let i = 1; i < 3; i++) {
let parentId = 0 //Math.floor(Math.pow(Math.random(), 2) * i)
tree[i] = {
id: i,
counter: 0,
childIds: []
}
tree[parentId].childIds.push(i)
}
return tree
}
const INCREMENT = 'INCREMENT'
const CREATE_NODE = 'CREATE_NODE'
const DELETE_NODE = 'DELETE_NODE'
const ADD_CHILD = 'ADD_CHILD'
const REMOVE_CHILD = 'REMOVE_CHILD'
const increment = (nodeId) => ({
type: INCREMENT,
nodeId
})
let nextId = 0
const createNode = () => ({
type: CREATE_NODE,
nodeId: `new_${nextId++}`
})
const deleteNode = (nodeId) => ({
type: DELETE_NODE,
nodeId
})
const addChild = (nodeId, childId) => ({
type: ADD_CHILD,
nodeId,
childId
})
const removeChild = (nodeId, childId) => ({
type: REMOVE_CHILD,
nodeId,
childId
})
var actions = {
increment,
createNode,
deleteNode,
addChild,
removeChild
}
class Node extends React.Component {
constructor(props){
super(props)
this.handleIncrementClick = this.handleIncrementClick.bind(this)
this.handleAddChildClick = this.handleAddChildClick.bind(this)
this.handleRemoveClick = this.handleRemoveClick.bind(this)
this.renderChild = this.renderChild.bind(this)
}
handleIncrementClick(){
const { increment, id } = this.props
increment(id)
}
handleAddChildClick(e){
e.preventDefault()
const { addChild, createNode, id } = this.props
const childId = createNode().nodeId
addChild(id, childId)
}
handleRemoveClick(e){
e.preventDefault()
const { removeChild, deleteNode, parentId, id } = this.props
removeChild(parentId, id)
deleteNode(id)
}
renderChild(childId) {
const { id } = this.props
return (
<li key={childId}>
<ConnectedNode id={childId} parentId={id} />
</li>
)
}
render() {
const { counter, parentId, childIds } = this.props
return (
<div>
Counter: {counter}
{' '}
<button onClick={this.handleIncrementClick}>
+
</button>
{' '}
{typeof parentId !== 'undefined' &&
<a href="#" onClick={this.handleRemoveClick} className='remove'
style={{ color: 'lightgray', textDecoration: 'none' }}>
×
</a>
}
<ul>
{childIds.map(this.renderChild)}
<li key="add">
<a href="#" onClick={this.handleAddChildClick} className='add'>
Add child
</a>
</li>
</ul>
</div>
)
}
}
function mapStateToProps(state, ownProps) {
return state[ownProps.id]
}
const ConnectedNode = connect(mapStateToProps, actions)(Node)
//import { INCREMENT, ADD_CHILD, REMOVE_CHILD, CREATE_NODE, DELETE_NODE } from '../actions'
const childIds = (state, action) => {
switch (action.type) {
case ADD_CHILD:
return [ ...state, action.childId ]
case REMOVE_CHILD:
return state.filter(id => id !== action.childId)
default:
return state
}
}
const node = (state, action) => {
switch (action.type) {
case CREATE_NODE:
return {
id: action.nodeId,
counter: 0,
childIds: []
}
case INCREMENT:
return {
...state,
counter: state.counter + 1
}
case ADD_CHILD:
case REMOVE_CHILD:
return {
...state,
childIds: childIds(state.childIds, action)
}
default:
return state
}
}
const getAllDescendantIds = (state, nodeId) => (
state[nodeId].childIds.reduce((acc, childId) => (
[ ...acc, childId, ...getAllDescendantIds(state, childId) ]
), [])
)
const deleteMany = (state, ids) => {
state = { ...state }
ids.forEach(id => delete state[id])
return state
}
function reducers(state = {}, action) {
const { nodeId } = action
if (typeof nodeId === 'undefined') {
return state
}
if (action.type === DELETE_NODE) {
const descendantIds = getAllDescendantIds(state, nodeId)
return deleteMany(state, [ nodeId, ...descendantIds ])
}
return {
...state,
[nodeId]: node(state[nodeId], action)
}
}
const tree = generateTree()
var div = document.getElementById('root')
const store = createStore(reducers, tree)
s = ReactDOM.render(<Provider store={store}>
<ConnectedNode id={0} />
</Provider>, div)
}
</script>
</head>
<body>
<div>测试</div>
<blockquote id='root'></blockquote>
</body>
</html>