Passed
Push — master ( 7f1200...974900 )
by D
02:25
created

Accordion.initIcons   A

Complexity

Conditions 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 3
1
"use strict";
2
Object.defineProperty(exports, "__esModule", { value: true });
3
const globals_1 = require("../globals/globals");
4
(function (root, factory) {
5
    if (typeof globals_1.define === 'function' && globals_1.define.amd) {
6
        globals_1.define([], factory);
7
    }
8
    else if (typeof module === 'object' && module.exports) {
9
        module.exports = factory();
10
    }
11
    else {
12
        root.Accordion = factory();
13
    }
14
}(typeof self !== 'undefined' ? self : this, function () {
0 ignored issues
show
Bug introduced by
The variable self seems to be never declared. If this is a global, consider adding a /** global: self */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
15
    class Accordion {
16
        constructor(options = {}) {
17
            this.options = options;
18
            this.options.el = typeof options.el === 'string' ? document.querySelectorAll(options.el) : options.el || document.querySelectorAll('.accordion');
19
            this.onInit();
20
        }
21
        onInit() {
22
            this.addClasses();
23
            this.initIcons();
24
            this.toggleState();
25
        }
26
        static closeAll(accordions) {
27
            accordions.forEach(function (accordion) {
28
                accordion.classList.remove('active');
29
            });
30
        }
31
        initIcons() {
32
            let that = this;
33
            if (this.options.togglerIcon) {
34
                this.options.el.forEach(function (element) {
35
                    let accordionToggler = document.createElement('div');
36
                    accordionToggler.classList.add('accordion-toggler__icon');
37
                    accordionToggler.innerHTML = that.options.togglerIcon;
38
                    element.firstElementChild.appendChild(accordionToggler);
39
                });
40
            }
41
        }
42
        addClasses() {
43
            this.options.el.forEach(function (element) {
44
                element.classList.add('accordion');
45
                element.firstElementChild.classList.add('accordion-toggler');
46
                element.firstElementChild.nextElementSibling.classList.add('accordion-collapse');
47
            });
48
        }
49
        toggleState() {
50
            let that = this;
51
            that.options.el.forEach(function (element) {
52
                element.firstElementChild.addEventListener('click', function () {
53
                    if (that.options.openOneCloseAll) {
54
                        Accordion.closeAll(that.options.el);
55
                        this.parentElement.classList.toggle('active');
56
                    }
57
                    else {
58
                        element.classList.toggle('active');
59
                    }
60
                });
61
            });
62
        }
63
    }
64
    return Accordion;
65
}));
66