$(function() {
//战绩查询
var GradeSearch = function() {
this.locationHref = "playerList.html";
this.fromCookie = false;
this.server = "all";
};
GradeSearch.prototype = {
constructor: GradeSearch,
init: function() {
var that = this;
this._initCookie();
$("#serversUl").niceScroll({
cursorcolor: "#D9D9D9",
cursorwidth: "8",
cursoropacitymin: 1,
cursorborder: 'none'
});
$("#searchBtn").on("click", function(e) {
that._submit(e);
});
$('#keyword').keydown(function(e) {
if (e.which == 13) {
that._submit(e);
}
});
$("#clearBtn").on("click", function(e) {
e.preventDefault();
that._clearCookie();
});
$("body").delegate('#records>a', 'click', function(e) {
e.preventDefault();
var value = $(this).find("label").html();
$("#keyword").val(value);
that.fromCookie = true;
that._submit(e);
});
$("#serversUl>li").on("click", function(e) {
e.preventDefault();
that.server = $(this).attr("data-server");
$("#servers").html(that.server);
});
$("#servers").on("click", function(e) {
e.preventDefault();
e.stopPropagation();
$("#serversUl").toggle();
});
$("body").on("click", function() {
var $rankUl = $("#serversUl");
if ($rankUl.css("display") != "none") {
$rankUl.css("display", "none");
}
});
//热门视频、正在直播
// this._getData().done(function(data) {
// var hotVideoHtml = '';
// var liveHtml = '';
// _.each(data.hot_video.slice(0, 4), function(item, key){
// if(key >= 2) return;
// hotVideoHtml += '
'+item.title+'
';
// });
// _.each(data.live, function(item, key){
// if(key >= 1) return;
// // console.log(item)
// var countUrls = item.url_media.split(",")
// liveHtml += ''+item.title+'
';
// });
// $('.video-left ul').html(hotVideoHtml);
// $('.video-right ul').html(liveHtml);
// });
},
_initCookie: function() {
var serverCookies = getCookie("pnCache");
if (serverCookies) {
$("#records").html('');
var sCookies = serverCookies.split("-").unique().reverse();
$.each(sCookies, function(i, cookiePn) {
var item = decodeURI(cookiePn);
var $key = i == 0 ? ("") : ("|");
$("#records").append($key);
});
$("#clearBtn").show();
} else {
$("#clearBtn").hide();
}
},
_submit: function(e) {
var that = this;
e.preventDefault();
var value = encodeURI($("#keyword").val());
if (value) {
$("#keyword").val("");
var shCookie = getCookie("pnCache"),
shCookieString = '',
sh;
if (shCookie) {
// var shCookieArr = shCookie.split("-");
// for (var i = 0, len = shCookieArr.length; i < len; i++) {
// var item = shCookieArr[i];
// if (i == 0) {
// shCookieString += item;
// } else {
// shCookieString += '-' + item;
// }
// }
sh = shCookie + "-" + value;
} else {
sh = value;
}
if (!this.fromCookie) {
addCookie("pnCache", sh);
setTimeout(function() {
that._initCookie();
});
}
this.fromCookie = false;
//数据上报
try {
window.external.data_report("click/search", "点击/搜索");
} catch (err) {}
//跳转
window.location.href = this.locationHref + "?sn=" + encodeURI(this.server) + "&pn=" + value;
}
},
_clearCookie: function() {
delCookie("pnCache");
$('#records').html('');
$('#clearBtn').hide();
},
_getData: function() {
return $.ajax({
url: 'http://lolbox.duowan.com/api/article/index.php?format=jsonp',
dataType: 'jsonp',
jsonpCallback: 'jQueryJsonp',
jsonp: 'callback'
});
}
};
Array.prototype.unique = function() {
var res = [];
var json = {};
for (var i = 0; i < this.length; i++) {
if (!json[this[i]]) {
res.push(this[i]);
json[this[i]] = 1;
}
}
return res;
};
function getCookie(cookieName) {
var cookieString = document.cookie;
var cookies = cookieString.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var start = cookie.indexOf(cookieName + '=');
if (start == -1 || start > 1) continue;
start += cookieName.length + 1;
return cookie.substring(start);
}
return null;
}
//写cookies
function addCookie(name, value) {
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie = name + "=" + value + ";expires=" + exp.toGMTString();
}
function delCookie(name) { //为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间
var date = new Date();
date.setTime(date.getTime() - 10000);
document.cookie = name + "=x; expires=" + date.toGMTString();
// document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
new GradeSearch().init();
});