1+ 'use strict' ; 
2+ 
3+ import  Rx  from  'rx' ; 
4+ import  Cycle  from  '@cycle/core' ; 
5+ import  { hJSX ,  makeDOMDriver }  from  '@cycle/dom' ; 
6+ 
7+ let  id  =  1 ; 
8+ 
9+ function  _random ( max )  { 
10+ 	return  Math . round ( Math . random ( ) * 1000 ) % max ; 
11+ } ; 
12+ 
13+ let  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' ] ; 
14+ let  colours  =  [ 'red' ,  'yellow' ,  'blue' ,  'green' ,  'pink' ,  'brown' ,  'purple' ,  'brown' ,  'white' ,  'black' ,  'orange' ] ; 
15+ let  nouns  =  [ 'table' ,  'chair' ,  'house' ,  'bbq' ,  'desk' ,  'car' ,  'pony' ,  'cookie' ,  'sandwich' ,  'burger' ,  'pizza' ,  'mouse' ,  'keyboard' ] ; 
16+ 
17+ function  buildData ( count  =  1000 )  { 
18+ 	var  data  =  [ ] ; 
19+ 	
20+ 	for ( var  i  =  0 ;  i  <  count ;  i ++ )  { 
21+ 		data . push ( { 
22+ 			id : id ++ , 
23+ 			label : adjectives [ _random ( adjectives . length ) ]  +  ' '  +  colours [ _random ( colours . length ) ]  +  ' '  +  nouns [ _random ( nouns . length ) ] 
24+ 		} ) ; 
25+ 	} 
26+ 	
27+ 	return  data ; 
28+ } ; 
29+ 
30+ const  Operations  =  { 
31+ 	Run : state  =>  ( { items : buildData ( ) ,  selected : undefined } ) , 
32+ 	Update : state  =>  ( { items : state . items . map ( ( item ,  idx )  =>  ( idx % 10  ===  0 )  ? ( { id :item . id ,  label : item . label + ' !!!' } )  : item ) ,  selected : state . selected } ) , 
33+ 	Add : state  =>  ( { items : state . items . concat ( buildData ( 1000 ) ) ,  selected : state . selected } ) , 
34+ 	SelectItem : item  =>  state  =>  ( { items : state . items ,  selected : item } ) , 
35+ 	RemoveItem : id  =>  state  =>  ( { items : state . items . filter ( item  =>  item . id  !==  id ) ,  selected : state . selected } ) , 
36+ 	HideAll : state  =>  ( { items : [ ] ,  selected : null ,  olditems : state . items } ) , 
37+ 	ShowAll : state  =>  ( { items : state . olditems ,  selected : null } ) , 
38+ 	RunLots : state  =>  ( { items : buildData ( 10000 ) ,  selected : null } ) , 
39+ 	Clear : state  =>  ( { items : [ ] ,  selected : null } ) , 
40+ 	SwapRows : state  =>  { 
41+ 		let  d  =  state . items . splice ( 0 ) ; 
42+ 		if ( d . length  >  10 )  { 
43+ 			var  a  =  d [ 4 ] ; 
44+ 			d [ 4 ]  =  d [ 9 ] ; 
45+ 			d [ 9 ]  =  a ; 
46+ 		} 
47+ 		return  { items : d ,  selected : state . selected } ; 
48+ 	} 
49+ } ; 
50+ 
51+ function  intent ( DOMSource )  { 
52+ 	return  Rx . Observable . merge ( 
53+ 		DOMSource . select ( '#run' ) . events ( 'click' ) . map ( evt  =>  Operations . Run ) , 
54+ 		DOMSource . select ( '#update' ) . events ( 'click' ) . map ( evt  =>  Operations . Update ) , 
55+ 		DOMSource . select ( '#add' ) . events ( 'click' ) . map ( evt  =>  Operations . Add ) , 
56+ 		DOMSource . select ( '#hideall' ) . events ( 'click' ) . map ( evt  =>  Operations . HideAll ) , 
57+ 		DOMSource . select ( '#showall' ) . events ( 'click' ) . map ( evt  =>  Operations . ShowAll ) , 
58+ 		DOMSource . select ( '#runlots' ) . events ( 'click' ) . map ( evt  =>  Operations . RunLots ) , 
59+ 		DOMSource . select ( '#clear' ) . events ( 'click' ) . map ( evt  =>  Operations . Clear ) , 
60+ 		DOMSource . select ( '#swaprows' ) . events ( 'click' ) . map ( evt  =>  Operations . SwapRows ) , 
61+ 		DOMSource . select ( '.remove' ) . events ( 'click' ) . map ( evt  =>  { 
62+ 			evt . preventDefault ( ) ; 
63+ 			evt . stopPropagation ( ) ; 
64+ 			
65+ 			let  el  =  evt . target ; 
66+ 			while ( el  &&  ! el . id )  { 
67+ 				el  =  el . parentNode ; 
68+ 			} 
69+ 			
70+ 			return  Operations . RemoveItem ( parseInt ( el . id ) ) ; 
71+ 		} ) , 
72+ 		DOMSource . select ( '.select' ) . events ( 'click' ) . map ( evt  =>  { 
73+ 			evt . preventDefault ( ) ; 
74+ 			evt . stopPropagation ( ) ; 
75+ 			
76+ 			let  el  =  evt . target ; 
77+ 			while ( el  &&  ! el . id )  { 
78+ 				el  =  el . parentNode ; 
79+ 			} 
80+ 			
81+ 			return  Operations . SelectItem ( parseInt ( el . id ) ) ; 
82+ 		} ) 
83+ 	) ; 
84+ } ; 
85+ 
86+ function  model ( operation$ )  { 
87+ 	return  operation$ . startWith ( { 
88+ 		items : [ ] 
89+ 	} ) . scan ( ( state ,  operation )  =>  { 
90+ 		return  operation ( state ) ; 
91+ 	} ) ; 
92+ } 
93+ 
94+ function  view ( state$ )  { 
95+ 	return  state$ . map ( state  => 
96+ 		< div  className = 'container' > 
97+ 			< div  className = 'jumbotron' > 
98+ 				< div  className = 'row' > 
99+ 					< div  className = 'col-md-8' > 
100+ 						< h1 > Cycle.js</ h1 > 
101+ 					</ div > 
102+ 					< div  className = 'col-md-4' > 
103+ 						< button  type = 'button'  className = 'btn btn-primary btn-block'  id = 'add' > Add 1000 rows</ button > 
104+ 						< button  type = 'button'  className = 'btn btn-primary btn-block'  id = 'run' > Create 1000 rows</ button > 
105+ 						< button  type = 'button'  className = 'btn btn-primary btn-block'  id = 'update' > Update every 10th row</ button > 
106+ 						< button  type = 'button'  className = 'btn btn-primary btn-block'  id = 'hideall' > HideAll</ button > 
107+ 						< button  type = 'button'  className = 'btn btn-primary btn-block'  id = 'showall' > ShowAll</ button > 
108+ 						< button  type = 'button'  className = 'btn btn-primary btn-block'  id = 'runlots' > Create lots of rows</ button > 
109+ 						< button  type = 'button'  className = 'btn btn-primary btn-block'  id = 'clear' > Clear</ button > 
110+ 						< button  type = 'button'  className = 'btn btn-primary btn-block'  id = 'swaprows' > Swap Rows</ button > 
111+ 						< h3  id = 'duration' > < span  className = 'glyphicon glyphicon-remove'  aria-hidden = 'true' > </ span >  </ h3 > 
112+ 					</ div > 
113+ 				</ div > 
114+ 			</ div > 
115+ 			< table  className = 'table table-hover table-striped test-data' > 
116+ 				< tbody > 
117+ 				{ 
118+ 					state . items . map ( item  => 
119+ 						< tr  id = { item . id }  className = { state . selected  ===  item . id  ? 'danger'  : '' } > 
120+ 							< td  className = 'col-md-1' > { item . id } </ td > 
121+ 							< td  className = 'col-md-4' > 
122+ 								< a  className = 'select' > { item . label } </ a > 
123+ 							</ td > 
124+ 							< td  className = 'col-md-1' > 
125+ 								< a  className = 'remove' > < span  className = 'glyphicon glyphicon-remove' > </ span > </ a > 
126+ 							</ td > 
127+ 							< td  className = 'col-md-6' /> 
128+ 						</ tr > 
129+ 					) 
130+ 				} 
131+ 				</ tbody > 
132+ 			</ table > 
133+ 		</ div > 
134+ 	) ; 
135+ } 
136+ 
137+ function  main ( sources )  { 
138+ 	const  state$  =  model ( intent ( sources . DOM ) ) ; 
139+ 	
140+ 	return  { 
141+ 		DOM : view ( state$ ) 
142+ 	} 
143+ } ; 
144+ 
145+ var  drivers  =  { 
146+ 	DOM : makeDOMDriver ( '#main' ) 
147+ } ; 
148+ 
149+ Cycle . run ( main ,  drivers ) ; 
0 commit comments