1
- 'use strict' ;
1
+ 'use strict'
2
2
3
- const _random = ( ( max ) => {
4
- return Math . round ( Math . random ( ) * 1000 ) % max ;
5
- } )
3
+ const _random = max => Math . random ( ) * max | 0
6
4
7
- 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" ] ;
8
- const colours = [ "red" , "yellow" , "blue" , "green" , "pink" , "brown" , "purple" , "brown" , "white" , "black" , "orange" ] ;
9
- const nouns = [ "table" , "chair" , "house" , "bbq" , "desk" , "car" , "pony" , "cookie" , "sandwich" , "burger" , "pizza" , "mouse" , "keyboard" ] ;
5
+ 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" ]
6
+ const colours = [ "red" , "yellow" , "blue" , "green" , "pink" , "brown" , "purple" , "brown" , "white" , "black" , "orange" ]
7
+ const nouns = [ "table" , "chair" , "house" , "bbq" , "desk" , "car" , "pony" , "cookie" , "sandwich" , "burger" , "pizza" , "mouse" , "keyboard" ]
10
8
11
9
const lenA = adjectives . length , lenB = colours . length , lenC = nouns . length
12
10
@@ -35,25 +33,25 @@ Doo.define(
35
33
async dooAfterRender ( ) {
36
34
this . tbody = this . shadow . querySelector ( '#tbody' )
37
35
this . shadow . querySelector ( this . scrollTarget ) . addEventListener ( 'click' , e => {
38
- e . preventDefault ( ) ;
36
+ e . preventDefault ( )
39
37
if ( e . target . parentElement . matches ( '.remove' ) ) {
40
- this . delete ( e . target . parentElement ) ;
38
+ this . delete ( e . target . parentElement )
41
39
} else if ( e . target . tagName === 'A' ) {
42
- this . select ( e . target ) ;
40
+ this . select ( e . target )
43
41
}
44
- } ) ;
42
+ } )
45
43
}
46
44
47
45
getParentRow ( elem ) {
48
46
while ( elem ) {
49
47
if ( elem . tagName === "TR" ) { return elem }
50
- elem = elem . parentNode ;
48
+ elem = elem . parentNode
51
49
}
52
- return undefined ;
50
+ return undefined
53
51
}
54
52
55
53
buildData ( count = 1000 ) {
56
- const data = [ ] ;
54
+ const data = [ ]
57
55
for ( let i = 0 ; i < count ; i ++ ) {
58
56
data . push ( { id : this . ID ++ , label : adjectives [ _random ( lenA ) ] + " " + colours [ _random ( lenB ) ] + " " + nouns [ _random ( lenC ) ] } )
59
57
}
@@ -69,36 +67,32 @@ Doo.define(
69
67
}
70
68
71
69
run ( ) {
70
+ this . clear ( )
72
71
this . data . rows = this . buildData ( )
73
- this . tbody . textContent = ''
74
72
this . renderTable ( )
75
73
}
76
74
77
75
add ( ) {
78
- let startRow = this . data . rows . length
79
76
this . data . rows = this . data . rows . concat ( this . buildData ( ) )
80
- this . appendData ( this . tbody , startRow )
77
+ this . renderTable ( this . data . rows )
81
78
}
82
79
83
80
runLots ( ) {
81
+ this . clear ( )
84
82
this . data . rows = this . buildData ( 10000 )
85
- this . tbody . textContent = ''
86
83
this . renderTable ( )
87
84
}
88
85
89
- update ( ) {
90
- let tr = this . tbody . querySelectorAll ( 'tr' )
86
+ update ( e ) {
91
87
for ( let i = 0 , len = this . data . rows . length ; i < len ; i += 10 ) {
92
- this . data . rows [ i ] . label += ' !!!' ;
93
- tr [ i ] . childNodes [ 1 ] . childNodes [ 0 ] . textContent = this . data . rows [ i ] . label
88
+ this . tbody . childNodes [ i ] . childNodes [ 1 ] . childNodes [ 0 ] . innerText = this . data . rows [ i ] . label += ' !!!'
94
89
}
95
90
}
96
91
97
92
select ( elem ) {
98
93
if ( this . selectedRow ) {
99
94
this . selectedRow . classList . remove ( 'danger' )
100
95
this . selectedRow = undefined
101
- // return should toggle IMO
102
96
}
103
97
let row = this . getParentRow ( elem )
104
98
if ( row ) {
@@ -117,8 +111,8 @@ Doo.define(
117
111
let node1 = this . tbody . childNodes [ 1 ]
118
112
let node2 = this . tbody . childNodes [ 998 ]
119
113
120
- let row1 = this . data . rows [ 1 ] ;
121
- this . data . rows [ 1 ] = this . data . rows [ 998 ] ;
114
+ let row1 = this . data . rows [ 1 ]
115
+ this . data . rows [ 1 ] = this . data . rows [ 998 ]
122
116
this . data . rows [ 998 ] = row1
123
117
124
118
this . tbody . insertBefore ( node2 , node1 )
@@ -129,19 +123,19 @@ Doo.define(
129
123
130
124
addEventListeners ( ) {
131
125
document . getElementById ( "main" ) . addEventListener ( 'click' , e => {
132
- e . preventDefault ( ) ;
126
+ e . preventDefault ( )
133
127
if ( e . target . matches ( '#runlots' ) ) {
134
- this . runLots ( e ) ;
128
+ this . runLots ( )
135
129
} else if ( e . target . matches ( '#run' ) ) {
136
- this . run ( e ) ;
130
+ this . run ( )
137
131
} else if ( e . target . matches ( '#add' ) ) {
138
- this . add ( e ) ;
132
+ this . add ( )
139
133
} else if ( e . target . matches ( '#update' ) ) {
140
- this . update ( ) ;
134
+ this . update ( )
141
135
} else if ( e . target . matches ( '#clear' ) ) {
142
- this . clear ( ) ;
136
+ this . clear ( )
143
137
} else if ( e . target . matches ( '#swaprows' ) ) {
144
- this . swapRows ( ) ;
138
+ this . swapRows ( )
145
139
}
146
140
} )
147
141
}
0 commit comments