2014-06-12 21:36:05 +08:00
|
|
|
|
define(function (require, exports, module) {
|
|
|
|
|
var htmlTemplate = '<section class="message-popup"><div class="message-popup-inner"><b class=""></b> <span></span></div></section>';
|
|
|
|
|
var iconMap = {
|
|
|
|
|
warn: "warning",
|
|
|
|
|
ok: "check",
|
|
|
|
|
error: "times-circle",
|
|
|
|
|
loading: "spinner"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var MessagePopup = function (type, content, options) {
|
|
|
|
|
var that = this;
|
|
|
|
|
var html = $(htmlTemplate);
|
|
|
|
|
var shown = false;
|
|
|
|
|
options = $.extend({ closeAfter: null }, options);
|
|
|
|
|
$("body").append(html);
|
|
|
|
|
|
|
|
|
|
var centerElement = function (ele) {
|
|
|
|
|
ele = ele || html;
|
|
|
|
|
|
|
|
|
|
var width = ele.width();
|
|
|
|
|
var height = ele.height();
|
|
|
|
|
|
|
|
|
|
ele.css({
|
|
|
|
|
"margin-left": "-" + (width / 2) + "px",
|
|
|
|
|
"margin-top": "-" + (height / 2 - 40) + "px"
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
this.show = function () {
|
2014-07-04 20:57:57 +08:00
|
|
|
|
html.css("opacity", "0");
|
|
|
|
|
html.show();
|
2014-06-12 21:36:05 +08:00
|
|
|
|
html.animate({ "margin-top": "-=20px", opacity: "1" }, "fast", "linear", function () {
|
|
|
|
|
shown = true;
|
|
|
|
|
if (options.closeAfter) {
|
|
|
|
|
that.delayClose();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
this.close = function () {
|
2014-07-04 20:57:57 +08:00
|
|
|
|
html.animate({ "margin-top": "-=20px", opacity: "0" }, "fast", "linear", function () {
|
|
|
|
|
html.hide();
|
2014-06-12 21:36:05 +08:00
|
|
|
|
html.remove();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
this.delayClose = function (timeout) {
|
2014-06-27 22:25:42 +08:00
|
|
|
|
timeout = timeout || options.closeAfter || 1500;
|
2014-06-12 21:36:05 +08:00
|
|
|
|
if (that.shown)
|
2014-06-27 22:25:42 +08:00
|
|
|
|
setTimeout(that.close, timeout);
|
2014-06-12 21:36:05 +08:00
|
|
|
|
else {
|
2014-06-27 22:25:42 +08:00
|
|
|
|
options.closeAfter = timeout;
|
2014-06-12 21:36:05 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
that.setState = function (type, content) {
|
|
|
|
|
that.type = type;
|
|
|
|
|
that.content = content;
|
|
|
|
|
};
|
|
|
|
|
Object.defineProperties(this, {
|
|
|
|
|
"content": {
|
|
|
|
|
get: function () {
|
|
|
|
|
return html.find("span").html();
|
|
|
|
|
},
|
|
|
|
|
set: function (value) {
|
|
|
|
|
html.find("span").html(value);
|
|
|
|
|
centerElement();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"type": {
|
|
|
|
|
get: function () {
|
|
|
|
|
return html.attr("data-type");
|
|
|
|
|
},
|
|
|
|
|
set: function (value) {
|
|
|
|
|
html.attr("data-type", value);
|
|
|
|
|
html.removeClass().addClass("message-popup message-popup-" + value);
|
|
|
|
|
html.find("b").removeClass().addClass("fa fa-" + iconMap[value] + (value === "loading" ? " fa-spin" : ""));
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"shown": {
|
|
|
|
|
get: function () {
|
|
|
|
|
return shown;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
this.content = content;
|
|
|
|
|
this.type = type;
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.MessagePopup = MessagePopup;
|
|
|
|
|
|
|
|
|
|
exports.showMessagePopup = function (icon, content, options) {
|
2014-08-18 23:12:35 +08:00
|
|
|
|
options = $.extend({ closeAfter: 2000 }, options);
|
2014-06-12 21:36:05 +08:00
|
|
|
|
|
|
|
|
|
var popup = new MessagePopup(icon, content, options);
|
|
|
|
|
popup.show();
|
|
|
|
|
return popup;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.confirm = function (title, content, yes, no) {
|
|
|
|
|
//TODO 确认界面
|
|
|
|
|
if (confirm(content))
|
|
|
|
|
yes && yes();
|
|
|
|
|
else no && no();
|
|
|
|
|
};
|
|
|
|
|
exports.alert = function (title, content, callback) {
|
|
|
|
|
//TODO 确认界面
|
2014-06-20 20:55:14 +08:00
|
|
|
|
if (!content) {
|
|
|
|
|
content = title || "提示";
|
|
|
|
|
title = "提示";
|
|
|
|
|
}
|
2014-06-12 21:36:05 +08:00
|
|
|
|
alert(content);
|
|
|
|
|
callback && callback();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//捕捉一些通用的事件
|
|
|
|
|
document.addEventListener("requestSupportError", function () {
|
|
|
|
|
exports.showMessagePopup("error", "无法执行网络请求,似乎没有安装12306订票助手哦。", { closeAfter: null });
|
|
|
|
|
});
|
2014-08-27 23:05:50 +08:00
|
|
|
|
document.addEventListener("verifyCodeLoadFailed", function () {
|
2014-06-13 19:52:22 +08:00
|
|
|
|
exports.showMessagePopup("error", "验证码加载失败,请点击验证码图片刷新哦。");
|
|
|
|
|
});
|
2014-08-27 23:05:50 +08:00
|
|
|
|
document.addEventListener("platformError", function () {
|
|
|
|
|
exports.alert("错误", "未能执行请求,可能12306已改版导致订票助手出错!");
|
|
|
|
|
});
|
2014-06-12 21:36:05 +08:00
|
|
|
|
});
|