1
1
import { html , render } from '../node_modules/lighterhtml/esm/index.js' ;
2
2
3
- const adjectives = [
4
- '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' ] ;
5
- const colours = [ 'red' , 'yellow' , 'blue' , 'green' , 'pink' , 'brown' , 'purple' , 'brown' , 'white' , 'black' , 'orange' ] ;
6
- const nouns = [ 'table' , 'chair' , 'house' , 'bbq' , 'desk' , 'car' , 'pony' , 'cookie' , 'sandwich' , 'burger' , 'pizza' , 'mouse' , 'keyboard' ] ;
7
-
8
- let data = [ ] ;
9
3
let did = 1 ;
10
- let selected = - 1 ;
11
-
12
- const add = ( ) => {
13
- data = data . concat ( buildData ( 1000 ) ) ;
14
- _render ( ) ;
15
- } ;
16
- const run = ( ) => {
17
- data = buildData ( 1000 ) ;
18
- _render ( ) ;
19
- } ;
20
- const runLots = ( ) => {
21
- data = buildData ( 10000 ) ;
22
- _render ( ) ;
23
- } ;
24
- const clear = ( ) => {
25
- data = [ ] ;
26
- _render ( ) ;
27
- } ;
28
- const interact = event => {
29
- event . preventDefault ( ) ;
30
- event . stopPropagation ( ) ;
31
- const { target} = event ;
32
- const id = + target . closest ( 'tr' ) . id ;
33
- if ( target . getAttribute ( 'data-interaction' ) === 'delete' ) {
34
- del ( id )
35
- } else {
36
- select ( id )
37
- }
38
- } ;
39
- const del = id => {
40
- const idx = data . findIndex ( d => d . id === id ) ;
41
- data . splice ( idx , 1 )
42
- _render ( ) ;
43
- } ;
44
- const select = id => {
45
- if ( selected > - 1 ) {
46
- data [ selected ] . selected = false ;
47
- }
48
- selected = data . findIndex ( d => d . id === id ) ;
49
- data [ selected ] . selected = true ;
50
- _render ( ) ;
4
+ const buildData = ( count ) => {
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
+ const data = [ ] ;
9
+ for ( let i = 0 ; i < count ; i ++ ) {
10
+ data . push ( {
11
+ id : did ++ ,
12
+ label : adjectives [ _random ( adjectives . length ) ] + " " + colours [ _random ( colours . length ) ] + " " + nouns [ _random ( nouns . length ) ]
13
+ } ) ;
14
+ }
15
+ return data ;
51
16
} ;
52
- const swapRows = ( ) => {
53
- if ( data . length > 998 ) {
54
- const tmp = data [ 1 ]
55
- data [ 1 ] = data [ 998 ]
56
- data [ 998 ] = tmp
57
- }
58
- _render ( ) ;
59
- } ;
60
- const update = ( ) => {
61
- for ( let i = 0 ; i < data . length ; i += 10 ) {
62
- data [ i ] . label += ' !!!' ;
63
- }
64
- _render ( ) ;
65
- } ;
66
- const buildData = count => {
67
- const data = [ ] ;
68
- for ( let i = 0 ; i < count ; i ++ ) {
69
- data . push ( {
70
- id : did ++ ,
71
- label : `${ adjectives [ _random ( adjectives . length ) ] } ${ colours [ _random ( colours . length ) ] } ${ nouns [ _random ( nouns . length ) ] } ` ,
72
- selected : false ,
73
- } ) ;
74
- }
75
- return data ;
76
- } ;
77
- const _random = max => {
78
- return Math . round ( Math . random ( ) * 1000 ) % max ;
17
+
18
+ const _random = max => Math . round ( Math . random ( ) * 1000 ) % max ;
19
+
20
+ const scope = {
21
+ add ( ) {
22
+ scope . data = scope . data . concat ( buildData ( 1000 ) ) ;
23
+ update ( main , scope ) ;
24
+ } ,
25
+ run ( ) {
26
+ scope . data = buildData ( 1000 ) ;
27
+ update ( main , scope ) ;
28
+ } ,
29
+ runLots ( ) {
30
+ scope . data = buildData ( 10000 ) ;
31
+ update ( main , scope ) ;
32
+ } ,
33
+ clear ( ) {
34
+ scope . data = [ ] ;
35
+ update ( main , scope ) ;
36
+ } ,
37
+ update ( ) {
38
+ const { data} = scope ;
39
+ for ( let i = 0 , { length} = data ; i < length ; i += 10 )
40
+ data [ i ] . label += ' !!!' ;
41
+ update ( main , scope ) ;
42
+ } ,
43
+ swapRows ( ) {
44
+ const { data} = scope ;
45
+ if ( data . length > 998 ) {
46
+ const tmp = data [ 1 ] ;
47
+ data [ 1 ] = data [ 998 ] ;
48
+ data [ 998 ] = tmp ;
49
+ }
50
+ update ( main , scope ) ;
51
+ } ,
52
+ interact ( event ) {
53
+ event . preventDefault ( ) ;
54
+ const a = event . target . closest ( 'a' ) ;
55
+ const id = parseInt ( a . closest ( 'tr' ) . id , 10 ) ;
56
+ scope [ a . dataset . action ] ( id ) ;
57
+ } ,
58
+ delete ( id ) {
59
+ const { data} = scope ;
60
+ const idx = data . findIndex ( d => d . id === id ) ;
61
+ data . splice ( idx , 1 ) ;
62
+ update ( main , scope ) ;
63
+ } ,
64
+ select ( id ) {
65
+ scope . selected = id ;
66
+ update ( main , scope ) ;
67
+ } ,
68
+ selected : - 1 ,
69
+ data : [ ] ,
79
70
} ;
80
71
81
- const container = document . getElementById ( 'container' ) ;
82
- const _render = ( ) => {
83
- render ( container , html . for ( container ) `
72
+ const main = document . getElementById ( 'container' ) ;
73
+ update ( main , scope ) ;
74
+
75
+ function update (
76
+ where ,
77
+ { data, selected, run, runLots, add, update, clear, swapRows, interact}
78
+ ) {
79
+ render ( where , html `
84
80
< div class ="container ">
85
81
< div class ="jumbotron ">
86
82
< div class ="row ">
@@ -90,50 +86,53 @@ const _render = () => {
90
86
< div class ="col-md-6 ">
91
87
< div class ="row ">
92
88
< div class ="col-sm-6 smallpad ">
93
- <button type="button" class="btn btn-primary btn-block" id="run" onclick=${ run } >Create 1,000 rows</button>
89
+ < button type ="button " class ="btn btn-primary btn-block "
90
+ id ="run " onclick =${ run } > Create 1,000 rows</ button >
94
91
</ div >
95
92
< div class ="col-sm-6 smallpad ">
96
- <button type="button" class="btn btn-primary btn-block" id="runlots" onclick=${ runLots } >Create 10,000 rows</button>
93
+ < button type ="button " class ="btn btn-primary btn-block "
94
+ id ="runlots " onclick =${ runLots } > Create 10,000 rows</ button >
97
95
</ div >
98
96
< div class ="col-sm-6 smallpad ">
99
- <button type="button" class="btn btn-primary
100
- btn-block" id="add" onclick=${ add } >Append 1,000 rows</button>
97
+ < button type ="button " class ="btn btn-primary btn-block "
98
+ id ="add " onclick =${ add } > Append 1,000 rows</ button >
101
99
</ div >
102
100
< div class ="col-sm-6 smallpad ">
103
- <button type="button" class="btn btn-primary
104
- btn-block" id="update" onclick=${ update } >Update every 10th row</button>
101
+ < button type ="button " class ="btn btn-primary btn-block "
102
+ id ="update " onclick =${ update } > Update every 10th row</ button >
105
103
</ div >
106
104
< div class ="col-sm-6 smallpad ">
107
- <button type="button" class="btn btn-primary
108
- btn-block" id="clear" onclick=${ clear } >Clear</button>
105
+ < button type ="button " class ="btn btn-primary btn-block "
106
+ id ="clear " onclick =${ clear } > Clear</ button >
109
107
</ div >
110
108
< div class ="col-sm-6 smallpad ">
111
- <button type="button" class="btn btn-primary
112
- btn-block" id="swaprows" onclick=${ swapRows } >Swap Rows</button>
109
+ < button type ="button " class ="btn btn-primary btn-block "
110
+ id ="swaprows " onclick =${ swapRows } > Swap Rows</ button >
113
111
</ div >
114
112
</ div >
115
113
</ div >
116
114
</ div >
117
115
</ div >
118
116
< table onclick =${ interact } class ="table table-hover table-striped test-data">
119
- <tbody>${ data . map ( item => html . for ( item ) `
120
- <tr id=${ item . id } class=${ item . selected ? 'danger' : '' } >
121
- <td class="col-md-1">${ item . id } </td>
122
- <td data-interaction='select' class="col-md-4">
123
- <a data-interaction='select'>${ item . label } </a>
117
+ < tbody > ${
118
+ data . map ( item => {
119
+ const { id, label} = item ;
120
+ return html . for ( item ) `
121
+ <tr id=${ id } class=${ id === selected ? 'danger' : '' } >
122
+ <td class="col-md-1">${ id } </td>
123
+ <td class="col-md-4">
124
+ <a data-action='select'>${ label } </a>
124
125
</td>
125
- <td data-interaction='delete' class="col-md-1">
126
- <a data-interaction ='delete'>
127
- <span data-interaction='delete' class="glyphicon glyphicon-remove" aria-hidden="true"></span>
126
+ <td class="col-md-1">
127
+ <a data-action ='delete'>
128
+ <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
128
129
</a>
129
130
</td>
130
131
<td class="col-md-6"></td>
131
- </tr>` )
132
- }
133
- </tbody>
132
+ </tr>` ;
133
+ } )
134
+ } </ tbody >
134
135
</ table >
135
136
< span class ="preloadicon glyphicon glyphicon-remove " aria-hidden ="true "> </ span >
136
137
</ div > ` ) ;
137
- } ;
138
-
139
- _render ( ) ;
138
+ }
0 commit comments