Navigation Placeholder

Page Numbering In Contempo...

In the aftermath of Blogger removing support for some third-party widgets in their widget store (? I guess), some third-party services simply stopped working. Among the casualties were a number of related posts and page numbering widgets. Some time ago, I acquired code for a decent page numbering mechanism from another site: Helplogger and adapted it for my own use with the Contempo template. The difficulty with some of these widgets is they would rely on external code repositories to do the heavy lifting. And when those repositories stopped working or were otherwise compromised, many users would be affected.

The original version is completely self-contained, but, required editing the theme code. I preferred something a bit less permanent, so, I stripped out some of the functionality and converted the code to a simple HTML/JavaScript widget. My version is still self-contained, but, it has limitations: it doesn't work with search results, label pages, archive pages or static pages. By the way, when placing the widget, I've found it works best when placed at the bottom of your sidebar. If you don't want to suffer through the compromises I've inflicted, try the original author's version instead by following the link to: Helplogger.

Proof Of Concept

Add the following code snippet as an HTML/JavaScript widget:
<script type='text/javascript'>
var maxPageSize=7; //Articles to show
var perPage=maxPageSize; //Articles to show
var numPages=3;//Number of links
var firstText ='First';
var lastText ='Last';
var prevText ='&larr;&nbsp;Previous';
var nextText ='Next&nbsp;&rarr;';
var ellipse = '&nbsp;&hellip;&nbsp;';
var urlactivepage=location.href;
var home_page="/";
var noPage;
var currentPage;
var currentPageNo;
var postLabel;

function Do_PageNumbering() {
var useClassicBloggerNav=0;

//No searches
if (urlactivepage.indexOf("q=")>-1) {useClassicBloggerNav=1;}
//No labels
if (urlactivepage.indexOf("/search/label/")>-1) {useClassicBloggerNav=1;}
//No archives
if (urlactivepage.indexOf(".com/20")>-1) {useClassicBloggerNav=1;}
//No archives (secondary pages)
if ((urlactivepage.indexOf("max-results=")>-1) && (urlactivepage.indexOf("#PageNo=")==-1)) {useClassicBloggerNav=1;}
//No pages
if (urlactivepage.indexOf("/p/")>-1) {useClassicBloggerNav=1;}

if (useClassicBloggerNav==0){
if(typeof firstText=="undefined")firstText="First";
if(typeof lastText=="undefined")lastText="Last";
pagecurrentg();
}//if
}//function

function looppagecurrentg(pageInfo){var html='';pageNumber=parseInt(numPages / 2);if(pageNumber==numPages-pageNumber){numPages=pageNumber*2+1}
pageStart=currentPageNo-pageNumber;if(pageStart<1)pageStart=1;lastPageNo=parseInt(pageInfo / perPage)+1;if(lastPageNo-1==pageInfo / perPage)lastPageNo=lastPageNo-1;pageEnd=pageStart+numPages-1;if(pageEnd>lastPageNo)pageEnd=lastPageNo;html+="<span class='showpageOf'>Page "+currentPageNo+' of '+lastPageNo+"</span>";var prevNumber=parseInt(currentPageNo)-1;if(currentPageNo>1){if(currentPage=="page"){html+='<span class="showpage firstpage"><a href="'+home_page+'">'+firstText+'</a></span>'}else{html+='<span class="displaypageNum firstpage"><a href="/search/label/'+postLabel+'?&max-results='+perPage+'">'+firstText+'</a></span>'}}
if(currentPageNo>2){if(currentPageNo==3){if(currentPage=="page"){html+='<span class="showpage"><a href="'+home_page+'">'+prevText+'</a></span>'}else{html+='<span class="displaypageNum"><a href="/search/label/'+postLabel+'?&max-results='+perPage+'">'+prevText+'</a></span>'}}else{if(currentPage=="page"){html+='<span class="displaypageNum"><a href="#" onclick="redirectpage('+prevNumber+');return false">'+prevText+'</a></span>'}else{html+='<span class="displaypageNum"><a href="#" onclick="redirectlabel('+prevNumber+');return false">'+prevText+'</a></span>'}}}
if(pageStart>1){if(currentPage=="page"){html+='<span class="displaypageNum"><a href="'+home_page+'">1</a></span>'}else{html+='<span class="displaypageNum"><a href="/search/label/'+postLabel+'?&max-results='+perPage+'">1</a></span>'}}
if(pageStart>2){html+=ellipse}
for(var jj=pageStart;jj<=pageEnd;jj++){if(currentPageNo==jj){html+='<span class="pagecurrent">'+jj+'</span>'}else if(jj==1){if(currentPage=="page"){html+='<span class="displaypageNum"><a href="'+home_page+'">1</a></span>'}else{html+='<span class="displaypageNum"><a href="/search/label/'+postLabel+'?&max-results='+perPage+'">1</a></span>'}}else{if(currentPage=="page"){html+='<span class="displaypageNum"><a href="#" onclick="redirectpage('+jj+');return false">'+jj+'</a></span>'}else{html+='<span class="displaypageNum"><a href="#" onclick="redirectlabel('+jj+');return false">'+jj+'</a></span>'}}}
if(pageEnd<lastPageNo-1){html+=ellipse}
if(pageEnd<lastPageNo){if(currentPage=="page"){html+='<span class="displaypageNum"><a href="#" onclick="redirectpage('+lastPageNo+');return false">'+lastPageNo+'</a></span>'}else{html+='<span class="displaypageNum"><a href="#" onclick="redirectlabel('+lastPageNo+');return false">'+lastPageNo+'</a></span>'}}
var nextnumber=parseInt(currentPageNo)+1;if(currentPageNo<(lastPageNo-1)){if(currentPage=="page"){html+='<span class="displaypageNum"><a href="#" onclick="redirectpage('+nextnumber+');return false">'+nextText+'</a></span>'}else{html+='<span class="displaypageNum"><a href="#" onclick="redirectlabel('+nextnumber+');return false">'+nextText+'</a></span>'}}
if(currentPageNo<lastPageNo){if(currentPage=="page"){html+='<span class="displaypageNum lastpage"><a href="#" onclick="redirectpage('+lastPageNo+');return false">'+lastText+'</a></span>'}else{html+='<span class="displaypageNum lastpage"><a href="#" onclick="redirectlabel('+lastPageNo+');return false">'+lastText+'</a></span>'}}
var pageArea=document.getElementsByName("pageArea");var blogPager=document.getElementById("blog-pager");for(var p=0;p<pageArea.length;p++){pageArea[p].innerHTML=html}
if(pageArea&&pageArea.length>0){html=''}
if(blogPager){blogPager.innerHTML=html}}

