This repository has been archived by the owner on Jan 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
/
jquery.confirm.js
179 lines (164 loc) · 6.29 KB
/
jquery.confirm.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
/*!
* jquery.confirm
*
* @version 2.7.0
*
* @author My C-Labs
* @author Matthieu Napoli <[email protected]>
* @author Russel Vela
* @author Marcus Schwarz <[email protected]>
*
* @license MIT
* @url https://myclabs.github.io/jquery.confirm/
*/
(function ($) {
/**
* Confirm a link or a button
* @param [options] {{title, text, confirm, cancel, confirmButton, cancelButton, post, submitForm, confirmButtonClass, modalOptionsBackdrop, modalOptionsKeyboard}}
*/
$.fn.confirm = function (options) {
if (typeof options === 'undefined') {
options = {};
}
this.click(function (e) {
e.preventDefault();
var newOptions = $.extend({
button: $(this)
}, options);
$.confirm(newOptions, e);
});
return this;
};
/**
* Show a confirmation dialog
* @param [options] {{title, text, confirm, cancel, confirmButton, cancelButton, post, submitForm, confirmButtonClass, modalOptionsBackdrop, modalOptionsKeyboard}}
* @param [e] {Event}
*/
$.confirm = function (options, e) {
// Log error and exit when no options.
if (typeof options == "undefined") {
console.error("No options given.");
return;
}
// Do nothing when active confirm modal.
if ($('.confirmation-modal').length > 0)
return;
// Parse options defined with "data-" attributes
var dataOptions = {};
if (options.button) {
var dataOptionsMapping = {
'title': 'title',
'text': 'text',
'confirm-button': 'confirmButton',
'submit-form': 'submitForm',
'cancel-button': 'cancelButton',
'confirm-button-class': 'confirmButtonClass',
'cancel-button-class': 'cancelButtonClass',
'dialog-class': 'dialogClass',
'modal-options-backdrop':'modalOptionsBackdrop',
'modal-options-keyboard':'modalOptionsKeyboard'
};
$.each(dataOptionsMapping, function(attributeName, optionName) {
var value = options.button.data(attributeName);
if (typeof value != "undefined") {
dataOptions[optionName] = value;
}
});
}
// Default options
var settings = $.extend({}, $.confirm.options, {
confirm: function () {
if (dataOptions.submitForm
|| (typeof dataOptions.submitForm == "undefined" && options.submitForm)
|| (typeof dataOptions.submitForm == "undefined" && typeof options.submitForm == "undefined" && $.confirm.options.submitForm)
) {
e.target.closest("form").submit();
} else {
var url = e && (('string' === typeof e && e) || (e.currentTarget && e.currentTarget.attributes['href'].value));
if (url) {
if (options.post) {
var form = $('<form method="post" class="hide" action="' + url + '"></form>');
$("body").append(form);
form.submit();
} else {
window.location = url;
}
}
}
},
cancel: function (o) {
},
button: null
}, options, dataOptions);
// Modal
var modalHeader = '';
if (settings.title !== '') {
modalHeader =
'<div class="modal-header">' +
'<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>' +
'<h4 class="modal-title">' + settings.title+'</h4>' +
'</div>';
}
var cancelButtonHtml = '';
if (settings.cancelButton) {
cancelButtonHtml =
'<button class="cancel btn ' + settings.cancelButtonClass + '" type="button" data-dismiss="modal">' +
settings.cancelButton +
'</button>'
}
var modalHTML =
'<div class="confirmation-modal modal fade" tabindex="-1" role="dialog">' +
'<div class="'+ settings.dialogClass +'">' +
'<div class="modal-content">' +
modalHeader +
'<div class="modal-body">' + settings.text + '</div>' +
'<div class="modal-footer">' +
'<button class="confirm btn ' + settings.confirmButtonClass + '" type="button" data-dismiss="modal">' +
settings.confirmButton +
'</button>' +
cancelButtonHtml +
'</div>' +
'</div>' +
'</div>' +
'</div>';
var modal = $(modalHTML);
// Apply modal options
if (typeof settings.modalOptionsBackdrop != "undefined" || typeof settings.modalOptionsKeyboard != "undefined") {
modal.modal({
backdrop: settings.modalOptionsBackdrop,
keyboard: settings.modalOptionsKeyboard
});
}
modal.on('shown.bs.modal', function () {
modal.find(".btn-primary:first").focus();
});
modal.on('hidden.bs.modal', function () {
modal.remove();
});
modal.find(".confirm").click(function () {
settings.confirm(settings.button);
});
modal.find(".cancel").click(function () {
settings.cancel(settings.button);
});
// Show the modal
$("body").append(modal);
modal.modal('show');
};
/**
* Globally definable rules
*/
$.confirm.options = {
text: "Are you sure?",
title: "",
confirmButton: "Yes",
cancelButton: "Cancel",
post: false,
submitForm: false,
confirmButtonClass: "btn-primary",
cancelButtonClass: "btn-default",
dialogClass: "modal-dialog",
modalOptionsBackdrop: true,
modalOptionsKeyboard: true
}
})(jQuery);