@@ -2,57 +2,50 @@ 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 ] ;
5
+ const pick = ( dict => dict [ Math . round ( Math . random ( ) * 1000 ) % dict . length ] ) ;
6
+ const label = ( ( ) => `${ pick ( adjectives ) } ${ pick ( colours ) } ${ pick ( nouns ) } ` ) ;
7
+
8
+ const { cloneNode, insertBefore} = Node . prototype ;
6
9
7
10
let ID = 1 , rows = [ ] , selection ;
8
11
const ROW = Symbol ( ) , ACTION = Symbol ( ) ;
12
+ const [ [ table ] , [ tbody ] , [ trow ] , buttons ] = 'table,tbody,#trow,button'
13
+ . split ( ',' ) . map ( s => document . querySelectorAll ( s ) ) ;
9
14
10
- const table = document . querySelector ( 'table' ) ;
11
- let tbody = document . querySelector ( 'tbody' ) ;
12
- const trow = document . querySelector ( '#trow' ) ;
13
-
14
- const { cloneNode, insertBefore} = Node . prototype ;
15
- const TBody = ( cloneNode . bind ( tbody , false ) ) ;
16
- const TRow = ( cloneNode . bind ( trow . content . firstChild , true ) ) ;
17
- const insert = ( ( row , before = null ) => insertBefore . call ( tbody , row , before ) ) ;
18
-
19
- const build = ( ( ) => {
15
+ const build = ( TRow => ( ) => {
20
16
const tr = TRow ( ) ;
21
17
const td1 = tr . firstChild , td2 = td1 . nextSibling , td3 = td2 . nextSibling ;
22
18
const a1 = td2 . firstChild , a2 = td3 . firstChild ;
23
- const label = `${ pick ( adjectives ) } ${ pick ( colours ) } ${ pick ( nouns ) } ` ;
24
19
td1 . firstChild . nodeValue = ID ++ ;
25
- ( tr . label = a1 . firstChild ) . nodeValue = label ;
20
+ ( tr . label = a1 . firstChild ) . nodeValue = label ( ) ;
26
21
a1 [ ACTION ] = select , a2 [ ACTION ] = remove ;
27
- return insert ( a1 [ ROW ] = a2 [ ROW ] = tr ) ;
28
- } ) ;
22
+ return a1 [ ROW ] = a2 [ ROW ] = tr ;
23
+ } ) ( cloneNode . bind ( trow . content . firstChild , true ) ) ;
29
24
30
- const create = count => [ ... Array ( count ) ] . map ( build ) ;
25
+ const insert = ( insertBefore . bind ( tbody ) ) ;
31
26
32
- const select = ( set => row => {
33
- set ( 'remove' ) , selection = row , set ( 'add' ) ;
34
- } ) ( setter => selection ?. classList [ setter ] ( 'danger' ) ) ;
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 ] ;
33
+ } ;
35
34
36
- const remove = ( match => row => {
37
- rows = rows . filter ( match , row ) , row . remove ( ) ;
38
- } ) ( function ( row ) { return row !== this ; } ) ;
35
+ const select = row => ( selection && ( selection . className = '' ) ,
36
+ ( selection = row ) . className = 'danger' ) ;
39
37
40
- const clear = patch => {
41
- rows = [ ] , selection = 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 ) ) ;
47
- } ;
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 ) ;
48
43
49
- document . querySelectorAll ( 'button' ) . forEach ( function ( button ) {
50
- button . addEventListener ( 'click' , this [ button . id ] ) ;
51
- } , {
52
- run ( ) { clear ( ( ) => rows = create ( 1000 ) ) ; } ,
53
- runlots ( ) { clear ( ( ) => rows = create ( 10000 ) ) ; } ,
54
- add ( ) { rows = [ ...rows , ...create ( 1000 ) ] ; } ,
55
- clear ( ) { clear ( ) ; } ,
44
+ buttons . forEach ( function ( b ) { b . onclick = this [ b . id ] ; } , {
45
+ run ( ) { create ( 1000 ) ; } ,
46
+ runlots ( ) { create ( 10000 ) ; } ,
47
+ add ( ) { create ( 1000 , rows ) ; } ,
48
+ clear,
56
49
update ( ) {
57
50
for ( let i = 0 ; i < rows . length ; i += 10 )
58
51
rows [ i ] . label . nodeValue += ' !!!' ;
@@ -64,7 +57,7 @@ document.querySelectorAll('button').forEach(function (button) {
64
57
}
65
58
} ) ;
66
59
67
- table . addEventListener ( 'click' , e => {
60
+ table . onclick = e => {
68
61
let { target : t } = e ;
69
62
e . stopPropagation ( ) , ( t [ ACTION ] ?? ( t = t . parentNode ) [ ACTION ] ) ?. ( t [ ROW ] ) ;
70
- } ) ;
63
+ } ;
0 commit comments