function totalcountdata(root){var feed=root.feed;var totaldata=parseInt(feed.openSearch$totalResults.$t,10);looppagecurrentg(totaldata)}

function pagecurrentg(){var thisUrl=urlactivepage;if(thisUrl.indexOf("/search/label/")!=-1){if(thisUrl.indexOf("?updated-max")!=-1){postLabel=thisUrl.substring(thisUrl.indexOf("/search/label/")+14,thisUrl.indexOf("?updated-max"))}else{postLabel=thisUrl.substring(thisUrl.indexOf("/search/label/")+14,thisUrl.indexOf("?&max"))}}
if(thisUrl.indexOf("?q=")==-1&&thisUrl.indexOf(".html")==-1){if(thisUrl.indexOf("/search/label/")==-1){currentPage="page";if(urlactivepage.indexOf("#PageNo=")!=-1){currentPageNo=urlactivepage.substring(urlactivepage.indexOf("#PageNo=")+8,urlactivepage.length)}else{currentPageNo=1}
document.write("<script src=\""+home_page+"feeds/posts/summary?max-results=1&alt=json-in-script&callback=totalcountdata\"><\/script>")}else{currentPage="label";if(thisUrl.indexOf("&max-results=")==-1){perPage=maxPageSize}
if(urlactivepage.indexOf("#PageNo=")!=-1){currentPageNo=urlactivepage.substring(urlactivepage.indexOf("#PageNo=")+8,urlactivepage.length)}else{currentPageNo=1}
document.write('<script src="'+home_page+'feeds/posts/summary/-/'+postLabel+'?alt=json-in-script&callback=totalcountdata&max-results=1" ><\/script>')}}}

function redirectpage(numberpage){jsonstart=(numberpage-1)*perPage;noPage=numberpage;var nameBody=document.getElementsByTagName('head')[0];var newInclude=document.createElement('script');newInclude.type='text/javascript';newInclude.setAttribute("src",home_page+"feeds/posts/summary?start-index="+jsonstart+"&max-results=1&alt=json-in-script&callback=finddatepost");nameBody.appendChild(newInclude)}

function redirectlabel(numberpage){jsonstart=(numberpage-1)*perPage;noPage=numberpage;var nameBody=document.getElementsByTagName('head')[0];var newInclude=document.createElement('script');newInclude.type='text/javascript';newInclude.setAttribute("src",home_page+"feeds/posts/summary/-/"+postLabel+"?start-index="+jsonstart+"&max-results=1&alt=json-in-script&callback=finddatepost");nameBody.appendChild(newInclude)}

function finddatepost(root){post=root.feed.entry[0];var timestamp1=post.published.$t.substring(0,19)+post.published.$t.substring(23,29);var timestamp=encodeURIComponent(timestamp1);if(currentPage=="page"){var pAddress="/search?updated-max="+timestamp+"&max-results="+perPage+"#PageNo="+noPage}else{var pAddress="/search/label/"+postLabel+"?updated-max="+timestamp+"&max-results="+perPage+"#PageNo="+noPage}
location.href=pAddress}

Do_PageNumbering();
</script>
And, this is one of the author's basic stylesheets for the page links. The following code snippet can be added using Theme > Customize > Advanced > Add CSS:
#blog-pager{clear:both;margin:30px auto;text-align:center; padding: 7px;}
.blog-pager {background: none;}
.displaypageNum a,.showpage a,.pagecurrent{font-size: 14px;padding: 5px 12px;margin-right:5px; color: #666; background-color:#eee;}
.displaypageNum a:hover,.showpage a:hover, .pagecurrent{background:#359BED;text-decoration:none;color: #fff;}
#blog-pager .pagecurrent{font-weight:bold;color: #fff;background:#359BED;}
.showpageOf{display:none!important}
#blog-pager .pages{border:none;}
I was able to get this code up and running with an out-of-the-box Contempo template. In my testing I have noticed a slight delay during the loading process for any given page and occasionally the code will throw a warning regarding script injection. It was an issue with the author's original code and I don't really know enough about its inner workings to fix it, so, I left it as is. Your mileage may vary. If you find this somewhat useful and want to try the original author's version instead, follow the link to: Helplogger. That's all I have for today.

Comments

  1. How to center header and show page list on all pages in contempo

    ReplyDelete
    Replies
    1. I use two page lists to get around the limitation. Hide the first page list in your layout and add a new page list from the add a widget menu. There is also some CSS to add for Contempo. Details. Centering headers in Contempo requires some CSS. Details.

      Delete

Post a Comment

Note: Anonymous posts are allowed, but, all comments are subject to moderation by the content manager. Comments may be rejected, edited or deleted at the sole discretion of the content manager.