-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.bb-accordion.js
210 lines (176 loc) · 6.66 KB
/
jquery.bb-accordion.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* jshint -W117 */
/*!
* BB Accordion 1.1.0
* https://github.com/bobbybol/accordion.js
* @license MIT licensed
*
* Copyright (C) 2017 bobbybol.com - A project by Bobby Bol
*/
;( function($, window, document, undefined) {
'use strict';
/**
* Setting the defaults
*/
var pluginName = 'bbAccordion';
var defaults = {
button: '.bb-accordion--button',
outer: '.bb-accordion--outer',
inner: '.bb-accordion--inner',
cssTransition: false,
transitionSpeed: 600,
changeButtonHtml: false,
toggledButtonHtml: 'Close Details',
exclusive: false
};
/**
* Composing the Plugin constructor
*/
function Plugin (element, originalSelector, options) {
// Create a settings property merged from defaults and passed options
this.settings = $.extend( {}, defaults, options );
// General purpose properties
this._defaults = defaults;
this._name = pluginName;
this.element = element;
// Plugin specific properties
this.wrapper = $(element);
this.originalSelector = originalSelector;
this.button = this.wrapper.find(this.settings.button);
this.outer = this.wrapper.find(this.settings.outer);
this.inner = this.outer.find(this.settings.inner);
this.cssTransition = this.settings.cssTransition;
this.transitionSpeed = this.settings.transitionSpeed;
this.changeButtonHtml = this.settings.changeButtonHtml;
this.originalButtonHtml = this.button.html();
this.toggledButtonHtml = this.settings.toggledButtonHtml;
this.exclusive = this.settings.exclusive;
this.isOpen = false;
this._innerHeight = 0;
// Initialize on construction
this.init();
}
// Extend the Plugin prototype with custom methods
$.extend(Plugin.prototype, {
// Helper :: update _innerHeight
_updateInnerHeight: function() {
this._innerHeight = this.inner.outerHeight(true);
},
// Helper :: resize an open accordion
_resizeToOpen: function() {
// set/update innerHeight
this._updateInnerHeight();
this.outer.css({
height: this._innerHeight
});
},
// Helper :: resize a closed accordion
_resizeToClosed: function () {
// Set outer height to 0
this.outer.css({
height: '0px',
'overflow-y': 'hidden'
});
},
// Toggle accordion open/closed
toggleMe: function () {
// Toggle isOpen variable and class
this.isOpen = !this.isOpen;
this.wrapper.toggleClass('bb-accordion--open');
if (this.isOpen) {
if (this.changeButtonHtml) {
this.button.html(this.toggledButtonHtml);
}
this._resizeToOpen();
} else {
if (this.changeButtonHtml) {
this.button.html(this.originalButtonHtml);
}
this._resizeToClosed();
}
},
// Open accordion
openMe: function() {
this.isOpen = true;
this.wrapper.addClass('bb-accordion--open');
if (this.changeButtonHtml) {
this.button.html(this.toggledButtonHtml);
}
this._resizeToOpen();
},
// Close accordion
closeMe: function() {
this.isOpen = false;
this.wrapper.removeClass('bb-accordion--open');
if (this.changeButtonHtml) {
this.button.html(this.originalButtonHtml);
}
this._resizeToClosed();
},
// Initialization
init: function() {
var self = this;
// Hide overflow
this.outer.css({
'overflow-y': 'hidden'
});
// Set `isOpen` based on whether class was specified
if (this.wrapper.hasClass('bb-accordion--open')) {
this.isOpen = true;
this._resizeToOpen();
} else {
this.isOpen = false;
this._resizeToClosed();
}
// Use built-in CSS transition if none is specified
if (!this.cssTransition) {
var speed = this.transitionSpeed;
this.outer.css({
'-webkit-transition': 'all ' + speed + 'ms ease-out 0s',
'-moz-transition': 'all ' + speed + 'ms ease-out 0s',
'transition': 'all ' + speed + 'ms ease-out 0s'
});
}
// Event listeners
this.button.click(function() {
// If exclusive is true, first close all accordions of same type
if(self.exclusive && !self.isOpen) {
$(self.originalSelector).bbAccordion('closeMe');
}
self.toggleMe();
});
// Resize
$(window).resize(function() {
if (self.isOpen) {
self._resizeToOpen();
}
});
}
});
/**
* Plugin wrapper around the constructor
* Discerns between invoking a method or the constructor
*/
$.fn[pluginName] = function(methodOrOptions) {
var originalSelector = this.selector;
// Check if plugin has initialized
if ( $(this).data(pluginName) && typeof methodOrOptions !== 'object' && methodOrOptions !== undefined ) {
// Check a method was passed that exists among plugin methods
if ( $(this).data(pluginName)[methodOrOptions] ) {
// Execute the requested method on each element
return this.each(function() {
$(this).data(pluginName)[methodOrOptions]();
});
} else {
console.warn('bbAccordion: you\'re trying to invoke a method that does not exist.');
}
}
// Otherwise, initialize the plugin
else {
return this.each(function() {
if (!$.data(this, pluginName)) {
$.data(this, pluginName, new Plugin(this, originalSelector, methodOrOptions));
}
});
}
};
})(jQuery, window, document);