-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathindex.js
276 lines (227 loc) · 6.08 KB
/
index.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* @author emmanuelolaojo
* @since 11/10/18
*
* The MagicGrid class is an
* implementation of a flexible
* grid layout.
*/
const EventEmitter = require("./event-emitter.js");
const {checkParams, getMin} = require("./utils.js");
const {READY_EVENT, POSITIONING_COMPLETE_EVENT, REPOSITIONING_DELAY} = require("./constant.js");
class MagicGrid extends EventEmitter{
/**
* Initializes the necessary variables
* for a magic grid.
*
* @param config - configuration object
*/
constructor (config) {
super();
checkParams(config);
if (config.container instanceof HTMLElement) {
this.container = config.container;
this.containerClass = config.container.className;
}
else {
this.containerClass = config.container;
this.container = document.querySelector(config.container);
}
this.static = config.static || false;
this.size = config.items;
this.gutter = config.gutter;
this.maxColumns = config.maxColumns || false;
this.useMin = config.useMin || false;
this.useTransform = config.useTransform;
this.animate = config.animate || false;
this.center = config.center;
this.styledItems = new Set();
this.resizeObserver = null;
this.isPositioning = false;
}
/**
* Set a new container. Useful in cases where
* the container reference changes for any reason.
*
* @param container {HTMLElement}
*/
setContainer(container){
const previousContainer = this.container;
this.container = container;
this.resizeObserver.unobserve(previousContainer);
this.resizeObserver.observe(container);
}
/**
* Initializes styles
*
* @private
*/
initStyles () {
if (!this.ready()) return;
this.container.style.position = "relative";
const items = this.items();
for (let i = 0; i < items.length; i++) {
if (this.styledItems.has(items[i])) continue;
let style = items[i].style;
style.position = "absolute";
if (this.animate) {
style.transition = `${this.useTransform ? "transform" : "top, left"} 0.2s ease`;
}
this.styledItems.add(items[i]);
}
}
/**
* Gets a collection of all items in a grid.
*
* @return {HTMLCollection}
* @private
*/
items () {
return this.container.children;
}
/**
* Calculates the width of a column.
*
* @return width of a column in the grid
* @private
*/
colWidth () {
return this.items()[0].getBoundingClientRect().width + this.gutter;
}
/**
* Initializes an array of empty columns
* and calculates the leftover whitespace.
*
* @return {{cols: Array, wSpace: number}}
* @private
*/
setup () {
let width = this.container.getBoundingClientRect().width;
let colWidth = this.colWidth();
let numCols = Math.floor(width/colWidth) || 1;
let cols = [];
if (this.maxColumns && numCols > this.maxColumns) {
numCols = this.maxColumns;
}
for (let i = 0; i < numCols; i++) {
cols[i] = {height: 0, index: i};
}
let wSpace = width - numCols * colWidth + this.gutter;
return {cols, wSpace};
}
/**
* Gets the next available column.
*
* @param cols list of columns
* @param i index of dom element
*
* @return {*} next available column
* @private
*/
nextCol (cols, i) {
if (this.useMin) {
return getMin(cols);
}
return cols[i % cols.length];
}
/**
* Positions each item in the grid, based
* on their corresponding column's height
* and index then stretches the container to
* the height of the grid.
*/
positionItems () {
if(this.isPositioning){
return;
}
this.isPositioning = true;
let { cols, wSpace } = this.setup();
let maxHeight = 0;
let colWidth = this.colWidth();
let items = this.items();
wSpace = this.center ? Math.floor(wSpace / 2) : 0;
this.initStyles();
for (let i = 0; i < items.length; i++) {
let col = this.nextCol(cols, i);
let item = items[i];
let topGutter = col.height ? this.gutter : 0;
let left = col.index * colWidth + wSpace + "px";
let top = col.height + topGutter + "px";
if(this.useTransform){
item.style.transform = `translate(${left}, ${top})`;
}
else{
item.style.top = top;
item.style.left = left;
}
col.height += item.getBoundingClientRect().height + topGutter;
if(col.height > maxHeight){
maxHeight = col.height;
}
}
this.container.style.height = maxHeight + this.gutter + "px";
this.isPositioning = false;
this.emit(POSITIONING_COMPLETE_EVENT);
}
/**
* Checks if every item has been loaded
* in the dom.
*
* @return {Boolean} true if every item is present
*/
ready () {
if (this.static) return true;
return this.items().length >= this.size;
}
/**
* Periodically checks that all items
* have been loaded in the dom. Calls
* this.listen() once all the items are
* present.
*
* @private
*/
getReady () {
let interval = setInterval(() => {
this.container = document.querySelector(this.containerClass);
if (this.ready()) {
clearInterval(interval);
this.listen();
}
}, 100);
}
observeContainerResize() {
if (this.resizeObserver) return;
this.resizeObserver = new ResizeObserver(() => {
setTimeout(() => {
this.positionItems();
}, REPOSITIONING_DELAY);
});
this.resizeObserver.observe(this.container);
}
/**
* Positions all the items and
* repositions them whenever the
* window size changes.
*/
listen () {
if (this.ready()) {
window.addEventListener("resize", () => {
setTimeout(() => {
this.positionItems();
}, REPOSITIONING_DELAY);
});
this.observeContainerResize();
this.positionItems();
this.emit(READY_EVENT);
}
else this.getReady();
}
onReady(callback) {
return this.addListener(READY_EVENT, callback);
}
onPositionComplete(callback) {
return this.addListener(POSITIONING_COMPLETE_EVENT, callback);
}
}
module.exports = MagicGrid;