pagination.js 8.52 KB
/**
 * @fileOverview 分页组件
 * @author:xiaoma
 * @date:2012-08-12
 */
define('plugins/pagination', function(require, exports)
{
	var $ = require("jquery");
	var common = require("common");
	var default_loadPageCount = 0 ;
	var loadPageCount = 1 ; //自动加载请求次数
	var autoload_list = [] ;//自动加载初始化列表
	var ajax_list = [] ; //AJAX初始化列表
	/**
	 * 显示底部加载样式
	 */
	function openLoading()
	{
		if($('#infscr-loading').length > 0)
		{
			$('#infscr-loading').show() ;
		}else
		{
		    var string  = '<div id="infscr-loading" style="margin: 0px auto; position: absolute;top:-50px;left:0;width: 100%;"><div class="loadding shoot_load" style="margin: 0 auto;"><div class="icon_loadding" style="margin: 0 auto;width: 270px;">我在努力加载中...</div></div></div>';
		    //'<div class="loadding shoot_load" id="infscr-loading" style="margin: 0 auto;"><div class="icon_loadding">正在努力的加载中...</div></div>';
			$('.footer').css({"position":"relative"}).append(string);
		}
	}
	/**
	 * 关闭底部加载
	 */
	function closeLoading()
	{
		$('#infscr-loading').hide() ;
	}
	//AJAX翻页
	exports.initAjax = function(pageId,appendTarget,beforeAppend,afterAppend)
	{
		if(ajax_list[pageId])
		{
			return false ;
		}else
		{
			ajax_list[pageId] = true ;
		}
		$('#'+pageId).find('a').live('click',function(){
			var ajaxUrl = $(this).attr('href');
			if(!ajaxUrl || ajaxUrl == undefined || ajaxUrl.substr( 0, 10) == 'javascript')
			{
				return false ;
			}
			if(beforeAppend)
			{
				beforeAppend() ;
			}
			$(appendTarget).html('<div class="tips_loading_big" >&nbsp;</div>');
			//页面定位至加载容器顶部
			var a = $(appendTarget).offset().top -50;
			$("html,body").scrollTop(a); 
			$.get(ajaxUrl,function (data)
			{
				$(appendTarget).html(data) ;
				if(afterAppend)
				{
					afterAppend() ;
				}
			});
			return false ;
		});
	};
	//自动加载,N页后出现页码
	exports.initAutoLoad = function(pageId,appendTarget,maxLoadPage,pageTarget,beforeAppend,afterAppend)
	{
		if(autoload_list[pageId])
		{
			return false ;
		}else
		{
			autoload_list[pageId] = true ;
		}
		var page_id = '#'+pageId;
		//var pageObject = $(page_id);
		//初始化事件 
		$(window).unbind("scroll.pagination");
		var isReturn = true; //标志AJAX请求是否返回
		var enabledScroll = true ; //是否启用scroll事件
		var getData = function (t,ajaxUrl)
		{
			isReturn = false ;//将请求设置为尚未返回
			if(!ajaxUrl)
			{
				var ajaxUrl = $('#'+pageId).find('a:last').attr('href');
			}
			if(!ajaxUrl || ajaxUrl.substr( 0, 4) != 'http')
			{
				return false ;
			}
			if(t == 'reload')
			{
				$(page_id).html('');
				$(appendTarget).html('<div class="tips_loading_big" id="pagination_loading">&nbsp;</div>');
				var a = $(appendTarget).offset().top -50;
				$("html,body").scrollTop(a); 
			}else{
				$(appendTarget).append('<div class="tips_loading_big" id="pagination_loading">&nbsp;</div>');
			}
			//开启加载样式
			openLoading();
			$.get(ajaxUrl,function (data)
			{
				isReturn = true ;//将请求设置为已返回
				loadPageCount++;
				if(beforeAppend)
				{
					beforeAppend() ;
				}
				//添加数据
				if(pageTarget)
				{
					var data = $("<div>"+data+"</div>");
					var pageContent = data.find(page_id).html() ;
					var content = data.find(appendTarget).html() ;
					$(page_id).html(pageContent); //切换分页
					$(appendTarget).append(content);//增加内容
				}else
				{
					$(page_id).remove();
					$(appendTarget).append(data) ;
					$(page_id).hide();
				}
				$('#pagination_loading').remove();
				if(afterAppend)
				{
					afterAppend(data);
				}
				var ajaxUrl = $('#'+pageId).find('a:last').attr('href');
				//验证翻页次数
				if(loadPageCount >= maxLoadPage || !ajaxUrl || ajaxUrl.substr( 0, 4) != 'http')
				{
					$(window).unbind("scroll.pagination");
					loadPageCount = default_loadPageCount ;//清除加载记录
					$(page_id).show();	//显示页码
				}else
				{
					$(page_id).hide();	//隐藏页码
				}
				//关闭加载样式
				closeLoading() ;
			});
		};
		if(loadPageCount < maxLoadPage){
			$(page_id).hide() ;
		}
		var bind = function ()
		{
			$(window).bind('scroll.pagination',function()
			{
				if(loadPageCount >= maxLoadPage)
				{
					return false;
				}
			    if( isReturn  && ($("body").height() - ($(window).height() + $(window).scrollTop()) < 250))
			    {
			    	getData();
			    }
			});
		}
		bind();
		$(page_id).find('a').live('click',function()
		{
			loadPageCount = default_loadPageCount ;//清除加载记录
			//绑定事件
			bind();
			ajaxUrl = $(this).attr('href');
			getData('reload',ajaxUrl);
			return false;
		});
	};
	
	//自动加载,N页后出现页码
	exports.initAutoLoad2 = function(pageId,appendTarget,maxLoadPage,pageTarget,beforeAppend,afterAppend)
	{
		if(autoload_list[pageId])
		{
			return false ;
		}else
		{
			autoload_list[pageId] = true ;
		}
		$(appendTarget).css({"overflow":"hidden"});
		var page_id = '#'+pageId;
		//var pageObject = $(page_id);
		//初始化事件 
		$(window).unbind("scroll.pagination");
		var isReturn = true; //标志AJAX请求是否返回
		var enabledScroll = true ; //是否启用scroll事件
		var getData = function (t,ajaxUrl)
		{
			isReturn = false ;//将请求设置为尚未返回
			if(!ajaxUrl)
			{
				var ajaxUrl = $('#'+pageId).find('span[rel="nextpage"]').html();
			}
			if(!ajaxUrl)
			{
				return false ;
			}
			if(t == 'reload')
			{
				$(page_id).html('');
				$(appendTarget).html('<div class="tips_loading_big" id="pagination_loading">&nbsp;</div>');
				var a = $(appendTarget).offset().top -50;
				$("html,body").scrollTop(a); 
			}else{
				$(appendTarget).append('<div class="tips_loading_big" id="pagination_loading">&nbsp;</div>');
			}
			openLoading();
			window.setTimeout(function()
			{
		         $.get(ajaxUrl,function (data)
                 {
                     isReturn = true ;//将请求设置为已返回
                     loadPageCount++;
                     if(beforeAppend)
                     {
                         beforeAppend() ;
                     }
                     //添加数据 
                     if(pageTarget)
                     {
                         var data = $("<div>"+data+"</div>");
                         var pageContent = data.find(page_id).html() ;
                         var content = data.find(appendTarget).html() ;
                         $(page_id).html(pageContent); //切换分页
                         $(appendTarget).append(content);//增加内容
                     }else
                     {
                         $(page_id).remove();
                         $(appendTarget).append(data) ;
                         $(page_id).hide();
                     }
                     $('#pagination_loading').remove();
                     if(afterAppend)
                     {
                         afterAppend(data);
                     }
                     var ajaxUrl = $('#'+pageId).find('span[rel="nextpage"]').html();
                     //验证翻页次数
                     if(loadPageCount >= maxLoadPage || !ajaxUrl || ajaxUrl.substr( 0, 4) != 'http')
                     {
                         $(window).unbind("scroll.pagination");
                         loadPageCount = default_loadPageCount ;//清除加载记录
                         $(page_id).show();  //显示页码
                     }else
                     {
                         $(page_id).hide();  //隐藏页码
                     }
                     //关闭加载样式
                     closeLoading() ;
                     common.errorProc();
                 });
			}, 0);
		};
		if(loadPageCount < maxLoadPage)
		{
			$(page_id).hide() ;
		}
		var bind = function ()
		{
			$(window).bind('scroll.pagination',function()
			{
				if(loadPageCount >= maxLoadPage)
				{
					return false;
				}
			    if( isReturn  && ($("body").height() - ($(window).height() + $(window).scrollTop()) < 250))
			    {
			    	getData();
			    }
			});
		}
		bind();
		$(page_id).find('a').live('click',function()
		{
			loadPageCount = default_loadPageCount ;//清除加载记录
			//绑定事件
			bind();
			ajaxUrl = $(this).attr('href');
			if(!ajaxUrl || ajaxUrl.substr( 0, 4) != 'http')
			{
				return false ;
			}
			//组装页码
			var p = $(this).attr('p');
			if(isNaN(p))
			{
				return false ;
			}
			if(ajaxUrl.indexOf('?') != -1)
			{
				ajaxUrl += '&page='+((p-1)*maxLoadPage+1);
			}else
			{
				ajaxUrl += '?page='+((p-1)*maxLoadPage+1);
			}
			getData('reload',ajaxUrl);
			return false;
		});
	};
});