-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
201 lines (157 loc) · 7.82 KB
/
script.js
File metadata and controls
201 lines (157 loc) · 7.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import {sleep} from "./scripts/sleep.js";
import {generateNewArray} from "./scripts/generateNewArray.js";
import {convertArrayToString} from "./scripts/convertArrayToString.js";
import {swap} from "./scripts/swap.js";
import {addBlocksToVisualizer} from './scripts/addBlocksToVisualizer.js';
import {applyOldTheme} from "./scripts/applyOldTheme.js";
import {speedController} from "./scripts/speedController.js";
//so that globals don't do scope pollution
function init(){
//globals
let arrayBeingAnimatedInitialState = [];
let arrayBeingAnimatedCurrentState = [];
let globalIndex = 0;
let globalPass = 1;
let shouldLoopRun = false;
let sleepFunctionTime = 1000;
//dom elements that we need to manipulate
let arrayToTextContainer = document.getElementsByClassName("array-to-text-container")[0];
let newArrayButton = document.getElementsByClassName("new-array-button")[0];
let startAnimationButton = document.getElementsByClassName("start-animation-button")[0];
let sortingStatusDiv = document.getElementsByClassName("sorting-status-div")[0];
let themeSwitchButton = document.getElementsByClassName("theme-switch-button")[0];
//FUNCTIONS
//default value
sortingStatusDiv.innerText="Please create a new array";
const indexIncrementHandler = ()=>{
globalIndex++;
if(globalIndex===arrayBeingAnimatedCurrentState.length-globalPass){
globalPass++;
globalIndex=0;
if(globalPass===arrayBeingAnimatedInitialState.length){
return false;
}
}
return true;
}
const changeArrayToTextContainer = (value)=>{
arrayToTextContainer.innerHTML = value;
}
//event handlers
const newArrayOnClickHandler = ()=>{
startAnimationButton.disabled=false;
sortingStatusDiv.innerText="Start Sorting";
shouldLoopRun=false;
let newArray = generateNewArray();
let arrayToStringValue = convertArrayToString(newArray);
changeArrayToTextContainer(arrayToStringValue);
addBlocksToVisualizer(newArray);
//assign the generated array to global so that we can keep track of length
//and call various functions
arrayBeingAnimatedInitialState = newArray;
arrayBeingAnimatedCurrentState = newArray;
globalIndex=0;
globalPass=1;
}
const startAnimationOnClickHandler = async()=>{
startAnimationButton.disabled=true;
sortingStatusDiv.innerText="In Progress";
//to prevent infinite loop
let iterationCounter = 0;
shouldLoopRun=true;
while(shouldLoopRun){
let index=globalIndex;
let blocksArray = document.getElementsByClassName("block");
let animationDurationInRoot = document.querySelector(":root").style.getPropertyValue("--animation-duration");
console.log(animationDurationInRoot);
if(animationDurationInRoot==="1s"){
sleepFunctionTime=1000;
}else if(animationDurationInRoot==="0.5s"){
sleepFunctionTime=500;
}else if(animationDurationInRoot==="0.25s"){
sleepFunctionTime=250;
}
//highlight the elements in focus
blocksArray[index].classList.add("block-in-focus");
blocksArray[index+1].classList.add("block-in-focus");
//if the current element is greater than the next element, swap them
if(arrayBeingAnimatedCurrentState[index]>arrayBeingAnimatedCurrentState[index+1]){
//animation classes, this starts the animation
blocksArray[index+1].classList.add('move-left');
blocksArray[index].classList.add('move-right');
//swap the elements internally
swap(index,index+1,arrayBeingAnimatedCurrentState);
}
//wait for animation to end
await sleep(sleepFunctionTime);
//now remove the focus from blocks and wait few more seconds
blocksArray[index+1].classList.remove('block-in-focus');
blocksArray[index].classList.remove('block-in-focus');
await sleep(sleepFunctionTime);
//if the user created a new array,
//then we set false to shouldLoopRun, if that is not the case
//or sorting has completed, keep running
if(!shouldLoopRun){
break;
}
//increment the index
//if it returns false,break the while loop
if(!indexIncrementHandler()){
sortingStatusDiv.innerText="Completed!";
break;
}
//if the loop is not broken
addBlocksToVisualizer(arrayBeingAnimatedCurrentState);
//to avoid infinite loop
iterationCounter++;
if(iterationCounter===1000){
break;
}
}
}
//event listeners
newArrayButton.addEventListener('click',newArrayOnClickHandler);
startAnimationButton.addEventListener("click",startAnimationOnClickHandler);
themeSwitchButton.addEventListener("click",function(){
if(this.classList.contains("theme-switch-button-light-mode")){
this.classList.remove("theme-switch-button-light-mode");
this.classList.add("theme-switch-button-dark-mode");
let root = document.querySelector(":root");
root.style.setProperty("--background-color-light","rgb(110, 99, 99)");
root.style.setProperty("--background-color-dark","rgb(51, 47, 47)");
root.style.setProperty("--container-box-shadow","rgb(29, 27, 27)");
root.style.setProperty("--container-color-light","white");
root.style.setProperty("--container-color-dark","rgb(223, 211, 211)");
root.style.setProperty("--container-color-darker","rgb(154, 144, 144)");
root.style.setProperty("--content-color-one-base","rgb(148, 0, 211)");
root.style.setProperty("--content-color-one-light","rgb(175, 24, 240)");
root.style.setProperty("--content-color-one-dark","rgb(104, 1, 148)");
root.style.setProperty("--content-color-two-base","rgb(71, 67, 67)");
root.style.setProperty("--disabled-button-background","rgb(154, 144, 144)");
root.style.setProperty("--disabled-button-color","rgb(0, 0, 0)");
localStorage.setItem("theme","dark");
}else{
this.classList.remove("theme-switch-button-dark-mode");
this.classList.add("theme-switch-button-light-mode");
let root = document.querySelector(":root");
root.style.setProperty("--background-color-light","#f5f5f5");
root.style.setProperty("--background-color-dark","#e9e8e8");
root.style.setProperty("--container-box-shadow","rgb(29, 27, 27)");
root.style.setProperty("--container-color-light","white");
root.style.setProperty("--container-color-dark","rgb(251, 249, 249)");
root.style.setProperty("--container-color-darker","rgb(221, 220, 220)");
root.style.setProperty("--content-color-one-base","#ff624f");
root.style.setProperty("--content-color-one-light","#fc7060");
root.style.setProperty("--content-color-one-dark","#e75646");
root.style.setProperty("--content-color-two-base","#484e49");
root.style.setProperty("--disabled-button-background","rgb(154, 144, 144)");
root.style.setProperty("--disabled-button-color","rgb(0, 0, 0)");
localStorage.setItem("theme","light");
}
})
//remembers the last selected theme
applyOldTheme();
//added event listener to speed controller
speedController();
}
init();