Skip to content

Instantly share code, notes, and snippets.

@rdallaire
Created July 24, 2013 23:44
Show Gist options
  • Save rdallaire/6075652 to your computer and use it in GitHub Desktop.
Save rdallaire/6075652 to your computer and use it in GitHub Desktop.
Ajax Tree
/*****************************************************************************************
* SIDE-BY-SIDE CATEGORY TREE AND BUYLIST CATEGORY TREE
*
* This ajax script retrieves either the store's buylist category tree or the regular
* category tree (based on which URL you are on) and inserts it into your theme. This
* enables your site to display BOTH category trees side by side.
*
* To Use:
* 1) Copy/paste the contents of for_theme.txt file into your theme.liquid file.
* 2) Edit the two category tree ID's indicated below (if necessary).
* 3) Include this script and the cross_domain_ajax.js script in your project.
*
* TODO: The category tree import is not instantaneous. Maybe someone can make the wait
* time look nicer.
*
* Version: 1.1, 4/1/13
*****************************************************************************************
*/
jQuery(document).ready(function($){
var storeAjax = {
/* -------------------------------------------------------------------------------- */
catTreeId: '#category_tree', // EDIT THIS: regular category tree ID
buylistCatTreeId: '#category_buylist_tree', // EDIT THIS: buylist category tree ID
storeUrl: 'http://supergamesinc.dev.crystalcommerce.com', // EDIT THIS: include the http:// and do not end with a slash
currentSiteType: 'store', // EDIT THIS: is this script located on the blog or the store?
/* -------------------------------------------------------------------------------- */
init: function() {
var currentURL = window.location.pathname;
if(storeAjax.currentSiteType == 'blog'){
//If we're on a blog, go grab both cat trees from the store
storeAjax.populateCatTree();
storeAjax.populateBuylistCatTree();
} else {
//If we're on a store, go grab the missing category tree (regular or buylist)
if(/buylist/i.test(currentURL)){
storeAjax.populateCatTree();
} else {
storeAjax.populateBuylistCatTree();
}
}
},
// CREATE ABSOLUTE LINKS FOR SCRAPED IMAGES
rewriteRelativeImgUrl: function(content, url){
var imgs = content.find('img');
if(imgs.length){
for (var i=0;i<imgs.length;++i) {
var src = $(links[i]).attr('src');
if(src.substring(0, 4) !== 'http'){ $(links[i]).attr('src', url + src); }
}
}
return content;
},
rewriteRelativeLinkUrl: function(content, url){
var links = content.find('a');
// CREATE ABSOLUTE LINKS FOR SCRAPED LINKS
if(links.length){
for (var i=0;i<links.length;++i) {
var path = $(links[i]).attr('href');
if(path.substring(0, 4) !== 'http'){ $(links[i]).attr('href', url + path); }
}
}
return content;
},
//Retrieve and insert the regular category tree
populateCatTree: function() {
$.ajax({
url: storeAjax.storeUrl,
cache: true,
type: 'GET',
success: function(response) {
/* Cat Tree */
// Taken out by Megan
// var ajaxedCatTree = $(response.responseText).find(storeAjax.catTreeId);
var ajaxedCatTree = $(response).find(storeAjax.catTreeId);
if(ajaxedCatTree.length){
ajaxedCatTree = storeAjax.rewriteRelativeImgUrl(ajaxedCatTree, storeAjax.storeUrl);
ajaxedCatTree = storeAjax.rewriteRelativeLinkUrl(ajaxedCatTree, storeAjax.storeUrl);
$(storeAjax.catTreeId).html(ajaxedCatTree.html());
}
},
complete: function(){
$(storeAjax.catTreeId).initCategoryTree();
}
});
},
//Retrieve and insert the buylist category tree
populateBuylistCatTree: function() {
$.ajax({
url: storeAjax.storeUrl+'/buylist',
cache: true,
type: 'GET',
success: function(response) {
// Taken out by Megan
// var ajaxedBuylistCatTree = $(response.responseText).find(storeAjax.buylistCatTreeId);
var ajaxedBuylistCatTree = $(response).find(storeAjax.buylistCatTreeId);
if(ajaxedBuylistCatTree.length){
ajaxedBuylistCatTree = storeAjax.rewriteRelativeImgUrl(ajaxedBuylistCatTree, storeAjax.storeUrl);
ajaxedBuylistCatTree = storeAjax.rewriteRelativeLinkUrl(ajaxedBuylistCatTree, storeAjax.storeUrl);
$(storeAjax.buylistCatTreeId).html(ajaxedBuylistCatTree.html());
}
},
complete: function(){
$(storeAjax.buylistCatTreeId).initCategoryTree();
}
});
}
}; // siteAjax OBJECT
storeAjax.init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment