@@ -8,11 +8,12 @@ const nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie"
8
8
9
9
const lenA = adjectives . length , lenB = colours . length , lenC = nouns . length
10
10
11
+ const DEFAULT_SIZE = 1000
12
+ const SWAP_ROW = 998
11
13
Doo . define (
12
14
class Main extends Doo {
13
15
constructor ( ) {
14
16
super ( 100 )
15
- this . scrollTarget = '.table'
16
17
this . defaultDataSet = 'rows'
17
18
this . ID = 1
18
19
this . data = {
@@ -32,7 +33,7 @@ Doo.define(
32
33
33
34
async dooAfterRender ( ) {
34
35
this . tbody = this . shadow . querySelector ( '#tbody' )
35
- this . shadow . querySelector ( this . scrollTarget ) . addEventListener ( 'click' , e => {
36
+ this . tbody . addEventListener ( 'click' , e => {
36
37
e . preventDefault ( )
37
38
if ( e . target . parentElement . matches ( '.remove' ) ) {
38
39
this . delete ( e . target . parentElement )
@@ -50,23 +51,13 @@ Doo.define(
50
51
return undefined
51
52
}
52
53
53
- buildData ( count = 1000 ) {
54
+ buildData ( count = DEFAULT_SIZE ) {
54
55
const data = [ ]
55
56
for ( let i = 0 ; i < count ; i ++ ) {
56
57
data . push ( { id : this . ID ++ , label : adjectives [ _random ( lenA ) ] + " " + colours [ _random ( lenB ) ] + " " + nouns [ _random ( lenC ) ] } )
57
58
}
58
59
return data
59
60
}
60
-
61
- getIndex ( row ) {
62
- let idx = this . data . rows . findIndex ( ( item , i ) => {
63
- if ( item . id === row . key ) {
64
- return i
65
- }
66
- } )
67
- return idx
68
- }
69
-
70
61
getIndex ( row ) {
71
62
let idx = this . data . rows . findIndex ( ( item , i ) => {
72
63
if ( item . id === row . key ) {
@@ -116,37 +107,36 @@ Doo.define(
116
107
this . selectedRow . classList . remove ( 'danger' )
117
108
this . selectedRow = undefined
118
109
}
119
- this . toggleSelect ( this . getParentRow ( elem ) )
110
+ if ( elem ) {
111
+ this . toggleSelect ( this . getParentRow ( elem ) )
112
+ }
120
113
}
121
114
122
- toggleSelect ( elem ) {
123
- let row = this . getParentRow ( elem )
115
+ toggleSelect ( row ) {
124
116
if ( row ) {
125
117
row . classList . toggle ( 'danger' )
126
118
if ( row . classList . contains ( 'danger' ) ) {
127
119
this . selectedRow = row
128
120
}
129
121
}
130
- }
122
+ }
131
123
132
124
clear ( ) {
125
+ this . tbody . textContent = null
133
126
this . data . rows = [ ]
134
- this . tbody . textContent = ''
135
127
}
136
128
137
129
swapRows ( ) {
138
- if ( this . data . rows . length > 998 ) {
130
+ if ( this . data . rows . length > SWAP_ROW ) {
139
131
let node1 = this . tbody . firstChild . nextSibling ,
140
- node2 = node1 . nextSibling ,
141
- node998 = this . tbody . childNodes [ 998 ] ,
142
- node999 = node998 . nextSibling ,
132
+ swapRow = this . tbody . childNodes [ SWAP_ROW ] ,
133
+ node999 = swapRow . nextSibling ,
143
134
row1 = this . data . rows [ 1 ]
144
135
145
- this . data . rows [ 1 ] = this . data . rows [ 998 ] ;
146
- this . data . rows [ 998 ] = row1
136
+ this . data . rows [ 1 ] = this . data . rows [ SWAP_ROW ] ;
137
+ this . data . rows [ SWAP_ROW ] = row1
147
138
148
- this . tbody . insertBefore ( node998 , node2 )
149
- this . tbody . insertBefore ( node1 , node999 )
139
+ this . tbody . insertBefore ( node1 . parentNode . replaceChild ( swapRow , node1 ) , node999 )
150
140
}
151
141
}
152
142
@@ -168,5 +158,7 @@ Doo.define(
168
158
}
169
159
} )
170
160
}
171
- }
172
- )
161
+ async connectedCallback ( ) {
162
+ super . connectedCallback ( )
163
+ }
164
+ } )
0 commit comments