forked from itchief/ui-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodal.js
94 lines (84 loc) · 3.81 KB
/
modal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
(function () {
if (typeof window.CustomEvent === "function") return false;
function CustomEvent(event, params) {
params = params || { bubbles: false, cancelable: false, detail: null };
var evt = document.createEvent('CustomEvent');
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
return evt;
}
window.CustomEvent = CustomEvent;
})();
$modal = function (options) {
var
_elemModal,
_eventShowModal,
_eventHideModal,
_hiding = false,
_destroyed = false,
_animationSpeed = 200;
function _createModal(options) {
var
elemModal = document.createElement('div'),
modalTemplate = '<div class="modal__backdrop" data-dismiss="modal"><div class="modal__content"><div class="modal__header"><div class="modal__title" data-modal="title">{{title}}</div><span class="modal__btn-close" data-dismiss="modal" title="Закрыть">×</span></div><div class="modal__body" data-modal="content">{{content}}</div>{{footer}}</div></div>',
modalFooterTemplate = '<div class="modal__footer">{{buttons}}</div>',
modalButtonTemplate = '<button type="button" class="{{button_class}}" data-handler={{button_handler}}>{{button_text}}</button>',
modalHTML,
modalFooterHTML = '';
elemModal.classList.add('modal');
modalHTML = modalTemplate.replace('{{title}}', options.title || 'Новое окно');
modalHTML = modalHTML.replace('{{content}}', options.content || '');
if (options.footerButtons) {
for (var i = 0, length = options.footerButtons.length; i < length; i++) {
var modalFooterButton = modalButtonTemplate.replace('{{button_class}}', options.footerButtons[i].class);
modalFooterButton = modalFooterButton.replace('{{button_handler}}', options.footerButtons[i].handler);
modalFooterButton = modalFooterButton.replace('{{button_text}}', options.footerButtons[i].text);
modalFooterHTML += modalFooterButton;
}
modalFooterHTML = modalFooterTemplate.replace('{{buttons}}', modalFooterHTML);
}
modalHTML = modalHTML.replace('{{footer}}', modalFooterHTML);
elemModal.innerHTML = modalHTML;
document.body.appendChild(elemModal);
return elemModal;
}
function _showModal() {
if (!_destroyed && !_hiding) {
_elemModal.classList.add('modal__show');
document.dispatchEvent(_eventShowModal);
}
}
function _hideModal() {
_hiding = true;
_elemModal.classList.remove('modal__show');
_elemModal.classList.add('modal__hiding');
setTimeout(function () {
_elemModal.classList.remove('modal__hiding');
_hiding = false;
}, _animationSpeed);
document.dispatchEvent(_eventHideModal);
}
function _handlerCloseModal(e) {
if (e.target.dataset.dismiss === 'modal') {
_hideModal();
}
}
_elemModal = _createModal(options || {});
_elemModal.addEventListener('click', _handlerCloseModal);
_eventShowModal = new CustomEvent('show.modal', { detail: _elemModal });
_eventHideModal = new CustomEvent('hide.modal', { detail: _elemModal });
return {
show: _showModal,
hide: _hideModal,
destroy: function () {
_elemModal.parentElement.removeChild(_elemModal),
_elemModal.removeEventListener('click', _handlerCloseModal),
_destroyed = true;
},
setContent: function (html) {
_elemModal.querySelector('[data-modal="content"]').innerHTML = html;
},
setTitle: function (text) {
_elemModal.querySelector('[data-modal="title"]').innerHTML = text;
}
}
};