@@ -2,6 +2,7 @@ import React from "react";
2
2
import StageControls from "./stagecontrols" ;
3
3
import ItemContainer from "./itemcontainer" ;
4
4
import SortingHelper from "../../helpers/sortinghelper" ;
5
+ import settings from "../../appsettings" ;
5
6
6
7
interface State {
7
8
renderedOn : number ;
@@ -13,12 +14,28 @@ class Stage extends React.Component<Props, State> {
13
14
// array to sort
14
15
private arrayToSort : number [ ] = [ ] ;
15
16
17
+ // original array
18
+ private rawArray : number [ ] = [ ] ;
19
+
20
+ // width of an item in stage
21
+ private itemWidth = 0 ;
22
+
23
+ // total items to sort
24
+ private itemCount = 0 ;
25
+
26
+ // width of stage
27
+ private stageWidth = 800 ;
28
+
29
+ // height of stage
30
+ private stageHeight = 400 ;
31
+
16
32
/**
17
33
* constructor of stage
18
34
*/
19
35
constructor ( props : Props , state : State ) {
20
36
super ( props , state ) ;
21
- this . arrayToSort = SortingHelper . generateRandomArray ( 80 , 400 ) ;
37
+ this . setItemWidth ( ) ;
38
+ this . generateRandomArray ( ) ;
22
39
}
23
40
24
41
/**
@@ -33,7 +50,11 @@ class Stage extends React.Component<Props, State> {
33
50
resetArray = { this . resetArray }
34
51
onItemWidthChange = { this . onItemWidthChange }
35
52
/>
36
- < ItemContainer items = { this . arrayToSort } maxHeight = { 400 } />
53
+ < ItemContainer
54
+ items = { this . arrayToSort }
55
+ maxHeight = { this . stageHeight }
56
+ itemWidth = { this . itemWidth }
57
+ />
37
58
</ div >
38
59
) ;
39
60
}
@@ -56,7 +77,7 @@ class Stage extends React.Component<Props, State> {
56
77
* reset array for sorting
57
78
*/
58
79
private resetArray = ( e : React . MouseEvent < HTMLElement > ) => {
59
- this . arrayToSort = SortingHelper . generateRandomArray ( 80 , 400 ) ;
80
+ this . generateRandomArray ( ) ;
60
81
this . setState ( { renderedOn : Date . now ( ) } ) ;
61
82
} ;
62
83
@@ -65,7 +86,29 @@ class Stage extends React.Component<Props, State> {
65
86
*/
66
87
private onItemWidthChange = ( e : React . ChangeEvent < HTMLElement > ) => {
67
88
let target : any = e . target ;
68
- console . log ( `Current item width ${ target . value } ` ) ;
89
+ this . itemWidth = SortingHelper . getItemWidth ( parseInt ( target . value ) ) ;
90
+ this . setItemWidth ( ) ;
91
+ this . arrayToSort = this . rawArray . slice ( 0 , this . itemCount ) ;
92
+ this . setState ( { renderedOn : Date . now ( ) } ) ;
93
+ } ;
94
+
95
+ /**
96
+ * set width of item
97
+ */
98
+ private setItemWidth = ( ) => {
99
+ if ( this . itemCount === 0 ) {
100
+ this . itemWidth = SortingHelper . getItemWidth ( settings . itemWidth . default ) ;
101
+ }
102
+ this . itemCount = this . stageWidth / ( this . itemWidth + 1 ) ;
103
+ } ;
104
+
105
+ /**
106
+ * generate random array
107
+ */
108
+ private generateRandomArray = ( ) => {
109
+ this . rawArray = SortingHelper . generateRandomArray ( 400 , this . stageHeight ) ;
110
+ this . arrayToSort = [ ...this . rawArray ] ;
111
+ this . arrayToSort = this . rawArray . slice ( 0 , this . itemCount ) ;
69
112
} ;
70
113
}
71
114
0 commit comments