feat:完成翻页辅助函数。
This commit is contained in:
		
							
								
								
									
										61
									
								
								scripts/helper/uk-paginator.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								scripts/helper/uk-paginator.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,61 @@
 | 
			
		||||
hexo.extend.helper.register("uk_paginator", function(option) {
 | 
			
		||||
    const options = Object.assign({
 | 
			
		||||
        base: "/",
 | 
			
		||||
        pagePath: "page/%d/",
 | 
			
		||||
        total: 1,
 | 
			
		||||
        current: 1,
 | 
			
		||||
        space: "...",
 | 
			
		||||
        sideSize: 5,
 | 
			
		||||
        midSize: 10,
 | 
			
		||||
        mainClasses: [],
 | 
			
		||||
        itemClasses: [],
 | 
			
		||||
    }, option);
 | 
			
		||||
    console.dir(options);
 | 
			
		||||
 | 
			
		||||
    let pageStructure = [];
 | 
			
		||||
    pageStructure.push(`<ul class="uk-pagination ${options.mainClasses.join(' ')}">`);
 | 
			
		||||
 | 
			
		||||
    if (options.total > 0 && options.current > 1) {
 | 
			
		||||
        pageStructure.push(`<li class="${options.itemClasses.join(' ')}"><a href="${options.base}${options.pagePath.replace('%d', 1)}"><span uk-pagination-previous></span></a></li>`);
 | 
			
		||||
    } else {
 | 
			
		||||
        pageStructure.push(`<li class="uk-disabled ${options.itemClasses.join(' ')}"><a href="${options.base}${options.pagePath.replace('%d', 1)}"><span uk-pagination-previous></span></a></li>`)
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    let pageGroups = [];
 | 
			
		||||
    // 生成起始页范围
 | 
			
		||||
    for (let p = 1; p <= Math.min(options.sideSize, options.total); p++) {
 | 
			
		||||
        pageGroups.push(p);
 | 
			
		||||
    }
 | 
			
		||||
    // 生成中间页范围
 | 
			
		||||
    if (options.current > options.sideSize && options.current < (options.total - options.sideSize + 1)) {
 | 
			
		||||
        for (let p = Math.max(pageGroups[pageGroups.length - 1] + 1, options.current - options.midSize / 2); p <= Math.min(options.total, options.current + options.midSize / 2); p++) {
 | 
			
		||||
            pageGroups.push(p);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 生成结束页范围
 | 
			
		||||
    for (let p = Math.max(pageGroups[pageGroups.length - 1] + 1, options.total - options.sideSize + 1); p <= options.total; p++) {
 | 
			
		||||
        pageGroups.push(p);
 | 
			
		||||
    }
 | 
			
		||||
    console.dir(pageGroups);
 | 
			
		||||
 | 
			
		||||
    pageGroups.forEach(function(page, index) {
 | 
			
		||||
        if (index > 0 && page - pageGroups[index - 1] > 1) {
 | 
			
		||||
            pageStructure.push(`<li class="uk-disabled ${options.itemClasses.join(' ')}"><span>${options.space}</span></li>`);
 | 
			
		||||
        }
 | 
			
		||||
        if (page === options.current) {
 | 
			
		||||
            pageStructure.push(`<li class="uk-active ${options.itemClasses.join(' ')}"><span>${page}</span></li>`)
 | 
			
		||||
        } else {
 | 
			
		||||
            pageStructure.push(`<li class="${options.itemClasses.join(' ')}"><a href="${options.base}${options.pagePath.replace('%d', page)}">${page}</a></li>`)
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    if (options.total > 0 && options.current + 1 <= options.total) {
 | 
			
		||||
        pageStructure.push(`<li class="${options.itemClasses.join(' ')}"><a href="${options.base}${options.pagePath.replace('%d', options.current + 1)}"><span uk-pagination-next></span></a></li>`)
 | 
			
		||||
    } else {
 | 
			
		||||
        pageStructure.push(`<li class="uk-disabled ${options.itemClasses.join(' ')}"><a href="${options.base}${options.pagePath.replace('%d', options.current + 1)}"><span uk-pagination-next></span></a></li>`)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pageStructure.push(`</ul>`);
 | 
			
		||||
 | 
			
		||||
    return pageStructure.join("\n");
 | 
			
		||||
});
 | 
			
		||||
		Reference in New Issue
	
	Block a user