-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
42 lines (34 loc) · 1.31 KB
/
main.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
// Copyright (c) 2019 Mateusz Furga <matfurga@gmail.com>
'use strict';
var pickedColor = 'blue';
var cube = undefined;
document.addEventListener('keypress', (e) => {
pickedColor = Cube.COLORS[(parseInt(e.key) - 1) % Cube.COLORS.length];
});
window.addEventListener('load', () => {
console.log('%c Copyright (c) 2019 Mateusz Furga <matfurga@gmail.com>.', 'color: green;');
console.log('%c Type `cube` in the console to perform cube operations.', 'color: green;');
var urlParams = new URLSearchParams(window.location.search);
var faces = {};
document.querySelectorAll('.field').forEach((field) => {
try {
faces[field.parentNode.id].push(Cube.COLORS.indexOf(field.dataset.color));
} catch (TypeError) {
faces[field.parentNode.id] = [];
faces[field.parentNode.id].push(Cube.COLORS.indexOf(field.dataset.color));
}
});
cube = new Cube(faces);
// Generate the random state by the URL parameter.
if (urlParams.has('rand')) {
cube.doRandomPermutation(urlParams.get('rand'));
}
var fields = document.querySelectorAll('.field');
fields.forEach((field) => {
field.addEventListener('click', function() {
this.dataset.color = pickedColor;
cube.setColorAndCommit(
this.parentNode.id, this.dataset.id, Cube.COLORS.indexOf(this.dataset.color));
});
});
});