Passed
Push — master ( 974900...d82885 )
by D
02:16
created

Popup.initCloseIcon   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 2
1
import {define} from "../globals/globals";
2
import {PopupInterface} from "../interfaces/PopupInterface";
3
import {PopupOptions} from "../types/PopupOptions";
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!.Popup = factory();
12
    }
13
}(typeof self !== 'undefined' ? self : this, function () {
14
15
    class Popup implements PopupInterface {
16
        public options: PopupOptions;
17
18
        constructor(options:PopupOptions) {
19
20
            if (!options.el) {
21
                throw new Error('No popup root selector')
22
            }
23
            if (!options.openers) {
24
                throw new Error('No popup openers')
25
            }
26
27
            this.options = options;
28
            this.options.el = typeof options.el === 'string' ? document.querySelector(options.el) : options.el;
29
            this.options.openers = typeof options.openers === 'string' ? document.querySelectorAll(options.openers) : options.openers;
30
            this.options.closable = options.closable || false;
31
            this.onInit();
32
            this.onLoad();
33
        }
34
35
        protected onInit(): void {
36
            this.initHTML();
37
            this.initEvents();
38
            this.initCloseIcon();
39
        }
40
41
        protected onLoad(): void {
42
            if(this.options && (this.options.onLoad as Function)) {
43
                this.options.onLoad!();
44
            }
45
        }
46
47
        private initHTML(): HTMLElement {
48
            this.options.el.classList.add('popup');
49
            this.options.el.firstElementChild.classList.add('popup__container');
50
51
            if (this.options.closable) {
52
                let popupClose = document.createElement('a');
53
                popupClose.href = '#';
54
                popupClose.classList.add('popup__close-btn');
55
                this.options.el.firstElementChild.appendChild(popupClose);
56
            }
57
58
            document.body.appendChild(this.options.el);
59
60
            return this.options.el;
61
        }
62
63
        protected initEvents(): void {
64
            this.open();
65
            this.close();
66
        }
67
68
        protected initCloseIcon(): void {
69
            if(this.options.closable && this.options.closeIcon) {
70
                let closeBtn = this.options.el.querySelector('.popup__close-btn');
71
                closeBtn.innerHTML = this.options.closeIcon;
72
            }
73
        }
74
75
        public open(): void {
76
            let that = this;
77
78
            that.options.openers.forEach(function (popupOpen:HTMLElement) {
79
                popupOpen.addEventListener('click', function (e: Event) {
80
                    e.preventDefault();
81
                    that.options.el.classList.add('active');
82
                    if(that.options.onOpen) {
83
                        that.options.onOpen.call(that);
84
                    }
85
                })
86
            })
87
        }
88
89
        public close(): void {
90
            let that = this;
91
92
            document.addEventListener('keydown', function(e: KeyboardEvent) {
93
                if(e.key === "Escape") {
94
                    that.options.el.classList.remove('active');
95
96
                    if(that.options.onClose) {
97
                        that.options.onClose.call(that);
98
                    }
99
                }
100
            });
101
102
            that.options.el.addEventListener('click', function (e: Event) {
103
104
                if (!(e.target! as Element).closest('.popup__container')) {
105
                    that.options.el.classList.remove('active');
106
107
                    if(that.options.onClose) {
108
                        that.options.onClose.call(that);
109
                    }
110
                }
111
            });
112
113
            let popupClose = that.options.el.querySelector('.popup__close-btn');
114
115
            if (popupClose) {
116
                popupClose.addEventListener('click', function (e: Event) {
117
                    e.preventDefault();
118
                    that.options.el.classList.remove('active');
119
120
                    if(that.options.onClose) {
121
                        that.options.onClose.call(that);
122
                    }
123
                })
124
            }
125
        }
126
127
        public manualOpen() {
128
            this.options.el.classList.add('active');
129
        }
130
131
        public manualClose() {
132
            this.options.el.classList.remove('active');
133
        }
134
    }
135
136
    return Popup;
137
}));