Light12306/Web12306/js/ui/widget_modalDialog.js

87 lines
2.4 KiB
JavaScript
Raw Normal View History

2014-08-26 21:29:58 +08:00
define(function (require, exports, module) {
var modalTemplate = $("div.modal-dialog");
var widget = require("./widget.js");
2014-08-30 01:32:27 +08:00
$.showModalDialog = function (content, options) {
2014-09-01 20:24:57 +08:00
options = $.extend({ destory: true, closeOnAction: true }, options);
2014-08-30 01:32:27 +08:00
$((options.image ? "<img src='" + options.image + "' />" : "") + "<p>" + content + "</p>").showModalDialog(options);
};
2014-08-26 21:29:58 +08:00
$.fn.showModalDialog = function (options) {
if (!this.length || this.is(":visible"))
return this;
2014-08-30 01:32:27 +08:00
options = $.extend({}, { buttons: [], cancelConfirm: null, destory: false, title: "", showCloseButton: true, closeOnAction: true }, options);
2014-08-26 21:29:58 +08:00
var template = modalTemplate.clone();
2014-08-30 01:32:27 +08:00
var closed = false;
2014-08-26 21:29:58 +08:00
$(document.body).append(template);
template.find(">div").append(this);
2014-08-30 01:32:27 +08:00
if (options.title)
template.find(">header>span").html(options.title);
else template.addClass("modal-popup").find(">header").remove();
2014-08-26 21:29:58 +08:00
this.show();
var hideModalDialog = function (callback) {
2014-08-30 01:32:27 +08:00
if (closed || options.cancelConfirm && !options.cancelConfirm())
2014-08-26 21:29:58 +08:00
return;
widget.hideFloatDialog(template, function () {
2014-08-30 01:32:27 +08:00
if (options.destory) {
template.find(">div").empty();
} else {
$(document.body).append(template.find(">div").children().hide());
}
2014-08-26 21:29:58 +08:00
template.remove();
callback && callback();
});
2014-08-30 01:32:27 +08:00
closed = true;
2014-08-26 21:29:58 +08:00
};
options.hide = hideModalDialog;
2014-09-01 20:24:57 +08:00
if (!options.showCloseButton) {
template.find(">header>i").remove();
} else {
template.find(">header>i").click(function() {
hideModalDialog();
});
}
2014-08-26 21:29:58 +08:00
//处理按钮
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 () {
2015-03-13 19:25:08 +08:00
var result = this.apply(template, [options, btn]);
if (result === true) {
hideModalDialog();
}
return result;
2014-08-26 21:29:58 +08:00
}.bind(this.callback));
}
2014-08-30 01:32:27 +08:00
if (options.closeOnAction || this.cancel) {
btn.click(function (e) {
if (e.cancel)
2014-08-30 01:32:27 +08:00
return;
2014-08-26 21:29:58 +08:00
hideModalDialog();
});
}
});
2014-09-03 23:20:12 +08:00
if (!options.buttons || !options.buttons.length) {
buttonContainer.remove();
}
if (options.width) {
template.css("width", options.width);
}
2014-08-26 21:29:58 +08:00
widget.showFloatDialog(template);
return this;
};
});