Skip to content

Instantly share code, notes, and snippets.

@jonathonbyrdziak
Created December 2, 2010 02:15
Show Gist options
  • Save jonathonbyrdziak/724626 to your computer and use it in GitHub Desktop.
Save jonathonbyrdziak/724626 to your computer and use it in GitHub Desktop.
AccordionCookie: jquery accordion script, requires: jquery oop class; jquery;
/* Accordion Cookie Class
*
* This class is designed to make accordions easy.
*
*/
var AccordionCookie = Class.extend({
/**
* Default options
*/
defaults: {
'openLink' : '.handle', //the css
'closeLink' : '.close', //the css of the close link
'subLinks' : '.slide', //the css of the main link
'trueAccordion' : false, //boolean, true means that only one submenu will display at a time
'cookieName' : 'AccordionCookie', //name of the cookie
},
/**
* Initializing
*/
init: function(options)
{
//initializing variables
this.o = jQuery.extend(this.defaults,options);
this.opens = jQuery(this.o.openLink);
this.subs = jQuery(this.o.subLinks);
this.closes = jQuery(this.o.closeLink);
this.each = new Array();
//run setup
this.loop();
this.loadPresets();
},
/**
* Looping through each item
*/
loop: function()
{
if (!this.opens) return false;
jQuery.each(this.opens, function(key,open){
this.each[key] = new AccordionCookieItem( this, open, this.subs[key], this.closes[key], key );
}.bind(this));
},
/**
* Remembers the accordion position and preloads it
*/
loadPresets: function()
{
var data = this.getPresets();
data.split(",");
jQuery.each(data, function(key, item){
if (item != ",")
{
this.each[item].clickOpen();
}
}.bind(this));
},
getPresets: function()
{
var data = this.cookie(this.o.cookieName);
return data;
},
setPresets: function()
{
//reasons to fail
if (!this.each) return false;
//initializing variables
var data = new Array();
var count = 0;
jQuery.each(this.each, function(k,obj){
if (obj.isOpen())
{
data[count] = obj.key;
count++;
}
});
this.cookie(this.o.cookieName, data+'');
},
/**
* Create a cookie with the given key and value and other optional parameters.
*
* @example $.cookie('the_cookie', 'the_value');
* @desc Set the value of a cookie.
* @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
* @desc Create a cookie with all available options.
* @example $.cookie('the_cookie', 'the_value');
* @desc Create a session cookie.
* @example $.cookie('the_cookie', null);
* @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
* used when the cookie was set.
*
* @param String key The key of the cookie.
* @param String value The value of the cookie.
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
* If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
* If set to null or omitted, the cookie will be a session cookie and will not be retained
* when the the browser exits.
* @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
* @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
* @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
* require a secure protocol (like HTTPS).
* @type undefined
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
cookie: function (key, value, options)
{
// key and value given, set cookie...
if (arguments.length > 1 && (value === null || typeof value !== "object")) {
options = jQuery.extend({}, options);
if (value === null) {
options.expires = -1;
}
if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}
return (document.cookie = [
encodeURIComponent(key), '=',
options.raw ? String(value) : encodeURIComponent(String(value)),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}
// key and possibly options given, get cookie...
options = value || {};
var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
var data = (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
return data;
}
});
/*
* Accordion Cookie item class
*
*/
var AccordionCookieItem = Class.extend({
/**
* Initializing
*/
init: function(parent, open, sub, close, key)
{
//initializing variables
this.AccordionCookie = parent;
this.key = key;
this.open = jQuery(open);
this.sub = jQuery(sub);
this.close = jQuery(close);
this.status = 'closed';
this.startListeners();
},
/**
* Initializing the events
*/
startListeners: function()
{
this.sub.hide();
this.open.click(function(){
this.clickToggle();
return;
}.bind(this));
this.close.click(function(){
this.clickClose();
return;
}.bind(this));
},
/**
* When an item is clicked
*/
clickToggle: function()
{
if (this.isOpen())
{
this.clickClose();
}
else
{
this.clickOpen();
}
},
/**
* clicked to close
*/
clickClose: function()
{
if (this.status == 'closed') return false;
this.status = 'closed';
this.sub.slideUp();
this.AccordionCookie.setPresets();
},
/**
* clicked to open
*/
clickOpen: function()
{
if (this.status == 'open') return false;
this.status = 'open';
this.sub.slideDown();
this.AccordionCookie.setPresets();
},
/**
* Is this thing open?
*/
isOpen: function()
{
if (this.status == 'closed')
return false;
return true;
}
});
jQuery(document).ready(function() {
var accord = new AccordionCookie();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment