Passed
Push — master ( b7f732...42e110 )
by Andreas
18:16
created

twisty.js ➔ toggle_twisty   A

Complexity

Conditions 3

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
dl 0
loc 18
rs 9.75
c 0
b 0
f 0
1
const slide_speed = 0.4;
0 ignored issues
show
Unused Code introduced by
The constant slide_speed seems to be never used. Consider removing it.
Loading history...
2
3
function toggle_twisty(id) {
4
    var element = document.getElementById(id);
5
6
    if (!element) {
7
        return;
8
    }
9
    let twisty = element.previousSibling.previousSibling.firstChild.nextSibling;
10
    twisty.classList.toggle('fa-caret-up');
11
    twisty.classList.toggle('fa-caret-down');
12
13
    if (element.parentNode.classList.contains('open')) {
14
        remove_anchor(id);
15
    } else {
16
        add_anchor(id);
17
    }
18
    element.parentNode.classList.toggle('open')
19
    element.parentNode.classList.toggle('closed')
20
}
21
22
function remove_anchor(id) {
23
    if (!window.location.hash) {
24
        return;
25
    }
26
27
    id = id.replace(/_contents[,]*/, '');
28
29
    var string = window.location.hash,
30
        regexp = new RegExp(id, 'g');
31
    string = string.replace(regexp, '');
32
33
    string = string.replace(/#[,]*/, '#');
34
    string = string.replace(/[,]*$/, '');
35
    string = string.replace(/,,/g, ',');
36
37
    window.location.hash = string;
38
}
39
40
function add_anchor(id) {
41
    window.location.hash = '#' + id.replace(/_contents$/, '');
42
}
43