This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
accordion.js
250 lines (198 loc) · 7.18 KB
/
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
var Mobify = window.Mobify = window.Mobify || {};
Mobify.$ = Mobify.$ || window.Zepto || window.jQuery;
Mobify.UI = Mobify.UI || {};
(function($, document) {
$.support = $.support || {};
$.extend($.support, {
'touch': 'ontouchend' in document
});
})(Mobify.$, document);
/**
@module Holds common functions relating to UI.
*/
Mobify.UI.Utils = (function($) {
var exports = {}
, has = $.support;
/**
Events (either touch or mouse)
*/
exports.events = (has.touch)
? {down: 'touchstart', move: 'touchmove', up: 'touchend'}
: {down: 'mousedown', move: 'mousemove', up: 'mouseup'};
/**
Returns the position of a mouse or touch event in (x, y)
@function
@param {Event} touch or mouse event
@returns {Object} X and Y coordinates
*/
exports.getCursorPosition = (has.touch)
? function(e) {e = e.originalEvent || e; return {x: e.touches[0].clientX, y: e.touches[0].clientY}}
: function(e) {return {x: e.clientX, y: e.clientY}};
/**
Returns prefix property for current browser.
@param {String} CSS Property Name
@return {String} Detected CSS Property Name
*/
exports.getProperty = function(name) {
var prefixes = ['Webkit', 'Moz', 'O', 'ms', '']
, testStyle = document.createElement('div').style;
for (var i = 0; i < prefixes.length; ++i) {
if (testStyle[prefixes[i] + name] !== undefined) {
return prefixes[i] + name;
}
}
// Not Supported
return;
};
// determine which transition event to use
function whichTransitionEvent(){
// http://stackoverflow.com/questions/5023514/how-do-i-normalize-css3-transition-functions-across-browsers
// hack for ios 3.1.* because of poor transition support.
if (/iPhone\ OS\ 3_1/.test(navigator.userAgent)) {
return undefined;
}
var el = document.createElement('fakeelement');
var transitions = {
'transition':'transitionEnd',
'OTransition':'oTransitionEnd',
'MSTransition':'msTransitionEnd',
'MozTransition':'transitionend',
'WebkitTransition':'webkitTransitionEnd'
}
var t;
for(t in transitions){
if( el.style[t] !== undefined ){
return transitions[t];
}
}
return;
};
$.extend(exports.events, {
'transitionend': whichTransitionEvent()
});
return exports;
})(Mobify.$);
Mobify.UI.Accordion = (function($, Utils) {
var has = $.support;
// Constructor
var Accordion = function(element) {
this.element = element;
this.$element = $(element);
this.dragRadius = 10;
this.bind();
};
Accordion.prototype.bind = function() {
var $element = this.$element
, xy
, dxy
, dragRadius = this.dragRadius;
function endTransition(){
// transition attached to .content elements, use parent to grab .item
var $item = $(this).parent();
// if the transition is ending
if ($item.hasClass('m-closed')) $(this).parent().removeClass('m-active');
// recalculate proper height
var height = 0;
$('.m-item', $element).each(function(index) {
var $item = $(this);
height += $item.height();
});
$element.css('min-height', height + 'px');
};
function close($item) {
// toggle opened and closed classes
$item.removeClass('m-opened');
$item.addClass('m-closed');
// toggle active class on close only if there is no transition support
if(!Utils.events.transitionend) $item.removeClass('m-active');
// set max-height to 0 upon close
$item.find('.m-content').css('max-height', 0)
};
function open($item) {
var $content = $item.find('.m-content');
$item.addClass('m-active');
$item.removeClass('m-closed');
$item.addClass('m-opened')
// determine which height function to use (outerHeight not supported by zepto)
var contentChildren = $content.children();
var contentHeight = ('outerHeight' in contentChildren) ? contentChildren['outerHeight']() : contentChildren['height']();
$content.css('max-height', contentHeight * 1.5 +'px');
// if transitions are supported, minimize browser reflow by adding the height
// of the to-be expanded content element to the height of the entire accordion
if (Utils.events.transitionend) {
$element.css('min-height', $element.height() + contentHeight + 'px');
}
};
function down(e) {
// get initial position on mouse/touch start
xy = Utils.getCursorPosition(e);
};
function move(e) {
// update position upon move
dxy = Utils.getCursorPosition(e);
};
function up(e) {
// if there is dragging, do not close/open accordion
if (dxy) {
dx = xy.x - dxy.x;
dy = xy.y - dxy.y;
dxy = undefined;
if ((dx*dx) + (dy*dy) > dragRadius*dragRadius) return;
}
// close or open item depending on active class
var $item = $(this).parent();
if ($item.hasClass('m-active')) {
close($item);
}
else {
open($item);
}
};
function click(e) {
e.preventDefault();
};
// Auto-open items that are hash linked
var hash = location.hash;
var $hashitem = $element.find('.m-header a[href="'+hash+'"]');
if ($hashitem.length) {
open($hashitem.parent());
}
// bind events
$element.find('.m-header')
.on(Utils.events.down, down)
.on(Utils.events.move, move)
.on(Utils.events.up, up)
.on('click', click);
if (Utils.events.transitionend) {
$element.find('.m-content').on(Utils.events.transitionend, endTransition);
}
};
Accordion.prototype.unbind = function() {
this.$element.off();
}
Accordion.prototype.destroy = function() {
this.unbind();
this.$element.remove();
this.$element = null;
}
return Accordion;
})(Mobify.$, Mobify.UI.Utils);
(function($) {
$.fn.accordion = function(action) {
this.each(function () {
var $this = $(this)
, accordion = $this._accordion
if (!accordion) {
accordion = new Mobify.UI.Accordion(this);
}
if (action) {
accordion[action]();
if (action === 'destroy') {
accordion = null;
}
}
$this._accordion = accordion;
})
return this;
};
})(Mobify.$);