-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootModal.js
129 lines (121 loc) · 4.73 KB
/
bootModal.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
/*
* Author: @sunnykgupta
* URL: https://github.com/sunnykgupta/bootModal
* Licence: MIT
*
* $.bootModal("Enter Title Here","Enter Body Text HERE",options);
* options is an object like:
* {
* footer -> bool //Decides if the footer is shown
* header -> bool //Decides if the header is shown
* primaryText -> string //Displayed on primary btn.
* secondaryText -> string // If present, secondary button is show and text is the value provided.
* primaryCallback -> function //Called when user clicks primary btn.
* secondaryCallback -> function //Called when user clicks secondary btn.
* }
*
*/
(function($) {
"use strict";
var defaultOptions = {
footer: false,
header: true,
headerText:"Warning!",
primaryCallback: $.noop,
secondaryCallback: $.noop,
dismissCallback: $.noop,
validationCallback: $.noop,
onShown: $.noop,
onShow: $.noop,
primaryText: "Ok",
secondaryText: "Cancel",
// close - will handle cross icon display + Hide on Esc key
close : true,
// hideOnBgClick - will handle any hide on clicking bg-overlay.
hideOnBgClick: true
};
$.bootModal = function(title, body, options) {
options = $.extend(true, {}, defaultOptions, options);
var close = '<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true" class="fa fa-times close-icon"></span><span class="sr-only">Close</span></button>'
,backdrop = options.hideOnBgClick ? true : 'static'
var footer = '<div class="modal-footer clearfix">' +
'<button type="button" class="btn btn-default" data-dismiss="modal">' + options.primaryText + '</button>';
if (options.secondaryText != ''){
footer += '<button type="button" class="btn btn-secondary" data-dismiss="modal">' + options.secondaryText + '</button>';
}else{
footer += '<span class="dummy-secondary"></div>';
}
footer += '</div>';
if (!options.footer)
footer = '';
close = options.close ? close : '';
return $('<div class="modal bootModal"' + 'data-backdrop="' + backdrop + '" data-keyboard="' + options.close +'">' +
'<div class="modal-dialog">' +
'<div class="modal-content">' +
'<div class="modal-header" style="border: 0">' +
close +
'<h4 class="modal-title heading-2">'+options.headerText+'</h4>' +
'</div>' +
'<div class="modal-body text-1">' +
'<p></p>' +
'</div>' +
footer +
'</div>' +
'</div>' +
'</div>')
.find('.modal-title')
.html(title).end()
.find('.modal-body')
.html(body).end()
.find('.modal-footer .btn-default')
.click( function(e){
options.primaryCallback(e);
$(this).closest('.modal').data('callback','primary');
}).end()
.find('.modal-footer .btn-secondary')
.click( function(e){
options.secondaryCallback(e);
$(this).closest('.modal').data('callback','secondary');
}).end()
.appendTo($('body'))
.on('show.bs.modal', options.onShow)
.on('shown.bs.modal', function(e) {
$('.modal-header', $(this)).css({
padding: '30px 15px 15px 30px'
});
$('.modal-body', $(this)).css({
padding: '15px 30px 40px 30px'
});
$('.modal-dialog', $(this)).css({
position: 'absolute',
left: '50%',
'margin-left': -$('.modal-dialog', $(this)).width()/2
});
$('.modal-dialog', $(this)).css({
top: '50%',
'margin-top': -$('.modal-dialog', $(this)).height()/2
});
$('.modal-header', $(this)).css({
padding: '30px 15px 15px 30px'
});
$('.modal-body', $(this)).css({
padding: '15px 30px 40px 30px'
});
options.onShown.apply($(this), arguments);
})
.modal('show')
.on('hidden.bs.modal', function(e) {
var callbackValue = $(this).data("callback");
if(callbackValue != "primary" && callbackValue != "secondary"){
options.dismissCallback();
}
$(this).remove();
})
.on('hide.bs.modal', function(e) {
var callbackValue = $(this).data("callback");
if(callbackValue != "primary" && callbackValue != "secondary"){
options.validationCallback();
}
});
};
}(jQuery));