﻿/**
* 说明：javascript 分页代码
* 编写：朱亚军
* 日期:2009.12.28
**/
/***
jQuery.extend
调用示例
$(function(){
    $.url = '/AjaxData/TopCommentsListHandler.ashx?page={0}&tmp='+Math.random();//ajax请求url，必须返回json类型
    $.isShowSummary = true;//是否显示概述信息
    $.listTemplate = "<li>序号：$$RowNumber$$ 姓名：$$USERNAME$$ 内容：$$COMMENT$$</li>";//模板，必须以“$$”开始和结束

    $('#page').GetPage(1);
    
});
***/
/**
* 说明：javascript 分页代码
* 编写：朱亚军
* 日期:2009.12.28
**/
/***
jQuery.extend
调用示例
$(function(){
    $.url = '/AjaxData/TopCommentsListHandler.ashx?page={0}&tmp='+Math.random();//ajax请求url，必须返回json类型
    $.isShowSummary = true;//是否显示概述信息
    $.listTemplate = "<li>序号：$$RowNumber$$ 姓名：$$USERNAME$$ 内容：$$COMMENT$$</li>";//模板，必须以“$$”开始和结束

    $('#page').GetPage(1);
    
});
***/
jQuery.extend({
    // 获取json里的值
    GetValueFromJson:function(key,data){
        var o;
        key = key.toLowerCase();

        if(typeof(data) == 'object' || typeof(data) == 'array'){
            for(var k in data){
                var _key = k.toLowerCase();
                
                if(_key != key){
                    switch(typeof(data[k])){
                        case "array":
                        case "object":
                            return this.GetValueFromJson(key,data[k]);   
                        case "string":
                        case "number":
                        case "boolean":
                        default:
                            break;    
                    }
                }
                else
                {
                    o = data[k];
                    return o;
                }
            }
        }
        
        return o;
    },
    
    //获取页码
    GetPages:function(_pageid, curPage, _pageSize, _pageCount ,_pageButtonCount,_isShowSummary){
            var strPage = "";        
            // 核心分页页码
            var _startno = parseInt(Math.ceil((curPage)/_pageButtonCount));
            var hasPrePages = (_startno > 1);
            var hasNextPages = (_startno * _pageButtonCount ) < _pageCount;
            startPageNo=(_startno - 1 ) * _pageButtonCount + 1;
            //首页
            strPage += "<a href='javascript:$(\"#"+_pageid+"\").GetPage(1);'>首页</a>";
            
            //上一页
            if(curPage>1)
                    strPage += "<a href='javascript:$(\"#"+_pageid+"\").GetPage("+(curPage-1)+");'>上一页</a>";
            else
                    strPage += "<span class=\"disabled\">上一页</span>";
            //上{0}页
            if(hasPrePages)
                    strPage += "<a href='javascript:$(\"#"+_pageid+"\").GetPage("+(startPageNo-1)+");' title='上"+_pageButtonCount+"页'>...</a>";
            else
                    strPage += "<span class=\"disabled\">...</span>";       
            
            for(var j = startPageNo ;j < startPageNo + _pageButtonCount ;j++) {
                if(j>_pageCount)break;
                if(j==curPage)
                    strPage += "<span class=\"current\">" + j + "</span>";
                else
                    strPage += "<a href='javascript:$(\"#"+_pageid+"\").GetPage(" + j + ");'>" + j + "</a>";
            }
            
            //下{0}页
            if(hasNextPages)
                    strPage += "<a href='javascript:$(\"#"+_pageid+"\").GetPage(" + (startPageNo + _pageButtonCount) + ");' title='下" + _pageButtonCount + "页'>...</a>";
            else
                    strPage += "<span class=\"disabled\">...</span>";
                    
            //下一页
            if(curPage<_pageCount)
                    strPage += "<a href='javascript:$(\"#"+_pageid+"\").GetPage("+(curPage+1)+");'>下一页</a>";
            else
                    strPage += "<span class=\"disabled\">下一页</span>";
                    
            //末页
            strPage += "<a href='javascript:$(\"#"+_pageid+"\").GetPage(" + _pageCount + ");'>末页</a>";
            
            if(_isShowSummary)
                 strPage += "<span>"+_pageSize+"/"+_pageCount+"</span>";
            
            return strPage;
    },
    
    //显示列表内容
    ShowList:function(objid,pageIndex,data,template){
            var strList="";
            var obj;
            
            if(!jQuery.isArray(data)) return;
            
            for(var j = 0;j < data.length;j++)
            {
                obj = data[j];
                strList += this.ParseJsonToHtml(obj,template);   
            }
            
            $("#"+objid).html(strList);
    },
    
    //模板转换
    ParseJsonToHtml:function(jsonData,template,index) {
        var reg = /\$\$(\w+)\$\$/ig;
        var retval = "";
        var arr;
        var regIndex = /\$\$index\$\$/ig;

        // 运算模板表达式
        if(typeof(template) == "function")
        {
            retval = template(jsonData);
        }
        else
        {
            retval = template;
        }      

        while ((arr = reg.exec(retval)) != null){   
             retval = retval.replace(arr[0],jsonData[arr[1]]); 
        } 
        
        return retval;
    },
    
    isUndefined:function(o){return typeof(o)=='undefined';},
    cachePageId:[],
    cacheObject:[]
 });
