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
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected onLoad(): void { |
41
|
|
|
if(this.options && (this.options.onLoad as Function)) { |
42
|
|
|
this.options.onLoad!(); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private initHTML(): HTMLElement { |
47
|
|
|
this.options.el.classList.add('popup'); |
48
|
|
|
this.options.el.firstElementChild.classList.add('popup__container'); |
49
|
|
|
|
50
|
|
|
if (this.options.closable) { |
51
|
|
|
let popupClose = document.createElement('a'); |
52
|
|
|
popupClose.href = '#'; |
53
|
|
|
popupClose.classList.add('popup__close-btn'); |
54
|
|
|
this.options.el.firstElementChild.appendChild(popupClose); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
document.body.appendChild(this.options.el); |
58
|
|
|
|
59
|
|
|
return this.options.el; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected initEvents(): void { |
63
|
|
|
this.open(); |
64
|
|
|
this.close(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public open(): void { |
68
|
|
|
let that = this; |
69
|
|
|
|
70
|
|
|
that.options.openers.forEach(function (popupOpen:HTMLElement) { |
71
|
|
|
popupOpen.addEventListener('click', function (e: Event) { |
72
|
|
|
e.preventDefault(); |
73
|
|
|
that.options.el.classList.add('active'); |
74
|
|
|
if(that.options.onOpen) { |
75
|
|
|
that.options.onOpen.call(that); |
76
|
|
|
} |
77
|
|
|
}) |
78
|
|
|
}) |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public close(): void { |
82
|
|
|
let that = this; |
83
|
|
|
|
84
|
|
|
document.addEventListener('keydown', function(e: KeyboardEvent) { |
85
|
|
|
if(e.key === "Escape") { |
86
|
|
|
that.options.el.classList.remove('active'); |
87
|
|
|
|
88
|
|
|
if(that.options.onClose) { |
89
|
|
|
that.options.onClose.call(that); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
}); |
93
|
|
|
|
94
|
|
|
that.options.el.addEventListener('click', function (e: Event) { |
95
|
|
|
|
96
|
|
|
if (!(e.target! as Element).closest('.popup__container')) { |
97
|
|
|
that.options.el.classList.remove('active'); |
98
|
|
|
|
99
|
|
|
if(that.options.onClose) { |
100
|
|
|
that.options.onClose.call(that); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
}); |
104
|
|
|
|
105
|
|
|
let popupClose = that.options.el.querySelector('.popup__close-btn'); |
106
|
|
|
|
107
|
|
|
if (popupClose) { |
108
|
|
|
popupClose.addEventListener('click', function (e: Event) { |
109
|
|
|
e.preventDefault(); |
110
|
|
|
that.options.el.classList.remove('active'); |
111
|
|
|
|
112
|
|
|
if(that.options.onClose) { |
113
|
|
|
that.options.onClose.call(that); |
114
|
|
|
} |
115
|
|
|
}) |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public manualOpen() { |
120
|
|
|
this.options.el.classList.add('active'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public manualClose() { |
124
|
|
|
this.options.el.classList.remove('active'); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return Popup; |
129
|
|
|
})); |