Skip to content

Commit 37f1772

Browse files
committed
Removed hard coded values of stage items
1 parent 2f1b5ad commit 37f1772

File tree

7 files changed

+85
-21
lines changed

7 files changed

+85
-21
lines changed

public/index.html

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
66
<meta name="viewport" content="width=device-width, initial-scale=1" />
77
<meta name="theme-color" content="#000000" />
8-
<meta
9-
name="description"
10-
content="Web site created using create-react-app"
11-
/>
8+
<meta name="description" content="Sorting visualizer" />
129
<link
1310
rel="stylesheet"
1411
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"

src/appsettings.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var settings = {
2+
itemWidth: {
3+
// default, min and max are index of [2, 4, 5, 10, 20, 40, 50] '
4+
// which are item width in pixel
5+
default: 4,
6+
min: 1,
7+
max: 7,
8+
},
9+
};
10+
11+
export default settings;

src/components/stage/itemcontainer.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ import React from "react";
33
interface Props {
44
items: number[];
55
maxHeight: number;
6+
itemWidth: number;
67
}
78

89
const ItemContainer: React.SFC<Props> = (props) => {
9-
const { maxHeight, items } = props;
10+
const { maxHeight, items, itemWidth } = props;
1011

1112
/** render each sortable item */
1213
const renderItem = (item: number) => {
13-
let style: React.CSSProperties = { marginTop: maxHeight - item };
14+
let style: React.CSSProperties = {
15+
marginTop: maxHeight - item,
16+
width: itemWidth,
17+
};
1418
return <div style={style}></div>;
1519
};
1620

src/components/stage/stage.tsx

+47-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from "react";
22
import StageControls from "./stagecontrols";
33
import ItemContainer from "./itemcontainer";
44
import SortingHelper from "../../helpers/sortinghelper";
5+
import settings from "../../appsettings";
56

67
interface State {
78
renderedOn: number;
@@ -13,12 +14,28 @@ class Stage extends React.Component<Props, State> {
1314
// array to sort
1415
private arrayToSort: number[] = [];
1516

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+
1632
/**
1733
* constructor of stage
1834
*/
1935
constructor(props: Props, state: State) {
2036
super(props, state);
21-
this.arrayToSort = SortingHelper.generateRandomArray(80, 400);
37+
this.setItemWidth();
38+
this.generateRandomArray();
2239
}
2340

2441
/**
@@ -33,7 +50,11 @@ class Stage extends React.Component<Props, State> {
3350
resetArray={this.resetArray}
3451
onItemWidthChange={this.onItemWidthChange}
3552
/>
36-
<ItemContainer items={this.arrayToSort} maxHeight={400} />
53+
<ItemContainer
54+
items={this.arrayToSort}
55+
maxHeight={this.stageHeight}
56+
itemWidth={this.itemWidth}
57+
/>
3758
</div>
3859
);
3960
}
@@ -56,7 +77,7 @@ class Stage extends React.Component<Props, State> {
5677
* reset array for sorting
5778
*/
5879
private resetArray = (e: React.MouseEvent<HTMLElement>) => {
59-
this.arrayToSort = SortingHelper.generateRandomArray(80, 400);
80+
this.generateRandomArray();
6081
this.setState({ renderedOn: Date.now() });
6182
};
6283

@@ -65,7 +86,29 @@ class Stage extends React.Component<Props, State> {
6586
*/
6687
private onItemWidthChange = (e: React.ChangeEvent<HTMLElement>) => {
6788
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);
69112
};
70113
}
71114

src/components/stage/stagecontrols.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from "react";
22
import { RangeOptions } from "./typings/rangeoptions";
3+
import settings from "../../appsettings";
34

45
interface Props {
56
resetArray: (event: React.MouseEvent<HTMLElement>) => void;
@@ -10,9 +11,9 @@ interface Props {
1011

1112
const StageControls: React.SFC<Props> = (props) => {
1213
const itemWidthRangeOptions: RangeOptions = {
13-
default: 10,
14-
min: 1,
15-
max: 50,
14+
default: settings.itemWidth.default,
15+
min: settings.itemWidth.min,
16+
max: settings.itemWidth.max,
1617
};
1718

1819
return (

src/helpers/sortinghelper.ts

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ class SortingHelper {
1010
() => Math.floor(Math.random() * max)
1111
);
1212
};
13+
14+
/**
15+
* return item width in pixel
16+
*/
17+
public static getItemWidth = (weight: number): number => {
18+
// width of each item will be the value + 1px margin
19+
// example if width is 3 then item width will be 49 + 1 = 4
20+
let itemWidth = [1, 3, 4, 9, 19, 39, 49];
21+
if (weight > itemWidth.length) {
22+
throw Error("Weight not valid for item width");
23+
}
24+
25+
return itemWidth[weight - 1];
26+
};
1327
}
1428

1529
export default SortingHelper;

tsconfig.json

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
{
22
"compilerOptions": {
33
"target": "es5",
4-
"lib": [
5-
"dom",
6-
"dom.iterable",
7-
"esnext"
8-
],
4+
"lib": ["dom", "dom.iterable", "esnext"],
95
"allowJs": true,
106
"skipLibCheck": true,
117
"esModuleInterop": true,
@@ -19,7 +15,5 @@
1915
"jsx": "react",
2016
"isolatedModules": true
2117
},
22-
"include": [
23-
"src"
24-
]
18+
"include": ["src", "public/appsettings.js"]
2519
}

0 commit comments

Comments
 (0)