Skip to content

Commit 7cc2e95

Browse files
author
astrielov
committed
things have changed
1 parent 3b0cc0d commit 7cc2e95

8 files changed

Lines changed: 595 additions & 439 deletions

File tree

.idea/workspace.xml

Lines changed: 101 additions & 157 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prototypes/temp.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function User(name) {
2+
this.name = name;
3+
}
4+
5+
function Admin(isPunishable) {
6+
this.isPunishable = isPunishable;
7+
}
8+
9+
Admin.prototype.punish = (name) => {
10+
console.log(this);
11+
if (this.isPunishable) { console.log(`${name} has been punished`); }
12+
};
13+
14+
Object.setPrototypeOf(Admin.prototype, User.prototype);
15+
16+
const human = {
17+
isHuman: true,
18+
};
19+
20+
User.prototype = human;
21+
22+
console.log(new User('Bob'));
23+
const admin = new Admin(true);
24+
console.log(admin);
25+
admin.punish('Greg');

selectors/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@
1010
</select>
1111
</body>
1212
<script src="selectors.js"></script>
13+
<script>
14+
select.style.width = '400px';
15+
select.style.height = '550px';
16+
</script>
1317
</html>

selectors/selectors.js

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,54 @@
1-
const fragment = document.createDocumentFragment();
21
const select = document.querySelector('select');
32
let dataArray;
4-
let parentIndex;
5-
select.style.width = '400px';
6-
select.style.height = '600px';
7-
8-
function hasNoParent(currentObj) {
9-
return !dataArray.some(e => (currentObj.left > e.left && currentObj.right < e.right));
10-
}
11-
12-
function hasParent(currentObj) {
13-
return dataArray.some((elem, index) => {
14-
if (elem.right === currentObj.right + 1 && elem.left < currentObj.left) {
15-
parentIndex = index;
16-
return true;
17-
}
18-
return false;
19-
});
20-
}
21-
22-
function hasChild(currentObj) {
23-
return currentObj.left + 1 !== currentObj.right;
24-
}
253

264
function setLeftPadding(charAmount) {
275
this.style.paddingLeft = `${charAmount}ch`;
286
}
297

