forked from mayhemer/logan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.collapse.js
164 lines (148 loc) · 4.15 KB
/
jquery.collapse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Collapse plugin for jQuery
* --
* source: http://github.com/danielstocks/jQuery-Collapse/
* site: http://webcloud.se/jQuery-Collapse
*
* @author Daniel Stocks (http://webcloud.se)
* Copyright 2013, Daniel Stocks
* Released under the MIT, BSD, and GPL Licenses.
*/
(function($) {
// Constructor
function Collapse (el, options) {
options = options || {};
var _this = this,
query = options.query || "> :even";
$.extend(_this, {
$el: el,
options : options,
sections: [],
isAccordion : options.accordion || false,
db : options.persist ? jQueryCollapseStorage(el[0].id) : false
});
// Figure out what sections are open if storage is used
_this.states = _this.db ? _this.db.read() : [];
// For every pair of elements in given
// element, create a section
_this.$el.find(query).each(function() {
var section = new Section($(this), _this);
_this.sections.push(section);
// Check current state of section
var state = _this.states[section._index()];
if(state === 0) {
section.$summary.removeClass("open");
}
if(state === 1) {
section.$summary.addClass("open");
}
// Show or hide accordingly
if(section.$summary.hasClass("open")) {
section.open(true);
}
else {
section.close(true);
}
});
// Capute ALL the clicks!
(function(scope) {
_this.$el.on("click", "[data-collapse-summary]",
$.proxy(_this.handleClick, scope));
}(_this));
}
Collapse.prototype = {
handleClick: function(e) {
e.preventDefault();
var sections = this.sections,
l = sections.length;
while(l--) {
if($.contains(sections[l].$summary[0], e.target)) {
sections[l].toggle();
break;
}
}
},
open : function(eq) {
if(isFinite(eq)) return this.sections[eq].open();
$.each(this.sections, function() {
this.open();
});
},
close: function(eq) {
if(isFinite(eq)) return this.sections[eq].close();
$.each(this.sections, function() {
this.close();
});
}
};
// Section constructor
function Section($el, parent) {
$.extend(this, {
isOpen : false,
$summary : $el
.attr("data-collapse-summary", "")
.wrapInner('<a href="#"/>'),
$details : $el.next(),
options: parent.options,
parent: parent
});
}
Section.prototype = {
toggle : function() {
if(this.isOpen) this.close();
else this.open();
},
close: function(bypass) {
this._changeState("close", bypass);
},
open: function(bypass) {
var _this = this;
if(_this.options.accordion && !bypass) {
$.each(_this.parent.sections, function() {
this.close();
});
}
_this._changeState("open", bypass);
},
_index: function() {
return $.inArray(this, this.parent.sections);
},
_changeState: function(state, bypass) {
var _this = this;
_this.isOpen = state == "open";
if($.isFunction(_this.options[state]) && !bypass) {
_this.options[state].apply(_this.$details);
} else {
if(_this.isOpen) _this.$details.show();
else _this.$details.hide();
}
_this.$summary.removeClass("open close").addClass(state);
_this.$details.attr("aria-hidden", state == "close");
_this.parent.$el.trigger(state, _this);
if(_this.parent.db) {
_this.parent.db.write(_this._index(), _this.isOpen);
}
}
};
// Expose in jQuery API
$.fn.extend({
collapse: function(options, scan) {
var nodes = (scan) ? $("body").find("[data-collapse]") : $(this);
return nodes.each(function() {
var settings = (scan) ? {} : options,
values = $(this).attr("data-collapse") || "";
$.each(values.split(" "), function(i,v) {
if(v) settings[v] = true;
});
new jQueryCollapse($(this), settings);
});
}
});
//jQuery DOM Ready
$(function() {
$.fn.collapse(false, true);
});
// Expose constructor to
// global namespace
jQueryCollapse = Collapse;
})(window.jQuery);