首页 > 代码库 > 观看历史
观看历史
/*
history.js
观看历史
by sunhw 2014-11-19 重写
*/
;(function(){
function History(option){
this.option = T.object.extend({
behistory : ‘‘,
nohistory : ‘‘,
hiscontent : ‘‘,
histitle : ‘‘,
hismore : ‘‘
}, option || {});
this.target = (F.client.isClient()) ? ‘‘ : ‘target="_blank"‘;
this.init();
}
//清除观看历史
History.prototype.clear = function(){
var me = this;
var clearHistory = function(){
T.dom.remove(me.beHistory);
if( me.noHistory && T.dom.hasClass(me.noHistory, ‘hidden‘) ){
T.dom.removeClass(me.noHistory, ‘hidden‘);
}
};
if( F.user.userid ){
var url = F.config.api + ‘/ajax/playstatus/removeall/‘;
F.get(url, function(json){
if( !json || json.status != 200 ){
return;
}
clearHistory();
});
}else{
try{
T.swf.getMovie(‘localStorage‘).remove(‘history_watch‘);
}catch(err){
console.log(err);
}
clearHistory();
}
};
//筛选处理
History.prototype.filtrate = function(o){
var me = this;
var obj = o || {};
var hislist = T.q(‘res-list‘, me.beHistory);
T.each(hislist, function(item){
if( obj.actType == ‘filter‘ ){
if( T.dom.getAttr(item, ‘data-type‘) != ‘long‘ ){
T.hide(item);
}
}else{
T.show(item);
}
});
};
//对点击筛选按钮以及清空、取消按钮事件绑定
History.prototype.bind = function(){
var me = this;
T.on(T.get(me.option.histitle), ‘click‘, function(e){
e = T.event.get(e);
var target = e.target,
id = T.dom.getAttr(target, ‘id‘);
switch(id){
case ‘deleteBtn‘:
T.dom.removeClass(T.get(‘deleInfo‘), ‘hidden‘);
break;
case ‘selectBtn‘:
if(!T.dom.hasClass(target, ‘select-current‘)){
T.dom.addClass(target, ‘select-current‘);
me.filtrate({‘actType‘ : ‘filter‘});
}else{
T.dom.removeClass(target, ‘select-current‘);
me.filtrate({‘actType‘ : ‘recover‘});
}
break;
case ‘confirmBtn‘:
me.clear();
break;
case ‘cancelBtn‘:
T.dom.addClass(target.parentNode, ‘hidden‘);
break;
default :
break;
}
});
};
//登录用户观看历史删除每条历史或者全部清除以及hover处理
History.prototype.historyHander = function(){
var me = this;
var hisWrap = T.q(‘res-wrap‘, me.beHistory),
hislist = T.q(‘res-list‘, me.beHistory);
if( hisWrap && hisWrap.length ){
T.dom.addClass(hisWrap[0], ‘first-wrap‘);
T.each(hisWrap, function(item){
T.on(item, ‘click‘, function(e){
e = T.event.get(e);
var target = e.target;
if( T.domn.hasClass(target, ‘colse‘) ){
var vid = T.dom.getAttr(target.parentNode.parentNode, ‘data-videoid‘),
url = F.config.api + ‘/ajax/playstatus/remove/galleryid/‘ + vid + ‘/‘;
F.get(url, function(json){
if( !json || json.status != 200 ){
return;
}
//单个删除观看历史数据
T.dom.remove(target.parentNode.parentNode);
});
// 计算本列表数目
if( !T.q(‘res-list‘, item).length ){
T.dom.remove(item);
}
// 计算全部
if( !T.q(‘res-list‘, me.beHistory) ){
me.clear();
}
}
});
});
}
if( hislist && hislist.length ){
T.each(hislist, function(item){
T.on(item, ‘mouseover‘, function(){
T.dom.addClass(item, ‘res-list-hover‘);
});
T.on(item, ‘mouseout‘, function(){
T.dom.removeClass(item, ‘res-list-hover‘);
});
});
}
me.bind();
};
//处理_mids COOKIE
History.prototype.disposeCookie = function(obj){
var me = this;
me.__mids.unshift(String(obj));
};
//设置_mids COOKIE
History.prototype.setCookie = function(){
var me = this;
me.__mids = T.array.unique(me.__mids);
while(me.__mids.length > 50){
me.__mids.pop();
}
F.cookie.set(‘_mids‘, me.__mids.join(‘,‘));
};
//未登录用户操作
History.prototype.localStorage = function(){
var me = this;
var html = ‘‘,
data = http://www.mamicode.com/[],
host = "http://" + location.host + "/";
var get = function(){
var temp = T.swf.getMovie(‘localStorage‘).getHistory();
if( T.isArray(temp) && temp.length ){
data = http://www.mamicode.com/temp;
T.each(data, function(item){
var vtp = ( T.array.indexOf([‘movie‘, ‘tv‘, ‘cartoon‘, ‘variety‘], item[2]) >= 0 ? ‘long‘ : item[2]);
var isFinish = ( item[5] == 0 ) ? ‘观看‘ : ‘重播‘;
html += ‘<a class="res-list cookie-list" data-type="‘ + vtp + ‘" href="http://www.mamicode.com/‘+host+‘‘ + item[4] + ‘" ‘ + me.target + ‘><b class="tit">‘ + item[3] + ‘</b><b class="seebtn">‘+isFinish+‘</b></a>‘;
me.disposeCookie(item[1]);
});
T.dom.insertHTML(me.hisContent, ‘beforeEnd‘, html );
if( me.noHistory ){
T.dom.addClass(me.noHistory, ‘hidden‘);
}
T.dom.removeClass(me.beHistory, ‘hidden‘);
T.dom.addClass(me.beHistory, ‘hisCookie‘);
T.on(T.get(me.option.hismore), ‘click‘, function(e){
T.event.stop(T.event.get(e));
F.tool.ajaxLogin.panel();
});
me.bind();
me.setCookie();
}else{
return;
}
};
if( window.localStorageReady == true ){
get();
}else{
T.observer.add(‘localStorageReady‘, function(){
get();
});
}
};
//登录用户操作
History.prototype.asyncStorage = function(){
var me = this;
var url = F.config.api + ‘/ajax/playstatus/getlist/7/‘;
F.get(url, function(json){
json = json ||{};
var data = http://www.mamicode.com/json.data;
if( json.status == 200 ){
if( !data || (!data.today.length && !data.thisweek.length && !data.earlier.length) ){
return;
}
var historyData = http://www.mamicode.com/{data : {}};
var traverse = function(datum){
T.each(datum, function(item){
me.disposeCookie(item.videoid);
});
};
if( data.today && data.today.length ){
historyData.data.today = {
title : ‘今天‘,
list : data.today,
target : me.target
};
traverse(data.today);
}
if( data.thisweek && data.thisweek.length ){
historyData.data.thisweek = {
title : ‘本周‘,
list : data.thisweek,
target : me.target
};
traverse(data.thisweek);
}
if( data.earlier && data.earlier.length ){
historyData.data.earlier = {
title : ‘更早‘,
list : data.earlier,
target : me.target
};
traverse(data.earlier);
}
var html = F.tpl.renderFile(‘tplHistory‘, historyData);
T.dom.insertHTML(me.hisContent, ‘beforeEnd‘, html);
if( me.noHistory ){
T.dom.addClass(me.noHistory, ‘hidden‘);
}
if( me.beHistory ){
T.dom.removeClass(me.beHistory, ‘hidden‘);
}
me.setCookie();
me.historyHander();
}else{
return;
}
});
};
History.prototype.init = function(){
var me = this;
me.__cookie = String(F.cookie.get(‘_mids‘));
me.__mids = me.__cookie ? me.__cookie.split(‘,‘) : [];
me.beHistory = T.get(me.option.behistory);
me.noHistory = T.get(me.option.nohistory);
me.hisContent = T.get(me.option.hiscontent);
if( F.user.userid ){
me.asyncStorage();
}else{
me.localStorage();
}
};
T.dom.ready(function(){
var history = new History({
behistory : ‘beHistory‘,//有观看历史的容器ID
nohistory : ‘noHistory‘,//无观看历史的容器ID
hiscontent : ‘historyContent‘,//存放观看历史的容器ID
histitle : ‘resultTit‘,//观看历史TITLE ID
hismore : ‘historyMore‘//查看更多ID
});
});
})();
观看历史