-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
179 lines (151 loc) · 4.58 KB
/
app.js
File metadata and controls
179 lines (151 loc) · 4.58 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
let numOfRows = 4;
let numOfCols = 10;
const MININT = 0;
const MAXINT = 20;
const state = {
empty: 0,
showNum: 1,
showChar: 2,
showPointer: 3
};
const stateControl = {
count: 0,
functions: [setEmptyState, setNumState, setCharState, setPointerState],
next(e) {
let elem = e.target;
this.count = parseInt(elem.getAttribute("data-state"));
// Create a continuous cycle - Increment the counter. Then modulo the result
// based on the number of functions in the array
this.count = ++this.count % this.functions.length;
this.functions[this.count](e);
}
}
/** Helper functions **/
function addGlobalEventListener(type, selector, callback) {
document.addEventListener(type, e => {
if (e.target.matches(selector)) {
callback(e);
}
});
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
/*
Get a random int between the unicode value starting from A
to the unicode value ending at Z. Convert to a character and return it.
*/
function getRandomChar() {
return String.fromCharCode(getRandomInt('A'.charCodeAt(), 'Z'.charCodeAt()));
}
function getNumOfCols() {
return parseInt(getComputedStyle(document.documentElement).getPropertyValue('--grid-cols'))
}
function getNumOfRows() {
return parseInt(getComputedStyle(document.documentElement).getPropertyValue('--grid-rows'))
}
function updateArraySize(cols = null, rows = null) {
if (cols) {
document.documentElement.style.setProperty('--grid-cols', `${cols}`);
}
if (rows) {
document.documentElement.style.setProperty('--grid-rows', `${rows}`);
}
}
function createGrid() {
// Headers
for (let i = 0; i < numOfCols; i++) {
createHeaderCell("#col-header", i);
}
for (let i = 0; i < numOfRows; i++) {
createHeaderCell("#row-header", i, true);
}
// Arrays
const arrayElem = document.querySelector('#array');
let cols = getNumOfCols();
let rows = getNumOfRows();
const totalCells = cols * rows;
for (let i = 0; i < totalCells; i++) {
arrayElem.appendChild(createCell());
}
}
function createHeaderCell(parent, i, row = false) {
let cell = document.createElement("div");
if (!row) {
cell.classList.add("col-header-cell");
}
else {
cell.classList.add("row-header-cell");
}
cell.textContent = i;
const header = document.querySelector(parent);
header.appendChild(cell);
}
function createCell() {
let cell = document.createElement('div');
cell.classList.add("cell");
cell.setAttribute("data-state", state.showNum);
cell.textContent = getRandomInt(MININT, MAXINT);
return cell
}
function setEmptyState(e) {
let elem = e.target;
elem.setAttribute("data-state", state.empty);
elem.textContent = ".";
elem.classList.toggle("empty-state")
elem.classList.toggle("pointer-state");
}
function setNumState(e) {
let elem = e.target;
elem.setAttribute("data-state", state.showNum);
elem.textContent = getRandomInt(MININT, MAXINT);
elem.classList.remove("pointer-state", "empty-state");
}
function setCharState(e) {
let elem = e.target;
elem.setAttribute("data-state", state.showChar);
elem.textContent = getRandomChar();
elem.classList.remove("pointer-state", "empty-state");
}
function setPointerState(e) {
let elem = e.target;
elem.setAttribute("data-state", state.showPointer);
elem.textContent = "△";
elem.classList.toggle("pointer-state");
}
// WIP - need to be able to update all cells not only by updating data attribute
// but also by running setEmptyState function;
function clearCells() {
const cells = document.querySelectorAll('.cell');
for (const elem of cells) {
elem.setAttribute("data-state", state.empty);
elem.textContent = ".";
elem.classList.toggle("empty-state")
elem.classList.toggle("pointer-state");
}
}
function updateGrid(e) {
let inputValue = e.target.value;
}
function addPointer(e) {
let elem = e.target;
elem.classList.toggle('index');
}
// eslint-disable-next-line no-unused-vars, no-undef
const sortableArray = Sortable.create(array, {
swap: true,
swapClass: 'highlight',
ghostClass: 'ghost',
animation: 100,
easing: 'cubic-bezier(0.65, 0, 0.35, 1)'
});
createGrid();
/** Event handling **/
// Cell state
document.addEventListener('click', e => {
if (e.target.matches('.cell')) {
stateControl.next(e);
}
});
// Pointers on column header
addGlobalEventListener('click', '.col-header-cell', addPointer);