1
+ 'use strict' ;
2
+
3
+ const _random = max => Math . random ( ) * max | 0 ;
4
+
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" ] ;
8
+
9
+ const lenA = adjectives . length , lenB = colours . length , lenC = nouns . length
10
+
11
+ import Timer from './doo.timer.js'
12
+
13
+ Doo . define (
14
+ class Main extends Doo {
15
+ constructor ( ) {
16
+ super ( 100 )
17
+ this . scrollTarget = '.table'
18
+ this . defaultDataSet = 'rows'
19
+ this . ID = 1
20
+ this . data = {
21
+ [ this . defaultDataSet ] : [ ]
22
+ }
23
+ this . add = this . add . bind ( this )
24
+ this . run = this . run . bind ( this )
25
+ this . runLots = this . runLots . bind ( this )
26
+ this . update = this . update . bind ( this )
27
+ this . clear = this . clear . bind ( this )
28
+ this . swaprows = this . swapRows . bind ( this )
29
+ this . addEventListeners ( )
30
+ this . selectedRow = undefined
31
+ document . querySelector ( ".ver" ) . innerHTML += ` ${ Doo . version } (non-keyed)`
32
+ document . title += ` ${ Doo . version } (non-keyed)`
33
+ }
34
+
35
+ async dooAfterRender ( ) {
36
+ this . tbody = this . shadow . querySelector ( '#tbody' )
37
+ this . shadow . querySelector ( this . scrollTarget ) . addEventListener ( 'click' , e => {
38
+ e . preventDefault ( ) ;
39
+ if ( e . target . parentElement . matches ( '.remove' ) ) {
40
+ this . delete ( e . target . parentElement ) ;
41
+ } else if ( e . target . tagName === 'A' ) {
42
+ this . select ( e . target ) ;
43
+ }
44
+ } ) ;
45
+ }
46
+
47
+ getParentRow ( elem ) {
48
+ while ( elem ) {
49
+ if ( elem . tagName === "TR" ) { return elem }
50
+ elem = elem . parentNode ;
51
+ }
52
+ return undefined ;
53
+ }
54
+
55
+ buildData ( count = 1000 ) {
56
+ const data = [ ] ;
57
+ for ( let i = 0 ; i < count ; i ++ ) {
58
+ data . push ( { id : this . ID ++ , label : adjectives [ _random ( lenA ) ] + " " + colours [ _random ( lenB ) ] + " " + nouns [ _random ( lenC ) ] } )
59
+ }
60
+ return data
61
+ }
62
+
63
+ delete ( elem ) {
64
+ let row = this . getParentRow ( elem )
65
+ if ( row ) {
66
+ this . tbody . removeChild ( row )
67
+ this . data . rows [ row . getAttribute ( 'key' ) ] = undefined
68
+ }
69
+ }
70
+
71
+ run ( ) {
72
+ this . data . rows = this . buildData ( )
73
+ if ( this . tbody . childNodes . length > this . data . rows . length ) {
74
+ this . tbody . textContent = ''
75
+ }
76
+ this . renderTable ( )
77
+ }
78
+
79
+ run ( e ) {
80
+ Timer . start ( 'tot' )
81
+ this . data . rows = this . buildData ( )
82
+ if ( this . tbody . childNodes . length > this . data . rows . length ) {
83
+ this . tbody . textContent = ''
84
+ }
85
+ this . renderTable ( )
86
+ // e.target.blur()
87
+ Timer . stop ( 'tot' )
88
+ }
89
+
90
+ add ( e ) {
91
+ Timer . start ( 'tot' )
92
+ let startRow = this . data . rows . length
93
+ this . data . rows = this . data . rows . concat ( this . buildData ( ) )
94
+ this . renderTable ( this . data . rows , startRow )
95
+ Timer . stop ( 'tot' )
96
+
97
+ }
98
+
99
+ runLots ( e ) {
100
+ this . data . rows = this . buildData ( 10000 )
101
+ if ( this . tbody . childNodes . length > this . data . rows . length ) {
102
+ this . tbody . textContent = ''
103
+ }
104
+ this . renderTable ( )
105
+ }
106
+
107
+ runLots ( e ) {
108
+ Timer . start ( 'tot' )
109
+ this . data . rows = this . buildData ( 10000 )
110
+ if ( this . tbody . childNodes . length > this . data . rows . length ) {
111
+ this . tbody . textContent = ''
112
+ }
113
+ this . renderTable ( )
114
+ // e.target.blur()
115
+ Timer . stop ( 'tot' )
116
+ }
117
+
118
+ update ( e ) {
119
+ for ( let i = 0 , len = this . data . rows . length ; i < len ; i += 10 ) {
120
+ this . tbody . childNodes [ i ] . childNodes [ 1 ] . childNodes [ 0 ] . innerText = this . data . rows [ i ] . label += ' !!!'
121
+ }
122
+ }
123
+
124
+ select ( elem ) {
125
+ if ( this . selectedRow ) {
126
+ this . selectedRow . classList . remove ( 'danger' )
127
+ this . selectedRow = undefined
128
+ }
129
+ let row = this . getParentRow ( elem )
130
+ if ( row ) {
131
+ row . classList . toggle ( 'danger' )
132
+ this . selectedRow = row
133
+ }
134
+ }
135
+
136
+ clear ( ) {
137
+ this . data . rows = [ ]
138
+ this . tbody . textContent = ''
139
+ }
140
+
141
+ swapRows ( ) {
142
+ if ( this . data . rows . length > 10 ) {
143
+ let node1 = this . tbody . childNodes [ 1 ]
144
+ let node2 = this . tbody . childNodes [ 998 ]
145
+
146
+ let row1 = this . data . rows [ 1 ] ;
147
+ this . data . rows [ 1 ] = this . data . rows [ 998 ] ;
148
+ this . data . rows [ 998 ] = row1
149
+
150
+ this . tbody . insertBefore ( node2 , node1 )
151
+ this . tbody . insertBefore ( node1 , this . tbody . childNodes [ 999 ] )
152
+
153
+ }
154
+ }
155
+
156
+ addEventListeners ( ) {
157
+ document . getElementById ( "main" ) . addEventListener ( 'click' , e => {
158
+ e . preventDefault ( ) ;
159
+ if ( e . target . matches ( '#runlots' ) ) {
160
+ this . runLots ( e ) ;
161
+ } else if ( e . target . matches ( '#run' ) ) {
162
+ this . run ( e ) ;
163
+ } else if ( e . target . matches ( '#add' ) ) {
164
+ this . add ( e ) ;
165
+ } else if ( e . target . matches ( '#update' ) ) {
166
+ this . update ( e ) ;
167
+ } else if ( e . target . matches ( '#clear' ) ) {
168
+ this . clear ( ) ;
169
+ } else if ( e . target . matches ( '#swaprows' ) ) {
170
+ this . swapRows ( e ) ;
171
+ }
172
+ } )
173
+ }
174
+ }
175
+ )
0 commit comments