forked from kylepaulsen/NanoModal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanomodal.js
463 lines (400 loc) · 14.8 KB
/
nanomodal.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
var nanoModal;
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var ModalEvent = require("./ModalEvent");
function El(tag, classNames) {
var doc = document;
var el = (tag.nodeType || tag === window) ? tag : doc.createElement(tag);
var eventHandlers = [];
if (classNames) {
el.className = classNames;
}
var onShowEvent = ModalEvent();
var onHideEvent = ModalEvent();
var addListener = function(event, handler) {
if (el.addEventListener) {
el.addEventListener(event, handler, false);
} else {
el.attachEvent("on" + event, handler);
}
eventHandlers.push({
event: event,
handler: handler
});
};
var removeListener = function(event, handler) {
if (el.removeEventListener) {
el.removeEventListener(event, handler);
} else {
el.detachEvent("on" + event, handler);
}
var t = eventHandlers.length;
var handlerObj;
while (t-- > 0) {
handlerObj = eventHandlers[t];
if (handlerObj.event === event && handlerObj.handler === handler) {
eventHandlers.splice(t, 1);
break;
}
}
};
var addClickListener = function(handler) {
if ("ontouchend" in document.documentElement) {
addListener("touchstart", handler);
} else {
addListener("click", handler);
}
};
var show = function(arg) {
if (el) {
el.style.display = "block";
onShowEvent.fire(arg);
}
};
var hide = function(arg) {
if (el) {
el.style.display = "none";
onHideEvent.fire(arg);
}
};
var isShowing = function() {
return el.style && el.style.display === "block";
};
var html = function(html) {
if (el) {
el.innerHTML = html;
}
};
var text = function(text) {
if (el) {
html("");
el.appendChild(doc.createTextNode(text));
}
};
var remove = function() {
if (el.parentNode) {
var x = eventHandlers.length;
var eventHandler;
while (x-- > 0) {
eventHandler = eventHandlers[x];
removeListener(eventHandler.event, eventHandler.handler);
}
el.parentNode.removeChild(el);
onShowEvent.removeAllListeners();
onHideEvent.removeAllListeners();
}
};
var add = function(elObject) {
var elementToAppend = elObject.el || elObject;
el.appendChild(elementToAppend);
};
return {
el: el,
addListener: addListener,
addClickListener: addClickListener,
onShowEvent: onShowEvent,
onHideEvent: onHideEvent,
show: show,
hide: hide,
isShowing: isShowing,
html: html,
text: text,
remove: remove,
add: add
};
}
module.exports = El;
},{"./ModalEvent":3}],2:[function(require,module,exports){
var El = require("./El");
function Modal(content, options, overlay, customShow, customHide) {
if (content === undefined) {
return;
}
options = options || {};
var modal = El("div", "nanoModal nanoModalOverride " + (options.classes || ""));
var contentContainer = El("div", "nanoModalContent");
var buttonArea = El("div", "nanoModalButtons");
var onRequestHideListenerId;
modal.add(contentContainer);
modal.add(buttonArea);
modal.el.style.display = "none";
var buttons = [];
var pub;
options.buttons = options.buttons || [{
text: "Close",
handler: "hide",
primary: true
}];
var removeButtons = function() {
var t = buttons.length;
while (t-- > 0) {
var button = buttons[t];
button.remove();
}
buttons = [];
};
var center = function() {
modal.el.style.marginLeft = -modal.el.clientWidth / 2 + "px";
};
var anyModalsOpen = function() {
var modals = document.querySelectorAll(".nanoModal");
var t = modals.length;
while (t-- > 0) {
if (modals[t].style.display !== "none") {
return true;
}
}
return false;
};
var defaultShow = function() {
if (!modal.isShowing()) {
// Call the static method from the Modal module.
Modal.resizeOverlay();
overlay.show(overlay);
modal.show(pub);
center();
}
};
var defaultHide = function() {
if (modal.isShowing()) {
modal.hide(pub);
if (!anyModalsOpen()) {
overlay.hide(overlay);
}
if (options.autoRemove) {
pub.remove();
}
}
};
var quickClone = function(obj) {
var newObj = {};
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
newObj[key] = obj[key];
}
}
return newObj;
};
pub = {
modal: modal,
overlay: overlay,
show: function() {
if (customShow) {
customShow(defaultShow, pub);
} else {
defaultShow();
}
return pub;
},
hide: function() {
if (customHide) {
customHide(defaultHide, pub);
} else {
defaultHide();
}
return pub;
},
onShow: function(callback) {
modal.onShowEvent.addListener(function() {
callback(pub);
});
return pub;
},
onHide: function(callback) {
modal.onHideEvent.addListener(function() {
callback(pub);
});
return pub;
},
remove: function() {
overlay.onRequestHide.removeListener(onRequestHideListenerId);
onRequestHideListenerId = null;
removeButtons();
modal.remove();
},
setButtons: function(buttonList) {
var btnIdx = buttonList.length;
var btnObj;
var btnEl;
var classes;
var giveButtonCustomClickListener = function(btnEl, btnObj) {
var pubCopy = quickClone(pub);
btnEl.addClickListener(function(e) {
pubCopy.event = e || window.event;
btnObj.handler(pubCopy);
});
};
removeButtons();
if (btnIdx === 0) {
buttonArea.hide();
} else {
buttonArea.show();
while (btnIdx-- > 0) {
btnObj = buttonList[btnIdx];
classes = "nanoModalBtn";
if (btnObj.primary) {
classes += " nanoModalBtnPrimary";
}
classes += btnObj.classes ? " " + btnObj.classes : "";
btnEl = El("button", classes);
if (btnObj.handler === "hide") {
btnEl.addClickListener(pub.hide);
} else if (btnObj.handler) {
giveButtonCustomClickListener(btnEl, btnObj);
}
btnEl.text(btnObj.text);
buttonArea.add(btnEl);
buttons.push(btnEl);
}
}
center();
return pub;
},
setContent: function(newContent) {
// Only good way of checking if a node in IE8...
if (newContent.nodeType) {
contentContainer.html("");
contentContainer.add(newContent);
} else {
contentContainer.html(newContent);
}
center();
content = newContent;
return pub;
},
getContent: function() {
return content;
}
};
onRequestHideListenerId = overlay.onRequestHide.addListener(function() {
if (options.overlayClose !== false && modal.isShowing()) {
pub.hide();
}
});
pub.setContent(content).setButtons(options.buttons);
document.body.appendChild(modal.el);
return pub;
}
var doc = document;
var getDocumentDim = function(name) {
var docE = doc.documentElement;
var scroll = "scroll" + name;
var offset = "offset" + name;
return Math.max(doc.body[scroll], docE[scroll],
doc.body[offset], docE[offset], docE["client" + name]);
};
// Make this a static function so that main.js has access to it so it can
// add a window keydown event listener. Modal.js also needs this function.
Modal.resizeOverlay = function() {
var overlay = doc.getElementById("nanoModalOverlay");
overlay.style.width = getDocumentDim("Width") + "px";
overlay.style.height = getDocumentDim("Height") + "px";
};
module.exports = Modal;
},{"./El":1}],3:[function(require,module,exports){
function ModalEvent() {
var listeners = {};
var nextListenerId = 0;
var addListener = function(callback) {
listeners[nextListenerId] = callback;
return nextListenerId++;
};
var removeListener = function(id) {
if (id) {
delete listeners[id];
}
};
var removeAllListeners = function() {
listeners = {};
};
var fire = function() {
for (var x = 0, num = nextListenerId; x < num; ++x) {
if (listeners[x]) {
listeners[x].apply(null, arguments);
}
}
};
return {
addListener: addListener,
removeListener: removeListener,
removeAllListeners: removeAllListeners,
fire: fire
};
}
module.exports = ModalEvent;
},{}],4:[function(require,module,exports){
var ModalEvent = require("./ModalEvent");
var nanoModalAPI = (function() {
var El = require("./El");
var Modal = require("./Modal");
var overlay;
var doc = document;
function init() {
if (!doc.querySelector("#nanoModalOverlay")) {
// Put the main styles on the page.
var styleObj = El("style");
var style = styleObj.el;
var firstElInHead = doc.querySelectorAll("head")[0].childNodes[0];
firstElInHead.parentNode.insertBefore(style, firstElInHead);
var styleText = ".nanoModal{position:absolute;top:100px;left:50%;display:none;z-index:9999;min-width:300px;padding:15px 20px 10px;-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px;background:#fff;background:-moz-linear-gradient(top,#fff 0,#ddd 100%);background:-webkit-gradient(linear,left top,left bottom,color-stop(0%,#fff),color-stop(100%,#ddd));background:-webkit-linear-gradient(top,#fff 0,#ddd 100%);background:-o-linear-gradient(top,#fff 0,#ddd 100%);background:-ms-linear-gradient(top,#fff 0,#ddd 100%);background:linear-gradient(to bottom,#fff 0,#ddd 100%);filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#dddddd', GradientType=0)}.nanoModalOverlay{position:absolute;top:0;left:0;width:100%;height:100%;z-index:9998;background:#000;display:none;-ms-filter:\"alpha(Opacity=50)\";-moz-opacity:.5;-khtml-opacity:.5;opacity:.5}.nanoModalButtons{border-top:1px solid #ddd;margin-top:15px;text-align:right}.nanoModalBtn{color:#333;background-color:#fff;display:inline-block;padding:6px 12px;margin:8px 4px 0;font-size:14px;text-align:center;white-space:nowrap;vertical-align:middle;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;border:1px solid transparent;-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px}.nanoModalBtn:active,.nanoModalBtn:focus,.nanoModalBtn:hover{color:#333;background-color:#e6e6e6;border-color:#adadad}.nanoModalBtn.nanoModalBtnPrimary{color:#fff;background-color:#428bca;border-color:#357ebd}.nanoModalBtn.nanoModalBtnPrimary:active,.nanoModalBtn.nanoModalBtnPrimary:focus,.nanoModalBtn.nanoModalBtnPrimary:hover{color:#fff;background-color:#3071a9;border-color:#285e8e}";
if (style.styleSheet) {
style.styleSheet.cssText = styleText;
} else {
styleObj.text(styleText);
}
// Make the overlay and put it on the page.
overlay = El("div", "nanoModalOverlay nanoModalOverride");
overlay.el.id = "nanoModalOverlay";
doc.body.appendChild(overlay.el);
// Add an event so that the modals can hook into it to close.
overlay.onRequestHide = ModalEvent();
var overlayCloseFunc = function() {
overlay.onRequestHide.fire();
};
overlay.addClickListener(overlayCloseFunc);
El(doc).addListener("keydown", function(e) {
var keyCode = e.which || e.keyCode;
if (keyCode === 27) { // 27 is Escape
overlayCloseFunc();
}
});
var windowEl = El(window);
var resizeOverlayTimeout;
windowEl.addListener("resize", function() {
if (resizeOverlayTimeout) {
clearTimeout(resizeOverlayTimeout);
}
resizeOverlayTimeout = setTimeout(Modal.resizeOverlay, 100);
});
// Make SURE we have the correct dimensions so we make the overlay the right size.
// Some devices fire the event before the document is ready to return the new dimensions.
windowEl.addListener("orientationchange", function() {
for (var t = 0; t < 3; ++t) {
setTimeout(Modal.resizeOverlay, 1000 * t + 200);
}
});
}
}
if (document.body) {
init();
}
var api = function(content, options) {
init();
return Modal(content, options, overlay, api.customShow, api.customHide);
};
api.resizeOverlay = Modal.resizeOverlay;
return api;
})();
// expose api to var outside browserify so that we can export a module correctly.
nanoModal = nanoModalAPI;
},{"./El":1,"./Modal":2,"./ModalEvent":3}]},{},[1,2,3,4]);
if (typeof window !== "undefined") {
if (typeof window.define === "function" && window.define.amd) {
window.define(function() {
return nanoModal;
});
}
window.nanoModal = nanoModal;
}
if (typeof module !== "undefined") {
module.exports = nanoModal;
}