view template/jquery.collapse.js @ 0:84057f728815

Initial commit. Already a few changes in here
author Eris Caffee <discordia@eldalin.com>
date Sun, 26 Jan 2014 21:32:19 -0800
parents
children
line source
1 /*!
2 * Collapse plugin for jQuery
3 * http://github.com/danielstocks/jQuery-Collapse/
4 *
5 * @author Daniel Stocks (http://webcloud.se)
6 * @version 0.9.1
7 * @updated 17-AUG-2010
8 *
9 * Copyright 2010, Daniel Stocks
10 * Released under the MIT, BSD, and GPL Licenses.
11 */
13 (function($) {
15 // Use a cookie counter to allow multiple instances of the plugin
16 var cookieCounter = 0;
18 $.fn.extend({
19 collapse: function(options) {
21 var defaults = {
22 head : "div.trigger",
23 group : "div",
24 cookieName : "collapse",
25 // Default function for showing content
26 show: function() {
27 this.show();
28 },
29 // Default function for hiding content
30 hide: function() {
31 this.hide();
32 }
33 };
34 var op = $.extend(defaults, options);
36 // Default CSS classes
37 var active = "active",
38 inactive = "inactive";
40 return this.each(function() {
42 // Increment coookie counter to ensure cookie name integrity
43 cookieCounter++;
44 var obj = $(this),
45 // Find all headers and wrap them in <a> for accessibility.
46 sections = obj.find(op.head).wrapInner('<a href="#"></a>'),
47 l = sections.length,
48 cookie = op.cookieName + "_" + cookieCounter;
49 // Locate all panels directly following a header
50 var panel = obj.find(op.head).map(function() {
51 var head = $(this)
52 if(!head.hasClass(active)) {
53 return head.next(op.group).hide()[0];
54 }
55 return head.next(op.group)[0];
56 });
58 // Bind event for showing content
59 obj.bind("show", function(e, bypass) {
60 var obj = $(e.target);
61 // ARIA attribute
62 obj.attr('aria-hidden', false)
63 .prev()
64 .removeClass(inactive)
65 .addClass(active);
66 // Bypass method for instant display
67 if(bypass) {
68 obj.show();
69 } else {
70 op.show.call(obj);
71 }
72 });
74 // Bind event for hiding content
75 obj.bind("hide", function(e, bypass) {
76 var obj = $(e.target);
77 obj.attr('aria-hidden', true)
78 .prev()
79 .removeClass(active)
80 .addClass(inactive);
81 if(bypass) {
82 obj.hide();
83 } else {
84 op.hide.call(obj);
85 }
86 });
88 // Look for existing cookies
89 if(cookieSupport) {
90 for (var c=0;c<=l;c++) {
91 var val = $.cookie(cookie + c);
92 // Show content if associating cookie is found
93 if ( val == c + "open" ) {
94 panel.eq(c).trigger('show', [true]);
95 // Hide content
96 } else if ( val == c + "closed") {
97 panel.eq(c).trigger('hide', [true]);
98 }
99 }
100 }
102 // Delegate click event to show/hide content.
103 obj.bind("click", function(e) {
104 var t = $(e.target);
105 // Check if header was clicked
106 if(!t.is(op.head)) {
107 // What about link inside header.
108 if ( t.parent().is(op.head) ) {
109 t = t.parent();
110 } else {
111 return;
112 }
113 e.preventDefault();
114 }
115 // Figure out what position the clicked header has.
116 var num = sections.index(t),
117 cookieName = cookie + num,
118 cookieVal = num,
119 content = t.next(op.group);
120 // If content is already active, hide it.
121 if(t.hasClass(active)) {
122 content.trigger('hide');
123 cookieVal += 'closed';
124 if(cookieSupport) {
125 $.cookie(cookieName, cookieVal, { path: '/', expires: 10 });
126 }
127 return;
128 }
129 // Otherwise show it.
130 content.trigger('show');
131 cookieVal += 'open';
132 if(cookieSupport) {
133 $.cookie(cookieName, cookieVal, { path: '/', expires: 10 });
134 }
135 });
136 });
137 }
138 });
140 // Make sure can we eat cookies without getting into trouble.
141 var cookieSupport = (function() {
142 try {
143 $.cookie('x', 'x', { path: '/', expires: 10 });
144 $.cookie('x', null);
145 }
146 catch(e) {
147 return false;
148 }
149 return true;
150 })();
151 })(jQuery);