-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.stretch.js
81 lines (70 loc) · 2.36 KB
/
jquery.stretch.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
(function() {
var $, accumulateHeight, addHeight, difference, expand, expandAll, getSpace, options, stretch;
var __hasProp = Object.prototype.hasOwnProperty, __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (__hasProp.call(this, i) && this[i] === item) return i; } return -1; };
$ = jQuery;
expand = function(elm, height) {
var mySpace;
elm = $(elm);
mySpace = getSpace(elm);
elm.css(options.mode, height - mySpace);
if (options.fixOverflow) elm.css("overflow", "auto");
};
expandAll = function(elms, totalHeight) {
var elm, height, _i, _len;
height = totalHeight / elms.length;
for (_i = 0, _len = elms.length; _i < _len; _i++) {
elm = elms[_i];
expand(elm, height);
}
};
addHeight = function(memo, curr) {
return memo + $(curr).outerHeight();
};
accumulateHeight = function(elms) {
var elm, h, _i, _len;
h = 0;
for (_i = 0, _len = elms.length; _i < _len; _i++) {
elm = elms[_i];
h = h + $(elm).outerHeight();
}
return h;
};
getSpace = function(elm) {
var aspect, aspects, result, tentativeDim, _i, _len;
result = 0;
aspects = ["margin-top", "margin-bottom", "padding-top", "padding-bottom"];
for (_i = 0, _len = aspects.length; _i < _len; _i++) {
aspect = aspects[_i];
tentativeDim = parseInt(elm.css(aspect));
if (tentativeDim) result += tentativeDim;
}
return result;
};
difference = function(array1, array2) {
var value, _i, _len, _results;
_results = [];
for (_i = 0, _len = array1.length; _i < _len; _i++) {
value = array1[_i];
if (__indexOf.call(array2, value) < 0) _results.push(value);
}
return _results;
};
options = {};
stretch = function(userOptions) {
var fixedHeight, heightProvider, nonVs, parent, parentHeight, self, space;
self = $(this);
options = {
mode: "height",
fixOverflow: true
};
options = $.extend(options, userOptions);
parent = self.parent();
space = getSpace(parent);
heightProvider = $(options.heightProvider || parent);
parentHeight = heightProvider.height() - space;
nonVs = difference(parent.children(":visible"), self);
fixedHeight = accumulateHeight(nonVs);
return expandAll(self, parentHeight - fixedHeight);
};
$.fn.stretch = stretch;
}).call(this);