Light12306/Web12306/js/ui/widget_message_popup.js
2014-09-10 21:49:46 +08:00

189 lines
4.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

define(function (require, exports, module) {
var md = require("./widget_modalDialog.js");
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 () {
html.css("opacity", "0");
html.show();
html.animate({ "margin-top": "-=20px", opacity: "1" }, "fast", "linear", function () {
shown = true;
if (options.closeAfter) {
that.delayClose();
}
});
};
this.close = function () {
html.animate({ "margin-top": "-=20px", opacity: "0" }, "fast", "linear", function () {
html.hide();
html.remove();
});
};
this.delayClose = function (timeout) {
timeout = timeout || options.closeAfter || 1500;
if (that.shown)
setTimeout(that.close, timeout);
else {
options.closeAfter = timeout;
}
};
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) {
options = $.extend({ closeAfter: 2000 }, options);
var popup = new MessagePopup(icon, content, options);
popup.show();
return popup;
};
exports.confirm = function (title, content, yes, no) {
$.showModalDialog(content, {
buttons: [
{ text: "确定", callback: yes, type: "primary" },
{ text: "取消", callback: no }
],
closeOnAction: true
});
};
exports.alert = function (title, content, callback) {
content = content || title;
$.showModalDialog(content, {
buttons: [
{ text: "确定", callback: callback, type: "primary" }
],
closeOnAction: true
});
};
exports.showFatalError = function () {
$.showModalDialog("12306又不肯配合或者干脆罢工了...现在抢票功能暂时无法使用请暂时使用12306官网购票订票助手依然会帮你的....", {
image: "/images/cat.png",
buttons: [
{
text: "打开12306官网",
type: "primary",
callback: function () {
window.open("https://kyfw.12306.cn/otn/leftTicket/init");
return false;
}
}, {
text: "刷新重试",
callback: function () {
self.location.reload();
return false;
}
}
],
closeOnAction: false
});
};
exports.showNonExtensionInstall = function () {
var isLb = navigator.userAgent.indexOf("LBBROWSER") != -1;
var btns = [
{
text: "刷新",
type: "default",
callback: function () {
self.location.reload();
return false;
}
}
];
if (!isLb) {
btns.unshift({
text: "安装猎豹浏览器",
type: "primary",
callback: function () {
window.open("http://dl.liebao.cn/coop/KSBrowser_12306.exe");
return false;
}
});
}
btns.unshift({
text: "安装扩展",
type: "primary",
callback: function () {
window.open("http://12306.fishlee.net/crx");
return false;
}
});
$.showModalDialog("您需要安装" + (isLb ? "" : "<u>猎豹浏览器</u>并") + "启用<u>最新版12306订票助手扩展</u>才可以使用极速版订票。请安装后刷新此页面。如果已安装,请检查是否不小心禁用了 :-(", {
image: "/images/cat.png",
buttons: btns,
closeOnAction: false
});
};
//捕捉一些通用的事件
document.addEventListener("requestSupportError", function () {
exports.showNonExtensionInstall();
});
document.addEventListener("verifyCodeLoadFailed", function () {
exports.showMessagePopup("error", "验证码加载失败,请点击验证码图片刷新哦。");
});
document.addEventListener("platformError", function () {
exports.showFatalError();
});
});