﻿<%
'::::::::::::::::::::::::::::::::::::
'::            ____       __       ::
'::    ___     \   \     / /       ::
'::   |   |     \   \   / /   WW   ::
'::   |   |      \   \ / /    EE   ::
'::   |   |       \  // /     LL   ::
'::   |   | _     / //  \     CC   ::
'::   |   || |   / / \   \    OO   ::
'::   |   || |  / /   \   \   MM   ::
'::   |   || | /_/     \___\  EE   ::
'::   |   || |___                  ::
'::   |___||_____| www.lxasp.com   ::
'::                                ::
'::::::::::::::::::::::::::::::::::::
'cvh = cvasp helper
'精简分页列表类
Class clsPageList

	Public tp
	Public IsLeaveBlank

	Private tPage, tPageNL, tPageCL, tPageHH, tPageTT, tPageHO, tPageTO, tPageHP, tPageTP
	Private m_CurPage, TotalPages, RecordCount, hadOpened


	Private PageNumCount
	Private PageNumCenter
	Private PageHaveRows
	Private PageQStrName
	Private PageRowsQS
	Private ArrayRowNums

	Private Sub Class_Initialize()
		On Error Resume Next
		ArrayRowNums = Array("10", "15", "20", "50", "100", "200", "5")
		PageHaveRows = 5
		IsLeaveBlank = False
		PageNumCount = 5
		PageNumCenter = True
		PageQStrName = "page="
		PageRowsQS = "rows"
		Set tp = New clsTpl
	End Sub
	Private Sub Class_Terminate()
		Set tp = Nothing
	End Sub

'只读属性
	'获取当前页码
	Public Property Get curpage()
		curpage = m_CurPage
	End Property
	'获取页数
	Public Property Get pages()
		pages = TotalPages
	End Property
	'获取记录个数
	Public Property Get count()
		count = RecordCount
	End Property
	'获取是否成功打开模板
	Public Property Get isopen()
		isopen = hadOpened
	End Property

'设置属性
	'设置每页行数的地址栏参数名称-默认:rows
	Public Property Let rname(v)
		PageRowsQS = v
	End Property
	Public Property Get rname()
		rname = PageRowsQS
	End Property
	'设置当前页码的地址栏参数名称-默认:page
	Public Property Let qname(v)
		PageQStrName = v
		If Right(v, 1) <> "=" Then PageQStrName = v & "="
	End Property
	Public Property Get qname()
		qname = PageQStrName
	End Property
	'设置每页有多少行记录-默认:5
	Public Property Let rows(v)
		PageHaveRows = v
	End Property
	Public Property Get rows()
		rows = PageHaveRows
	End Property
	'设置页码列表有多少个数字-默认:5
	Public Property Let nums(v)
		PageNumCount = v
	End Property
	Public Property Get nums()
		nums = PageNumCount
	End Property
	'设置页码列表的当前页数字是否居中-默认:True
	Public Property Let center(v)
		PageNumCenter = v
	End Property
	Public Property Get center()
		center = PageNumCenter
	End Property
	'设置行数下拉列表中的可选的数字
	'默认:Array("10", "15", "20", "50", "100", "200", "5")
	Public Property Let optrows(v)
		If IsArray(v) Then ArrayRowNums = v
	End Property

