﻿// JScript 文件

function PagerControl(pageSize,step){
    this.PageSize = pageSize;
    this.Step = step;
    this.PageCount = 0;
    this.CurrentPage = 1;
    //this.HeaderTemplate = "[ ";
    this.HeaderTemplate = "";
    this.ItemTemplate = "<a href=\"javascript:{#reloadfunction}({#id},{#page})\" >{#title}</a>&nbsp;";
    this.SelectedItemTemplate = "{#title} ";    
    //this.FooterTemplate = " ]";
    this.FooterTemplate = "";
    this.HostControlID = null;
}

PagerControl.prototype.RenderPager = function(reloadFunction,id){
    var result = "";
    
    if(this.PageCount > 1){
        result += this.HeaderTemplate;
        var startPoint = Math.floor((this.CurrentPage/this.Step)) * this.Step;
        if((this.CurrentPage % this.Step) == 0) {
            startPoint -= this.Step;
        }
        
        if(startPoint >= this.Step){
            result += this.ItemTemplate.replace("{#page}",startPoint).replace("{#title}","...").replace("{#reloadfunction}",reloadFunction).replace("{#id}",id);
            //result += this.ItemTemplate.replace("{#page}",startPoint).replace("{#title}","上一页").replace("{#reloadfunction}",reloadFunction).replace("{#id}",id);
        }
        
        for(var i=startPoint +1;i<=this.PageCount && i<=(startPoint + this.Step);i++){
            if(i != this.CurrentPage){
                result += this.ItemTemplate.replace("{#page}",i).replace("{#title}",i).replace("{#reloadfunction}",reloadFunction).replace("{#id}",id);
            }else{
                result += this.SelectedItemTemplate.replace("{#title}",i);
            }
        }
        
        if(startPoint < (this.PageCount - this.Step)){
            result += this.ItemTemplate.replace("{#page}",(startPoint + this.Step +1)).replace("{#title}","...").replace("{#reloadfunction}",reloadFunction).replace("{#id}",id);
            //result += this.ItemTemplate.replace("{#page}",(startPoint + this.Step +1)).replace("{#title}","下一页").replace("{#reloadfunction}",reloadFunction).replace("{#id}",id);
        }
        result += this.FooterTemplate;
    }
    
    $(this.HostControlID).innerHTML = "<span style=\"padding:10px;\"><span id=\"ListPager1\">"+result+"</span></span>";
    
    //alert($(this.HostControlID).innerHTML);
    

}

function RepeaterControl(){
    
    this.HeaderTemplate = "";
    this.ItemTemplate = "";
    this.SelectedItemTemplate = "";
    this.FooterTemplate = "";
    this.AlternatingItemTemplate = "";
    this.SeparatorTemplate = "";
    this.HostControlID = null;
    
    this.DataSource = null;
}

RepeaterControl.prototype.DataBind = function(){
    var htmlString = "";
    htmlString += this.HeaderTemplate;
    
    
    for(var count=0;count<this.DataSource.length;count++){
        var node = this.DataSource[count];
        var row = this.ItemTemplate
        
        if(this.AlternatingItemTemplate != "" && (count+1)% 2 == 0 ){
            row = this.AlternatingItemTemplate;
        }
       
        for (var i = 0; i < node.attributes.length; i++){
			var re = new RegExp("{@" + node.attributes[i].nodeName + "}","g");
			row = row.replace(re,node.attributes[i].nodeValue);
		}
		
		for (var i = 0; i < node.childNodes.length; i++){
		    if(node.nodeType != 3 && node.childNodes[i].firstChild != null){
			    var re = new RegExp("{\\$" + node.childNodes[i].nodeName + "}","g");
			    row = row.replace(re,node.childNodes[i].firstChild.nodeValue);
			}
		}
		
		// eval javascript
		evalRe = new RegExp("{Eval(.*?)}","g");
		
		myArray = row.match(evalRe);
		
		if(myArray != null){
		    for(var i=0;i<myArray.length;i++){
		        var result = eval(myArray[i].replace(evalRe,"$1"));
		        row = row.replace(myArray[i],result);
		    }
		}
        
        htmlString += row;
        
        if(this.SeparatorTemplate != "" && count + 1 != this.DataSource.length){
            htmlString += this.SeparatorTemplate;
        }
        
        
    }
    
    htmlString += this.FooterTemplate;
    
    $(this.HostControlID).innerHTML = htmlString;
    
    //alert($(this.HostControlID).innerHTML);
}
