Skip to content

Commit 7fef77e

Browse files
committed
more features implemented
1 parent 1ba00b7 commit 7fef77e

8 files changed

Lines changed: 94 additions & 15 deletions

File tree

‎src/components/Animation/Animation.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class Animation {
4040
var oldID = unSortedBars[k].id;
4141
unSortedBars[k].id = unSortedBars[i].id;
4242
unSortedBars[i].id = oldID; // SG 07/11/2022 09:27 temp1
43+
44+
var oldContent = unSortedBars[i].getAttribute("data");
45+
unSortedBars[i].setAttribute("data", unSortedBars[k].getAttribute("data"));
46+
unSortedBars[k].setAttribute("data", oldContent); // SG 07/11/2022 09:27 temp1
47+
4348
}
4449

4550

@@ -76,6 +81,16 @@ class Animation {
7681
setTimeout(() => {
7782
const [firstBarI, swapHeight] = animation[i];
7883
arrayBars[firstBarI].style.height = swapHeight + "px";
84+
let firstBarHeight = parseInt(arrayBars[firstBarI].style.height.replace('px', ''));
85+
var fontSize = arrayBars[firstBarI].getAttribute('font-size');
86+
87+
// SG 07/13/2022 16:22 making sure that larger font does not overflow, so it height is not enough, set conetnt empty
88+
if(firstBarHeight-10 > fontSize) {
89+
arrayBars[firstBarI].setAttribute('data', swapHeight);
90+
} else {
91+
arrayBars[firstBarI].setAttribute('data', '');
92+
}
93+
arrayBars[firstBarI].id = swapHeight;
7994
}, i * delay);
8095
}
8196
}

