1
1
import {
2
2
Block ,
3
3
fragment ,
4
+ FragmentBlock ,
4
5
stringToDOM ,
5
6
withKey ,
6
7
} from '/Users/aidenybai/Projects/aidenybai/million/packages/block/index' ;
@@ -65,6 +66,7 @@ const random = (max) => Math.round(Math.random() * 1000) % max;
65
66
66
67
let nextId = 1 ;
67
68
let list = [ ] ;
69
+ let oldCache = { } ;
68
70
let selected = 0 ;
69
71
let main ;
70
72
@@ -126,17 +128,22 @@ const swapRows = () => {
126
128
return false ;
127
129
} ;
128
130
129
- const select = ( id ) => {
131
+ let prevSelected ;
132
+ const select = ( id , el ) => {
133
+ if ( prevSelected ) prevSelected . className = '' ;
130
134
selected = id ;
131
- update ( ) ;
135
+ el . className = 'danger' ;
136
+ prevSelected = el ;
137
+ const row = main . children . findIndex ( ( block ) => block . props . id === id ) ;
138
+ row . props . className = 'danger' ;
139
+ const prevRow = main . children . findIndex ( ( block ) => block . props . id === id ) ;
140
+ prevRow . props . className = '' ;
132
141
} ;
133
142
134
143
const remove = ( id ) => {
135
- list . splice (
136
- list . findIndex ( ( item ) => item . id === id ) ,
137
- 1
138
- ) ;
139
- update ( ) ;
144
+ const index = list . findIndex ( ( item ) => item . id === id ) ;
145
+ list . splice ( index , 1 ) ;
146
+ main . children . splice ( index , 1 ) ;
140
147
} ;
141
148
142
149
const Row = ( ( ) => {
@@ -158,12 +165,6 @@ const Row = (() => {
158
165
{
159
166
path : [ 1 , 0 ] ,
160
167
edits : [
161
- {
162
- type : 'event' ,
163
- listener : 'select' ,
164
- name : 'onClick' ,
165
- hole : 'select' ,
166
- } ,
167
168
{
168
169
type : 'child' ,
169
170
hole : 'label' ,
@@ -172,18 +173,6 @@ const Row = (() => {
172
173
] ,
173
174
inits : [ ] ,
174
175
} ,
175
- {
176
- path : [ 2 , 0 ] ,
177
- edits : [
178
- {
179
- type : 'event' ,
180
- listener : 'remove' ,
181
- name : 'onClick' ,
182
- hole : 'remove' ,
183
- } ,
184
- ] ,
185
- inits : [ ] ,
186
- } ,
187
176
{
188
177
path : [ ] ,
189
178
edits : [
@@ -227,14 +216,6 @@ function render(oldCache, newCache) {
227
216
id : item . id ,
228
217
label : item . label ,
229
218
className : isSelected ? 'danger' : '' ,
230
- remove : withKey ( ( ) => {
231
- remove ( item . id ) ;
232
- return false ;
233
- } , id ) ,
234
- select : withKey ( ( ) => {
235
- select ( item . id ) ;
236
- return false ;
237
- } , id ) ,
238
219
} ,
239
220
id ,
240
221
false
@@ -246,99 +227,115 @@ function render(oldCache, newCache) {
246
227
) ;
247
228
}
248
229
249
- new Block (
250
- stringToDOM (
251
- '<div class="container"><div class="jumbotron"><div class="row"><div class="col-md-6"><h1>Million</h1></div><div class="col-md-6"><div class="row"><div class="col-sm-6 smallpad"><button type="button" class="btn btn-primary btn-block" id="run">Create 1,000 rows</button></div><div class="col-sm-6 smallpad"><button type="button" class="btn btn-primary btn-block" id="runlots">Create 10,000 rows</button></div><div class="col-sm-6 smallpad"><button type="button" class="btn btn-primary btn-block" id="add">Append 1,000 rows</button></div><div class="col-sm-6 smallpad"><button type="button" class="btn btn-primary btn-block" id="update">Update every 10th row</button></div><div class="col-sm-6 smallpad"><button type="button" class="btn btn-primary btn-block" id="clear">Clear</button></div><div class="col-sm-6 smallpad"><button type="button" class="btn btn-primary btn-block" id="swaprows">Swap Rows</button></div></div></div></div></div><table class="table table-hover table-striped test-data"><tbody></tbody></table><span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span></div>'
252
- ) ,
253
- [
254
- {
255
- path : [ 0 , 0 , 1 , 0 , 0 , 0 ] ,
256
- edits : [
257
- {
258
- type : 'event' ,
259
- name : 'onClick' ,
260
- listener : create1k ,
261
- } ,
262
- ] ,
263
- inits : [ ] ,
264
- } ,
265
- {
266
- path : [ 0 , 0 , 1 , 0 , 1 , 0 ] ,
267
- edits : [
268
- {
269
- type : 'event' ,
270
- name : 'onClick' ,
271
- listener : create10k ,
272
- } ,
273
- ] ,
274
- inits : [ ] ,
275
- } ,
276
- {
277
- path : [ 0 , 0 , 1 , 0 , 2 , 0 ] ,
278
- edits : [
279
- {
280
- type : 'event' ,
281
- name : 'onClick' ,
282
- listener : append1k ,
283
- } ,
284
- ] ,
285
- inits : [ ] ,
286
- } ,
287
- {
288
- path : [ 0 , 0 , 1 , 0 , 3 , 0 ] ,
289
- edits : [
290
- {
291
- type : 'event' ,
292
- name : 'onClick' ,
293
- listener : updateEvery10 ,
294
- } ,
295
- ] ,
296
- inits : [ ] ,
297
- } ,
298
- {
299
- path : [ 0 , 0 , 1 , 0 , 4 , 0 ] ,
300
- edits : [
301
- {
302
- type : 'event' ,
303
- name : 'onClick' ,
304
- listener : ( ) => {
305
- clear ( ) ;
306
- update ( ) ;
307
- return false ;
230
+ ( ( ) => {
231
+ new Block (
232
+ stringToDOM (
233
+ '<div class="container"><div class="jumbotron"><div class="row"><div class="col-md-6"><h1>Million</h1></div><div class="col-md-6"><div class="row"><div class="col-sm-6 smallpad"><button type="button" class="btn btn-primary btn-block" id="run">Create 1,000 rows</button></div><div class="col-sm-6 smallpad"><button type="button" class="btn btn-primary btn-block" id="runlots">Create 10,000 rows</button></div><div class="col-sm-6 smallpad"><button type="button" class="btn btn-primary btn-block" id="add">Append 1,000 rows</button></div><div class="col-sm-6 smallpad"><button type="button" class="btn btn-primary btn-block" id="update">Update every 10th row</button></div><div class="col-sm-6 smallpad"><button type="button" class="btn btn-primary btn-block" id="clear">Clear</button></div><div class="col-sm-6 smallpad"><button type="button" class="btn btn-primary btn-block" id="swaprows">Swap Rows</button></div></div></div></div></div><table class="table table-hover table-striped test-data"><tbody></tbody></table><span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span></div>'
234
+ ) ,
235
+ [
236
+ {
237
+ path : [ 0 , 0 , 1 , 0 , 0 , 0 ] ,
238
+ edits : [
239
+ {
240
+ type : 'event' ,
241
+ name : 'onClick' ,
242
+ listener : create1k ,
308
243
} ,
309
- } ,
310
- ] ,
311
- inits : [ ] ,
312
- } ,
313
- {
314
- path : [ 0 , 0 , 1 , 0 , 5 , 0 ] ,
315
- edits : [
316
- {
317
- type : 'event' ,
318
- name : 'onClick' ,
319
- listener : swapRows ,
320
- } ,
321
- ] ,
322
- inits : [ ] ,
323
- } ,
324
- {
325
- path : [ 1 , 0 ] ,
326
- edits : [
327
- {
328
- type : 'child' ,
329
- hole : 'rows' ,
330
- index : 0 ,
331
- } ,
332
- ] ,
333
- inits : [ ] ,
334
- } ,
335
- ] ,
336
- { rows : ( main = fragment ( [ ] ) ) } ,
337
- undefined ,
338
- undefined
339
- ) . mount ( document . getElementById ( 'main' ) ) ;
244
+ ] ,
245
+ inits : [ ] ,
246
+ } ,
247
+ {
248
+ path : [ 0 , 0 , 1 , 0 , 1 , 0 ] ,
249
+ edits : [
250
+ {
251
+ type : 'event' ,
252
+ name : 'onClick' ,
253
+ listener : create10k ,
254
+ } ,
255
+ ] ,
256
+ inits : [ ] ,
257
+ } ,
258
+ {
259
+ path : [ 0 , 0 , 1 , 0 , 2 , 0 ] ,
260
+ edits : [
261
+ {
262
+ type : 'event' ,
263
+ name : 'onClick' ,
264
+ listener : append1k ,
265
+ } ,
266
+ ] ,
267
+ inits : [ ] ,
268
+ } ,
269
+ {
270
+ path : [ 0 , 0 , 1 , 0 , 3 , 0 ] ,
271
+ edits : [
272
+ {
273
+ type : 'event' ,
274
+ name : 'onClick' ,
275
+ listener : updateEvery10 ,
276
+ } ,
277
+ ] ,
278
+ inits : [ ] ,
279
+ } ,
280
+ {
281
+ path : [ 0 , 0 , 1 , 0 , 4 , 0 ] ,
282
+ edits : [
283
+ {
284
+ type : 'event' ,
285
+ name : 'onClick' ,
286
+ listener : ( ) => {
287
+ clear ( ) ;
288
+ return false ;
289
+ } ,
290
+ } ,
291
+ ] ,
292
+ inits : [ ] ,
293
+ } ,
294
+ {
295
+ path : [ 0 , 0 , 1 , 0 , 5 , 0 ] ,
296
+ edits : [
297
+ {
298
+ type : 'event' ,
299
+ name : 'onClick' ,
300
+ listener : swapRows ,
301
+ } ,
302
+ ] ,
303
+ inits : [ ] ,
304
+ } ,
305
+ {
306
+ path : [ 1 , 0 ] ,
307
+ edits : [
308
+ {
309
+ type : 'event' ,
310
+ name : 'onClick' ,
311
+ listener : ( event ) => {
312
+ const el = event . target ;
313
+ const row = el . closest ( 'tr' ) ;
314
+ const id = Number ( row . firstChild . textContent ) ;
315
+ if ( el . matches ( '.glyphicon-remove' ) ) {
316
+ row . remove ( ) ;
317
+ remove ( id ) ;
318
+ } else {
319
+ select ( id , row ) ;
320
+ }
321
+ return false ;
322
+ } ,
323
+ } ,
324
+ {
325
+ type : 'child' ,
326
+ hole : 'rows' ,
327
+ index : 0 ,
328
+ } ,
329
+ ] ,
330
+ inits : [ ] ,
331
+ } ,
332
+ ] ,
333
+ { rows : ( main = fragment ( [ ] ) ) } ,
334
+ undefined ,
335
+ undefined
336
+ ) . mount ( document . getElementById ( 'main' ) ) ;
337
+ } ) ( ) ;
340
338
341
- let oldCache = { } ;
342
339
function update ( ) {
343
340
let newCache = { } ;
344
341
main . patch ( render ( oldCache , newCache ) ) ;
0 commit comments