//刷票页面 $(function () { var $doc = $(document); var initialized = false; var currentUser = null; //UI初始化 $("#seatTypeList label").each(function (i, e) { e.dataset["index"] = i; }); //联系人搜索 $("#addp_search_key") .searchDelay() .bind("search.fish.searchdelay", function (e, key) { if (!key) { $("#contactList button").show(); $("#addp_search_clear").prop("disabled", true); } else { $("#contactList button").hide().filter("[data-key*=" + key + "], [data-fl*=" + key.toUpperCase() + "]").show(); $("#addp_search_clear").prop("disabled", !key); } }); $("#addp_search_clear").click(function () { $("#addp_search_key").val("").searchDelay("search"); }); //日期选择 (function () { var dateLoopContainer = $("#dateLoopBox"); var date = new Date().trimToDay(); var html = []; for (var j = 0; j < 25; j++) { var nd = date.addDays(j); html.push(""); } dateLoopContainer.html(html.join("")); $doc.on("click", "#dateLoopContainer button", function () { //点击删除 var btn = $(this); var dt = this.dataset.fulldate; currentUser.currentProfile.dateloop = _.without(currentUser.currentProfile.dateloop, dt, null); currentUser.save(); btn.remove(); $("#dateLoop .modal-body button[data-fulldate='" + dt + "']").prop("disabled", false); $("#dateLoopBtn").prop("disabled", false); message.sendToTab({ action: "removeDateLoop", detail: dt }); }); $doc.on("click", "#dateLoopBox button", function () { var btn = $(this); currentUser.currentProfile.dateloop.push(this.dataset.fulldate); $("#dateLoopContainer").append(btn.clone().removeClass("btn-primary").addClass("btn-default")); this.disabled = true; currentUser.save(); $("#dateLoopBtn").prop("disabled", currentUser.currentProfile.dateloop.length >= 5); if (currentUser.currentProfile.dateloop.length >= 5) { $("#dateLoop").modal("hide"); } message.sendToTab({ action: "addDateLoop", detail: this.dataset.fulldate }); }); message.addAction("addDateLoop", function () { $("#dateLoop .modal-body button[data-fulldate='" + this + "']:enabled").click(); }); message.addAction("removeDateLoop", function () { $("#dateLoopContainer button[data-fulldate='" + this + "']").click(); }); })(); var applyProfile = function (p) { if (!p) return; //处理席别 var seatContainer = $("#seatTypeList"); var bakst = p.selectedSeatType; seatContainer.find(":checkbox:checked").each(function () { this.checked = false; $(this).change(); }); $.each(bakst, function () { seatContainer.find(":checkbox[value=" + this + "]:not(:checked)").prop("checked", true).change(); }); p.selectedSeatType = bakst; $("#trains").val(p.selectedTrain.join(',')).change(); trains.importTags(p.selectedTrain.join(',')); //联系人 $("#passengers").html($.map(currentUser.currentProfile.passengers, function (e) { return e.toHtml(true); }).join("")); $("#btnAddPassengerD, #btnAddPassengerM").prop("disabled", currentUser.currentProfile.passengers.length >= 5); //日期轮查 (function () { var dateloop = $("#dateLoopContainer"); var cd = new Date().trimToDay(); dateloop.empty(); $("#dateLoop button").prop("disabled", false); var loopFiltered = []; $.each(currentUser.currentProfile.dateloop, function () { var dd = new Date(this + ''); if (dd >= cd) { dateloop.append(""); loopFiltered.push(this + ''); } currentUser.currentProfile.dateloop = loopFiltered; $("#dateLoop button[data-fulldate='" + this + "']").prop("disabled", true); }); $("#dateLoopBtn").prop("disabled", currentUser.currentProfile.dateloop.length >= 5); })(); //各种选项 $("input[data-profile-key], select[data-profile-key]").each(function () { var ele = this; var $ele = $(this); var type = ele.type; var key = ele.dataset.profileKey; var profile = currentUser.currentProfile; var propertyvalue = profile[key]; if (typeof propertyvalue == 'boolean') { if (type == "radio") { $("input[name=" + ele.name + "][value=" + (propertyvalue ? 1 : 0) + "]")[0].checked = true; } else if (type == "checkbox") { ele.checked = propertyvalue; } else { $ele.val(propertyvalue ? "1" : "0"); } } else { $ele.val(propertyvalue); } }); message.sendToTab({ action: "profileChanged", detail: JSON.parse(JSON.stringify(p)) }); }; var applyPassengers = function () { if (!currentUser) return; $("#contactList").empty(); if (!currentUser.passengers || !currentUser.passengers.length) { $("#addp_refresh").bsButton("loading"); currentUser.reloadPassengers(); return; } var list = $.map(currentUser.passengers, function (e) { return e.toHtml(); }); $("#contactList").html(list.join("")).find(); $("#addp_refresh").bsButton("reset"); $("#addPassenger").trigger("show.bs.modal"); }; //席别 $("input:checkbox[name=seat]").change(function () { if (!currentUser) return; var $cb = $(this); var $parent = $cb.parent(); var $container = $parent.parent(); if (this.checked) { //已选中? $parent.addClass("highlight"); //将其移动到队列末端 var $p = $parent.prevAll().find(":checkbox:checked").parent().last(); $p.length ? $p.after($parent) : $container.prepend($parent); if (currentUser && initialized) currentUser.currentProfile.selectedSeatType.push(this.value); if (initialized) message.sendActionToTab("addSeat", this.value); } else { //取消选择,则放回原来的位置 var index = $parent[0].dataset.index; var notChecked = $parent.nextAll().find(":checkbox:not(:checked)").parent(); $parent.removeClass("highlight"); if (!notChecked.length) $container.append($parent); else { if (index > notChecked.last()[0].dataset.index) { notChecked.last().after($parent); } else if (index < notChecked.first()[0].dataset.index) { notChecked.first().before($parent); } else { for (var j = 1; j < notChecked.length; j++) { if (notChecked[j - 1].dataset.index < index && notChecked[j].dataset.index > index) { notChecked.eq(j - 1).after($parent); break; } } } } if (currentUser && initialized) currentUser.currentProfile.selectedSeatType = _.without(currentUser.currentProfile.selectedSeatType, this.value); if (initialized) message.sendActionToTab("removeSeat", this.value); } if (currentUser && initialized) currentUser.save(); }); $doc.on("click", ".seatCheckLink", function () { var codes = this.dataset.target; $("input:checkbox[name=seat]:checked").each(function () { this.checked = false; $(this).change(); }); for (var j = 0; j < codes.length; j++) { $("input:checkbox[name=seat][value=" + codes[j] + "]").prop("checked", true).change(); } message.sendActionToTab("seatReset", currentUser.currentProfile.selectedSeatType); }); message.addAction("addSeat", function () { $("input:checkbox[name=seat][value=" + this + "]:not(:checked)").prop("checked", true).change(); }); message.addAction("removeSeat", function () { $("input:checkbox[name=seat][value=" + this + "]:checked").prop("checked", false).change(); }); //车次 var trains = $("#trains").tagsInput({ height: "20px", defaultText: "输入车次", removeWithBackspace: true, spacer: ",. |\n", width: "100%", onChange: function () { if (!currentUser || !initialized) return; currentUser.currentProfile.selectedTrain = _.filter($("#trains").val().split(','), function (e) { return e; }); currentUser.save(); }, onAddTag: function (id) { if (!initialized) return; message.sendActionToTab("addTrain", id); }, onRemoveTag: function (id) { if (!initialized) return; message.sendActionToTab("removeTrain", id); }, autosize: false, onBeforeAdd: function (v) { return v ? v.toUpperCase() : ""; } }); $("#addtrain_ok").click(function () { var val = $.trim($("#addtrain_code").val()); if (!val) return; //自动转换成正则表达式 val = val.replace(/[\s,,.。-]/g, "|").replace(/[\^\$]/g, "").toUpperCase(); if (trains.tagExist(val)) return; try { new RegExp("^" + val + "$"); } catch (e) { alert("表达式不正确..."); return; } trains.addTag(val); $("#addTrain").modal("hide"); }); $doc.on("click", ".cmd-addtrain", function () { if (!trains.tagExist(this.dataset.code)) trains.addTag(this.dataset.code); }); message.addAction("addTrain", function () { var code = this + ''; if (!trains.tagExist(code)) trains.addTag(code); }); message.addAction("removeTrain", function () { var code = this + ''; if (trains.tagExist(code)) trains.removeTag(code); }); //联系人 (function () { $doc.on("show.bs.modal", "#addPassenger", function () { var container = $("#addPassenger"); var buttons = container.find("[data-key]"); buttons.prop("disabled", false); buttons.find("i").remove(); $.each(currentUser.currentProfile.passengers, function () { buttons.filter("[data-key='" + this.key + "']").prop("disabled", true).prepend(''); }); }); var addPassenger = function (p) { if (_.findWhere(currentUser.currentProfile.passengers, { key: p.key })) return; currentUser.currentProfile.passengers.push(p); $("#passengers").append(p.toHtml(true)); $("#btnAddPassengerD, #btnAddPassengerM").prop("disabled", currentUser.currentProfile.passengers.length >= 5); currentUser.save(); message.sendActionToTab("addPassenger", p); }; var removePassenger = function (p) { if (!_.findWhere(currentUser.currentProfile.passengers, { key: p.key })) return; currentUser.currentProfile.passengers = _.without(currentUser.currentProfile.passengers, p); $("#btnAddPassengerD, #btnAddPassengerM").prop("disabled", false); currentUser.save(); message.sendActionToTab("removePassenger", p); }; $doc.on("click", "#addPassenger button[data-key]", function () { if (currentUser.currentProfile.passengers.length >= 5) { alert("亲,联系人最多只能选择五个哦。"); $("#addPassenger").modal("hide"); return; } this.disabled = true; $(this).prepend(''); var key = this.dataset.key; var p = _.findWhere(currentUser.passengers, { key: key }); addPassenger(p); if (currentUser.currentProfile.passengers.length >= 5) { $("#addPassenger").modal("hide"); } }); $doc.on("click", "#passengers button[data-key]", function () { var key = this.dataset.key; var $btn = $(this); $btn.remove(); removePassenger(_.findWhere(currentUser.currentProfile.passengers, { key: key })); }); $("#addmp_ok").click(function () { if (currentUser.currentProfile.passengers.length >= 5) { alert("亲,联系人最多只能选择五个哦。"); $("#enterPassenger").modal("hide"); return; } var idtype = $("#addmp_idtype").val(); var id = $("#addmp_id").val(); var name = $("#addmp_name").val(); var type = $("#addmp_tickettype").val(); if (!name || !id) { alert("咦,名字或证件号没写...."); return; } var p = _.findWhere(currentUser.currentProfile.passengers, { name: name, idtype: idtype, id: id }); if (p) { alert("这位客官,同名同证件号的乘客已经添加过了哦。。"); return; } var pm = new Passenger(name, type, passengerTypeCode[type], idtype, idTypeCode[idtype], id, ""); pm.save = false; addPassenger(pm); $("#enterPassenger").modal("hide"); }); message.addAction("addPassenger", function () { //查找联系人 var p = _.findWhere(currentUser.passengers, { name: this.name, typename: this.typename, id: this.id }); if (p) addPassenger(p); }); message.addAction("removePassenger", function () { //查找联系人 var p = _.findWhere(currentUser.passengers, { name: this.name, typename: this.typename, id: this.id }); if (p) { $("#passengers button[data-key='" + p.key + "']").click(); } }); })(); //时间选择初始化 var ranges = $("#timerangeContainer select"); for (var i = 0; i < 24; i++) { var number = (i < 10 ? "0" : "") + i; ranges.each(function () { this.options[i] = new Option(number + ":" + (this.dataset.isend ? "59" : "00"), i); }); } //各种杂项以及通过 data-profile-key 进行绑定的元素 (function () { $doc.on("change", "[data-profile-key]", function () { if (!currentUser) return; var ele = this; var $ele = $(this); var type = ele.type; var key = ele.dataset.profileKey; var profile = currentUser.currentProfile; var propertyvalue = profile[key]; if (typeof propertyvalue == 'boolean') { if (type == "radio") { profile[key] = $("input[name=" + ele.name + "]:checked")[0].value == "1"; } else if (type == "checkbox") { profile[key] = ele.checked; } else { profile[key] = $ele.val() == "1"; } } else if (typeof propertyvalue == 'number') { profile[key] = parseInt($ele.val() + ''); } else { profile[key] = $.trim($ele.val()); } profile.save(); if (initialized) { message.sendActionToTab("miscChanged", { key: key, value: profile[key] }); } }); $doc.on("change", "[data-sysconfigkey]", function () { if (!sysConfig) return; var ele = this; var $ele = $(this); var type = ele.type; var key = ele.dataset.sysconfigkey; var profile = {}; if (type == "radio") { profile[key] = $("input[name=" + ele.name + "]:checked")[0].value == "1"; } else if (type == "checkbox") { profile[key] = ele.checked; } else { profile[key] = $.trim($ele.val()); } message.sendAction("setUserConfig", profile); }); message.addAction("miscChanged", function () { var obj = $("[data-profile-key='" + this.key + "']"); var dom; if (!obj.length) return; var type = obj.attr("type"); if (typeof this.value === 'boolean') { if (type === "radio") { dom = $("input[name=" + obj[0].name + "][value=" + (this.value ? 1 : 0) + "]")[0]; if (dom.checked === this.value) return; dom.checked = this.value; } else if (type === "checkbox") { if (obj[0].checked === this.value) return; obj[0].checked = this.value; } else { if (obj.val() == this.value) return; obj.val(this.value ? "1" : "0"); } } else { if (obj.val() == this.value) return; obj.val(this.value); } obj.change(); }); })(); //出行计划保存 $("#cmdSaveTravelMethod").click(function () { message.sendToTab({ action: "getStationInfo" }, function (response) { var info = response.detail; if (!info || !info.fromCode || !info.toCode) { alert("您还木有选择出发地目标地和出发时间呢。。。"); return; } $.extend(currentUser.currentProfile, info); var name = prompt("请输入新的出行计划的名称(如果名称已经存在,则会被覆盖):", info.fromText + "--" + info.toText); if (!name) return; //移除老的 $("#historyList button").each(function () { if ($(this).text() == name) { var buttonGroup = $(this).closest(".btn-group"); currentUser.savedProfile.remove(buttonGroup.data("travel")); buttonGroup.removeData("travel"); buttonGroup.remove(); return false; } }); var pro = new Profile(currentUser.currentProfile); pro.name = name; currentUser.savedProfile.add(pro); var html = $('
'); html.data("travel", pro); $("#historyList").append(html); notification.create({ iconUrl: LARGEICON, message: "出行计划已保存~", title: "订票助手" }); }); }); $doc.on("click", "button[data-cmd=deleteTravelWay]", function () { var buttonGroup = $(this).closest(".btn-group"); var name = buttonGroup.find("button:first").text(); if (!confirm("确定要删除出行计划【" + name + "】吗?")) return; currentUser.savedProfile.remove(buttonGroup.data("travel")); buttonGroup.removeData("travel"); buttonGroup.remove(); }); $doc.on("click", "#historyList button.btn-info", function () { var btn = $(this); var name = $.trim(btn.text()); var profile = _.findWhere(currentUser.savedProfile.list, { name: name }); currentUser.resetCurrentProfile(profile); applyProfile(currentUser.currentProfile); message.sendActionToTab("profileReload", profile); $("#travelHistory").modal("hide"); }); //重置请求 $("#btnResetProfile").click(function () { if (!currentUser) return; currentUser.currentProfile.reset(); applyProfile(currentUser.currentProfile); currentUser.save(); }); //刷新联系人 $("#addp_refresh").click(function () { if (!currentUser || this.disabled) return; currentUser.passengers = []; applyPassengers(); }); if (currentUser) { applyProfile(currentUser.currentProfile); applyPassengers(); } //还原状态 var applyUser = function (user) { initialized = false; if (!user) return; currentUser = user; applyProfile(user.currentProfile); applyPassengers(); //出行计划 var travelContainer = $("#historyList"); travelContainer.empty(); $.each(currentUser.savedProfile.list, function () { var t = this; var html = $(''); html.data("travel", this); travelContainer.append(html); }); //绑定事件 currentUser.on("passengerLoaded", function () { applyPassengers(); }); currentUser.on("userSaved", function () { message.sendToTab({ action: "profileChanged", detail: JSON.parse(JSON.stringify(currentUser.currentProfile)) }); }); initialized = true; }; //处理消息 (function () { message.addAction("stationInfoUpdate", function () { if (currentUser) { $.extend(currentUser.currentProfile, this); currentUser.save(); } }); message.addAction("getCurrentProfile", function (response) { if (!currentUser) return; response({ detail: JSON.parse(JSON.stringify(currentUser.currentProfile)) }); }); message.addAction("getCurrentUser", function (response) { if (!currentUser) return; response({ detail: JSON.parse(JSON.stringify(currentUser)) }); }); message.addAction("passengerUpdated", function () { currentUser.reloadPassengers(this); }); message.addAction("userOptionChanged", function () { currentUser.options = $.extend(currentUser.options, this); currentUser.save(); }); message.addAction("setResignMode", function() { var p = this; if (p.profile) { currentUser.currentProfile = $.extend(currentUser.currentProfile, p.profile); //禁用学生票的选择 $("input:checkbox[name=optStudent]").prop("disabled", true); } else { $("input:checkbox[name=optStudent]").prop("disabled", false); } }); })(); (function () { //刷新统计 var counters = $("#queryStat"); var refreshCount = 0; var statData = null; var lastCity = null; var enableBus = new Date() < new Date("2014-01-25 00:00:00"); var enableSfc = new Date() < new Date("2014-02-15 00:00:00"); var manuallyHide = false; var checkIsBus = function (fromText, toText) { if (!enableBus || manuallyHide) return false; if (fromText.indexOf("北京") === -1) return false; var target = ["呼和浩特", "太原", "菏泽", "大连", "合肥", "南京", "武汉"]; var valid = _.some(target, function (e) { return toText.indexOf(e) !== -1; }); return valid; }; var checkSfc = function(formText, toText) { if (!enableSfc || manuallyHide) return false; return true; }; message.addAction("queryStatisticsReport", function () { statData = this; var stat = this.stat; var profile = currentUser.currentProfile; if (lastCity !== profile.fromCode + profile.toCode) { refreshCount = 0; lastCity = profile.fromCode + profile.toCode; } refreshCount++; counters.css("visibility", "visible"); counters.find("span:eq(0)").html("第 " + refreshCount + " 次查票"); if (this.failed) { counters.find("span:eq(1)").html("查询失败"); counters.find("span:eq(2)").html("查询失败"); } else { counters.find("span:eq(1)").html("" + stat.original.length + " 趟车"); counters.find("span:eq(2)").html(stat.filtered.length + " 趟车过滤"); } if ($("#btnRefresh.btn-success, #btnRefresh.btn-info").length) { chrome.extension.sendMessage({ ticketEvent: 'refresh', times: refreshCount }); } //$("#bus").hide(); //if (refreshCount >= 30) { // var a = $("#bus"); // if (checkIsBus(profile.fromText, profile.toText)) { // //可以橙色大巴 // a.find("span").html("免费橙色大巴送您顺利回家"); // a.find("a:eq(0)").attr("href", "http://www.ijinshan.com/bus/"); // a.show(); // } else if (checkSfc(profile.fromText, profile.toText)) { // //显示顺风车 // a.find("span").html("免费顺风车助您顺利回家"); // a.find("a:eq(0)").attr("href", "http://www.shunfengche.org/NewYearAction.aspx"); // a.show(); // } //} }); $("#bus a:last").click(function() { manuallyHide = true; $(this).parent().hide(); }); $(document).on("show.bs.modal", "#queryResult", function () { var div = $("#queryResult .modal-body"); var html = []; var showGroup = function (reason, title) { var list = _.where(statData.stat.filtered, { reason: reason }); if (!list.length) return; html.push("" + title + ":" + _.map(list, function (e) { return e.code; }).join("; ")); }; showGroup(1, " 因无票被过滤"); showGroup(2, "因发站不同过滤"); showGroup(3, "因到站不同过滤"); showGroup(4, "时间不对被过滤"); showGroup(5, "非预定车次过滤"); if (!html.length) { html.push("没有车次被过滤"); } div.html(html.join("