@@ -2,62 +2,56 @@ const adjectives = ['pretty', 'large', 'big', 'small', 'tall', 'short', 'long',
2
2
const colours = [ 'red' , 'yellow' , 'blue' , 'green' , 'pink' , 'brown' , 'purple' , 'brown' , 'white' , 'black' , 'orange' ] ;
3
3
const nouns = [ 'table' , 'chair' , 'house' , 'bbq' , 'desk' , 'car' , 'pony' , 'cookie' , 'sandwich' , 'burger' , 'pizza' , 'mouse' , 'keyboard' ] ;
4
4
5
- const pick = ( dict => dict [ Math . round ( Math . random ( ) * 1000 ) % dict . length ] ) ;
6
- const label = ( ( ) => `${ pick ( adjectives ) } ${ pick ( colours ) } ${ pick ( nouns ) } ` ) ;
5
+ const pick = dict => dict [ Math . round ( Math . random ( ) * 1000 ) % dict . length ] ;
6
+ const label = ( ) => `${ pick ( adjectives ) } ${ pick ( colours ) } ${ pick ( nouns ) } ` ;
7
+ const labelOf = r => r . firstChild . nextSibling . firstChild . firstChild ;
7
8
8
- const { cloneNode, insertBefore} = Node . prototype ;
9
-
10
- let ID = 1 , rows = [ ] , selection ;
11
- const ROW = Symbol ( ) , ACTION = Symbol ( ) ;
12
9
const [ [ table ] , [ tbody ] , [ trow ] , buttons ] = 'table,tbody,#trow,button'
13
10
. split ( ',' ) . map ( s => document . querySelectorAll ( s ) ) ;
11
+ const { children : rows } = tbody ;
14
12
15
- const build = ( TRow => ( ) => {
16
- const tr = TRow ( ) ;
17
- const td1 = tr . firstChild , td2 = td1 . nextSibling , td3 = td2 . nextSibling ;
18
- const a1 = td2 . firstChild , a2 = td3 . firstChild ;
19
- td1 . firstChild . nodeValue = ID ++ ;
20
- ( tr . label = a1 . firstChild ) . nodeValue = label ( ) ;
21
- a1 [ ACTION ] = select , a2 [ ACTION ] = remove ;
22
- return a1 [ ROW ] = a2 [ ROW ] = tr ;
23
- } ) ( cloneNode . bind ( trow . content . firstChild , true ) ) ;
24
-
25
- const insert = ( insertBefore . bind ( tbody ) ) ;
26
-
27
- const create = ( count , old ) => {
28
- ! old && clear ( ) ;
29
- const data = [ ] ;
30
- for ( let i = 0 ; i < count ; i ++ )
31
- data [ i ] = insert ( build ( ) , null ) ;
32
- rows = [ ... ( old ?? [ ] ) , ... data ] ;
13
+ const { cloneNode , insertBefore } = Node . prototype ;
14
+ const clone = n => cloneNode . call ( n , true ) ;
15
+ const insert = insertBefore . bind ( tbody ) ;
16
+
17
+ let ID = 1 , SEL , TMPL , SIZE ;
18
+
19
+ const create = ( count , add ) => {
20
+ if ( SIZE !== count )
21
+ TMPL = clone ( trow . content ) , [ ... Array ( ( SIZE = count ) / 50 - 1 ) ]
22
+ . forEach ( ( ) => TMPL . appendChild ( clone ( TMPL . firstChild ) ) ) ;
23
+ ! add && ( clear ( ) , tbody . remove ( ) ) ;
24
+ while ( count ) {
25
+ for ( let r of TMPL . children )
26
+ ( r . $id ??= r . firstChild . firstChild ) . nodeValue = ID ++ ,
27
+ ( r . $label ??= labelOf ( r ) ) . nodeValue = label ( ) , count -- ;
28
+ insert ( clone ( TMPL ) , null ) ;
29
+ }
30
+ ! add && table . appendChild ( tbody ) ;
33
31
} ;
34
32
35
- const select = row => ( selection && ( selection . className = '' ) ,
36
- ( selection = row ) . className = 'danger' ) ;
37
-
38
- const remove = row =>
39
- ( row . remove ( ) , ( rows = new Set ( rows ) ) . delete ( row ) , rows = [ ...rows ] ) ;
40
-
41
- const clear = ( ) => ( rows . length && ( tbody . textContent = '' ) ,
42
- rows = [ ] , selection = null ) ;
33
+ const clear = ( ) => ( tbody . textContent = '' , SEL = null ) ;
43
34
44
35
buttons . forEach ( function ( b ) { b . onclick = this [ b . id ] ; } , {
45
36
run ( ) { create ( 1000 ) ; } ,
46
37
runlots ( ) { create ( 10000 ) ; } ,
47
- add ( ) { create ( 1000 , rows ) ; } ,
38
+ add ( ) { create ( 1000 , true ) ; } ,
48
39
clear,
49
40
update ( ) {
50
41
for ( let i = 0 ; i < rows . length ; i += 10 )
51
- rows [ i ] . label . nodeValue += ' !!!' ;
42
+ labelOf ( rows [ i ] ) . nodeValue += ' !!!' ;
52
43
} ,
53
44
swaprows ( ) {
54
- if ( rows . length > 998 )
55
- insert ( rows [ 1 ] , rows [ 998 ] ) , insert ( rows [ 998 ] , rows [ 2 ] ) ,
56
- [ rows [ 1 ] , rows [ 998 ] ] = [ rows [ 998 ] , rows [ 1 ] ] ;
45
+ const [ , r1 , r2 ] = rows , r998 = rows [ 998 ] ;
46
+ r998 && ( insert ( r1 , r998 ) , insert ( r998 , r2 ) ) ;
57
47
}
58
48
} ) ;
59
49
60
- table . onclick = e => {
61
- let { target : t } = e ;
62
- e . stopPropagation ( ) , ( t [ ACTION ] ?? ( t = t . parentNode ) [ ACTION ] ) ?. ( t [ ROW ] ) ;
50
+ tbody . onclick = e => {
51
+ const t = e . target , r = t . closest ( 'tr' ) ;
52
+ const td2 = r ?. firstChild . nextSibling , td3 = td2 ?. nextSibling ;
53
+ e . stopPropagation ( ) ;
54
+ t === td2 ?. firstChild ?
55
+ ( SEL && ( SEL . className = '' ) , ( SEL = r ) . className = 'danger' ) :
56
+ ( t === td3 ?. firstChild || t === td3 ?. firstChild . firstChild ) && r . remove ( ) ;
63
57
} ;
0 commit comments