src/ts/modules/accordion.ts   A
last analyzed

Complexity

Total Complexity 11
Complexity/F 2.2

Size

Lines of Code 75
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 58
mnd 6
bc 6
fnc 5
dl 0
loc 75
rs 10
bpm 1.2
cpm 2.2
noi 0
c 0
b 0
f 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A Accordion.onInit 0 5 5
A Accordion.addClasses 0 6 1
A Accordion.toggleState 0 12 2
A Accordion.closeAll 0 4 1
A Accordion.initToggleIcon 0 10 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.initToggleIcon();
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 initToggleIcon() {
37
            let that = this;
38
39
            if(this.options.toggleIcon) {
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.toggleIcon 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
}));