-
Notifications
You must be signed in to change notification settings - Fork 0
/
MsgBoxHelper.bootstrap.js
122 lines (100 loc) · 3.9 KB
/
MsgBoxHelper.bootstrap.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
var MsgBoxHelper = {
__defaultOptions: {
dialogClassName: "",
buttonClassName: "",
onValidate: null,
beforeShow: null
},
setOptions(options) {
Object.assign(this.__defaultOptions, options);
},
msgInfo: function (msgText, callback)
{
MsgBoxHelper.customMessage(msgText, ["OK"], callback);
},
msgAsk: function (msgText, callback)
{
MsgBoxHelper.customMessage(msgText, ["Sim", "Não"], function (button) {
if (isFunction(callback))
callback(button === 0);
});
},
msgConfirm: function (msgText, callback)
{
MsgBoxHelper.customMessage(msgText, ["OK", "Cancelar"], function (button) {
if (isFunction(callback))
callback(button === 0);
});
},
customMessage: function (msg, buttons, callback, options)
{
let opt = {};
Object.assign(opt, this.__defaultOptions);
Object.assign(opt, options);
var msgBox = jQuery(".message-box");
if (msgBox.length > 0)
msgBox.remove();
msgBox =
jQuery("<div>").addClass("modal").addClass("message-box").attr("role", "dialog")
.append(jQuery("<div>").addClass("modal-dialog").attr("role", "document")
.append(jQuery("<div>").addClass("modal-content")
.append(jQuery("<div>").addClass("modal-body"))
.append(jQuery("<div>").addClass("modal-footer"))
)
);
jQuery("body").append(msgBox);
msgBox.find(".modal-content").addClass(opt.dialogClassName);
let divContent = jQuery("<div class='message-content'>");
msgBox.find(".modal-body").append(divContent);
if (typeof msg === "string")
{
divContent.addClass("message-text");
divContent.text(msg);
}
else
divContent.append(jQuery(msg));
//msgBox.find(".modal-body").html(msg);
var footer = msgBox.find(".modal-footer");
if (buttons === null || buttons.length == 0)
{
if (footer.length > 0)
footer.remove();
}
else
{
var footer = msgBox.find(".modal-footer");
footer.empty();
for (var i = 0; i < buttons.length; i++)
footer.append(
jQuery("<button>")
.attr("type", "button")
.data("button-index", i)
.addClass("btn btn-primary")
.addClass(opt.buttonClassName)
.css({ minWidth: "90px" })
.text(buttons[i])
.click(function (event) {
var clickedIndex = jQuery(event.target).data("button-index");
if (Br1Helper.isFunction(opt.onValidate))
if (!opt.onValidate(idx, msgBox))
return;
msgBox.on('hidden.bs.modal', function (e) {
setTimeout(function () {
if ( callback !== null && typeof callback !== 'undefined')
callback(clickedIndex);
}, 100);
});
msgBox.modal("hide");
})
);
}
msgBox.modal({
backdrop: "static",
show: true
});
return msgBox;
},
closeModal: function() {
jQuery(".message-box").modal("hide");
}
};