@@ -6,23 +6,40 @@ let adjectives = ["pretty", "large", "big", "small", "tall", "short", "long", "h
6
6
let colours = [ "red" , "yellow" , "blue" , "green" , "pink" , "brown" , "purple" , "brown" , "white" , "black" , "orange" ] ;
7
7
let nouns = [ "table" , "chair" , "house" , "bbq" , "desk" , "car" , "pony" , "cookie" , "sandwich" , "burger" , "pizza" , "mouse" , "keyboard" ] ;
8
8
9
- function _random ( max : number )
10
- {
11
- return Math . round ( Math . random ( ) * 1000 ) % max ;
9
+ type Row = {
10
+ id : number ;
11
+ label : string ;
12
+ labelTrigger : mim . ITrigger < string > ;
13
+ selectedTrigger : mim . ITrigger < string > ;
12
14
}
13
15
16
+
17
+
14
18
let nextID = 1 ;
15
19
16
- function buildRows ( main : Main , count : number ) : Row [ ]
20
+ function buildRows ( /* main: Main,*/ count : number ) : Row [ ]
17
21
{
18
22
let rows = new Array < Row > ( count ) ;
23
+ let label : string ;
19
24
for ( let i = 0 ; i < count ; i ++ )
20
- rows [ i ] = new Row ( main , nextID ++ ,
21
- `${ adjectives [ _random ( adjectives . length ) ] } ${ colours [ _random ( colours . length ) ] } ${ nouns [ _random ( nouns . length ) ] } ` ) ;
25
+ {
26
+ label = `${ adjectives [ rand ( adjectives . length ) ] } ${ colours [ rand ( colours . length ) ] } ${ nouns [ rand ( nouns . length ) ] } ` ;
27
+ rows [ i ] = {
28
+ id : nextID ++ ,
29
+ label,
30
+ labelTrigger : mim . createTrigger ( label ) ,
31
+ selectedTrigger : mim . createTrigger ( )
32
+ } ;
33
+ }
22
34
23
35
return rows ;
24
36
}
25
37
38
+ function rand ( max : number )
39
+ {
40
+ return Math . round ( Math . random ( ) * 1000 ) % max ;
41
+ }
42
+
26
43
27
44
28
45
function Button ( props : mim . IHtmlButtonElementProps , children : any [ ] ) : any
@@ -36,9 +53,9 @@ function Button( props: mim.IHtmlButtonElementProps, children: any[]): any
36
53
37
54
class Main extends mim . Component
38
55
{
39
- private rows : Row [ ] = null ;
56
+ @ mim . trigger ( 0 ) private rows : Row [ ] = null ;
40
57
public selectedRow : Row = undefined ;
41
- private vnTBody : mim . IElmVN < HTMLElement > ;
58
+ @ mim . ref private vnTBody : mim . IElmVN < HTMLElement > ;
42
59
43
60
public render ( )
44
61
{
@@ -61,49 +78,66 @@ class Main extends mim.Component
61
78
</ div >
62
79
</ div >
63
80
< table class = "table table-hover table-striped test-data" >
64
- < tbody vnref = { ( r ) => this . vnTBody = r } updateStrategy = { { disableKeyedNodeRecycling : true } } >
65
- { this . rows }
66
- </ tbody >
81
+ { this . renderRows }
67
82
</ table >
68
83
< span class = "preloadicon glyphicon glyphicon-remove" aria-hidden = "true" > </ span >
69
84
</ div > ) ;
70
85
}
71
86
87
+ private renderRows ( ) : any
88
+ {
89
+ return < tbody vnref = { this . vnTBody } updateStrategy = { { disableKeyedNodeRecycling : true } } >
90
+ { this . rows ?. map ( row =>
91
+ < tr class = { row . selectedTrigger } key = { row } >
92
+ < td class = "col-md-1" > { row . id } </ td >
93
+ < td class = "col-md-4" > < a click = { [ this . onSelectRowClicked , row ] } > { row . labelTrigger } </ a > </ td >
94
+ < td class = "col-md-1" >
95
+ < a click = { [ this . onDeleteRowClicked , row ] } >
96
+ < span class = "glyphicon glyphicon-remove" aria-hidden = "true" />
97
+ </ a >
98
+ </ td >
99
+ < td class = "col-md-6" />
100
+ </ tr >
101
+ ) }
102
+ </ tbody >
103
+ }
104
+
72
105
private onCreate1000 ( )
73
106
{
74
- this . rows = buildRows ( this , 1000 ) ;
107
+ this . rows = buildRows ( 1000 ) ;
75
108
this . selectedRow = undefined ;
76
- this . vnTBody . setChildren ( this . rows ) ;
77
109
}
78
110
79
111
private onAppend1000 ( )
80
112
{
81
- let newRows = buildRows ( this , 1000 ) ;
113
+ let newRows = buildRows ( 1000 ) ;
82
114
this . rows = this . rows ? this . rows . concat ( newRows ) : newRows ;
83
- this . vnTBody . growChildren ( undefined , newRows ) ;
84
115
}
85
116
86
117
private onUpdateEvery10th ( )
87
118
{
88
119
if ( ! this . rows )
89
120
return ;
90
121
122
+ let row : Row ;
91
123
for ( let i = 0 ; i < this . rows . length ; i += 10 )
92
- this . rows [ i ] . updateLabel ( )
124
+ {
125
+ row = this . rows [ i ] ;
126
+ row . labelTrigger . set ( row . label += " !!!" )
127
+ }
93
128
}
94
129
130
+
95
131
private onCreate10000 ( )
96
132
{
97
- this . rows = buildRows ( this , 10000 ) ;
133
+ this . rows = buildRows ( 10000 ) ;
98
134
this . selectedRow = undefined ;
99
- this . vnTBody . setChildren ( this . rows ) ;
100
135
}
101
136
102
137
private onClear ( )
103
138
{
104
139
this . rows = null ;
105
140
this . selectedRow = undefined ;
106
- this . vnTBody . setChildren ( null ) ;
107
141
}
108
142
109
143
private onSwapRows ( )
@@ -117,84 +151,29 @@ class Main extends mim.Component
117
151
}
118
152
}
119
153
120
- public onSelectRowClicked ( rowToSelect : Row )
154
+ public onSelectRowClicked = ( e : MouseEvent , rowToSelect : Row ) : void =>
121
155
{
122
156
if ( rowToSelect === this . selectedRow )
123
157
return ;
124
158
125
159
if ( this . selectedRow )
126
- this . selectedRow . trVN . setProps ( { class : undefined } ) ;
160
+ this . selectedRow . selectedTrigger . set ( null ) ;
127
161
128
162
this . selectedRow = rowToSelect ;
129
- this . selectedRow . trVN . setProps ( { class : "danger" } ) ;
163
+ rowToSelect . selectedTrigger . set ( "danger" ) ;
130
164
}
131
165
132
- public onDeleteRowClicked ( rowToDelete : Row )
166
+ public onDeleteRowClicked = ( e : MouseEvent , rowToDelete : Row ) : void =>
133
167
{
134
168
if ( rowToDelete === this . selectedRow )
135
169
this . selectedRow = undefined ;
136
170
137
- let id = rowToDelete . id ;
138
- let i = this . rows . findIndex ( row => row . id == id ) ;
171
+ let i = this . rows . indexOf ( rowToDelete ) ;
139
172
this . rows . splice ( i , 1 ) ;
140
- this . vnTBody . spliceChildren ( i , 1 ) ;
173
+ this . vnTBody . spliceChildren ( i , 1 , undefined , mim . TickSchedulingType . Sync ) ;
141
174
}
142
175
}
143
176
144
- let glyphVN = < span class = "glyphicon glyphicon-remove" aria-hidden = "true" /> ;
145
- let lastCellVN = < td class = "col-md-6" /> ;
146
-
147
- class Row extends mim . Component
148
- {
149
- main : Main ;
150
- id : number ;
151
- // label: string;
152
- labelVN : mim . ITextVN ;
153
- trVN : mim . IElmVN < HTMLTableRowElement > ;
154
-
155
- constructor ( main : Main , id : number , label : string )
156
- {
157
- super ( ) ;
158
-
159
- this . main = main ;
160
- this . id = id ;
161
- // this.label = label;
162
- this . labelVN = mim . createTextVN ( label ) ;
163
- }
164
-
165
- public willMount ( )
166
- {
167
- this . trVN = < tr >
168
- < td class = "col-md-1" > { this . id } </ td >
169
- < td class = "col-md-4" > < a click = { this . onSelectClicked } > { this . labelVN } </ a > </ td >
170
- < td class = "col-md-1" > < a click = { this . onDeleteClicked } > { glyphVN } </ a > </ td >
171
- { lastCellVN }
172
- </ tr > as mim . IElmVN < HTMLTableRowElement > ;
173
- }
174
-
175
- @mim . noWatcher
176
- public render ( )
177
- {
178
- return this . trVN ;
179
- }
180
-
181
- public updateLabel ( )
182
- {
183
- // this.labelVN.setText( this.label += " !!!");
184
- this . labelVN . setText ( this . labelVN . text + " !!!" ) ;
185
- }
186
-
187
- private onDeleteClicked ( )
188
- {
189
- this . main . onDeleteRowClicked ( this ) ;
190
- }
191
-
192
- private onSelectClicked ( )
193
- {
194
- this . main . onSelectRowClicked ( this ) ;
195
- }
196
- }
197
-
198
177
199
178
200
179
mim . mount ( < Main /> , document . getElementById ( 'main' ) ) ;
0 commit comments