Skip to content

Commit 717660f

Browse files
committed
minor updates
1 parent 8654bfe commit 717660f

2 files changed

Lines changed: 74 additions & 57 deletions

File tree

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,76 @@
1-
import './main-canvas.css';
2-
import React, { Component } from 'react'
3-
import { InputHandler } from '../InputHandler/InputHandler';
4-
import { Canvas } from './Canvas';
5-
import { InputField } from './InputField';
6-
import { CONSTANTS } from '../../Utility/config'
7-
import Navigation from '../Page/partial/Navbar/Navigation';
8-
1+
import "./main-canvas.css";
2+
import React, { Component } from "react";
3+
import { InputHandler } from "../InputHandler/InputHandler";
4+
import { Canvas } from "./Canvas";
5+
import { InputField } from "./InputField";
6+
import { CONSTANTS } from "../../Utility/config";
7+
import Navigation from "../Page/partial/Navbar/Navigation";
8+
import { Animation } from "../Animation/Animation";
99

1010
class MainCanvas extends Component {
1111
constructor(props) {
1212
super(props);
1313
this.InputHandler = new InputHandler();
14-
this.state = {
14+
this.state = {
1515
generatedArray: InputHandler.getGeneratedArray,
1616
inputArrayLength: InputHandler.getGeneratedArray.length,
17-
options: { // SG 07/11/2022 12:36 load config from localStorage if exists
18-
delay: JSON.parse(localStorage.getItem('options'))?.delay || CONSTANTS.DELAY,
19-
skipJ: JSON.parse(localStorage.getItem('options'))?.skipJ || false,
20-
processingColor: JSON.parse(localStorage.getItem('options'))?.processingColor || CONSTANTS.PROCESSING[0],
21-
defaultBarColor: JSON.parse(localStorage.getItem('options'))?.defaultBarColor || CONSTANTS.DEFAULT[0] ,
22-
sortedBarColor: JSON.parse(localStorage.getItem('options'))?.sortedBarColor || CONSTANTS.SORTED[0],
23-
currentMinBarColor: JSON.parse(localStorage.getItem('options'))?.currentMinBarColor || CONSTANTS.CURRENT_MIN[0],
24-
showHeight: JSON.parse(localStorage.getItem('options'))?.showHeight || false
25-
}
17+
options: {
18+
// SG 07/11/2022 12:36 load config from localStorage if exists
19+
delay:
20+
JSON.parse(localStorage.getItem("options"))?.delay || CONSTANTS.DELAY,
21+
skipJ: JSON.parse(localStorage.getItem("options"))?.skipJ || false,
22+
processingColor:
23+
JSON.parse(localStorage.getItem("options"))?.processingColor ||
24+
CONSTANTS.PROCESSING[0],
25+
defaultBarColor:
26+
JSON.parse(localStorage.getItem("options"))?.defaultBarColor ||
27+
CONSTANTS.DEFAULT[0],
28+
sortedBarColor:
29+
JSON.parse(localStorage.getItem("options"))?.sortedBarColor ||
30+
CONSTANTS.SORTED[0],
31+
currentMinBarColor:
32+
JSON.parse(localStorage.getItem("options"))?.currentMinBarColor ||
33+
CONSTANTS.CURRENT_MIN[0],
34+
showHeight:
35+
JSON.parse(localStorage.getItem("options"))?.showHeight || false,
36+
},
2637
};
2738
}
2839

2940
componentDidMount() {
30-
const darkTheme = localStorage.getItem('theme') === "dark";
31-
const options = JSON.parse(localStorage.getItem('options'));
32-
33-
// SG 07/16/2022 20:03 if option not provided and theme is dark, set default bar colro to white
34-
if(darkTheme && !options?.defaultBarColor) {
35-
this.setState({options: {...this.state.options, defaultBarColor: 'white'}})
41+
const darkTheme = localStorage.getItem("theme") === "dark";
42+
const options = JSON.parse(localStorage.getItem("options"));
43+
44+
// SG 07/16/2022 20:03 if option not provided and theme is dark, set default bar colro to white
45+
if (darkTheme && !options?.defaultBarColor) {
46+
this.setState({
47+
options: { ...this.state.options, defaultBarColor: "white" },
48+
});
3649
}
3750
}
3851

39-
4052
render() {
4153
const { options, generatedArray } = this.state;
42-
54+
4355
return (
4456
<>
45-
<Navigation themeToggle={true}/>
46-
<InputField name = {['generic-input', 'instructions']} InputHandler={this} generatedArray={generatedArray} inputArray={generatedArray}/>
47-
<Canvas defaultBarColor={this.state.options.defaultBarColor} showHeight={this.state.options.showHeight} generatedArray={this.state.generatedArray} barColor={options.defaultBarColor} InputHandler={this}/>
57+
<Navigation themeToggle={true} />
58+
<InputField
59+
name={["generic-input", "instructions"]}
60+
InputHandler={this}
61+
generatedArray={generatedArray}
62+
inputArray={generatedArray}
63+
/>
64+
<Canvas
65+
defaultBarColor={this.state.options.defaultBarColor}
66+
showHeight={this.state.options.showHeight}
67+
generatedArray={this.state.generatedArray}
68+
barColor={options.defaultBarColor}
69+
InputHandler={this}
70+
/>
4871
</>
49-
)
72+
);
5073
}
5174
}
5275

53-
54-
55-
export default MainCanvas;
76+
export default MainCanvas;
Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
1-
import { Animation } from "../../Animation/Animation"
2-
1+
import { Animation } from "../../Animation/Animation";
32

43
class HeapSort {
5-
64
constructor() {
75
this.options = null;
86
}
97

108
getIndexDOM(element, DOM) {
11-
for(const e of DOM) {
12-
if(element === parseInt(e.id)) return DOM.indexOf(e);
9+
for (const e of DOM) {
10+
if (element === parseInt(e.id)) return DOM.indexOf(e);
1311
}
1412
}
1513

1614
async heapify(array, n, i) {
1715
let max = i;
18-
let left = (2 * i) + 1;
19-
let right = (2 * i) + 2;
20-
let DOM = Array.from(document.querySelectorAll('.array-bars'));
16+
let left = 2 * i + 1;
17+
let right = 2 * i + 2;
18+
let DOM = Array.from(document.querySelectorAll(".array-bars"));
2119
let currentBar = this.getIndexDOM(array[i], DOM);
2220

2321
// SG 07/15/2022 14:06 making sure that the last bar - when n = 1, is not colored
24-
if(n > 2) {
22+
if (n > 2) {
2523
DOM[currentBar].style.backgroundColor = this.options.processingColor;
2624
}
2725

28-
if(left < n && array[left] > array[max]) {
26+
if (left < n && array[left] > array[max]) {
2927
max = left;
3028
}
3129

32-
if(right < n && array[right] > array[max]) {
30+
if (right < n && array[right] > array[max]) {
3331
max = right;
3432
}
3533

36-
if(max !== i) {
34+
if (max !== i) {
3735
// SG 07/15/2022 13:23 swapping, sorted
3836
let first = this.getIndexDOM(array[i], DOM);
3937
let second = this.getIndexDOM(array[max], DOM);
4038

41-
if(!this.options.skipJ) {
39+
if (!this.options.skipJ) {
4240
await Animation.getAnimation(this.options.delay);
4341
}
4442

@@ -52,18 +50,18 @@ class HeapSort {
5250
}
5351

5452
// SG 07/15/2022 14:06 making sure that the last bar - when n = 1, is not colored
55-
if(n > 2) {
53+
if (n > 2) {
5654
DOM[currentBar].style.backgroundColor = this.options.defaultBarColor;
5755
}
5856
}
5957

6058
async heapSort(array, n) {
61-
for(let i = Math.floor(n / 2) - 1; i >= 0; i--) {
59+
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
6260
await this.heapify(array, n, i);
6361
}
6462

65-
for(let i = n - 1; i >= 0; i--) {
66-
let DOM = Array.from(document.querySelectorAll('.array-bars'));
63+
for (let i = n - 1; i >= 0; i--) {
64+
let DOM = Array.from(document.querySelectorAll(".array-bars"));
6765

6866
// SG 07/15/2022 13:20 swaping value to the end
6967
let first = this.getIndexDOM(array[0], DOM);
@@ -74,15 +72,15 @@ class HeapSort {
7472

7573
// SG 07/15/2022 13:52 coloring sorted bars
7674
DOM[second].style.backgroundColor = this.options.sortedBarColor;
77-
75+
7876
let temp = array[0];
7977
array[0] = array[i];
8078
array[i] = temp;
81-
82-
await this.heapify(array, i , 0);
79+
80+
await this.heapify(array, i, 0);
8381
}
8482

85-
return array;;
83+
return array;
8684
}
8785

8886
async perform(options, array) {
@@ -91,6 +89,4 @@ class HeapSort {
9189
}
9290
}
9391

94-
95-
96-
export { HeapSort }
92+
export { HeapSort };

0 commit comments

Comments
 (0)