30-
function recursivePaste(index = 0, paddingCharAmount = 0) {
31-
const currentObj = dataArray[index];
8+
function fetchElement(siblings, left, right, depth = 0) {
9+
dataArray.forEach((item) => {
10+
if (item.left === left) {
11+
siblings.push(item);
3212

33-
if (hasNoParent(currentObj)) {
34-
paddingCharAmount = 0;
35-
}
13+
const currItem = siblings[siblings.length - 1];
14+
currItem.depth = depth;
3615

37-
const op = new Option(currentObj.name, currentObj.id);
38-
setLeftPadding.call(op, paddingCharAmount * 2);
39-
fragment.appendChild(op);
16+
if (item.left + 1 < item.right) {
17+
currItem.childs = fetchElement([], item.left + 1, item.right - 1, depth + 1);
18+
}
4019

41-
if (hasChild(currentObj)) {
42-
paddingCharAmount += 1;
43-
}
20+
if (right && item.right < right) {
21+
siblings = fetchElement(siblings, item.right + 1, right, depth);
22+
}
23+
}
24+
});
25+
return siblings;
26+
}
4427

45-
while (!hasChild(currentObj) && hasParent(currentObj)) {
46-
dataArray.splice(parentIndex, 1);
47-
index -= 1;
48-
paddingCharAmount -= 1;
28+
function makeDocumentFragment(nestedSetData) {
29+
const fragment = document.createDocumentFragment();
30+
31+
function paste(array) {
32+
array.forEach((x) => {
33+
const op = new Option(x.name, x.id);
34+
setLeftPadding.call(op, x.depth * 2);
35+
fragment.appendChild(op);
36+
if (x.childs) {
37+
paste(x.childs);
38+
}
39+
});
4940
}
5041

51-
index += 1;
52-
if (index < dataArray.length) {
53-
recursivePaste(index, paddingCharAmount);
54-
}
42+
paste(nestedSetData);
43+
return fragment;
5544
}
5645

5746
fetch('https://sectors-enpoint.herokuapp.com/sectors')
5847
.then(response => response.json())
5948
.then((array) => {
60-
dataArray = array.sort((a, b) => (a.left - b.left));
49+
dataArray = array.sort((a, b) => a.left - b.left);
6150

62-
recursivePaste();
51+
const nestedSetData = fetchElement([], 1, dataArray.length * 2);
52+
const fragment = makeDocumentFragment(nestedSetData);
6353
select.appendChild(fragment);
6454
});

selectors/selectorsOld.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const fragment = document.createDocumentFragment();
2+
const select = document.querySelector('select');
3+
let dataArray;
4+
let parentIndex;
5+
select.style.width = '400px';
6+
select.style.height = '600px';
7+
8+
function hasNoParent(currentObj) {
9+
return !dataArray.some(e => (currentObj.left > e.left && currentObj.right < e.right));
10+
}
11+
12+
function hasParent(currentObj) {
13+
return dataArray.some((elem, index) => {
14+
if (elem.right === currentObj.right + 1 && elem.left < currentObj.left) {
15+
parentIndex = index;
16+
return true;
17+
}
18+
return false;
19+
});
20+
}
21+
22+
function hasChild(currentObj) {
23+
return currentObj.left + 1 !== currentObj.right;
24+
}
25+
26+
function setLeftPadding(charAmount) {
27+
this.style.paddingLeft = `${charAmount}ch`;
28+
}
29+
30+
function recursivePaste(index = 0, paddingCharAmount = 0) {
31+
const currentObj = dataArray[index];
32+
33+
if (hasNoParent(currentObj)) {
34+
paddingCharAmount = 0;
35+
}
36+
37+
const op = new Option(currentObj.name, currentObj.id);
38+
setLeftPadding.call(op, paddingCharAmount * 2);
39+
fragment.appendChild(op);
40+
41+
if (hasChild(currentObj)) {
42+
paddingCharAmount += 1;
43+
}
44+
45+
while (!hasChild(currentObj) && hasParent(currentObj)) {
46+
dataArray.splice(parentIndex, 1);
47+
index -= 1;
48+
paddingCharAmount -= 1;
49+
}
50+
51+
index += 1;
52+
if (index < dataArray.length) {
53+
recursivePaste(index, paddingCharAmount);
54+
}
55+
}
56+
57+
fetch('https://sectors-enpoint.herokuapp.com/sectors')
58+
.then(response => response.json())
59+
.then((array) => {
60+
dataArray = array.sort((a, b) => (a.left - b.left));
61+
62+
recursivePaste();
63+
select.appendChild(fragment);
64+
});

tictactoe/index.js

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
let moves = [];
22
let currentMove = 0;
3-
let pulledMoves = [];
4-
let pulledCurrentMove = 0;
5-
let pulledN;
3+
let pulledGameState = {};
64

75
const gameField = document.querySelector('.field');
86
const undoButt = document.querySelector('.undo-btn');
@@ -187,27 +185,33 @@ function clearField() {
187185
cells.forEach(cell => cell.classList.remove('r'));
188186
}
189187

190-
function render() {
188+
function doRestart() {
189+
removeWinTitle();
190+
removeWinCells();
191+
disableButton.call(undoButt);
192+
disableButton.call(redoButt);
193+
clearField();
191194
moves = [];
192195
currentMove = 0;
193-
clearField();
194-
removeWinCells();
195-
removeWinTitle();
196+
}
197+
198+
function render() {
199+
doRestart();
196200

197-
if (pulledN !== N) {
201+
if (!pulledGameState || pulledGameState.N !== N) {
198202
return;
199203
}
200-
for (let i = 0; i < pulledMoves.length; i += 1) {
204+
for (let i = 0; i < pulledGameState.moves.length; i += 1) {
201205
const event = new CustomEvent('click', {
202206
bubbles: true,
203207
detail: {
204208
custom: true,
205209
},
206210
});
207-
gameField.querySelector(`#c-${pulledMoves[i].cellId}`).dispatchEvent(event);
211+
gameField.querySelector(`#c-${pulledGameState.moves[i].cellId}`).dispatchEvent(event);
208212
}
209213

210-
for (let i = pulledMoves.length; i > pulledCurrentMove; i -= 1) {
214+
for (let i = pulledGameState.moves.length; i > pulledGameState.currentMove; i -= 1) {
211215
const event = new CustomEvent('click', {
212216
bubbles: true,
213217
detail: {
@@ -220,12 +224,6 @@ function render() {
220224
checkWin();
221225
}
222226

223-
function pullFromStorage() {
224-
pulledMoves = JSON.parse(localStorage.getItem('moves'));
225-
pulledCurrentMove = JSON.parse(localStorage.getItem('currentMove'));
226-
pulledN = JSON.parse(localStorage.getItem('N'));
227-
}
228-
229227
function removeCellPlayerClass() {
230228
const move = moves[currentMove];
231229
const cell = document.querySelector(`#c-${move.cellId}`);
@@ -239,19 +237,12 @@ function addCellPlayerClass() {
239237
}
240238

241239
function pushToStorage() {
242-
localStorage.setItem('moves', JSON.stringify(moves));
243-
localStorage.setItem('N', JSON.stringify(N));
244-
localStorage.setItem('currentMove', JSON.stringify(currentMove));
240+
const gameState = { moves, N, currentMove };
241+
localStorage.setItem('gameState', JSON.stringify(gameState));
245242
}
246243

247-
function doRestart() {
248-
removeWinTitle();
249-
removeWinCells();
250-
disableButton.call(undoButt);
251-
disableButton.call(redoButt);
252-
clearField();
253-
moves = [];
254-
currentMove = 0;
244+
function pullFromStorage() {
245+
pulledGameState = JSON.parse(localStorage.getItem('gameState'));
255246
}
256247

257248
gameField.addEventListener('click', (e) => {
@@ -322,15 +313,9 @@ restartButt.addEventListener('click', () => {
322313
localStorage.clear();
323314
});
324315

325-
window.addEventListener('storage', (e) => {
326-
if (e.key === null) {
327-
doRestart();
328-
return;
329-
}
330-
if (e.key === 'currentMove') {
331-
pullFromStorage();
332-
render();
333-
}
316+
window.addEventListener('storage', () => {
317+
pullFromStorage();
318+
render();
334319
});
335320

336321
try {

0 commit comments

Comments
 (0)