Skip to content

Commit cae21cd

Browse files
committed
reorganised source, closes #27
1 parent dde46f9 commit cae21cd

File tree

1 file changed

+82
-58
lines changed

1 file changed

+82
-58
lines changed

jquery.matchHeight.js

+82-58
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,65 @@
55
*/
66

77
;(function($) {
8+
/*
9+
* internal
10+
*/
11+
12+
var _previousResizeWidth = -1,
13+
_updateTimeout = -1;
14+
15+
/*
16+
* _rows
17+
* utility function returns array of jQuery selections representing each row
18+
* (as displayed after float wrapping applied by browser)
19+
*/
20+
21+
var _rows = function(elements) {
22+
var tolerance = 1,
23+
$elements = $(elements),
24+
lastTop = null,
25+
rows = [];
26+
27+
// group elements by their top position
28+
$elements.each(function(){
29+
var $that = $(this),
30+
top = $that.offset().top - _parse($that.css('margin-top')),
31+
lastRow = rows.length > 0 ? rows[rows.length - 1] : null;
32+
33+
if (lastRow === null) {
34+
// first item on the row, so just push it
35+
rows.push($that);
36+
} else {
37+
// if the row top is the same, add to the row group
38+
if (Math.floor(Math.abs(lastTop - top)) <= tolerance) {
39+
rows[rows.length - 1] = lastRow.add($that);
40+
} else {
41+
// otherwise start a new row group
42+
rows.push($that);
43+
}
44+
}
45+
46+
// keep track of the last row top
47+
lastTop = top;
48+
});
49+
50+
return rows;
51+
};
52+
53+
/*
54+
* _parse
55+
* value parse utility function
56+
*/
57+
58+
var _parse = function(value) {
59+
// parse value and convert NaN to 0
60+
return parseFloat(value) || 0;
61+
};
62+
63+
/*
64+
* $.fn.matchHeight
65+
* plugin definition
66+
*/
867

968
$.fn.matchHeight = function(byRow) {
1069

@@ -43,6 +102,19 @@
43102
return this;
44103
};
45104

105+
/*
106+
* plugin global options
107+
*/
108+
109+
$.fn.matchHeight._groups = [];
110+
$.fn.matchHeight._throttle = 80;
111+
$.fn.matchHeight._maintainScroll = false;
112+
113+
/*
114+
* $.fn.matchHeight._apply
115+
* apply matchHeight to given elements
116+
*/
117+
46118
$.fn.matchHeight._apply = function(elements, byRow) {
47119
var $elements = $(elements),
48120
rows = [$elements];
@@ -134,7 +206,8 @@
134206
};
135207

136208
/*
137-
* _applyDataApi will apply matchHeight to all elements with a data-match-height attribute
209+
* $.fn.matchHeight._applyDataApi
210+
* applies matchHeight to all elements with a data-match-height attribute
138211
*/
139212

140213
$.fn.matchHeight._applyDataApi = function() {
@@ -158,36 +231,30 @@
158231
};
159232

160233
/*
161-
* _update function will re-apply matchHeight to all groups with the correct options
234+
* $.fn.matchHeight._update
235+
* updates matchHeight on all current groups with their correct options
162236
*/
163-
164-
$.fn.matchHeight._groups = [];
165-
$.fn.matchHeight._throttle = 80;
166-
$.fn.matchHeight._maintainScroll = false;
167-
168-
var previousResizeWidth = -1,
169-
updateTimeout = -1;
170237

171238
$.fn.matchHeight._update = function(event) {
172239
// prevent update if fired from a resize event
173240
// where the viewport width hasn't actually changed
174241
// fixes an event looping bug in IE8
175242
if (event && event.type === 'resize') {
176243
var windowWidth = $(window).width();
177-
if (windowWidth === previousResizeWidth)
244+
if (windowWidth === _previousResizeWidth)
178245
return;
179-
previousResizeWidth = windowWidth;
246+
_previousResizeWidth = windowWidth;
180247
}
181248

182249
// throttle updates
183-
if (updateTimeout === -1) {
184-
updateTimeout = setTimeout(function() {
250+
if (_updateTimeout === -1) {
251+
_updateTimeout = setTimeout(function() {
185252

186253
$.each($.fn.matchHeight._groups, function() {
187254
$.fn.matchHeight._apply(this.elements, this.byRow);
188255
});
189256

190-
updateTimeout = -1;
257+
_updateTimeout = -1;
191258

192259
}, $.fn.matchHeight._throttle);
193260
}
@@ -203,47 +270,4 @@
203270
// update heights on load and resize events
204271
$(window).bind('load resize orientationchange', $.fn.matchHeight._update);
205272

206-
/*
207-
* rows utility function
208-
* returns array of jQuery selections representing each row
209-
* (as displayed after float wrapping applied by browser)
210-
*/
211-
212-
var _rows = function(elements) {
213-
var tolerance = 1,
214-
$elements = $(elements),
215-
lastTop = null,
216-
rows = [];
217-
218-
// group elements by their top position
219-
$elements.each(function(){
220-
var $that = $(this),
221-
top = $that.offset().top - _parse($that.css('margin-top')),
222-
lastRow = rows.length > 0 ? rows[rows.length - 1] : null;
223-
224-
if (lastRow === null) {
225-
// first item on the row, so just push it
226-
rows.push($that);
227-
} else {
228-
// if the row top is the same, add to the row group
229-
if (Math.floor(Math.abs(lastTop - top)) <= tolerance) {
230-
rows[rows.length - 1] = lastRow.add($that);
231-
} else {
232-
// otherwise start a new row group
233-
rows.push($that);
234-
}
235-
}
236-
237-
// keep track of the last row top
238-
lastTop = top;
239-
});
240-
241-
return rows;
242-
};
243-
244-
var _parse = function(value) {
245-
// parse value and convert NaN to 0
246-
return parseFloat(value) || 0;
247-
};
248-
249-
})(jQuery);
273+
})(jQuery);

0 commit comments

Comments
 (0)