Passed
Pull Request — master (#5)
by Jesús
01:51
created

src/components/grid.ts   A

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 39
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 33
mnd 2
bc 2
fnc 1
dl 0
loc 39
rs 10
bpm 2
cpm 3
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A grid.ts ➔ createGrid 0 32 3
1
import { toggleBox } from '../core/state';
2
import { elements } from '../core/dom';
3
import { updateDisplay } from './display';
4
import { showDisabledBoxToast } from './toast';
5
6
export function createGrid(): void {
7
  elements.grid.innerHTML = '';
8
9
  for (let i = 0; i < 12; i++) {
10
    const box = document.createElement('button');
11
    box.className = 'box';
12
    box.dataset.index = i.toString();
13
    box.type = 'button';
14
15
    const bitValue = Math.pow(2, 11 - i);
16
    box.setAttribute('aria-pressed', 'false');
17
    box.setAttribute('aria-label', `Bit ${i + 1}, value ${bitValue}`);
18
19
    const label = document.createElement('span');
20
    label.className = 'bit-label';
21
    label.textContent = bitValue.toString();
22
    label.setAttribute('aria-hidden', 'true');
23
    box.appendChild(label);
24
25
    box.addEventListener('click', () => {
26
      if (box.dataset.isDisabled === 'true') {
27
        showDisabledBoxToast();
28
        return;
29
      }
30
      toggleBox(i);
31
      updateDisplay();
32
    });
33
34
    elements.grid.appendChild(box);
35
  }
36
37
  updateDisplay();
38
}
39