-
Notifications
You must be signed in to change notification settings - Fork 2
/
leveller.js
139 lines (116 loc) · 3.95 KB
/
leveller.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
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// Node/CommonJS
factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
// Constructor
// ===========
var Leveller = function (elements, options) {
this.$elements = $(elements);
this.options = $.extend({}, Leveller.DEFAULTS, options);
};
Leveller.DATA_KEY = 'plugin_leveller';
Leveller.DEFAULTS = {
level: true,
resetBefore: true,
cssProperty: 'min-height',
heightMethod: 'outerHeight',
offsetMethod: 'offset',
alignment: 'top'
};
Leveller.prototype.level = function () {
var i = 0;
var perRow = this.options.columns || this.getPerRow();
if (this.options.resetBefore) {
this.reset();
}
for ( ; i < this.$elements.length; i += perRow) {
this.adjustElements(this.$elements.slice(i, i + perRow));
}
};
Leveller.prototype.reset = function () {
var $styleElements = this.options.cssSelector ? this.$elements.find(this.options.cssSelector) : this.$elements;
$styleElements.css(this.options.cssProperty, '');
};
Leveller.prototype.adjustElements = function ($elements) {
var i = 0;
var targetHeight = this.getTallestHeight($elements);
for ( ; i < $elements.length; i++) {
this.adjustElement($elements.eq(i), targetHeight);
}
};
Leveller.prototype.adjustElement = function ($element, targetHeight) {
var currentHeight = $element[this.options.heightMethod]();
var diff = targetHeight - currentHeight;
if (diff === 0) return;
var isHeightProperty = (this.options.cssProperty.indexOf('eight') > 0);
var $styleElement = this.options.cssSelector ? $element.find(this.options.cssSelector) : $element;
var styleValue;
if (isHeightProperty && this.options.cssSelector) {
styleValue = $styleElement[this.options.heightMethod]() + diff;
} else if (isHeightProperty) {
styleValue = targetHeight;
} else {
styleValue = parseInt($styleElement.css(this.options.cssProperty), 10) + diff;
}
if (typeof this.options.adjustBy === 'string') {
styleValue += parseInt($element.css(this.options.adjustBy), 10);
} else if (typeof this.options.adjustBy === "number") {
styleValue += this.options.adjustBy;
}
$styleElement.css(this.options.cssProperty, styleValue);
};
Leveller.prototype.getPerRow = function () {
var i = 0;
var offset = {};
var $element, current, last;
for ( ; i < this.$elements.length; i++) {
$element = this.$elements.eq(i);
offset = $element[this.options.offsetMethod]();
current = offset.top;
if (this.options.alignment === 'middle') {
current += $element[this.options.heightMethod]() / 2;
} else if (this.options.alignment === 'bottom') {
current += $element[this.options.heightMethod]();
}
if (typeof last !== 'undefined' && current > last) {
break;
}
last = current;
}
return i;
};
Leveller.prototype.getTallestHeight = function ($elements) {
var heightMethod = this.options.heightMethod;
$elements = $elements || this.$elements;
var heights = $.map($elements, function (element, i) {
return $(element)[heightMethod]();
});
return Math.max.apply(null, heights);
};
// Plugin
// ======
function Plugin (option) {
var data = this.data(Leveller.DATA_KEY);
var options = $.extend({}, Leveller.DEFAULTS, typeof option === 'object' && option);
if (!data) {
if (options.level && option === 'reset') options.level = false;
this.data(Leveller.DATA_KEY, (data = new Leveller(this, options)));
}
if (typeof option === 'string') {
data[option]();
} else if (options.level) {
data.level();
}
return this;
}
$.fn.leveller = Plugin;
$.fn.leveller.Constructor = Leveller;
}));