Skip to content

Instantly share code, notes, and snippets.

@fawx
Created July 6, 2012 20:34
Show Gist options
  • Save fawx/3062609 to your computer and use it in GitHub Desktop.
Save fawx/3062609 to your computer and use it in GitHub Desktop.
sugar js
/*
* page functions are grouped by <body>'s class.
* all pages in a class fire init().
* named functions are fired according to <body>'s id.
* hyphens are stripped from classes and ids automatically.
*
*/
SUGAR = {
common : {
init : function() {
// all external links should open in a new tab
$("a[href^='http']").attr("target", "_blank");
// most pages have galleries so get that started
$("section.gallery").show().cycle().css("background", "none");
// make sure nav is showing the correct items according to the url
$("nav .active").parentsUntil("nav").show().addClass("active");
if ($(".active").last().children("ul").length == 0) {
$(".active").last().addClass("youngest");
}
// navigation
$("nav li:not(.contact, .buzz) a").click(function(e) {
e.preventDefault();
// restyle the navigation
$("nav ul, nav li").removeClass("active").removeClass("youngest");
$(this).siblings("ul").slideDown('fast');
$(this).parentsUntil("nav").addClass("active");
if ($(this).siblings("ul").length == 0) {
$(this).parent().addClass("youngest");
}
// load the new page if it's not just a menu link
if (!$(this).parent().hasClass("menu")) {
var url = $(this).attr("href");
var newpage = $("<div></div>").load(url + " #main", function() {
$("#main").replaceWith( $(this).children("#main") );
$("body").attr( "class", $(this).children("#main").attr("class") )
history.pushState(null, "", url);
UTIL.loadEvents();
});
}
});
}
}
}
UTIL = {
fire : function(func,funcname, args){
var namespace = SUGAR;
funcname = (funcname === undefined) ? 'init' : funcname;
if (func !== '' && namespace[func] && typeof namespace[func][funcname] == 'function'){
namespace[func][funcname](args);
}
},
loadEvents : function() {
var bodyId = document.body.id;
// hit up common first.
UTIL.fire('common');
// do all the classes too.
$.each(document.body.className.split(/\s+/), function(i, classnm) {
UTIL.fire(classnm.replace(/-/g, ''));
UTIL.fire(classnm.replace(/-/g, ''),bodyId);
});
UTIL.fire('common','finalize');
}
};
$(document).ready(UTIL.loadEvents);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment