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,21 +33,21 @@ 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 ) {
@@ -59,35 +57,47 @@ Doo.define(
59
57
}
60
58
return data
61
59
}
60
+ getIndex ( row ) {
61
+ let idx = this . data . rows . findIndex ( ( item , i ) => {
62
+ if ( item . id === row . key ) {
63
+ return i
64
+ }
65
+ } )
66
+ return idx
67
+ }
62
68
63
69
delete ( elem ) {
64
70
let row = this . getParentRow ( elem )
65
71
if ( row ) {
66
72
this . tbody . removeChild ( row )
67
- this . data . rows [ row . getAttribute ( 'key' ) ] = undefined
73
+ let idx = this . getIndex ( row )
74
+ if ( idx !== undefined ) {
75
+ this . data . rows . splice ( idx , 1 )
76
+ }
77
+
68
78
}
69
79
}
70
80
71
81
run ( ) {
82
+ this . clear ( )
72
83
this . data . rows = this . buildData ( )
73
- this . tbody . textContent = ''
74
84
this . renderTable ( )
85
+
75
86
}
76
87
77
88
add ( ) {
78
- let startRow = this . data . rows . length
79
89
this . data . rows = this . data . rows . concat ( this . buildData ( ) )
80
- this . appendData ( this . tbody , startRow )
90
+ this . renderTable ( this . data . rows )
81
91
}
82
92
83
93
runLots ( ) {
94
+
95
+ this . clear ( )
84
96
this . data . rows = this . buildData ( 10000 )
85
- this . tbody . textContent = ''
86
97
this . renderTable ( )
87
98
}
88
99
89
100
update ( ) {
90
- let tr = this . tbody . querySelectorAll ( 'tr' )
91
101
for ( let i = 0 , len = this . data . rows . length ; i < len ; i += 10 ) {
92
102
this . tbody . childNodes [ i ] . childNodes [ 1 ] . childNodes [ 0 ] . innerText = this . data . rows [ i ] . label += ' !!!'
93
103
}
@@ -97,12 +107,16 @@ Doo.define(
97
107
if ( this . selectedRow ) {
98
108
this . selectedRow . classList . remove ( 'danger' )
99
109
this . selectedRow = undefined
100
- // return should toggle IMO
101
110
}
102
- let row = this . getParentRow ( elem )
111
+ this . toggleSelect ( this . getParentRow ( elem ) )
112
+ }
113
+
114
+ toggleSelect ( row ) {
103
115
if ( row ) {
104
116
row . classList . toggle ( 'danger' )
105
- this . selectedRow = row
117
+ if ( row . classList . contains ( 'danger' ) ) {
118
+ this . selectedRow = row
119
+ }
106
120
}
107
121
}
108
122
@@ -128,21 +142,21 @@ Doo.define(
128
142
129
143
addEventListeners ( ) {
130
144
document . getElementById ( "main" ) . addEventListener ( 'click' , e => {
131
- e . preventDefault ( ) ;
145
+ e . preventDefault ( )
132
146
if ( e . target . matches ( '#runlots' ) ) {
133
- this . runLots ( e ) ;
147
+ this . runLots ( )
134
148
} else if ( e . target . matches ( '#run' ) ) {
135
- this . run ( e ) ;
149
+ this . run ( )
136
150
} else if ( e . target . matches ( '#add' ) ) {
137
- this . add ( e ) ;
151
+ this . add ( )
138
152
} else if ( e . target . matches ( '#update' ) ) {
139
- this . update ( ) ;
153
+ this . update ( )
140
154
} else if ( e . target . matches ( '#clear' ) ) {
141
- this . clear ( ) ;
155
+ this . clear ( )
142
156
} else if ( e . target . matches ( '#swaprows' ) ) {
143
- this . swapRows ( ) ;
157
+ this . swapRows ( )
144
158
}
145
159
} )
146
- }
160
+ }
147
161
}
148
162
)
0 commit comments