1
+ 'use strict' ;
2
+
3
+
4
+ const startTime = { }
5
+ let tot = [ ]
6
+ const start = function ( name ) {
7
+ // document.querySelector('#time').blur()
8
+
9
+ tot . push ( new Date ( ) . getTime ( ) )
10
+ if ( ! startTime [ name ] ) {
11
+ startTime [ name ] = [ new Date ( ) . getTime ( ) ]
12
+ }
13
+ } ;
14
+ const stop = function ( name ) {
15
+ if ( startTime [ name ] ) {
16
+ startTime [ name ] . push ( new Date ( ) . getTime ( ) )
17
+ console . log ( 'DooHTML' , name , 'took:' , startTime [ name ] [ 1 ] - startTime [ name ] [ 0 ] ) ;
18
+ if ( tot . length === 2 ) {
19
+ console . log ( 'DooHTML Tot:' , startTime [ name ] [ 1 ] - tot [ 0 ] )
20
+ tot = [ ]
21
+ }
22
+ startTime [ name ] = undefined
23
+ }
24
+ } ;
25
+
26
+
27
+ const _random = ( ( max ) => {
28
+ return Math . round ( Math . random ( ) * 1000 ) % max ;
29
+ } )
30
+
31
+ 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" ] ;
32
+ const colours = [ "red" , "yellow" , "blue" , "green" , "pink" , "brown" , "purple" , "brown" , "white" , "black" , "orange" ] ;
33
+ const nouns = [ "table" , "chair" , "house" , "bbq" , "desk" , "car" , "pony" , "cookie" , "sandwich" , "burger" , "pizza" , "mouse" , "keyboard" ] ;
34
+
35
+ const lenA = adjectives . length , lenB = colours . length , lenC = nouns . length
36
+
37
+ Doo . define (
38
+ class Main extends Doo {
39
+ constructor ( ) {
40
+ super ( 100 )
41
+ this . scrollTarget = '.table'
42
+ this . defaultDataSet = 'rows'
43
+ this . ID = 1
44
+ this . data = {
45
+ [ this . defaultDataSet ] : [ ]
46
+ }
47
+ this . add = this . add . bind ( this )
48
+ this . run = this . run . bind ( this )
49
+ this . runLots = this . runLots . bind ( this )
50
+ this . update = this . update . bind ( this )
51
+ this . clear = this . clear . bind ( this )
52
+ this . swaprows = this . swapRows . bind ( this )
53
+ this . addEventListeners ( )
54
+ this . selectedRow = undefined
55
+ document . querySelector ( ".ver" ) . innerHTML += ` ${ Doo . version } (non-keyed)`
56
+ document . title += ` ${ Doo . version } (non-keyed)`
57
+ }
58
+
59
+ async dooAfterRender ( ) {
60
+ this . tbody = this . shadow . querySelector ( '#tbody' )
61
+ this . shadow . querySelector ( this . scrollTarget ) . addEventListener ( 'click' , e => {
62
+ e . preventDefault ( ) ;
63
+ if ( e . target . parentElement . matches ( '.remove' ) ) {
64
+ this . delete ( e . target . parentElement ) ;
65
+ } else if ( e . target . tagName === 'A' ) {
66
+ this . select ( e . target ) ;
67
+ }
68
+ } ) ;
69
+ }
70
+
71
+ getParentRow ( elem ) {
72
+ while ( elem ) {
73
+ if ( elem . tagName === "TR" ) { return elem }
74
+ elem = elem . parentNode ;
75
+ }
76
+ return undefined ;
77
+ }
78
+
79
+ buildData ( count = 1000 ) {
80
+ const data = [ ] ;
81
+ for ( let i = 0 ; i < count ; i ++ ) {
82
+ data . push ( { id : this . ID ++ , label : adjectives [ _random ( lenA ) ] + " " + colours [ _random ( lenB ) ] + " " + nouns [ _random ( lenC ) ] } )
83
+ }
84
+ return data
85
+ }
86
+
87
+ delete ( elem ) {
88
+ let row = this . getParentRow ( elem )
89
+ if ( row ) {
90
+ this . tbody . removeChild ( row )
91
+ this . data . rows [ row . getAttribute ( 'key' ) ] = undefined
92
+ }
93
+ }
94
+
95
+ run ( ) {
96
+ this . data . rows = this . buildData ( )
97
+ this . tbody . textContent = ''
98
+ this . renderTable ( )
99
+ }
100
+
101
+ run ( e ) {
102
+ start ( 'buildData' )
103
+ this . data . rows = this . buildData ( )
104
+ stop ( 'buildData' )
105
+ start ( 'run' )
106
+ this . tbody . textContent = ''
107
+ this . renderTable ( )
108
+ e . target . blur ( )
109
+
110
+ stop ( 'run' )
111
+ }
112
+
113
+
114
+
115
+ add ( ) {
116
+ let startRow = this . data . rows . length
117
+ this . data . rows = this . data . rows . concat ( this . buildData ( ) )
118
+ this . appendData ( this . tbody , startRow )
119
+ }
120
+
121
+ runLots ( ) {
122
+ this . data . rows = this . buildData ( 10000 )
123
+ this . tbody . textContent = ''
124
+ this . renderTable ( )
125
+ }
126
+ runLots ( e ) {
127
+ start ( 'buildData' )
128
+ this . data . rows = this . buildData ( 10000 )
129
+ stop ( 'buildData' )
130
+ start ( 'runLots' )
131
+ this . tbody . textContent = ''
132
+ this . renderTable ( )
133
+ e . target . blur ( )
134
+
135
+ stop ( 'runLots' )
136
+ }
137
+
138
+ run ( e ) {
139
+ start ( 'buildData' )
140
+ this . data . rows = this . buildData ( )
141
+ stop ( 'buildData' )
142
+ start ( 'run' )
143
+ this . tbody . textContent = ''
144
+ this . renderTable ( )
145
+ e . target . blur ( )
146
+
147
+ stop ( 'run' )
148
+ }
149
+
150
+ add ( e ) {
151
+ start ( 'append' )
152
+ let startRow = this . data . rows . length
153
+ this . data . rows = this . data . rows . concat ( this . buildData ( ) )
154
+ stop ( 'append' )
155
+ start ( 'runAppend' )
156
+ this . appendData ( this . tbody , startRow , 1000 )
157
+ e . target . blur ( )
158
+ stop ( 'runAppend' )
159
+ }
160
+
161
+ update ( ) {
162
+ let tr = this . tbody . querySelectorAll ( 'tr' )
163
+ for ( let i = 0 , len = this . data . rows . length ; i < len ; i += 10 ) {
164
+ this . data . rows [ i ] . label += ' !!!' ;
165
+ tr [ i ] . childNodes [ 1 ] . childNodes [ 0 ] . innerHTML = this . data . rows [ i ] . label
166
+ }
167
+ }
168
+
169
+ select ( elem ) {
170
+ if ( this . selectedRow ) {
171
+ this . selectedRow . classList . remove ( 'danger' )
172
+ this . selectedRow = undefined
173
+ // return should toggle IMO
174
+ }
175
+ let row = this . getParentRow ( elem )
176
+ if ( row ) {
177
+ row . classList . toggle ( 'danger' )
178
+ this . selectedRow = row
179
+ }
180
+ }
181
+
182
+ clear ( ) {
183
+ this . data . rows = [ ]
184
+ this . tbody . innerHTML = ''
185
+ }
186
+
187
+ swapRows ( ) {
188
+ if ( this . data . rows . length > 10 ) {
189
+ let node1 = this . tbody . childNodes [ 1 ]
190
+ let node2 = this . tbody . childNodes [ 998 ]
191
+
192
+ let row1 = this . data . rows [ 1 ] ;
193
+ this . data . rows [ 1 ] = this . data . rows [ 998 ] ;
194
+ this . data . rows [ 998 ] = row1
195
+
196
+ this . tbody . insertBefore ( node2 , node1 )
197
+ this . tbody . insertBefore ( node1 , this . tbody . childNodes [ 999 ] )
198
+
199
+ }
200
+ }
201
+
202
+ addEventListeners ( ) {
203
+ document . getElementById ( "main" ) . addEventListener ( 'click' , e => {
204
+ e . preventDefault ( ) ;
205
+ if ( e . target . matches ( '#runlots' ) ) {
206
+ this . runLots ( e ) ;
207
+ } else if ( e . target . matches ( '#run' ) ) {
208
+ this . run ( e ) ;
209
+ } else if ( e . target . matches ( '#add' ) ) {
210
+ this . add ( e ) ;
211
+ } else if ( e . target . matches ( '#update' ) ) {
212
+ this . update ( ) ;
213
+ } else if ( e . target . matches ( '#clear' ) ) {
214
+ this . clear ( ) ;
215
+ } else if ( e . target . matches ( '#swaprows' ) ) {
216
+ this . swapRows ( ) ;
217
+ }
218
+ } )
219
+ }
220
+ }
221
+ )
0 commit comments