Skip to content

Instantly share code, notes, and snippets.

@rdallaire
Created April 25, 2014 23:24
Show Gist options
  • Save rdallaire/11306594 to your computer and use it in GitHub Desktop.
Save rdallaire/11306594 to your computer and use it in GitHub Desktop.
$(document).crystalcore('ajaxify', {
url : 'http://topdeckcardsandgames.dev.crystalcommerce.com/',
elem : '.ajaxify',
cached : true,
key : 'tree',
duration : 1
});
/*
$('.depth-2.nonleaf-category').on('click', function(e){
e.preventDefault();
console.log('frack');
});
*/
var DTREE = {
// SETUP FOR DESKTOP TREE
setup: function(fire) {
// DON'T FIRE SCRIPT ON MOBILE
if(fire === false) { return; }
DTREE.move();
DTREE.events.bind();
},
// APPEND CAT TREE FROM MOBILE
move: function() {
MTREE.cleanup();
if(!$('#desktop-tree').children('.category-tree').length) {
$('#mobile-tree .category-tree').appendTo('#desktop-tree');
}
},
// OPEN FLYOUT CAT TREE
open: function(elem) {
console.log('frack');
var sub = elem, // Tab
subOne = elem.closest('.sub-category-tree'), // Tab Container
subTwo = sub.children('.sub-category-tree'), // Tab Content
subNew = sub.nextAll('.placeholder:first'), // New Placeholder
oneTall = subOne.height(), // Tab Container Height
newTall = subNew.height(), // New Placeholder Height
oldTall = 0, // Old Placeholder Height
holders = $('#desktop-tree .placeholder'), // Placeholders for Loop
subOld, i;
// ASSIGN HEIGHT TO THE 'TAB CONTAINER'
subOne.css('height', subOne.height() + 'px');
// LOOP THRU ALL PLACEHOLDER & HIDE THEM
$('#desktop-tree .placeholder').each(function(){
if($(this).html() != '<span></span>'){
subOld = $(this);
oldTall = subOld.height();
subOld.html('<span></span>');
subOld.hide();
return false;
}
});
// POPULATE NEW PLACEHOLDER WITH TAB CONTENT
subNew.children('span:first').html(subTwo.html());
// PRESERVE PLACEHOLDER HEIGHT FOR GOOD UX
if(newTall > oldTall) {
subOne.css('height', (oneTall - oldTall + newTall) + 'px');
} else {
subOne.css('height', 'auto');
subOne.css('height', (oneTall + oldTall) + 'px');
}
// RESET OLD PLACEHOLDER, REMOVE 'EXPANDED'
$('#desktop-tree .depth-2.expanded').removeClass('expanded');
// SHOW NEW PLACEHOLDER
subNew.slideDown(250);
// ADD EXPANDED CLASS
sub.addClass('expanded');
},
// CLOSE FLYOUT CAT TREE
close: function(elem) {
//console.log('dtree close');
// ASSIGN NEEDED VARIABLES
var sub = elem;
var subOne = elem.closest('.sub-category-tree');
// SET HEIGHT OF TAB CONTAINER
subOne.css('height', 'auto');
// REMOVE EXPANDED CLASS
sub.removeClass('expanded');
// HIDE THE ACTIVE PLACEHOLDER
$('#desktop-tree .category-tree .placeholder').slideUp(250, function() {
$(this).html('<span></span>');
});
},
// EVENT HANDLERS CLEANUP
cleanup: function() {
DTREE.events.unbind();
},
// CALLBACKS FOR EVENTS
callbacks: {
/*
noleaf: function(e) {
e.preventDefault();
},
*/
subClick: function(e) {
e.preventDefault();
// DETERMINE TO OPEN OR CLOSE
if($(this).hasClass('expanded')) {
DTREE.close($(this));
} else {
DTREE.open($(this));
}
},
mouse: function(e) {
// HIDE PLACEHOLDER SPANS
$('#desktop-tree .placeholder').html('<span></span>').hide();
// REMOVE "EXPANDED" CLASS
$('#desktop-tree .expanded').removeClass('expanded');
},
},
// DTREE EVENT HANDLERS
events: {
bind: function() {
DTREE.events.unbind();
// NONLEAF CLICK ELEMENTS
$('#desktop-tree .nonleaf-category a').on(Crystal.touchClick, DTREE.callbacks.noleaf);
// DEPTH-2 CLICK ELEMENTS
$('#desktop-tree .depth-2.nonleaf-category').on(Crystal.touchClick, DTREE.callbacks.subClick);
// MOUSE EVENTS
$('#desktop-tree .category-tree').on('mouseleave', DTREE.callbacks.mouse);
},
unbind: function() {
$('#desktop-tree .nonleaf-category a').off(Crystal.touchClick, DTREE.callbacks.noleaf);
$('#desktop-tree .depth-2.nonleaf-category').off(Crystal.touchClick, DTREE.callbacks.subClick);
$('#desktop-tree .category-tree').off('mouseleave', DTREE.callbacks.mouse);
}
}
};
DTREE.events.bind();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment