Skip to content

Commit 4d34503

Browse files
committed
more UI
1 parent 443ee3c commit 4d34503

9 files changed

Lines changed: 111 additions & 63 deletions

File tree

‎src/components/Animation/Animation.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { Document } from "postcss";
22

33
class Animation {
44

5+
constructor() {
6+
// SG 07/09/2022 15:11 singleton pattern
7+
if(Animation._instance) {
8+
return Animation._instance;
9+
}
10+
Animation._instance = this;
11+
}
12+
13+
514
// SG 07/07/2022 22:00 define differnet animaiton here and call it in sorting algorithms
615

716

‎src/components/Canvas/InputField.js‎

Lines changed: 87 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,109 @@
11
import React, { Component } from 'react'
22
import { InputHandler } from '../InputHandler/InputHandler.js'
3+
import SelectionSort from '../Strategy/Algorithms/SelectionSort.js';
4+
import { Strategy } from '../Strategy/Strategy.js';
35
import { Options } from './options/Options.js';
4-
6+
import { ReactComponent as Play } from '../../img/play.svg'
7+
import { ReactComponent as Replay } from '../../img/replay.svg'
58
class InputField extends Component {
69

710
constructor(props) {
811
super(props);
912
this.InputHandler = new InputHandler();
1013
this.name = props.name;
14+
this.selectionSort = new SelectionSort();
15+
this.Strategy = new Strategy();
16+
this.state = {
17+
algorithm: ''
18+
}
1119
}
1220

1321

1422
render() {
15-
const {inputArrayLength} = this.props.InputHandler.state;
23+
const {inputArrayLength, options} = this.props.InputHandler.state;
24+
const { algorithm } = this.state;
25+
26+
1627

1728
return (
18-
<div className={`flex items-center justify-around border-b p-3 w-sreen flex-wrap ${this.name[0] + ' '}`}>
19-
<div className="flex flex-col justify-center items-center h-full mb-3 sm:mb-0">
20-
<div className="flex">
21-
<input placeholder={`Array size (MAX ${InputHandler.getAllowedMaxInputSize()})`} value={inputArrayLength === 0 ? '' : inputArrayLength} onChange={(e) => {
22-
// SG 07/07/2022 21:03 Input validation, only accepts a number that is less than the allowed array size
23-
(Number(e.target.value) || !e.target.value)
24-
&& (e.target.value <= InputHandler.getAllowedMaxInputSize())
25-
? this.props.InputHandler.setState({inputArrayLength: e.target.value.replace(/\D/g, '')})
26-
: this.props.InputHandler.setState({inputArrayLength: InputHandler.getAllowedMaxInputSize()})
29+
<div className="flex flex-col">
30+
31+
<div className={`flex items-center justify-around p-3 w-sreen gap-4 flex-wrap ${this.name[0] + ' '}`}>
32+
<div className="flex flex-col justify-center items-center h-full sm:mb-0">
33+
<div className="flex">
34+
<input placeholder={`Array size (MAX ${InputHandler.getAllowedMaxInputSize()})`} value={inputArrayLength === 0 ? '' : inputArrayLength} onChange={(e) => {
35+
// SG 07/07/2022 21:03 Input validation, only accepts a number that is less than the allowed array size
36+
(Number(e.target.value) || !e.target.value)
37+
&& (e.target.value <= InputHandler.getAllowedMaxInputSize())
38+
? this.props.InputHandler.setState({inputArrayLength: e.target.value.replace(/\D/g, '')})
39+
: this.props.InputHandler.setState({inputArrayLength: InputHandler.getAllowedMaxInputSize()})
40+
}}
41+
className=" bg-gray-200 rounded-md text-center p-[.5em] font-medium w-[57%] sm:w-100"
42+
maximum={InputHandler.getAllowedMaxInputSize()}
43+
/>
44+
<button onClick={() => {
45+
this.props.InputHandler.setState({generatedArray: InputHandler.handleInputRequest(inputArrayLength)});
46+
}} className="bg-blue-300 p-2 ml-3 rounded-md text-white font-medium hover:scale-[1.02] hover:bg-blue-400 transition duration-200 ease-in-out">Generate Array</button>
47+
</div>
48+
{/* <p className={`text-gray-700 mt-1 ${this.name[1] + ' '}`}>Array size should not exceed {InputHandler.getAllowedMaxInputSize()}.</p> */}
49+
</div>
50+
51+
<div className="w-[41em] overflow-auto flex gap-3 p-1">
52+
<div onClick={() => {
53+
this.Strategy.setStrategy = this.selectionSort;
54+
this.selectionSort.setOptions = options;
55+
this.setState({algorithm: 'selection'});
2756
}}
28-
className=" bg-gray-200 rounded-md text-center p-[.5em] font-medium w-[57%] sm:w-100"
29-
maximum={InputHandler.getAllowedMaxInputSize()}
30-
/>
31-
<button onClick={() => {
57+
className={`hover:scale-[1.02] transition-all duration-200 ease-in-out border-2 ${algorithm === 'selection' ? "bg-blue-100 border-blue-300" : "border-gray-300"} min-w-[150px] rounded-xl text-gray-600 flex flex-col p-3 items-center`}>
58+
59+
<p className="font-semibold">Selection Sort</p>
60+
</div>
61+
62+
<div onClick={() => {
63+
this.Strategy.setStrategy = this.selectionSort;
64+
this.selectionSort.setOptions = options;
65+
this.setState({algorithm: 'merge'});
66+
}}
67+
className={`hover:scale-[1.02] transition-all duration-200 ease-in-out border-2 ${algorithm === 'merge' ? "bg-blue-100 border-blue-300" : "border-gray-300"} min-w-[150px] rounded-xl text-gray-600 flex flex-col p-3 items-center`}>
68+
69+
<p className="font-semibold">Merge Sort</p>
70+
</div>
71+
72+
<div onClick={() => {
73+
this.Strategy.setStrategy = this.selectionSort;
74+
this.selectionSort.setOptions = options;
75+
this.setState({algorithm: 'quick'});
76+
}}
77+
className={`hover:scale-[1.02] transition-all duration-200 ease-in-out border-2 ${algorithm === 'quick' ? "bg-blue-100 border-blue-300" : "border-gray-300"} min-w-[150px] rounded-xl text-gray-600 flex flex-col p-3 items-center`}>
78+
79+
<p className="font-semibold">Quick Sort</p>
80+
</div>
81+
82+
<div onClick={() => {
83+
this.Strategy.setStrategy = this.selectionSort;
84+
this.selectionSort.setOptions = options;
85+
this.setState({algorithm: 'bubble'});
86+
}}
87+
className={`hover:scale-[1.02] transition-all duration-200 ease-in-out border-2 ${algorithm === 'bubble' ? "bg-blue-100 border-blue-300" : "border-gray-300"} min-w-[150px] rounded-xl text-gray-600 flex flex-col p-3 items-center`}>
88+
<p className="font-semibold">Bubble Sort</p>
89+
</div>
90+
91+
</div>
92+
93+
<Options options={this.props.InputHandler}/>
94+
</div>
95+
96+
<div className="flex self-center group gap-3">
97+
<div onClick={() => this.Strategy.perform(options)} className={`p-3 rounded-lg flex ${this.state.algorithm ? "bg-green-600 cursor-pointer hover:bg-green-700" : "bg-gray-600"}`}>
98+
<p className="text-white font-semibold mr-2">Sort</p><Play className={`${this.state.algorithm && "group-hover:scale-[1.15] transition-all duration-200 ease-in-out"}`}/>
99+
</div>
100+
101+
<div className="self-center group">
102+
<div onClick={() => { //TODO need to make sure to kill setTimeout
32103
this.props.InputHandler.setState({generatedArray: InputHandler.handleInputRequest(inputArrayLength)});
33-
}} className="bg-blue-300 p-2 ml-3 rounded-md text-white font-medium hover:scale-[1.02] hover:bg-blue-400 transition duration-200 ease-in-out">Generate Array</button>
104+
}} className={`p-3 rounded-lg flex ${this.state.algorithm ? "bg-green-600 cursor-pointer hover:bg-green-700" : "bg-gray-600"}`}><Replay/></div>
34105
</div>
35-
{/* <p className={`text-gray-700 mt-1 ${this.name[1] + ' '}`}>Array size should not exceed {InputHandler.getAllowedMaxInputSize()}.</p> */}
36106
</div>
37-
<Options options={this.props.InputHandler}/>
38107
</div>
39108
)
40109
}

‎src/components/Canvas/MainCanvas.js‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ class MainCanvas extends Component {
2424
currentMinBarColor: CONSTANTS.CURRENT_MIN[0]
2525
}
2626
};
27-
this.selectionSort = new SelectionSort();
28-
this.Strategy = new Strategy().setStrategy = this.selectionSort;
29-
Strategy.setTargetArray = this.state.generatedArray;
3027
}
3128

3229

@@ -35,18 +32,8 @@ class MainCanvas extends Component {
3532
console.log(options);
3633
return (
3734
<>
38-
<InputField name = {['generic-input', 'instructions']} InputHandler = {this}/>
35+
<InputField name = {['generic-input', 'instructions']} InputHandler={this}/>
3936
<Canvas generatedArray={this.state.generatedArray} barColor={options.defaultBarColor}/>
40-
{this.state.generatedArray.length >= 1 &&
41-
<div className="">
42-
<div className="">
43-
<button onClick={async () => { // SG 07/08/2022 15:03 start sorting using whatever algorithm assigned to Strategy
44-
this.selectionSort.setOptions = this.state.options;
45-
await this.Strategy.perform(options);
46-
}}>Selection Sort</button>
47-
</div>
48-
</div>
49-
}
5037
</>
5138
)
5239
}

‎src/components/Canvas/options/Options.js‎

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class Options extends Component {
117117
}/>
118118
))}
119119
</div>
120-
<p className={`bg-white inline z-10 absolute top-[100%] border-2 p-1 rounded-md hidden peer-hover:block tool-tip-options text-gray-600`}><span className="font-bold">Minimum</span> bar color</p>
120+
<p className={`bg-white inline z-10 absolute top-[100%] border-2 p-1 rounded-md hidden peer-hover:block tool-tip-options text-gray-600`}>Current<span className="font-bold ml-1">optimum</span> bar color</p>
121121
</div>
122122

123123

@@ -146,32 +146,8 @@ class Options extends Component {
146146
</div>
147147
<p className="font-semibold text-gray-500">{this.props.options.state.options.skipJ ? "ON" : "OFF"}</p>
148148
<p className={`bg-white inline z-10 absolute top-[100%] border-2 p-1 rounded-md hidden peer-hover:block tool-tip-options text-gray-600`}><span className="font-bold">Faster</span> Animation</p>
149-
</div>
150-
151-
152-
153-
154-
155-
156-
157-
158-
{/* <p>Default bar color</p>
159-
<p>Current Minimum bar</p>
160-
<p>Processing color</p>
161-
<p>Sorted color</p>
162-
<p>Delay</p>
163-
<div className="flex">
164-
<SlideBar mainCanvas={this.props.options}/>
165-
<p className="ml-10">{this.props.options.state.options?.delay}</p>
166-
</div>
167-
<div className="flex cursor-pointer" onClick={() => {this.setState({showAdvancedOptions: !showAdvancedOptions})}}>
168-
<p>Show advanced options</p>
169-
<ArrowDown className="scale-[.80]"/>
170-
</div>
171-
</div>
172-
{showAdvancedOptions &&
173-
<div>
174-
<p>Omit j iteration</p> */}
149+
</div>
150+
175151
</div>
176152

177153
</div>

‎src/components/Canvas/options/SlideBar.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useState } from 'react'
22

