@@ -12,11 +12,12 @@ let tbody = document.querySelector('tbody');
12
12
const trow = document . querySelector ( '#trow' ) ;
13
13
14
14
const { cloneNode, insertBefore} = Node . prototype ;
15
- const clone = ( cloneNode . bind ( trow . content . firstChild , true ) ) ;
15
+ const TBody = ( cloneNode . bind ( tbody , false ) ) ;
16
+ const TRow = ( cloneNode . bind ( trow . content . firstChild , true ) ) ;
16
17
const insert = ( ( row , before = null ) => insertBefore . call ( tbody , row , before ) ) ;
17
18
18
19
const build = ( ( ) => {
19
- const tr = clone ( ) ;
20
+ const tr = TRow ( ) ;
20
21
const td1 = tr . firstChild , td2 = td1 . nextSibling , td3 = td2 . nextSibling ;
21
22
const a1 = td2 . firstChild , a2 = td3 . firstChild ;
22
23
const label = `${ pick ( adjectives ) } ${ pick ( colours ) } ${ pick ( nouns ) } ` ;
@@ -36,19 +37,22 @@ const remove = (match => row => {
36
37
rows = rows . filter ( match , row ) , row . remove ( ) ;
37
38
} ) ( function ( row ) { return row !== this ; } ) ;
38
39
39
- const clear = ( ) => {
40
+ const clear = patch => {
40
41
rows = [ ] , selection = null ;
41
- const clone = tbody . cloneNode ( ) ;
42
- tbody . remove ( ) , insertBefore . call ( table , tbody = clone , null ) ;
42
+ const empty = ! tbody . firstChild ;
43
+ if ( ! empty || patch )
44
+ ! empty && patch ? ( tbody . textContent = '' , patch ( ) ) :
45
+ ( tbody . remove ( ) , tbody = TBody ( ) , patch ?. ( ) ,
46
+ insertBefore . call ( table , tbody , null ) ) ;
43
47
} ;
44
48
45
49
document . querySelectorAll ( 'button' ) . forEach ( function ( button ) {
46
50
button . addEventListener ( 'click' , this [ button . id ] ) ;
47
51
} , {
48
- run ( ) { clear ( ) , rows = create ( 1000 ) ; } ,
49
- runlots ( ) { clear ( ) , rows = create ( 10000 ) ; } ,
52
+ run ( ) { clear ( ( ) => rows = create ( 1000 ) ) ; } ,
53
+ runlots ( ) { clear ( ( ) => rows = create ( 10000 ) ) ; } ,
50
54
add ( ) { rows = [ ...rows , ...create ( 1000 ) ] ; } ,
51
- clear,
55
+ clear ( ) { clear ( ) ; } ,
52
56
update ( ) {
53
57
for ( let i = 0 ; i < rows . length ; i += 10 )
54
58
rows [ i ] . label . nodeValue += ' !!!' ;
0 commit comments