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

Accordion.initIcons   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 10
rs 9.9
c 0
b 0
f 0
cc 2
1
import {define} from "../globals/globals";
2
import {AccordionInterface} from "../interfaces/AccordionInterface";
3
import {AccordionOptions} from '../types/AccordionOptions'
4
5
(function (root, factory) {
6
    if (typeof define === 'function' && define.amd) {
7
        define([], factory);
8
    } else if (typeof module === 'object' && module.exports) {
9
        module.exports = factory();
10
    } else {
11
        root!.Accordion = factory();
12
    }
13
}(typeof self !== 'undefined' ? self : this, function () {
14
15
    class Accordion implements AccordionInterface {
16
        public options: AccordionOptions;
17
18
        constructor(options: AccordionOptions = {}) {
19
            this.options = options as object;
20
            this.options.el = typeof options.el === 'string' ? document.querySelectorAll(options.el) : options.el || document.querySelectorAll('.accordion');
21
            this.onInit();
22
        }
23
24
        protected onInit(): void {
25
            this.addClasses();
26
            this.initIcons();
27
            this.toggleState();
28
        }
29
30
        static closeAll(accordions: NodeListOf<HTMLElement>): void {
31
            accordions.forEach(function (accordion: Element) {
32
                accordion.classList.remove('active')
33
            })
34
        }
35
36
        protected initIcons() {
37
            let that = this;
38
39
            if(this.options.togglerIcon) {
40
                (this.options.el as NodeListOf<HTMLElement>).forEach(function (element: HTMLElement) {
41
                    let accordionToggler = document.createElement('div');
42
                    accordionToggler.classList.add('accordion-toggler__icon');
43
                    accordionToggler.innerHTML = that.options.togglerIcon as string;
44
                    element.firstElementChild!.appendChild(accordionToggler);
45
                })
46
            }
47
        }
48
49
        protected addClasses(): void {
50
            (this.options.el as NodeListOf<HTMLElement>).forEach(function (element: HTMLElement) {
51
                element.classList.add('accordion');
52
                element.firstElementChild!.classList.add('accordion-toggler');
53
                element.firstElementChild!.nextElementSibling!.classList.add('accordion-collapse');
54
            })
55
        }
56
57
        protected toggleState(): void {
58
            let that = this;
59
60
            (that.options.el as NodeListOf<HTMLElement>).forEach(function (element: HTMLElement) {
61
                element.firstElementChild!.addEventListener('click', function () {
62
                    if(that.options.openOneCloseAll) {
63
                        Accordion.closeAll(that.options.el as NodeListOf<HTMLElement>);
64
                        (this as HTMLElement).parentElement!.classList.toggle('active')
65
                    }
66
                    else {
67
                        element.classList.toggle('active')
68
                    }
69
                })
70
            })
71
        }
72
    }
73
74
    return Accordion;
75
}));