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