fix #150
fix #163 增加更加多样的分页输出样式选项,包括 wrapTag 外层包裹标签名,默认ol wrapClass 外层包裹类名 itemTag 内层标签名, 默认li textTag 直接输出文字的标签名 currentClass 当前聚焦类名 prevClass 上一页类名 nextClass 下一页类名
This commit is contained in:
@@ -32,52 +32,91 @@ class Typecho_Widget_Helper_PageNavigator_Box extends Typecho_Widget_Helper_Page
|
||||
* @param string $currentClass 当前激活元素class
|
||||
* @return void
|
||||
*/
|
||||
public function render($prevWord = 'PREV', $nextWord = 'NEXT', $splitPage = 3, $splitWord = '...', $currentClass = 'current')
|
||||
{
|
||||
public function render($prevWord = 'PREV', $nextWord = 'NEXT', $splitPage = 3, $splitWord = '...', array $template = array())
|
||||
{
|
||||
if ($this->_total < 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
$default = array(
|
||||
'itemTag' => 'li',
|
||||
'textTag' => 'span',
|
||||
'currentClass' => 'current',
|
||||
'prevClass' => 'prev',
|
||||
'nextClass' => 'next'
|
||||
);
|
||||
|
||||
$template = array_merge($default, $template);
|
||||
extract($default);
|
||||
|
||||
// 定义item
|
||||
$itemBegin = empty($itemTag) ? '' : ('<' . $itemTag . '>');
|
||||
$itemCurrentBegin = empty($itemTag) ? '' : ('<' . $itemTag
|
||||
. (empty($currentClass) ? '' : ' class="' . $currentClass . '"') . '>');
|
||||
$itemPrevBegin = empty($itemTag) ? '' : ('<' . $itemTag
|
||||
. (empty($prevClass) ? '' : ' class="' . $prevClass . '"') . '>');
|
||||
$itemNextBegin = empty($itemTag) ? '' : ('<' . $itemTag
|
||||
. (empty($nextClass) ? '' : ' class="' . $nextClass . '"') . '>');
|
||||
$itemEnd = empty($itemTag) ? '' : ('</' . $itemTag . '>');
|
||||
$textBegin = empty($textTag) ? '' : ('<' . $textTag . '>');
|
||||
$textEnd = empty($textTag) ? '' : ('</' . $textTag . '>');
|
||||
$linkBegin = '<a href="%s">';
|
||||
$linkCurrentBegin = empty($itemTag) ? ('<a href="%s"'
|
||||
. (empty($currentClass) ? '' : ' class="' . $currentClass . '"') . '>')
|
||||
: $linkBegin;
|
||||
$linkPrevBegin = empty($itemTag) ? ('<a href="%s"'
|
||||
. (empty($prevClass) ? '' : ' class="' . $prevClass . '"') . '>')
|
||||
: $linkBegin;
|
||||
$linkNextBegin = empty($itemTag) ? ('<a href="%s"'
|
||||
. (empty($nextClass) ? '' : ' class="' . $nextClass . '"') . '>')
|
||||
: $linkBegin;
|
||||
$linkEnd = '</a>';
|
||||
|
||||
$from = max(1, $this->_currentPage - $splitPage);
|
||||
$to = min($this->_totalPage, $this->_currentPage + $splitPage);
|
||||
|
||||
//输出上一页
|
||||
if ($this->_currentPage > 1) {
|
||||
echo '<li><a class="prev" href="' . str_replace($this->_pageHolder, $this->_currentPage - 1, $this->_pageTemplate) . $this->_anchor . '">'
|
||||
. $prevWord . '</a></li>';
|
||||
echo $itemPrevBegin . sprintf($linkPrevBegin,
|
||||
str_replace($this->_pageHolder, $this->_currentPage - 1, $this->_pageTemplate) . $this->_anchor)
|
||||
. $prevWord . $linkEnd . $itemEnd;
|
||||
}
|
||||
|
||||
//输出第一页
|
||||
if ($from > 1) {
|
||||
echo '<li><a href="' . str_replace($this->_pageHolder, 1, $this->_pageTemplate) . $this->_anchor . '">1</a></li>';
|
||||
echo $itemBegin . sprintf($linkBegin, str_replace($this->_pageHolder, 1, $this->_pageTemplate) . $this->_anchor)
|
||||
. '1' . $linkEnd . $itemEnd;
|
||||
|
||||
if ($from > 2) {
|
||||
//输出省略号
|
||||
echo '<li><span>' . $splitWord . '</span></li>';
|
||||
echo $itemBegin . $textBegin . $splitWord . $textEnd . $itemEnd;
|
||||
}
|
||||
}
|
||||
|
||||
//输出中间页
|
||||
for ($i = $from; $i <= $to; $i ++) {
|
||||
echo '<li' . ($i != $this->_currentPage ? '' : ' class="' . $currentClass . '"') . '><a href="' .
|
||||
str_replace($this->_pageHolder, $i, $this->_pageTemplate) . $this->_anchor . '">'
|
||||
. $i . '</a></li>';
|
||||
$current = ($i == $this->_currentPage);
|
||||
|
||||
echo ($current ? $itemCurrentBegin : $itemBegin) . sprintf(($current ? $linkCurrentBegin : $linkBegin),
|
||||
str_replace($this->_pageHolder, $i, $this->_pageTemplate) . $this->_anchor)
|
||||
. $i . $linkEnd . $itemEnd;
|
||||
}
|
||||
|
||||
//输出最后页
|
||||
if ($to < $this->_totalPage) {
|
||||
if ($to < $this->_totalPage - 1) {
|
||||
echo '<li><span>' . $splitWord . '</span></li>';
|
||||
echo $itemBegin . $textBegin . $splitWord . $textEnd . $itemEnd;
|
||||
}
|
||||
|
||||
echo '<li><a href="' . str_replace($this->_pageHolder, $this->_totalPage, $this->_pageTemplate) . $this->_anchor . '">'
|
||||
. $this->_totalPage . '</a></li>';
|
||||
|
||||
echo $itemBegin . sprintf($linkBegin, str_replace($this->_pageHolder, $this->_totalPage, $this->_pageTemplate) . $this->_anchor)
|
||||
. $this->_totalPage . $linkEnd . $itemEnd;
|
||||
}
|
||||
|
||||
//输出下一页
|
||||
if ($this->_currentPage < $this->_totalPage) {
|
||||
echo '<li><a class="next" href="' . str_replace($this->_pageHolder, $this->_currentPage + 1, $this->_pageTemplate)
|
||||
. $this->_anchor . '">' . $nextWord . '</a></li>';
|
||||
echo $itemNextBegin . sprintf($linkNextBegin,
|
||||
str_replace($this->_pageHolder, $this->_currentPage + 1, $this->_pageTemplate) . $this->_anchor)
|
||||
. $nextWord . $linkEnd . $itemEnd;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+18
-7
@@ -1362,11 +1362,23 @@ class Widget_Archive extends Widget_Abstract_Contents
|
||||
* @param string $splitWord 分割字符
|
||||
* @return void
|
||||
*/
|
||||
public function pageNav($prev = '«', $next = '»', $splitPage = 3, $splitWord = '...',
|
||||
$class = 'page-navigator', $currentClass = 'current', $label = 'ol')
|
||||
public function pageNav($prev = '«', $next = '»', $splitPage = 3, $splitWord = '...', $template = '')
|
||||
{
|
||||
if ($this->have()) {
|
||||
$hasNav = false;
|
||||
$default = array(
|
||||
'wrapTag' => 'ol',
|
||||
'wrapClass' => 'page-navigator'
|
||||
);
|
||||
|
||||
if (is_string($template)) {
|
||||
parse_str($template, $config);
|
||||
} else {
|
||||
$config = $template;
|
||||
}
|
||||
|
||||
$template = array_merge($default, $config);
|
||||
|
||||
$this->_total = (false === $this->_total ? $this->size($this->_countSql) : $this->_total);
|
||||
$this->pluginHandle()->trigger($hasNav)->pageNav($this->_currentPage, $this->_total,
|
||||
$this->parameter->pageSize, $prev, $next, $splitPage, $splitWord);
|
||||
@@ -1380,11 +1392,10 @@ class Widget_Archive extends Widget_Abstract_Contents
|
||||
$nav = new Typecho_Widget_Helper_PageNavigator_Box($this->_total,
|
||||
$this->_currentPage, $this->parameter->pageSize, $query);
|
||||
|
||||
/** 添加判断防止输入错误的标签,判断逻辑:如果为 <ul>,使用 <ul>;否则,使用 <ol> */
|
||||
$label = ($label == 'ul') ? 'ul' : 'ol';
|
||||
echo '<' . $label . ' class="' . $class . '">';
|
||||
$nav->render($prev, $next, $splitPage, $splitWord, $currentClass);
|
||||
echo '</' . $label . '>';
|
||||
echo '<' . $template['wrapTag'] . (empty($template['wrapClass'])
|
||||
? '' : ' class="' . $template['wrapClass'] . '"') . '>';
|
||||
$nav->render($prev, $next, $splitPage, $splitWord, $template);
|
||||
echo '</' . $template['wrapTag'] . '>';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user