-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
249 lines (216 loc) · 7.35 KB
/
scripts.js
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
var carousel = document.querySelector('.carousel');
var cells = carousel.querySelectorAll('.carousel__cell');
var cellsRange = document.querySelector('.cells-range');
var cellCount = cellsRange.min; // cellCount set from cells-range input value
var selectedIndex = 0;
var cellWidth = carousel.offsetWidth;
var cellHeight = carousel.offsetHeight;
var isHorizontal = false;
var rotateFn = isHorizontal ? 'rotateY' : 'rotateX';
var radius, theta;
// console.log( cellWidth, cellHeight );
cellsRange.addEventListener( 'input', changeCarousel );
changeCarousel();
function rotateCarousel() {
var angle = theta * selectedIndex * -1;
carousel.style.transform = 'translateZ(' + -radius + 'px) ' +
rotateFn + '(' + angle + 'deg)';
}
let rotateToggle = false;
let formVisible = false;
let rotateInterval;
//spin carousel on toggle
var spinButton = document.querySelector('#spinner');
spinButton.addEventListener("click", function() {
// not spinning
if (rotateToggle){
spinButton.classList.remove('blink');
spinButton.classList.add('green-btn');
spinButton.classList.remove('red-btn');
spinButton.innerHTML = "Spin!";
rotateToggle = false;
if (rotateInterval !== null)
{
clearInterval(rotateInterval);
}
}
else{
//spinning
rotateToggle = true;
spinButton.classList.add('blink');
spinButton.classList.remove('green-btn');
spinButton.classList.add('red-btn');
spinButton.innerHTML = "Stop"
rotateInterval = setInterval(() => {
if (rotateToggle)
{
selectedIndex++;
rotateCarousel();
}
}, 100)
}
});
var showNamesFormButton = document.querySelector('#showNamesFormButton');
showNamesFormButton.addEventListener("click", function() {
formVisible = !formVisible; // toggle form visibility tracker
toggleDiv("newName")
if (formVisible){
showNamesFormButton.innerHTML = "Hide Names Menu";
}
else{
showNamesFormButton.innerHTML = "Update Names";
}
});
var addNamesButton = document.querySelector('#addNew');
addNamesButton.addEventListener("click", function() {
cellCount++;
new_name();
});
var updateNamesButton = document.querySelector('#updateNamesButton');
updateNamesButton.addEventListener("click", function() {
update_names();
});
var resetNamesButton = document.querySelector('#resetNamesButton');
resetNamesButton.addEventListener("click", function() {
reset_names();
});
function toggleDiv(id) {
var div = document.getElementById(id);
div.style.display = div.style.display == "none" ? "block" : "none";
//attempted to animate a collapse, but html needs restructured
// var height = div.clientHeight;
// var width = div.clientWidth;
// div.style.height = height + 'px';
// div.style.width = width + 'px';
// if(div.style.visibility == 'hidden'){
// div.style.visibility = 'visible';
// //div.style.opacity = '1';
// div.style.height = height + 'px';
// div.style.width = width + 'px';
// div.style.padding = '.5em';
// }
// else{
// div.style.visibility = 'hidden';
// //div.style.opacity = '0';
// div.style.height = '0';
// div.style.width = '0';
// div.style.padding = '0';
// }
}
// var nextButton = document.querySelector('.next-button');
// nextButton.addEventListener( 'click', function() {
// if(this.innerHTML == "Start") {
// window.setInterval(function() {
// selectedIndex++;
// rotateCarousel();
// }, 100);
// } else if(this.innerHTML == "Stop") {
// } else { //reset
// }
// // selectedIndex++;
// // rotateCarousel();
// });
function changeCarousel() {
cellCount = cellsRange.value;
theta = 360 / cellCount;
var cellSize = isHorizontal ? cellWidth : cellHeight;
radius = Math.round( ( cellSize / 2) / Math.tan( Math.PI / cellCount ) );
for ( var i=0; i < cells.length; i++ ) {
var cell = cells[i];
if ( i < cellCount ) {
// visible cell
cell.style.opacity = 1;
var cellAngle = theta * i;
cell.style.transform = rotateFn + '(' + cellAngle + 'deg) translateZ(' + radius + 'px)';
} else {
// hidden cell
cell.style.opacity = 0;
cell.style.transform = 'none';
}
}
rotateCarousel();
}
/*
Add a new form to the page
*/
async function new_name()
{
console.log(`evaluating adding new name. current value = ${cellsRange.value}, max value = ${cellsRange.max}`);
if (Number(cellsRange.value) < Number(cellsRange.max)){
cellsRange.value++;
}
else{
console.log("could not add new name, max limit reached");
alert(`You have reached the maximum number of names.`)
return;
}
var ct = cellsRange.value;
var div1 = document.createElement('div');
div1.innerHTML = document.getElementById('newNameTemplate').innerHTML;
div1.id = "nameTable" + ct;
div1.querySelector(".name-text").innerHTML = "Name " + ct + ":";
div1.querySelector(".name-input").id = "NameInput" + ct;
var cellDiv = document.createElement('div');
cellDiv.innerHTML = document.getElementById('newCellTemplate').innerHTML;
cellDiv.id = `name${ct}`;
console.log(`Created ${cellDiv.id}`);
var deleteButton = div1.querySelector(".delete-button");
deleteButton.id = "DeleteButton" + ct;
deleteButton.innerHTML = "X";
deleteButton.onclick = function (){delete_name(deleteButton.id)};
console.log(`Created ${deleteButton.id}`);
console.log(deleteButton)
await document.getElementById('nameTableWrapper').appendChild(div1);
await document.getElementById('carousel').appendChild(cellDiv);
changeCarousel();
}
// function to delete the newly added set of elements
function delete_name(eleId)
{
cellCount--;
console.log(`attempting to delete ${eleID}`);
d = document;
var ele = d.getElementById(eleId);
var parentEle = d.getElementById('newName');
parentEle.removeChild(ele);
}
function update_names(){
cellCount = cellsRange.value;
console.log( `cell length ${cellCount}`)
for ( var i=0; i < cellCount; i++ ) {
var cell = cells[i];
console.log(`Updating name within NameInput${i+1}`)
var cellNameInput = document.querySelector(`#NameInput${i+1}`).value;
cell.innerHTML = cellNameInput;
console.log(`testing inner html of cell: ${cell.innerHTML}`);
}
changeCarousel();
}
function reset_names(){
//cellsRange.value = cellsRange.min;
var carousel = document.getElementById("carousel");
carousel.removeChild(carousel.childNodes[0]);
cellCount--;
changeCarousel();
//await document.getElementById('carousel').removeChild(cellDiv);
}
window.addEventListener('load', function() {
//cellsRange.value = cells.cellCount;
this.console.log(`cell range value ${cellsRange.value}`);
console.log('All assets are loaded')
})
// var orientationRadios = document.querySelectorAll('input[name="orientation"]');
// ( function() {
// for ( var i=0; i < orientationRadios.length; i++ ) {
// var radio = orientationRadios[i];
// radio.addEventListener( 'change', onOrientationChange );
// }
// })();
// function onOrientationChange() {
// var checkedRadio = document.querySelector('input[name="orientation"]:checked');
// isHorizontal = checkedRadio.value == 'horizontal';
// rotateFn = isHorizontal ? 'rotateY' : 'rotateX';
// changeCarousel();
// }
// // set initials
// onOrientationChange();