1+ import Doz from 'doz'
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 did = 1 ;
9+ let selected = - 1 ;
10+
11+ const actions = {
12+ add ( ) {
13+ this . getStore ( 'records' ) . data = this . getStore ( 'records' ) . data . concat ( buildData ( 1000 ) ) ;
14+ } ,
15+
16+ run ( ) {
17+ this . getStore ( 'records' ) . data = buildData ( 1000 ) ;
18+ } ,
19+
20+ runLots ( ) {
21+ this . getStore ( 'records' ) . data = buildData ( 10000 ) ;
22+ } ,
23+
24+ clear ( ) {
25+ this . getStore ( 'records' ) . data = [ ] ;
26+ } ,
27+
28+ del ( id ) {
29+ const data = this . getStore ( 'records' ) . data ;
30+ const idx = data . findIndex ( d => d . id === id ) ;
31+ data . splice ( idx , 1 ) ;
32+ } ,
33+
34+ interact ( e ) {
35+ const td = e . target . closest ( 'td' ) ;
36+ const interaction = td . getAttribute ( 'data-interaction' ) ;
37+ const id = parseInt ( td . parentNode . id ) ;
38+ if ( interaction === 'delete' ) {
39+ this . action . del ( id ) ;
40+ } else {
41+ this . action . select ( id ) ;
42+ }
43+ } ,
44+
45+ select ( id ) {
46+ const data = this . getStore ( 'records' ) . data ;
47+ if ( selected > - 1 ) {
48+ data [ selected ] . selected = false ;
49+ }
50+ selected = data . findIndex ( d => d . id === id ) ;
51+ data [ selected ] . selected = true ;
52+ } ,
53+
54+ swapRows ( ) {
55+ this . mainComponent . prepareCommit ( ) ;
56+ const data = this . getStore ( 'records' ) . data ;
57+ if ( data . length > 998 ) {
58+ const tmp = data [ 1 ] ;
59+ data [ 1 ] = data [ 998 ] ;
60+ data [ 998 ] = tmp ;
61+ }
62+ this . mainComponent . commit ( ) ;
63+ } ,
64+
65+ update ( ) {
66+ this . mainComponent . prepareCommit ( ) ;
67+ const data = this . getStore ( 'records' ) . data ;
68+ for ( let i = 0 ; i < data . length ; i += 10 ) {
69+ data [ i ] . label += ' !!!' ;
70+ }
71+ this . mainComponent . commit ( ) ;
72+ }
73+ } ;
74+
75+ const buildData = count => {
76+ const data = [ ] ;
77+ for ( let i = 0 ; i < count ; i ++ ) {
78+ data . push ( {
79+ id : did ++ ,
80+ label : `${ adjectives [ _random ( adjectives . length ) ] } ${ colours [ _random ( colours . length ) ] } ${ nouns [ _random ( nouns . length ) ] } ` ,
81+ selected : false ,
82+ } ) ;
83+ }
84+ return data ;
85+ } ;
86+
87+ const _random = max => {
88+ return Math . round ( Math . random ( ) * 1000 ) % max ;
89+ } ;
90+
91+ new Doz ( {
92+ store : 'records' ,
93+ actions,
94+ root : '#container' ,
95+ props : {
96+ data : [ ]
97+ } ,
98+ template ( h ) {
99+ return h `
100+ <div class="container">
101+ <div class="jumbotron">
102+ <div class="row">
103+ <div class="col-md-6">
104+ <h1>Doz</h1>
105+ </div>
106+ <div class="col-md-6">
107+ <div class="row">
108+ <div class="col-sm-6 smallpad">
109+ <button type="button" class="btn btn-primary btn-block" id="run" onclick="${ this . action . run } ">Create 1,000 rows</button>
110+ </div>
111+ <div class="col-sm-6 smallpad">
112+ <button type="button" class="btn btn-primary btn-block" id="runlots" onclick="${ this . action . runLots } ">Create 10,000 rows</button>
113+ </div>
114+ <div class="col-sm-6 smallpad">
115+ <button type="button" class="btn btn-primary btn-block" id="add" onclick="${ this . action . add } ">Append 1,000 rows</button>
116+ </div>
117+ <div class="col-sm-6 smallpad">
118+ <button type="button" class="btn btn-primary btn-block" id="update" onclick="${ this . action . update } ">Update every 10th row</button>
119+ </div>
120+ <div class="col-sm-6 smallpad">
121+ <button type="button" class="btn btn-primary btn-block" id="clear" onclick="${ this . action . clear } ">Clear</button>
122+ </div>
123+ <div class="col-sm-6 smallpad">
124+ <button type="button" class="btn btn-primary btn-block" id="swaprows" onclick="${ this . action . swapRows } ">Swap Rows</button>
125+ </div>
126+ </div>
127+ </div>
128+ </div>
129+ </div>
130+ <table onclick="${ this . action . interact } " class="table table-hover table-striped test-data">
131+ <tbody>
132+ ${ this . props . data . map (
133+ item => h `
134+ <tr id="${ item . id } " class="${ item . selected ? 'danger' : '' } " >
135+ <td class="col-md-1">${ item . id } </td>
136+ <td class="col-md-4" >
137+ <a>${ item . label } </a>
138+ </td>
139+ <td data-interaction="delete" class="col-md-1">
140+ <a>
141+ <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
142+ </a>
143+ </td>
144+ <td class="col-md-6"></td>
145+ </tr>`
146+ ) }
147+ </tbody>
148+ </table>
149+ <span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span>
150+ </div>
151+ `
152+ } ,
153+ } ) ;
0 commit comments