@@ -8,11 +8,13 @@ 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 CHILD_1 = 1
13
+ const CHILD_998 = 998
11
14
Doo . define (
12
15
class Main extends Doo {
13
16
constructor ( ) {
14
17
super ( 100 )
15
- this . scrollTarget = '.table'
16
18
this . defaultDataSet = 'rows'
17
19
this . ID = 1
18
20
this . data = {
@@ -32,7 +34,7 @@ Doo.define(
32
34
33
35
async dooAfterRender ( ) {
34
36
this . tbody = this . shadow . querySelector ( '#tbody' )
35
- this . shadow . querySelector ( this . scrollTarget ) . addEventListener ( 'click' , e => {
37
+ this . tbody . addEventListener ( 'click' , e => {
36
38
e . preventDefault ( )
37
39
if ( e . target . parentElement . matches ( '.remove' ) ) {
38
40
this . delete ( e . target . parentElement )
@@ -50,8 +52,8 @@ Doo.define(
50
52
return undefined
51
53
}
52
54
53
- buildData ( count = 1000 ) {
54
- const data = [ ] ;
55
+ buildData ( count = DEFAULT_SIZE ) {
56
+ const data = [ ]
55
57
for ( let i = 0 ; i < count ; i ++ ) {
56
58
data . push ( { id : this . ID ++ , label : adjectives [ _random ( lenA ) ] + " " + colours [ _random ( lenB ) ] + " " + nouns [ _random ( lenC ) ] } )
57
59
}
@@ -62,7 +64,7 @@ Doo.define(
62
64
let row = this . getParentRow ( elem )
63
65
if ( row ) {
64
66
this . tbody . removeChild ( row )
65
- this . data . rows [ row . getAttribute ( 'key' ) ] = undefined
67
+ this . data . rows . splice ( row . rowIndex , 1 )
66
68
}
67
69
}
68
70
@@ -114,27 +116,16 @@ Doo.define(
114
116
return elem . classList . contains ( 'danger' )
115
117
}
116
118
swapRows ( ) {
117
- const A = 1 , B = 998
118
- if ( this . data . rows . length > B ) {
119
+ if ( this . data . rows . length > CHILD_998 ) {
120
+ let node1 = this . tbody . childNodes [ CHILD_1 ] ,
121
+ swapRow = this . tbody . childNodes [ CHILD_998 ] ,
122
+ node999 = swapRow . nextSibling ,
123
+ row1 = this . data . rows [ CHILD_1 ]
119
124
120
- let a = this . tbody . childNodes [ A ] ,
121
- b = this . tbody . childNodes [ B ] ,
122
- row1 = this . data . rows [ 1 ]
123
-
124
- this . data . rows [ A ] = this . data . rows [ B ] ;
125
- this . data . rows [ B ] = row1
126
-
127
- const selected1 = this . isRowSelected ( a )
128
- const selected2 = this . isRowSelected ( b )
129
- this . updateRow ( A , this . data . rows , this . tbody )
130
- this . updateRow ( B , this . data . rows , this . tbody )
131
-
132
- if ( selected1 ) {
133
- this . select ( this . tbody . childNodes [ B ] )
134
- }
135
- if ( selected2 ) {
136
- this . select ( this . tbody . childNodes [ A ] )
137
- }
125
+ this . data . rows [ CHILD_1 ] = this . data . rows [ CHILD_998 ] ;
126
+ this . data . rows [ CHILD_998 ] = row1
127
+ this . tbody . insertBefore ( swapRow , node1 )
128
+ this . tbody . insertBefore ( node1 , node999 )
138
129
}
139
130
}
140
131
@@ -157,5 +148,7 @@ Doo.define(
157
148
}
158
149
} )
159
150
}
160
- }
161
- )
151
+ async connectedCallback ( ) {
152
+ super . connectedCallback ( )
153
+ }
154
+ } )
0 commit comments