jQuery.fn.extend({
    GetPage:function(pageIndex){
            var _pageCount = jQuery.isUndefined(jQuery.pageCount) ? 10 : jQuery.pageCount;
            var _pageButtonCount = jQuery.isUndefined($.pageButtonCount) ? 10 : jQuery.pageButtonCount;
            var _pageSize = jQuery.isUndefined(jQuery.pageSize) ? 10 : jQuery.pageSize;
            //var _data = jQuery.isUndefined(jQuery.jsonData) ? [] : $.jsonData;
            var _isShowSummary = jQuery.isUndefined(jQuery.isShowSummary) ? false : jQuery.isShowSummary;
            var _listid = jQuery.isUndefined(jQuery.listId) ? "list" : jQuery.listId;
            var _listtemplate = jQuery.isUndefined(jQuery.listTemplate) ? "" : jQuery.listTemplate;
            var _loadingid = jQuery.isUndefined(jQuery.loadingId) ? "" : jQuery.loadingId;
            var _isOnlyShowList = jQuery.isUndefined(jQuery.isOnlyShowList) ? false : jQuery.isOnlyShowList;
            var _emptyDataTip = jQuery.isUndefined(jQuery.emptyDataTip) ? "暂无数据！！！" : jQuery.emptyDataTip;
            var _pageid = "page";
            var _url = jQuery.isUndefined(jQuery.url) ? "" : jQuery.url;
            var startPageNo=1;
            var curPage = 1;
            var cachePageId = jQuery.cachePageId;
            var cacheObject = jQuery.cacheObject;
            
            try{
                _pageid=this[0].id;
            }
            catch(e)
            {
                alert(e.name)
            }
            //
            if(_listtemplate == "")
            {
                return;
            }

            if(isNaN(pageIndex))
                    curPage = 1;
            curPage = pageIndex;
            if(curPage < 0)
                    curPage = 1;
            if(curPage > _pageCount)
                    curPage = _pageCount;
           
            if($.trim(_url) == "") return;
            
            if(_loadingid != "")
            {
                $('#'+_loadingid).show();
            }
            
            //防止id冲突，引起翻页效果失效
            if(jQuery.inArray(_pageid, cachePageId) == -1)
            {
                var props = {url:_url,listid:_listid,listtemplate:_listtemplate,
                            isshowsummary:_isShowSummary,pagebuttoncount:_pageButtonCount,
                            loadingid:_loadingid,isonlyshowlist:_isOnlyShowList,
                            emptydatatip:_emptyDataTip
                            };
                
                cachePageId.push(_pageid);
                cacheObject.push(props);
            }
            else
            {
                var i = jQuery.inArray(_pageid, cachePageId);
                var obj = cacheObject[i];
                
                _pageButtonCount = obj["pagebuttoncount"]
                _url = obj["url"];
                _listid = obj["listid"];
                _listtemplate = obj["listtemplate"];
                _loadingid = obj["loadingid"];
                _isOnlyShowList = obj["isonlyshowlist"];
                _isShowSummary = obj["isshowsummary"];
                _emptyDataTip = obj["emptydatatip"];
            }
            
            _url = _url.replace("{0}",curPage);
            
            //获取数据
            $.get(_url,
                function(result){
                    
                    var _jsonData;
                    
                    try
                    {
                        _jsonData = eval(result);
                    }
                    catch(e)
                    {
                        _jsonData = {};
                    }
                    
                    var _pageCount = jQuery.GetValueFromJson("pagecount",_jsonData);
                    var recordCount = jQuery.GetValueFromJson("recordCount",_jsonData);
                    var _data = jQuery.GetValueFromJson("records",_jsonData);
                    var _strTemplate = "";

                    if(_loadingid != "")
                    {
                        $('#'+_loadingid).hide();
                    }
                    
                    //设置分页内容
                    jQuery.ShowList(_listid,curPage,_data, _listtemplate);
                
                    if(!_isOnlyShowList){
                        if(jQuery.isArray(_data) && _data.length > 0)
                        {
                            var pagenos = jQuery.GetPages(_pageid, curPage, _pageSize, _pageCount ,_pageButtonCount,_isShowSummary);
 
                            $('#'+_pageid).html(pagenos);
                        }
                    }
                   if(!jQuery.isArray(_data) || _data.length == 0){
                        $('#'+_listid).html(_emptyDataTip);
                   }
                    
            },'json');        
    }
    
});
