-
Notifications
You must be signed in to change notification settings - Fork 30
/
minimasonry.esm.js
236 lines (197 loc) · 7.2 KB
/
minimasonry.esm.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
var MiniMasonry = function(conf) {
this._sizes = [];
this._columns = [];
this._container = null;
this._count = null;
this._width = 0;
this._removeListener = null;
this._currentGutterX = null;
this._currentGutterY = null;
this._resizeTimeout = null,
this.conf = {
baseWidth: 255,
gutterX: null,
gutterY: null,
gutter: 10,
container: null,
minify: true,
ultimateGutter: 5,
surroundingGutter: true,
direction: 'ltr',
wedge: false
};
this.init(conf);
return this;
};
MiniMasonry.prototype.init = function(conf) {
for (var i in this.conf) {
if (conf[i] != undefined) {
this.conf[i] = conf[i];
}
}
if (this.conf.gutterX == null || this.conf.gutterY == null) {
this.conf.gutterX = this.conf.gutterY = this.conf.gutter;
}
this._currentGutterX = this.conf.gutterX;
this._currentGutterY = this.conf.gutterY;
console.log(this._currentGutterX);
this._container = typeof this.conf.container == 'object' && this.conf.container.nodeName ?
this.conf.container :
document.querySelector(this.conf.container);
if (!this._container) {
throw new Error('Container not found or missing');
}
var onResize = this.resizeThrottler.bind(this);
window.addEventListener("resize", onResize);
this._removeListener = function() {
window.removeEventListener("resize", onResize);
};
this.layout();
};
MiniMasonry.prototype.reset = function() {
this._sizes = [];
this._columns = [];
this._count = null;
this._width = this._container.clientWidth;
var minWidth = this.conf.baseWidth;
if (this._width < minWidth) {
this._width = minWidth;
this._container.style.minWidth = minWidth + 'px';
}
if (this.getCount() == 1) {
// Set ultimate gutter when only one column is displayed
this._currentGutterX = this.conf.ultimateGutter;
// As gutters are reduced, two column may fit, forcing to 1
this._count = 1;
} else if (this._width < (this.conf.baseWidth + (2 * this._currentGutterX))) {
// Remove gutter when screen is to low
this._currentGutterX = 0;
} else {
this._currentGutterX = this.conf.gutterX;
}
};
MiniMasonry.prototype.getCount = function() {
if (this.conf.surroundingGutter) {
return Math.floor((this._width - this._currentGutterX) / (this.conf.baseWidth + this._currentGutterX));
}
return Math.floor((this._width + this._currentGutterX) / (this.conf.baseWidth + this._currentGutterX));
};
MiniMasonry.prototype.computeWidth = function() {
var width;
if (this.conf.surroundingGutter) {
width = ((this._width - this._currentGutterX) / this._count) - this._currentGutterX;
} else {
width = ((this._width + this._currentGutterX) / this._count) - this._currentGutterX;
}
width = Number.parseFloat(width.toFixed(2));
return width;
};
MiniMasonry.prototype.layout = function() {
if (!this._container) {
console.error('Container not found');
return;
}
this.reset();
//Computing columns count
if (this._count == null) {
this._count = this.getCount();
}
//Computing columns width
var colWidth = this.computeWidth();
for (var i = 0; i < this._count; i++) {
this._columns[i] = 0;
}
//Saving children real heights
var children = this._container.children;
for (var k = 0;k< children.length; k++) {
// Set colWidth before retrieving element height if content is proportional
children[k].style.width = colWidth + 'px';
this._sizes[k] = children[k].clientHeight;
}
var startX;
if (this.conf.direction == 'ltr') {
startX = this.conf.surroundingGutter ? this._currentGutterX : 0;
} else {
startX = this._width - (this.conf.surroundingGutter ? this._currentGutterX : 0);
}
if (this._count > this._sizes.length) {
//If more columns than children
var occupiedSpace = (this._sizes.length * (colWidth + this._currentGutterX)) - this._currentGutterX;
if (this.conf.wedge === false) {
if (this.conf.direction == 'ltr') {
startX = ((this._width - occupiedSpace) / 2);
} else {
startX = this._width - ((this._width - occupiedSpace) / 2);
}
} else {
if (this.conf.direction == 'ltr') ; else {
startX = this._width - this._currentGutterX;
}
}
}
//Computing position of children
for (var index = 0;index < children.length; index++) {
var nextColumn = this.conf.minify ? this.getShortest() : this.getNextColumn(index);
var childrenGutter = 0;
if (this.conf.surroundingGutter || nextColumn != this._columns.length) {
childrenGutter = this._currentGutterX;
}
var x;
if (this.conf.direction == 'ltr') {
x = startX + ((colWidth + childrenGutter) * (nextColumn));
} else {
x = startX - ((colWidth + childrenGutter) * (nextColumn)) - colWidth;
}
var y = this._columns[nextColumn];
children[index].style.transform = 'translate3d(' + Math.round(x) + 'px,' + Math.round(y) + 'px,0)';
this._columns[nextColumn] += this._sizes[index] + (this._count > 1 ? this.conf.gutterY : this.conf.ultimateGutter);//margin-bottom
}
this._container.style.height = (this._columns[this.getLongest()] - this._currentGutterY) + 'px';
};
MiniMasonry.prototype.getNextColumn = function(index) {
return index % this._columns.length;
};
MiniMasonry.prototype.getShortest = function() {
var shortest = 0;
for (var i = 0; i < this._count; i++) {
if (this._columns[i] < this._columns[shortest]) {
shortest = i;
}
}
return shortest;
};
MiniMasonry.prototype.getLongest = function() {
var longest = 0;
for (var i = 0; i < this._count; i++) {
if (this._columns[i] > this._columns[longest]) {
longest = i;
}
}
return longest;
};
MiniMasonry.prototype.resizeThrottler = function() {
// ignore resize events as long as an actualResizeHandler execution is in the queue
if ( !this._resizeTimeout ) {
this._resizeTimeout = setTimeout(function() {
this._resizeTimeout = null;
//IOS Safari throw random resize event on scroll, call layout only if size has changed
if (this._container.clientWidth != this._width) {
this.layout();
}
// The actualResizeHandler will execute at a rate of 30fps
}.bind(this), 33);
}
};
MiniMasonry.prototype.destroy = function() {
if (typeof this._removeListener == "function") {
this._removeListener();
}
var children = this._container.children;
for (var k = 0;k< children.length; k++) {
children[k].style.removeProperty('width');
children[k].style.removeProperty('transform');
}
this._container.style.removeProperty('height');
this._container.style.removeProperty('min-width');
};
export { MiniMasonry as default };