33
function SlideBar(props) {
44
const [value, setValue] = useState(0);
5-
const MAX = 51;
5+
const MAX = 200;
66
const getBackgroundSize = () => {
77
return {
88
backgroundSize: `${(value * 100) / MAX}% 100%`,

‎src/config.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const PINK = '#ff595e';
77

88
const CONSTANTS = {
99
margin: 2,
10-
DEFAULT: [YELLOW, 'red', LIGHT_GREEN, PINK],
10+
DEFAULT: [YELLOW, PINK, LIGHT_GREEN, PURPLE],
1111
SORTED: [LIGHT_GREEN, 'red', YELLOW, PURPLE],
1212
PROCESSING: [DARK_BLUE, 'red', LIGHT_GREEN, PURPLE],
13-
CURRENT_MIN: [LIGHT_GREEN, 'red', YELLOW, PURPLE],
13+
CURRENT_MIN: [LIGHT_GREEN, YELLOW, DARK_BLUE, PURPLE, 'red'],
1414
DELAY: 4, // SG 07/09/2022 13:54 4ms
1515
}
1616

‎src/img/play.svg‎

Lines changed: 3 additions & 0 deletions
Loading

‎src/img/replay.svg‎

Lines changed: 4 additions & 0 deletions
Loading

‎src/index.css‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
body {
7-
margin: 0;
7+
margin: 0; /* Keep body margin 0, otherwise it'll mess up some calculations for array genereation */
88
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
99
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
1010
sans-serif;

0 commit comments

Comments
 (0)