'主要调用方法
	'首先调用方法:打开模板文件
	Public Function opentpl(fn)
		opentpl = tp.open(fn)
		If Not IsEmpty(opentpl) Then
			hadOpened = getPageTpls()
			If Not hadOpened Then opentpl=empty
		End If
	End Function

	'重要调用方法:获取当前页的记录数组
	'参数列表:
	'rs   --已经打开的rs对象(可用数据库类获得)
	'htp  --用于装载返回的模板内容
	'rc   --记录总数.设置为 empty 则自动获取
	'url  --分页跳转的URL地址不要带page=参数.设置为 empty 则自动获取
	'pgn  --手工指定当前页码.设置为 empty 则自动获取(PS:静态生成时有用!)
	Public Function getrows(rs, htp, rc, url, pgn)
		Dim cc, qrws
		On Error Resume Next
		If rc <= 0 Then cc = rsCountFast(rs) Else cc = rc
		qrws = Trim(Request.QueryString(PageRowsQS))
		If Len(qrws) = 0 Then
			PageHaveRows = 5
		Else
			qrws = tonum(qrws)
			If qrws = 0 Then qrws = 1
			PageHaveRows = qrws
		End If
		htp = GenHtmTurnPage(rs, cc, url, pgn)
		htp = tp7.tidy(htp, "cs")
		getrows = rs.getrows(PageHaveRows)
		If Err Then getrows = Empty
	End Function
	
	'获取模板中解析出来的各个区块的内容
	Private Function getPageTpls()
		Dim t, suc
		Dim n, i, l

		On Error Resume Next

		suc = 0
		t = tp.getblock("pnhead", 0)
		If Len(t) > 0 Then tPageHH = t: suc = suc + 1

		t = tp.getblock("pnhead", 1)
		If Len(t) > 0 Then tPageHO = t: suc = suc + 1

		t = tp.getblock("pnhead", 2)
		If Len(t) > 0 Then tPageHP = t: suc = suc + 1


		t = tp.getblock("pnnums", 0)
		If Len(t) > 0 Then tPageNL = t: suc = suc + 1

		t = tp.getblock("pnnums", 1)
		If Len(t) > 0 Then tPageCL = t: suc = suc + 1


		t = tp.getblock("pnfoot", 0)
		If Len(t) > 0 Then tPageTT = t: suc = suc + 1

		t = tp.getblock("pnfoot", 1)
		If Len(t) > 0 Then tPageTO = t: suc = suc + 1

		t = tp.getblock("pnfoot", 2)
		If Len(t) > 0 Then tPageTP = t: suc = suc + 1

		If Len(tp.getblock("psorows", 0)) > 0 Then suc = suc + 1

		If suc = 9 Then

			n = ArrayRowNums
			l = UBound(n)

			tp.Into "psorows"
			For i = 0 To l
				tp.addnew
				tp("n") = n(i)
				tp.Update
			Next
			tp.break

			getPageTpls = True

		Else
			getPageTpls = False
		End If

	End Function

	'生成翻页的HTML代码!!此类的核心算法!!
	Function GenHtmTurnPage(m_rs, m_rscount, ByVal linkUrl, mypageno)

		Dim PagePrevNN, PagePrevN1, PageNextN1, PageNextNN
		Dim pgNL, toPage

		Dim PageNo

		'Dim TotalPages,IsLeaveBlank,PageNumCount,PageNumCenter

		Dim s1, s2, sp, hf, hf1
		Dim s, lnk

		On Error Resume Next

		TotalPages = CalcTotalPage(m_rscount, PageHaveRows)

		PagePrevNN = 0
		PageNextNN = 0

		RecordCount = m_rscount

		If Len(mypageno) > 0 Then
			PageNo = mypageno 'Request.QueryString(PageQStrName)
		Else
			PageNo = Request.QueryString(Replace(PageQStrName, "=", ""))
		End If

		If PageNo = "" Then PageNo = 1
		If IsNumeric(PageNo) Then PageNo = CLng(PageNo) Else PageNo = 1
		If PageNo > TotalPages Then PageNo = TotalPages
		If PageNo < 1 Then PageNo = 1
		If RecordCount <> 0 Then
			m_rs.AbsolutePosition = (PageNo - 1) * PageHaveRows + 1
		Else
			GenHtmTurnPage = Empty
			Exit Function
		End If

		If Len(linkUrl) > 0 Then
			If InStr(1, linkUrl, "?") Then
				lnk = linkUrl & "&"
			Else
				lnk = linkUrl & "?"
			End If
		Else
			linkUrl = AutoGetPageUrl
			If InStr(1, linkUrl, "?") Then
				lnk = linkUrl & "&"
			Else
				lnk = linkUrl & "?"
			End If
		End If

		sp = (PageNo - (PageNo Mod PageNumCount)) / PageNumCount
		If sp < 1 Then sp = 0

		s1 = sp * PageNumCount + 1
		s2 = sp * PageNumCount + PageNumCount
		If PageNo < s1 Then
			sp = sp - 1
			s1 = sp * PageNumCount + 1
			s2 = sp * PageNumCount + PageNumCount
		End If

		'居中当前页码
		If PageNumCenter <> 0 Then
			If PageNumCount Mod 2 = 0 Then
				If PageNo > CInt(PageNumCount / 2) Then
					s1 = PageNo - CInt(PageNumCount / 2) + 1
					s2 = PageNo + CInt(PageNumCount / 2)
					If s1 < 1 Then s1 = 1: s2 = PageNumCount
				End If
			Else
				hf = ((s2 - s1) / 2) + 1
				If PageNo > hf Then
					s1 = PageNo - (hf - 1)
					s2 = PageNo + (hf - 1)
					If s1 < 1 Then s1 = 1: s2 = PageNumCount
				End If
			End If
		End If

		PagePrevN1 = PageNo - 1
		PageNextN1 = PageNo + 1

		'设置前N页页码
		If PageNo <> 1 And s2 > PageNumCount Then
			PagePrevNN = s1 - 1
		End If
		'输出整排数字页码
		For s = s1 To s2
			If s <= 0 Then Exit For
			If s = PageNo Then
				pgNL = pgNL & tp.tag(tPageCL, "PageCurrNN", s)
			Else
				pgNL = pgNL & tp.tag(tPageNL, "PageNums", s) 'Replace(tPageNL,"{=PageNums}",s)
			End If
			If s = TotalPages Then Exit For
		Next
		'设置后N页页码
		If (PageNo <> TotalPages And s - 1 <> TotalPages) And Not (s <= s2 And s = TotalPages) Then
			PageNextNN = s
		End If

		If IsLeaveBlank Then
			If s <= s2 Then
				For s = s + 1 To s2
					pgNL = pgNL & tp.tag(tPageCL, "PageCurrNN", "&nbsp;&nbsp;")
				Next
			End If
		End If

		toPage = tp.tp(0) 'tPage


		toPage = tp.Repx(toPage, "pnnums", pgNL)

		If PagePrevNN > 0 Then
			toPage = tp.Repx(toPage, "pnhead", tPageHH)
		Else
			If PageNo = 1 Then
				toPage = tp.Repx(toPage, "pnhead", tPageHO)
			Else
				toPage = tp.Repx(toPage, "pnhead", tPageHP)
			End If
		End If

		If PageNextNN > 0 Then
			toPage = tp.Repx(toPage, "pnfoot", tPageTT)
		Else
			If PageNo = TotalPages Then
				toPage = tp.Repx(toPage, "pnfoot", tPageTO)
			Else
				toPage = tp.Repx(toPage, "pnfoot", tPageTP)
			End If
		End If

		toPage = tp.tag(toPage, "PageBaseLink", Server.HtmlEncode(lnk))


		toPage = tp.tag(toPage, "PagePrevNN", PagePrevNN)
		toPage = tp.tag(toPage, "PagePrevN1", PagePrevN1)

		toPage = tp.tag(toPage, "PageNextNN", PageNextNN)
		toPage = tp.tag(toPage, "PageNextN1", PageNextN1)


		toPage = tp.tag(toPage, "PageMaxNN", PageNumCount)

		toPage = tp.tag(toPage, "RecordCount", RecordCount)

		toPage = tp.tag(toPage, "PageTotalNN", TotalPages)

		toPage = tp.tag(toPage, "PageCurrNN", PageNo)

		toPage = tp.tag(toPage, "PageHaveRows", PageHaveRows)

		toPage = tp.tag(toPage, "PageQS", PageQStrName)
		toPage = tp.tag(toPage, "PageRowsQS", PageRowsQS)


		m_CurPage = PageNo

		GenHtmTurnPage = toPage

		If Err Then GenHtmTurnPage = Empty
	End Function
	
	'计算获得总页数
	Function CalcTotalPage(m_rscount, m_PerItems)
		On Error Resume Next
		CalcTotalPage = Abs(Int(-Abs(m_rscount / m_PerItems)))
		If Err Then CalcTotalPage = 1
	End Function

	'快速获取记录个数
	Function rsCountFast(rs)
		On Error Resume Next
		Dim MAXINT: MAXINT = &H7FFFFFFF
	'   rsCountFast = 0
	'   rs.MoveLast
	'   rsCountFast = rs.AbsolutePosition
	'   If rsCountFast > 0 Then Exit Function
		rs.AbsolutePosition = MAXINT
		rs.MovePrevious
		rsCountFast = rs.AbsolutePosition
		If rsCountFast < 0 Then rsCountFast = 0
		If Err Then rsCountFast = 0
	End Function

	'从地址栏URL参数中去掉“page=”参数，并保留其他参数
	Function AutoGetPageUrl()

		Dim p1, p2, p3, qs, pn, qs1, qk, sfn
		Dim LenQStr ',PageQStrName

		'PageQStrName="page="

		sfn = Request.ServerVariables("SCRIPT_NAME")
		qs = Request.ServerVariables("QUERY_STRING")

		If Right(qs, 1) = "&" Then qs = Left(qs, Len(qs) - 1)

		LenQStr = Len(PageQStrName) - 1

		qk = 0
		p3 = 0
		p1 = InStr(1, qs, "&" & PageQStrName, 1)
		If p1 > 0 Then
			p3 = 2 + LenQStr
			p2 = InStr(p1 + p3, qs, "&")
			If p2 > 0 Then
				qk = 1 '在中间
				pn = Mid(qs, p1 + p3, p2 - (p1 + p3))
			Else
				qk = 2 '在最后
				pn = Mid(qs, p1 + p3)
			End If
		Else
			p3 = 0
			p1 = InStr(1, qs, "" & PageQStrName, 1)
			If p1 > 0 Then
				p3 = 1 + LenQStr
				p2 = InStr(p1 + p3, qs, "&")
				If p2 > 0 Then
					qk = 3 '在最前
					pn = Mid(qs, p1 + p3, p2 - (p1 + p3))
				Else
					qk = 4 '仅此一个
					pn = Mid(qs, p1 + p3)
				End If
			End If
		End If
		Select Case qk
		Case 1, 2
			qs1 = Replace(qs, "&" & PageQStrName & Trim(pn), "", 1, -1, 1)
		Case 3
			qs1 = Replace(qs, "" & PageQStrName & Trim(pn) & "&", "", 1, -1, 1)
		Case 4
			qs1 = ""
		Case Else
			qs1 = qs
		End Select

		If Len(qs1) > 0 Then
			AutoGetPageUrl = sfn & "?" & qs1
		Else
			AutoGetPageUrl = sfn
		End If

	End Function

End Class
%>