-
Notifications
You must be signed in to change notification settings - Fork 0
/
jbmo.umd.js
203 lines (171 loc) · 6.05 KB
/
jbmo.umd.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
(function(root, factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root.jbmo = factory();
}
}(this, function() {
if (typeof this.jbmo !== 'undefined') {
return this.jbmo;
}
var _addEvent = (function () {
var _elem = document.createElement("div");
if (_elem.addEventListener) {
return function (event, elem, func) {
elem.addEventListener(event, func, false);
};
} else if (_elem.attachEvent) {
return function (event, elem, func) {
elem.attachEvent("on" + event, func);
};
}
}());
var _classExp = function (className) {
return new RegExp("(^|\\b)" + className + "(\\b|$)", "ig");
};
var _addClass = function (elem, className) {
if (!elem.className.match(_classExp(className))) {
elem.className += " " + className;
}
};
var _removeClass = function (elem, className) {
elem.className = elem.className.replace(_classExp(className), "");
};
var _extend = function (object1, object2) {
var _newObject = {};
for (var i in object1) {
if (typeof object2 != "undefined" && typeof object2[i] != "undefined") {
_newObject[i] = object2[i];
} else {
_newObject[i] = object1[i];
}
}
return _newObject;
};
var _init = function () {
// variables
var _jbmos = [],
_activeClassname = "actived",
_noScrollClassname = "jbmo-noscroll",
_modalIdName = "jbmo-modal",
_containerClassname = "jbmo-container",
_closeButtonClassname = "jbmo-close",
_bodyElem = document.body,
_modalElem = document.createElement("div");
// create modal element
_modalElem.id = _modalIdName;
_bodyElem.appendChild(_modalElem);
_modalElem.onclick = function (event) {
if (event.target == _modalElem) {
allApi.closeAll(true);
}
};
// what modal close need to do
var _closeModal = function () {
_removeClass(_modalElem, _activeClassname);
_removeClass(_bodyElem, _noScrollClassname);
};
// get all actived modal containers
var _getActivedContainers = function () {
return document.querySelectorAll("." + _containerClassname + "." + _activeClassname);
};
var _defaultSetting = {
closeButton: true,
modalLock: false,
onShow: null,
onDestroy: null,
onClose: null,
containerClassname: ""
};
function create(content, setting) {
var _containerElem = document.createElement("div"),
_setting = _extend(_defaultSetting, setting),
_api;
// make a container
_containerElem.className = _containerClassname + " " + _setting.containerClassname;
if (typeof content == "string") {
_containerElem.innerHTML = content;
} else if (typeof content.tagName != "undefined") {
_containerElem.appendChild(content);
} else {
throw new "jbmo: invalid content."
}
_modalElem.appendChild(_containerElem);
if (_setting.closeButton) {
var _closeButtonElem = document.createElement("div");
_closeButtonElem.className = _closeButtonClassname;
_closeButtonElem.onclick = function () {
_close();
};
_containerElem.appendChild(_closeButtonElem);
}
var _show = function () {
_addClass(_containerElem, _activeClassname);
_addClass(_modalElem, _activeClassname);
_addClass(_bodyElem, _noScrollClassname);
if (typeof _setting.onShow == "function") {
_setting.onShow();
}
};
var _close = function (closeByModal) {
if (_setting.modalLock && closeByModal) {
return;
}
_removeClass(_containerElem, _activeClassname);
if (typeof _setting.onClose == "function") {
_setting.onClose();
}
if (!_getActivedContainers().length) {
_closeModal();
}
};
var _destroy = function () {
var apiIndex = _jbmos.indexOf(_api);
// remove from dom
_modalElem.removeChild(_containerElem);
// remove from caches
if (apiIndex !== -1) {
_jbmos.splice(apiIndex, 1);
}
if (typeof _setting.onDestroy == "function") {
_setting.onDestroy();
}
if (!_getActivedContainers().length) {
_closeModal();
}
};
_api = {
show: _show,
close: _close,
destroy: _destroy
};
_jbmos.push(_api);
return _api;
};
var allApi = {
get: function () {
return _jbmos;
},
show: function () {
for (var i = 0, len = _jbmos.length; i < len; i += 1) {
_jbmos[i].show();
}
},
close: function (closeByModal) {
for (var i = 0, len = _jbmos.length; i < len; i += 1) {
_jbmos[i].close(closeByModal);
}
},
destroy: function () {
for (var i = _jbmos.length - 1; i >= 0; i -= 1) {
_jbmos[i].destroy();
}
}
};
return {
create: create,
all: allApi
}
};
return _init();
}));