Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Cleaner URLs, add taxonomy tags
Also removed share links
  • Loading branch information
szk11001 committed May 18, 2015
1 parent bccd69b commit 4c5345b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 85 deletions.
178 changes: 94 additions & 84 deletions js/custom.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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 = $('<span />',{
'class': 'clas-taxonomy-type',
'text': taxonomyType
});

var spanTaxName = $('<span />',{
'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;
Expand All @@ -163,12 +179,6 @@ jQuery(document).ready(function($) {
'class': 'clas-entry-title'
}).appendTo(header);

var aShare = $('<a />', {
'class': 'clas-share-post',
'href': '?list='+slug,
'text': 'Share'
}).appendTo(header);

var divDate = $('<div />',{
'class': 'clas-post-date',
'html': date
Expand Down Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -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){
Expand All @@ -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){
Expand All @@ -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,
Expand All @@ -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){
Expand All @@ -411,8 +422,7 @@ jQuery(document).ready(function($) {
renderStoriesList(response);
},
dataType: "json"
});
//History.pushState(postIDs, tagTitle, "?division="+tag);
});
});

$('#news-archive-search').keypress(function(event){
Expand Down
2 changes: 1 addition & 1 deletion page-news-archive.php
Expand Up @@ -119,13 +119,13 @@ get_header(); ?>

?>
<div class="span8 follow-hero">
<span class="clas-taxonomy-type" style="display:none"></span><span class="clas-taxonomy-name" style="display:none"></span>
<?php
foreach ( $latestpost as $post ) : setup_postdata( $post );
?>
<article data-id="<?php echo $post->ID ?>">
<header class="entry-header">
<h3 class="clas-entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<a href="?list=<?php echo $post->post_name ?>" class="clas-share-post">Share</a>
<div class="clas-post-date"><?php the_time('F j, Y'); ?></div>
<?php
if( has_post_thumbnail() ){
Expand Down

0 comments on commit 4c5345b

Please sign in to comment.