1
- const adjectives = [ "pretty" , "large" , "big" , "small" , "tall" , "short" , "long" , "handsome" , "plain" , "quaint" , "clean" , "elegant" , "easy" , "angry" , "crazy" , "helpful" , "mushy" , "odd" , "unsightly" , "adorable" , "important" , "inexpensive" , "cheap" , "expensive" , "fancy" ] ;
2
- const colours = [ "red" , "yellow" , "blue" , "green" , "pink" , "brown" , "purple" , "brown" , "white" , "black" , "orange" ] ;
3
- const nouns = [ "table" , "chair" , "house" , "bbq" , "desk" , "car" , "pony" , "cookie" , "sandwich" , "burger" , "pizza" , "mouse" , "keyboard" ] ;
4
- const [ l1 , l2 , l3 ] = [ adjectives . length , colours . length , nouns . length ] ;
1
+ "use strict" ;
5
2
6
- const nTemplates = ( n ) => Math . round ( n / 100 ) , tbody = document . getElementsByTagName ( 'tbody' ) [ 0 ] ;
7
- let data = [ ] , index = 1 , i , lbl , selected ;
3
+ const gAdjectives = [ "pretty" , "large" , "big" , "small" , "tall" , "short" , "long" , "handsome" , "plain" , "quaint" , "clean" , "elegant" , "easy" , "angry" , "crazy" , "helpful" , "mushy" , "odd" , "unsightly" , "adorable" , "important" , "inexpensive" , "cheap" , "expensive" , "fancy" ] ;
4
+ const gColours = [ "red" , "yellow" , "blue" , "green" , "pink" , "brown" , "purple" , "brown" , "white" , "black" , "orange" ] ;
5
+ const gNouns = [ "table" , "chair" , "house" , "bbq" , "desk" , "car" , "pony" , "cookie" , "sandwich" , "burger" , "pizza" , "mouse" , "keyboard" ] ;
6
+ const nts = { 1000 : 5 , 10000 : 125 } ;
8
7
9
- function create ( n = 1000 ) { if ( tbody . children . length ) clear ( ) ; append ( n ) ; }
10
- function append ( n = 1000 ) {
11
- const nt = nTemplates ( n ) ; let j = 0 , r1 , r2 , r3 ;
12
- const itemTemplate = document . getElementById ( 'itemTemplate' ) . content ;
13
- while ( nt >= itemTemplate . children . length * 2 ) itemTemplate . appendChild ( itemTemplate . cloneNode ( true ) ) ;
14
- while ( nt > itemTemplate . children . length ) itemTemplate . appendChild ( itemTemplate . firstElementChild . cloneNode ( true ) ) ;
15
-
16
- const ids = Array . prototype . map . call ( itemTemplate . querySelectorAll ( 'td:first-child' ) , i => i . firstChild )
17
- const labels = Array . prototype . map . call ( itemTemplate . querySelectorAll ( 'a.lbl' ) , i => i . firstChild ) ;
18
-
19
- while ( ( n -= nt ) >= 0 ) {
20
- for ( i = 0 ; i < nt ; i ++ , j ++ ) {
21
- r1 = Math . round ( Math . random ( ) * 1000 ) % l1 ;
22
- r2 = Math . round ( Math . random ( ) * 1000 ) % l2 ;
23
- r3 = Math . round ( Math . random ( ) * 1000 ) % l3 ;
24
- ( ids [ i ] . nodeValue = index ++ ) && data . push ( labels [ i ] . nodeValue = `${ adjectives [ r1 ] } ${ colours [ r2 ] } ${ nouns [ r3 ] } ` )
25
- }
26
- tbody . appendChild ( itemTemplate . cloneNode ( true ) ) ;
27
- }
28
- }
29
- function update ( ) {
30
- const labels = tbody . querySelectorAll ( 'a.lbl' ) , length = labels . length ;
31
- for ( i = 0 ; i < length ; i += 10 ) labels [ i ] . firstChild . nodeValue = data [ i ] += ' !!!'
32
- }
33
- function clear ( ) { ( data = [ ] ) && ( tbody . textContent = '' ) }
8
+ new class {
9
+ index = 1 ; data = [ ] ; labels = null ; invalidLabels = true ;
10
+ tbody = document . getElementsByTagName ( 'tbody' ) [ 0 ] ;
11
+ constructor ( ) {
12
+ this . tbody . onclick = this . onclick ( ) ;
13
+ for ( let key of [ 'run' , 'runlots' , 'add' , 'update' , 'clear' , 'swaprows' ] ) {
14
+ document . getElementById ( key ) . onclick = ( e ) => { e . stopPropagation ( ) ; this [ key ] ( ) ; }
15
+ }
16
+ } ;
17
+ run ( n = 1000 ) { if ( this . data . length ) this . clear ( ) ; this . add ( n ) ; } ;
18
+ runlots ( ) { this . run ( 10000 ) } ;
19
+ add ( n = 1000 ) {
20
+ const adjectives = gAdjectives , colours = gColours , nouns = gNouns ;
21
+ const l1 = adjectives . length , l2 = colours . length , l3 = nouns . length ;
22
+ const nt = nts [ n ] ;
23
+ let i , j = 0 , r1 , r2 , r3 ; ;
34
24
35
- function swap ( ) {
36
- if ( tbody . children . length < 999 ) return ; const first = tbody . firstElementChild ;
37
- [ data [ 1 ] , data [ 998 ] ] = [ data [ 998 ] , data [ 1 ] ] ;
38
- tbody . insertBefore ( tbody . insertBefore ( first . nextElementSibling ,
39
- tbody . children [ 998 ] ) . nextElementSibling , first . nextElementSibling ) ;
40
- }
41
- tbody . onclick = ( e ) => {
42
- e . preventDefault ; e . stopPropagation ;
43
- if ( e . target . matches ( 'a.lbl' ) ) {
44
- const element = e . target . parentNode . parentNode ;
45
- if ( element === selected ) selected . className = selected . className ? "" : "danger" ;
46
- else {
47
- if ( selected ) selected . className = "" ;
48
- ( element . className = "danger" ) && ( selected = element )
25
+ const itemTemplates = document . getElementById ( 'itemTemplate' ) . content . cloneNode ( true ) ;
26
+ if ( itemTemplates . children . length < nt ) {
27
+ const itemTemplate = itemTemplates . firstElementChild ;
28
+ while ( nt >= itemTemplates . children . length * 2 ) itemTemplates . appendChild ( itemTemplates . cloneNode ( true ) ) ;
29
+ while ( nt > itemTemplates . children . length ) itemTemplates . appendChild ( itemTemplate . cloneNode ( true ) ) ; ;
30
+ }
31
+ const ids = Array . prototype . map . call ( itemTemplates . querySelectorAll ( `td:first-child` ) , i => i . firstChild )
32
+ const labels = Array . prototype . map . call ( itemTemplates . querySelectorAll ( `a.lbl` ) , i => i . firstChild ) ;
33
+
34
+ while ( ( n -= nt ) >= 0 ) {
35
+ for ( i = 0 ; i < nt ; i ++ , j ++ ) {
36
+ r1 = Math . round ( Math . random ( ) * 1000 ) % l1 ;
37
+ r2 = Math . round ( Math . random ( ) * 1000 ) % l2 ;
38
+ r3 = Math . round ( Math . random ( ) * 1000 ) % l3 ;
39
+ ids [ i ] . nodeValue = this . index ++ ;
40
+ this . data . push ( labels [ i ] . nodeValue = `${ adjectives [ r1 ] } ${ colours [ r2 ] } ${ nouns [ r3 ] } ` ) ;
41
+ }
42
+ this . tbody . appendChild ( itemTemplates . cloneNode ( true ) ) ;
43
+ } this . invalidLabels = true ;
44
+ } ;
45
+ update ( ) {
46
+ if ( this . invalidLabels ) this . labels = this . tbody . querySelectorAll ( 'a.lbl' ) ; this . invalidLabels = false ;
47
+ const labels = this . labels , length = labels . length , data = this . data ;
48
+ let i ; for ( i = 0 ; i < length ; i += 10 ) labels [ i ] . firstChild . nodeValue = data [ i ] += ' !!!' ;
49
+ } ;
50
+ clear ( ) { this . tbody . textContent = '' ; this . data = [ ] ; this . invalidLabels = true ; } ;
51
+ swaprows ( ) {
52
+ const tbody = this . tbody , data = this . data ;
53
+ if ( tbody . children . length < 999 ) return ; this . invalidLabels = true ;
54
+ const first = tbody . firstElementChild ;
55
+ [ data [ 1 ] , data [ 998 ] ] = [ data [ 998 ] , data [ 1 ] ] ;
56
+ tbody . insertBefore ( tbody . insertBefore ( first . nextElementSibling ,
57
+ tbody . children [ 998 ] ) . nextElementSibling , first . nextElementSibling ) ;
58
+ } ;
59
+ onclick ( ) {
60
+ let selected ;
61
+ return ( e ) => {
62
+ e . stopPropagation ;
63
+ if ( e . target . matches ( 'a.lbl' ) ) {
64
+ e . preventDefault ;
65
+ const element = e . target . parentNode . parentNode ;
66
+ if ( element === selected ) selected . className = selected . className ? "" : "danger" ;
67
+ else {
68
+ if ( selected ) selected . className = "" ;
69
+ element . className = "danger" ; selected = element ;
70
+ }
71
+ } else if ( e . target . matches ( 'span.remove' ) ) {
72
+ const element = e . target . parentNode . parentNode . parentNode ;
73
+ const index = Array . prototype . indexOf . call ( this . tbody . children , element ) ;
74
+ element . remove ( ) ; this . invalidLabels = true ;
75
+ this . data . splice ( index , 1 ) ;
76
+ }
49
77
}
50
- } else if ( e . target . matches ( 'span.remove' ) ) {
51
- const element = e . target . parentNode . parentNode . parentNode ;
52
- data . splice ( Array . prototype . indexOf . call ( tbody . children , element ) ) && tbody . removeChild ( element ) ;
53
78
}
54
- }
55
- for ( let [ key , fn ] of Object . entries ( {
56
- run : create , runlots : ( ) => create ( 10000 ) ,
57
- add : append , update, clear, swaprows : swap
58
- } ) ) document . getElementById ( key ) . onclick = ( e ) => { e . stopPropagation ( ) , fn ( ) }
79
+ } ( ) ;
0 commit comments