diff --git a/js/custom.js b/js/custom.js index 474710b..df89345 100644 --- a/js/custom.js +++ b/js/custom.js @@ -97,6 +97,10 @@ jQuery(document).ready(function($) { History.options.initialTitle = 'News Archive'; History.options.disableSuid = true; + String.prototype.capitalize = function() { + return this.charAt(0).toUpperCase() + this.slice(1); + }; + function retrievePreviousIDs(startingElement){ var postsArray = []; var currentPost = startingElement; @@ -127,7 +131,7 @@ jQuery(document).ready(function($) { return postsArray; } - function renderPosts(postsArray){ + function renderPosts(postsArray, taxonomyType, taxonomyName){ $.ajax({ type: "POST", url: clas_object.ajaxurl, @@ -140,6 +144,18 @@ jQuery(document).ready(function($) { $(".span8.follow-hero").html(" "); var firstID = response[0].id; var lastID = response[responseLength].id; + if( taxonomyName != undefined && taxonomyType != undefined ){ + var spanTaxType = $('',{ + 'class': 'clas-taxonomy-type', + 'text': taxonomyType + }); + + var spanTaxName = $('',{ + 'class': 'clas-taxonomy-name', + 'text': taxonomyName + }); + $(".span8.follow-hero").append(spanTaxType).append(spanTaxName); + } for(var post in response){ var current = response[post]; var id = current.id; @@ -163,12 +179,6 @@ jQuery(document).ready(function($) { 'class': 'clas-entry-title' }).appendTo(header); - var aShare = $('', { - 'class': 'clas-share-post', - 'href': '?list='+slug, - 'text': 'Share' - }).appendTo(header); - var divDate = $('
',{ 'class': 'clas-post-date', 'html': date @@ -219,7 +229,7 @@ jQuery(document).ready(function($) { }); } - function renderStoriesList(postObj){ + function renderStoriesList(postObj, taxonomyType, taxonomyName){ $(".news-archive-list").empty(); var postIDs = []; var i = 0; @@ -245,86 +255,87 @@ jQuery(document).ready(function($) { }).appendTo(li); i++; } - renderPosts(postIDs); + renderPosts(postIDs, taxonomyType, taxonomyName); paginateNews(); return postIDs; } + History.Adapter.onDomLoad(function(){ + var index; + if( (index = state.url.indexOf('?post/')) !== -1 ){ + var urlQuery = state.url.substring(index); + var postSlug = urlQuery.substring(6); // length of "?post/" is 6 + var postInList = $("ul.news-archive-list").find("[data-slug='" + postSlug + "']"); + + var postIDs = retrievePostIDs(postInList); + renderPosts(postIDs); + } else if( (index = state.url.indexOf('?division') !== -1 ) ){ + var urlQuery = state.url.substring(state.url.indexOf('?division')); + var divisionSlug = urlQuery.substring(10); //length of "?divison/" is 10 + + var divisionTitle = divisionSlug.toLowerCase().replace(/(-)/g, ' ').replace(/\b[a-z]/g, function(letter) { + return letter.toUpperCase(); + }); + + $.ajax({ + type: "POST", + url: clas_object.ajaxurl, + data: { + action: 'clas_tag_posts', + tag: divisionSlug + }, + success: function(response){ + renderStoriesList(response, 'Division', divisionTitle); + }, + dataType: "json" + }); + } else if( (index = state.url.indexOf('?category/')) !== -1 || (index = state.url.indexOf('?tag/')) !== -1 ) { + var urlQuery = state.url.substring(index+1); + var taxonomyType = urlQuery.substring(0,urlQuery.indexOf('/')); + var taxonomyName = urlQuery.substring(urlQuery.indexOf('/')+1); + + var taxonomyTypeCapitalized = taxonomyType.toLowerCase().replace(/(-)/g, ' ').replace(/\b[a-z]/g, function(letter) { + return letter.toUpperCase(); + }); + var taxonomyNameCapitalized = taxonomyName.capitalize(); + $.ajax({ + type: "POST", + url: clas_object.ajaxurl, + data: { + action: 'clas_taxid', + taxonomyType: taxonomyType, + taxonomyName: taxonomyName + }, + success: function(response){ + var taxonomyID = response; + $.ajax({ + type: "POST", + url: clas_object.ajaxurl, + data: { + action: 'clas_cat_posts', + taxonomy: taxonomyType, + value: taxonomyID + }, + success: function(response){ + renderStoriesList(response, taxonomyTypeCapitalized, taxonomyNameCapitalized); + }, + dataType: "json" + }); + }, + dataType: "json" + }); + } + }); + History.Adapter.bind(window, 'statechange', function(){ var state = History.getState(); if( state.url == clas_object.siteurl+'/news-archive/' ){ window.location.href = clas_object.siteurl+'/news-archive'; return false; } - //renderPosts(state.data); }); - var index; - if( (index = state.url.indexOf('?list=')) !== -1 ){ - var urlHash = state.url.substring(index); - var postName = urlHash.substring(urlHash.indexOf('=')+1); - var element = $("ul.news-archive-list").find("[data-slug='" + postName + "']"); - - var postIDs = retrievePostIDs(element); - renderPosts(postIDs); - } else if( (index = state.url.indexOf('?division=')) !== -1 ) { - var urlHash = state.url.substring(index); - var divisionName = urlHash.substring(urlHash.indexOf('=')+1); - var tagTitle = divisionName.toLowerCase().replace(/(-)/g, ' ').replace(/\b[a-z]/g, function(letter) { - return letter.toUpperCase(); - }); - - $.ajax({ - type: "POST", - url: clas_object.ajaxurl, - data: { - action: 'clas_tag_posts', - tag: divisionName - }, - success: function(response){ - renderStoriesList(response); - }, - dataType: "json" - }); - //History.pushState(postIDs, tagTitle, "?division="+divisionName); - } else if( (index = state.url.indexOf('?category=')) !== -1 || (index = state.url.indexOf('?tag=')) !== -1 ){ - var urlHash = state.url.substring(index); - var taxonomyName = urlHash.substring(urlHash.indexOf('=')+1); - var taxonomy; - if( (index = state.url.indexOf('?category=')) !== -1 ){ - taxonomy = 'category'; - } else { - taxonomy = 'tag'; - } - $.ajax({ - type: "POST", - url: clas_object.ajaxurl, - data: { - action: 'clas_taxid', - taxonomyType: taxonomy, - taxonomyName: taxonomyName - }, - success: function(response){ - var taxonomyID = response; - $.ajax({ - type: "POST", - url: clas_object.ajaxurl, - data: { - action: 'clas_cat_posts', - taxonomy: taxonomy, - value: taxonomyID - }, - success: function(response){ - var postIDs = renderStoriesList(response); - }, - dataType: "json" - }); - }, - dataType: "json" - }); - } - $(document.body).on('click', 'ul.news-archive-list li', function(event){ event.preventDefault(); @@ -334,7 +345,7 @@ jQuery(document).ready(function($) { var postTitle = $(this).data('slug'); var postLink = this.children[0].href; - History.pushState(postIDs, postTitle, "?list="+postTitle); + History.pushState(postIDs, postTitle, "?post/"+postTitle); }); $('div.news-tags a').click(function(event){ @@ -353,11 +364,11 @@ jQuery(document).ready(function($) { tag: tag }, success: function(response){ - renderStoriesList(response); + renderStoriesList(response, 'Division', tagTitle); }, dataType: "json" - }); - History.pushState(postIDs, tagTitle, "?division="+tag); + }); + History.pushState({}, tagTitle, '?division/'+tag); }); $('select.category,select.tag').change(function(event){ @@ -376,7 +387,7 @@ jQuery(document).ready(function($) { }); var taxonomySlug = taxonomyName.toLowerCase().replace(/(\s)/g, '-'); - + taxonomyCapitalized = taxonomy.capitalize(); $.ajax({ type: "POST", url: clas_object.ajaxurl, @@ -386,11 +397,11 @@ jQuery(document).ready(function($) { value: taxonomyID }, success: function(response){ - var postIDs = renderStoriesList(response); + renderStoriesList(response, taxonomyCapitalized, taxonomyTitle); }, dataType: "json" }); - History.pushState(postIDs, taxonomyTitle, "?"+taxonomy+"="+taxonomySlug); + History.pushState({}, taxonomyTitle, "?"+taxonomy+"/"+taxonomySlug); }); $('div.date-sort-button a').click(function(event){ @@ -411,8 +422,7 @@ jQuery(document).ready(function($) { renderStoriesList(response); }, dataType: "json" - }); - //History.pushState(postIDs, tagTitle, "?division="+tag); + }); }); $('#news-archive-search').keypress(function(event){ diff --git a/page-news-archive.php b/page-news-archive.php index bcd682d..6ea6728 100644 --- a/page-news-archive.php +++ b/page-news-archive.php @@ -119,13 +119,13 @@ get_header(); ?> ?>