@@ -7,13 +7,12 @@ const colours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "b
7
7
const nouns = [ "table" , "chair" , "house" , "bbq" , "desk" , "car" , "pony" , "cookie" , "sandwich" , "burger" , "pizza" , "mouse" , "keyboard" ]
8
8
9
9
const lenA = adjectives . length , lenB = colours . length , lenC = nouns . length
10
- const DEFAULT_SIZE = 1000
11
- const CHILD_1 = 1
12
- const CHILD_998 = 998
10
+
13
11
Doo . define (
14
12
class Main extends Doo {
15
13
constructor ( ) {
16
14
super ( 100 )
15
+ this . scrollTarget = '.table'
17
16
this . defaultDataSet = 'rows'
18
17
this . ID = 1
19
18
this . data = {
@@ -24,7 +23,7 @@ Doo.define(
24
23
this . runLots = this . runLots . bind ( this )
25
24
this . update = this . update . bind ( this )
26
25
this . clear = this . clear . bind ( this )
27
- this . swapRows = this . swapRows . bind ( this )
26
+ this . swaprows = this . swapRows . bind ( this )
28
27
this . addEventListeners ( )
29
28
this . selectedRow = undefined
30
29
document . querySelector ( ".ver" ) . innerHTML += ` ${ Doo . version } (non-keyed)`
@@ -33,7 +32,7 @@ Doo.define(
33
32
34
33
async dooAfterRender ( ) {
35
34
this . tbody = this . shadow . querySelector ( '#tbody' )
36
- this . tbody . addEventListener ( 'click' , e => {
35
+ this . shadow . querySelector ( this . scrollTarget ) . addEventListener ( 'click' , e => {
37
36
e . preventDefault ( )
38
37
if ( e . target . parentElement . matches ( '.remove' ) ) {
39
38
this . delete ( e . target . parentElement )
@@ -51,10 +50,10 @@ Doo.define(
51
50
return undefined
52
51
}
53
52
54
- buildData ( count = DEFAULT_SIZE ) {
55
- const data = new Array ( count )
53
+ buildData ( count = 1000 ) {
54
+ const data = [ ] ;
56
55
for ( let i = 0 ; i < count ; i ++ ) {
57
- data [ i ] = { id : this . ID ++ , label : ` ${ adjectives [ _random ( lenA ) ] } ${ colours [ _random ( lenB ) ] } ${ nouns [ _random ( lenC ) ] } ` }
56
+ data . push ( { id : this . ID ++ , label : adjectives [ _random ( lenA ) ] + " " + colours [ _random ( lenB ) ] + " " + nouns [ _random ( lenC ) ] } )
58
57
}
59
58
return data
60
59
}
@@ -63,13 +62,13 @@ Doo.define(
63
62
let row = this . getParentRow ( elem )
64
63
if ( row ) {
65
64
this . tbody . removeChild ( row )
66
- this . data . rows . splice ( row . rowIndex , 1 )
65
+ this . data . rows [ row . getAttribute ( 'key' ) ] = undefined
67
66
}
68
67
}
69
68
70
69
run ( ) {
71
70
this . data . rows = this . buildData ( )
72
- if ( this . tbody . childNodes . length >= this . data . rows . length ) {
71
+ if ( this . tbody . childNodes . length > this . data . rows . length ) {
73
72
this . tbody . textContent = ''
74
73
}
75
74
this . renderTable ( )
@@ -114,40 +113,49 @@ Doo.define(
114
113
isRowSelected ( elem ) {
115
114
return elem . classList . contains ( 'danger' )
116
115
}
117
-
118
116
swapRows ( ) {
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 ]
117
+ const A = 1 , B = 998
118
+ if ( this . data . rows . length > B ) {
119
+
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
124
126
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 )
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
+ }
129
138
}
130
- }
139
+ }
140
+
131
141
132
142
addEventListeners ( ) {
133
- const actions = {
134
- 'run' : this . run ,
135
- 'runlots' : this . runLots ,
136
- 'add' : this . add ,
137
- 'update' : this . update ,
138
- 'clear' : this . clear ,
139
- 'swaprows' : this . swapRows ,
140
- runAction : ( e ) => {
141
- e . preventDefault ( )
142
- if ( actions [ e . target . id ] ) {
143
- actions [ e . target . id ] ( )
144
- }
143
+ document . getElementById ( "main" ) . addEventListener ( 'click' , e => {
144
+ e . preventDefault ( )
145
+ if ( e . target . matches ( '#runlots' ) ) {
146
+ this . runLots ( )
147
+ } else if ( e . target . matches ( '#run' ) ) {
148
+ this . run ( )
149
+ } else if ( e . target . matches ( '#add' ) ) {
150
+ this . add ( )
151
+ } else if ( e . target . matches ( '#update' ) ) {
152
+ this . update ( )
153
+ } else if ( e . target . matches ( '#clear' ) ) {
154
+ this . clear ( )
155
+ } else if ( e . target . matches ( '#swaprows' ) ) {
156
+ this . swapRows ( )
145
157
}
146
- }
147
-
148
- document . getElementById ( "main" ) . addEventListener ( 'click' , e => actions . runAction ( e ) )
158
+ } )
149
159
}
150
- async connectedCallback ( ) {
151
- super . connectedCallback ( )
152
- }
153
- } )
160
+ }
161
+ )
0 commit comments