‎src/components/Canvas/Canvas.js‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Component } from 'react'
22
import { CONSTANTS } from '../../Utility/config.js';
3-
3+
import ShowValue from './ShowValue.js';
4+
import './style.css'
45
class Canvas extends Component {
56

67
constructor(props) {
@@ -9,20 +10,33 @@ class Canvas extends Component {
910

1011

1112
render() {
12-
const { generatedArray } = this.props;
13+
const { generatedArray, showHeight, defaultBarColor } = this.props;
14+
1315
const arrayLength = generatedArray.length;
1416
const MARGIN = CONSTANTS.margin;
1517
const SCREEN_WIDTH = window.innerWidth;
1618
const BAR_WIDTH = Math.floor((SCREEN_WIDTH - (MARGIN*arrayLength)) / arrayLength);
1719
const MARGIN_BTW_CANVAS_OPTIONS = 122;
1820
const CANVAS_HEIGHT = window.innerHeight - CONSTANTS.OFFSET + MARGIN_BTW_CANVAS_OPTIONS +"px";
21+
const FONT_SIZE = Math.floor(BAR_WIDTH * 0.4);
1922

23+
const PINK = "#ff595e";
24+
const PURPLE = "#6a4c93";
25+
26+
console.log(defaultBarColor)
2027

2128
return (
22-
<div className={`w-full flex justify-center dark:bg-lightGray`} style={{minHeight:CANVAS_HEIGHT}}>
29+
<div className={`w-full flex justify-center dark:bg-lightGray relative`} style={{minHeight:CANVAS_HEIGHT}}>
30+
2331
<div className="flex items-end">
2432
{generatedArray.map((element) => {
25-
return <div id={`${element}`} key={Math.random()} style={{height: element+'px', width: BAR_WIDTH+'px', backgroundColor: this.props.barColor}} className={`array-bars m-[1px] rounded-t-md`}/>
33+
return (
34+
<div id={`${element}`} font-size={FONT_SIZE} data={`${(BAR_WIDTH > 25 && element > FONT_SIZE+16 && showHeight) ? element : ''}`} length="40px" key={Math.random()} style={{"--bar-font-size" :FONT_SIZE+"px", height: element+'px', width: BAR_WIDTH+'px', backgroundColor: this.props.barColor}}
35+
className={`array-bars m-[1px] rounded-t-md relative ${(defaultBarColor !== PINK && defaultBarColor !== PURPLE && defaultBarColor !== 'black') ? "after:text-gray-700" : "after:text-white"}`}>
36+
{/* {(BAR_WIDTH > 38 && element > FONT_SIZE+16) && <p className={`text-center font-bold text-gray-800`} style={{fontSize: FONT_SIZE+"px"}}>{element}</p>} */}
37+
</div>
38+
)
39+
2640
})}
2741
</div>
2842
</div>

‎src/components/Canvas/InputField.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ReactComponent as Replay } from '../../img/replay.svg'
88
import { MergeSort } from '../Strategy/Algorithms/MergeSort.js';
99
import { RadixSortLSD } from '../Strategy/Algorithms/RadixSortLSD.js';
1010
import AlgorithmSelection from './AlgorithmSelection.js';
11+
import ShowValue from './ShowValue.js';
1112

1213

1314
class InputField extends Component {
@@ -60,7 +61,10 @@ class InputField extends Component {
6061

6162

6263
<div className="flex gap-5 md:gap-10 flex-wrap justify-center items-center">
63-
<Options options={this.props.InputHandler} algorithm={algorithm}/>
64+
65+
<Options options={this.props.InputHandler} algorithm={algorithm}/>
66+
67+
6468
<div className="flex self-center gap-3">
6569
<AlgorithmSelection strategy={this.Strategy} options={this}/>
6670
<div onClick={async () => {
@@ -85,6 +89,7 @@ class InputField extends Component {
8589
<Replay className={` ${this.state.algorithm && inputArrayLength && "group-hover:rotate-[330deg]"} transition-all duration-200 ease-out`}/>
8690
</div>
8791
</div>
92+
<ShowValue mainCanvasInputHandler={this.props.InputHandler} inputArrayLength={inputArrayLength}/>
8893

8994
</div>
9095
</div>

‎src/components/Canvas/MainSortingCanvas.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class MainCanvas extends Component {
2323
processingColor: JSON.parse(localStorage.getItem('options'))?.processingColor || CONSTANTS.PROCESSING[0],
2424
defaultBarColor: JSON.parse(localStorage.getItem('options'))?.defaultBarColor || CONSTANTS.DEFAULT[0] ,
2525
sortedBarColor: JSON.parse(localStorage.getItem('options'))?.sortedBarColor || CONSTANTS.SORTED[0],
26-
currentMinBarColor: JSON.parse(localStorage.getItem('options'))?.currentMinBarColor || CONSTANTS.CURRENT_MIN[0] ,
26+
currentMinBarColor: JSON.parse(localStorage.getItem('options'))?.currentMinBarColor || CONSTANTS.CURRENT_MIN[0],
27+
showHeight: JSON.parse(localStorage.getItem('options'))?.showHeight || false
2728
}
2829
};
2930
}
@@ -36,7 +37,7 @@ class MainCanvas extends Component {
3637
<>
3738
<Navigation />
3839
<InputField name = {['generic-input', 'instructions']} InputHandler={this} inputArray={generatedArray}/>
39-
<Canvas generatedArray={this.state.generatedArray} barColor={options.defaultBarColor}/>
40+
<Canvas defaultBarColor={this.state.options.defaultBarColor} showHeight={this.state.options.showHeight} generatedArray={this.state.generatedArray} barColor={options.defaultBarColor} InputHandler={this}/>
4041
</>
4142
)
4243
}

‎src/components/Canvas/ShowValue.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React, { useState } from 'react'
2+
import { CONSTANTS } from '../../Utility/config';
3+
4+
function ShowValue({mainCanvasInputHandler, inputArrayLength}) {
5+
const MARGIN = CONSTANTS.margin;
6+
const SCREEN_WIDTH = window.innerWidth;
7+
const BAR_WIDTH = Math.floor((SCREEN_WIDTH - (MARGIN*inputArrayLength)) / inputArrayLength);
8+
9+
10+
const [enable, setEnable] = useState(JSON.parse(localStorage.getItem('options'))?.showHeight || false);
11+
console.log(enable);
12+
return (
13+
<div className={`${BAR_WIDTH > 25 ? 'block' : 'hidden'} dark:lg:hover:bg-gray-200 lg:hover:bg-lightGreen group relative rounded-md ${mainCanvasInputHandler.state.options.showHeight && "dark:bg-gray-200 bg-lightGreen"}`} onClick={() => {
14+
mainCanvasInputHandler.setState(prev => ({options: {...prev.options, showHeight: !mainCanvasInputHandler.state.options.showHeight}}));
15+
setEnable(!enable);
16+
let j = JSON.parse(localStorage.getItem('options'));
17+
j["showHeight"] = !enable;
18+
localStorage.setItem('options', JSON.stringify(j))
19+
}}>
20+
21+
<div className={`flex items-center justify-center relative space-y-2 cursor-pointer h-full border-2 dark:border-gray-200 border-gray-300 dark:border-[1px] rounded-md ${mainCanvasInputHandler.state.options.showHeight && "dark:border-gray-400"}`}>
22+
<div className={`p-2 peer`}>
23+
{<div className={`scale-[1.3] ${mainCanvasInputHandler.state.options.showHeight ? "text-white dark:text-[#7B7B7c]" : "text-[#7B7B7c]"} w-5 text-center font-bold`}>N</div>}
24+
</div>
25+
</div>
26+
<p className={`shadow-lg bg-white z-10 absolute top-[4.4em] left-[50%] -translate-x-[50%] p-2 rounded-md hidden group-hover:block tool-tip-options text-gray-600 whitespace-nowrap font-semibold`}>Show values</p>
27+
</div>
28+
)
29+
}
30+
31+
export default ShowValue

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ function Option({options, mainCanvasInputHandler, algorithm}) {
99
useEffect(() => {
1010
if(options.hasDropDown) {
1111
var dropdown = document.getElementById(options.name + "-option");
12-
document.addEventListener('click', (event) => {
13-
var isClickInsideElement = dropdown.contains(event.target);
14-
if (!isClickInsideElement) {
15-
setOpen(false);
16-
}
17-
});
12+
if(dropdown) {
13+
document.addEventListener('click', (event) => {
14+
var isClickInsideElement = dropdown.contains(event.target);
15+
if (!isClickInsideElement) {
16+
setOpen(false);
17+
}
18+
});
19+
}
1820
}
1921
}, [open])
2022

@@ -31,7 +33,7 @@ function Option({options, mainCanvasInputHandler, algorithm}) {
3133
</div>
3234
)
3335
}
34-
36+
3537
if(options.name === "DELAY") {
3638
return (
3739
<div className="flex flex-col items-center relative space-y-2 cursor-pointer">
@@ -62,7 +64,7 @@ function Option({options, mainCanvasInputHandler, algorithm}) {
6264
</div>
6365
<div className={`h-3 w-3 rounded-full`} style={{backgroundColor: mainCanvasInputHandler.state.options[options.stateName]}}/>
6466
<div className={`options-dropdown transition-all duration-200 ease-in-out absolute bg-white dark:bg-darkGray dark:border-lightDark p-2 rounded-md border-2 top-[100%] space-y-1 ${open ? "scale-100" : "scale-0 -translate-y-[7em]"}`}>
65-
{CONSTANTS["CURRENT_MIN"].map((color) => (
67+
{CONSTANTS[options.name].map((color) => (
6668
<div
6769
key={color}
6870
style={{backgroundColor: color}}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class Options extends Component {
7171

7272

7373
render() {
74+
7475
const { algorithm } = this.props;
7576

7677
return (

‎src/components/Canvas/style.css‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.array-bars::after {
2+
content: attr(data);
3+
position: absolute;
4+
top: 0;
5+
left: 50%;
6+
transform: translate(-50%);
7+
font-size: var(--bar-font-size);
8+
font-weight: 700;
9+
10+
}

0 commit comments

Comments
 (0)