-
Notifications
You must be signed in to change notification settings - Fork 0
/
MercuryModal.js
161 lines (151 loc) · 5.95 KB
/
MercuryModal.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
;(function($) {
$.MercuryModal = function (elem, options){
var settings = $.extend(true, {}, $.MercuryModal.defaults, options);
settings.before();
if(elem && typeof elem == 'object'){
if(!settings.content) settings.content = '';
settings.content += $(elem).html();
}
var length = $('.modal').length;
var modalIdLength;
if($('.' + settings.id).length){
modalIdLength = $($('.modal')[length - 1]).attr('id');
modalIdLength = modalIdLength.substr(modalIdLength.lastIndexOf('-') + 1, modalIdLength.length);
modalIdLength = parseInt(modalIdLength) + 1;
}
else{
modalIdLength = 0;
}
length = modalIdLength;
var buttonsLength = $('.'+ settings.id +'-button').length;
var modalContent = '<div class="modal fade '+ settings.id +'" id="'+ settings.id +'-'+ modalIdLength +'" role="dialog" aria-labelledby="modal" aria-hidden="false" style="z-index: '+ (settings.zIndex + 10 * length) +'">';
modalContent += ' <div class="modal-dialog" style="'+ (settings.width == 'auto' ? '' : ('width: 100%; max-width: '+ (typeof settings.width == 'number' ? (settings.width + 'px') : settings.width)) + ';') +'"> <div class="modal-content">';
if(settings.show.header) {
modalContent += '<div class="modal-header" style="text-align: '+ settings.textAlign.header +';';
if(settings.show.footer == false && !settings.content) {
modalContent += 'border-bottom: 0 solid transparent;';
}
modalContent += '">';
if(settings.show.closeButton) {
modalContent += '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>';
}
if (settings.title) {
modalContent += '<h4 class="modal-title">';
modalContent += settings.title;
modalContent += '</h4>';
}
modalContent += '</div>';
}
if (settings.content) {
modalContent += '<div class="modal-body" style="text-align: '+ settings.textAlign.middle +'">';
modalContent += settings.content;
modalContent += '</div>';
if(settings.show.footer) {
modalContent += '<div class="modal-footer" style="text-align: '+ settings.textAlign.footer +';">';
}
}
else if(settings.show.footer) {
modalContent += '<div class="modal-footer" style="border-top:0; text-align: '+ settings.textAlign.footer +';">';
}
$.each(settings.buttons, function(index, value){
if (value.text) {
modalContent += '<button type="button" id="'+ settings.id +'-button-'+ (index + buttonsLength) +'" class="'+ settings.id +'-button btn '+ value.class +'"';
if(value.dismiss){
modalContent += ' data-dismiss="modal"';
}
modalContent += '>';
modalContent += value.text;
modalContent += '</button>';
}
});
modalContent += '</div></div></div></div>';
$(modalContent).appendTo('body');
var modalDiv = $('#'+ settings.id +'-'+ modalIdLength);
modalDiv.modal({
keyboard: settings.keyboard,
backdrop: settings.backdrop
});
$.each(settings.buttons, function(index, value){
if(value.text){
if (typeof value.click == 'function') {
modalDiv.on('click', '#'+ settings.id +'-button-'+ (index + buttonsLength), function(){
value.click($(this));
});
}
}
});
modalDiv.on('hidden.bs.modal', function(e){
settings.hide(e);
$(this).data('bs.modal', null);
$(this).remove();
});
$($('.modal-backdrop:not(.mercuryBackdrop)')[0]).css('z-index', (settings.zIndex + 10 * (length - 1) + 1)).addClass('backdrop-'+ settings.id).addClass('mercuryBackdrop');
settings.ready();
return this;
}
$.MercuryModal.defaults = {
id: 'mercuryModal',
buttons:[
{
click: function(){},
text: null,
class: 'btn-default',
dismiss: true
}
],
show: {
header: true,
footer: true,
closeButton: true
},
title: null,
content: null,
textAlign: {
header: 'left',
middle: 'left',
footer: 'center'
},
zIndex: 1050,
width: '600px',
keyboard: true,
backdrop: true,
ready: function(){},
hide: function(){},
before: function(){}
};
$.MercuryModal.closeLast = function(selector){
if(!selector || !selector.length) selector = '.modal';
if($(selector).length - 1 > 0){
$($(selector)[$(selector).length - 1]).modal('hide');
}
}
$('body').on('keyup', function(e){
if(e.keyCode == 27){
$.MercuryModal.closeLast();
}
});
// Helpers
$.fn.MercuryModal = function(options) {
return this.each(function() {
$.MercuryModal(this, options);
});
}
$.fn.closeModal = function(){
return this.each(function() {
$(this).modal('hide');
});
}
window.CloseAllModals = function(){
$('.modal').closeModal();
}
window.MercuryModal = function MercuryModal(options) {
return $.MercuryModal(null, options);
}
window.CloseLastModal = function CloseLastModal(selector) {
return $.MercuryModal.closeLast(selector);
}
window.numberOfModals = $.MercuryModal.numberOfModals = function (selector){
if(!selector || !selector.length) selector = '.modal';
return($(selector).length);
}
})(jQuery);