87 lines
2.4 KiB
JavaScript
87 lines
2.4 KiB
JavaScript
define(function (require, exports, module) {
|
|
var modalTemplate = $("div.modal-dialog");
|
|
var widget = require("./widget.js");
|
|
|
|
$.showModalDialog = function (content, options) {
|
|
options = $.extend({ destory: true, closeOnAction: true }, options);
|
|
$((options.image ? "<img src='" + options.image + "' />" : "") + "<p>" + content + "</p>").showModalDialog(options);
|
|
};
|
|
|
|
$.fn.showModalDialog = function (options) {
|
|
if (!this.length || this.is(":visible"))
|
|
return this;
|
|
|
|
options = $.extend({}, { buttons: [], cancelConfirm: null, destory: false, title: "", showCloseButton: true, closeOnAction: true }, options);
|
|
|
|
var template = modalTemplate.clone();
|
|
var closed = false;
|
|
$(document.body).append(template);
|
|
template.find(">div").append(this);
|
|
if (options.title)
|
|
template.find(">header>span").html(options.title);
|
|
else template.addClass("modal-popup").find(">header").remove();
|
|
this.show();
|
|
|
|
var hideModalDialog = function (callback) {
|
|
if (closed || options.cancelConfirm && !options.cancelConfirm())
|
|
return;
|
|
|
|
widget.hideFloatDialog(template, function () {
|
|
if (options.destory) {
|
|
template.find(">div").empty();
|
|
} else {
|
|
$(document.body).append(template.find(">div").children().hide());
|
|
}
|
|
template.remove();
|
|
callback && callback();
|
|
});
|
|
closed = true;
|
|
};
|
|
options.hide = hideModalDialog;
|
|
|
|
if (!options.showCloseButton) {
|
|
template.find(">header>i").remove();
|
|
} else {
|
|
template.find(">header>i").click(function() {
|
|
hideModalDialog();
|
|
});
|
|
}
|
|
//处理按钮
|
|
var buttonContainer = template.find(">footer");
|
|
$.each(options.buttons, function () {
|
|
var btn = $("<button type='button' class='button button-" + (this.type || "default") + "'></button>");
|
|
buttonContainer.append(btn);
|
|
btn.text(this.text || "按钮");
|
|
if (this.icon) {
|
|
btn.prepend("<i class='fa fa-" + this.icons + "' />");
|
|
}
|
|
if (this.callback) {
|
|
btn.click(function () {
|
|
var result = this.apply(template, [options, btn]);
|
|
if (result === true) {
|
|
hideModalDialog();
|
|
}
|
|
return result;
|
|
}.bind(this.callback));
|
|
}
|
|
if (options.closeOnAction || this.cancel) {
|
|
btn.click(function (e) {
|
|
if (e.cancel)
|
|
return;
|
|
hideModalDialog();
|
|
});
|
|
}
|
|
});
|
|
if (!options.buttons || !options.buttons.length) {
|
|
buttonContainer.remove();
|
|
}
|
|
if (options.width) {
|
|
template.css("width", options.width);
|
|
}
|
|
|
|
widget.showFloatDialog(template);
|
|
|
|
return this;
|
|
};
|
|
});
|