2023-04-10 15:36:59 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2020-09-29 21:20:34 +00:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import { tracked } from '@glimmer/tracking';
|
2022-03-08 17:28:36 +00:00
|
|
|
import { next } from '@ember/runloop';
|
2020-09-29 21:20:34 +00:00
|
|
|
import { action } from '@ember/object';
|
|
|
|
import { minIndex, max } from 'd3-array';
|
|
|
|
|
|
|
|
export default class FlexMasonry extends Component {
|
|
|
|
@tracked element = null;
|
|
|
|
|
|
|
|
@action
|
|
|
|
captureElement(element) {
|
|
|
|
this.element = element;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
reflow() {
|
2022-03-08 17:28:36 +00:00
|
|
|
next(() => {
|
2020-10-23 20:54:09 +00:00
|
|
|
// There's nothing to do if there is no element
|
|
|
|
if (!this.element) return;
|
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
const items = this.element.querySelectorAll(
|
|
|
|
':scope > .flex-masonry-item'
|
|
|
|
);
|
2020-10-23 20:54:09 +00:00
|
|
|
|
|
|
|
// Clear out specified order and flex-basis values in case this was once a multi-column layout
|
|
|
|
if (this.args.columns === 1 || !this.args.columns) {
|
|
|
|
for (let item of items) {
|
|
|
|
item.style.flexBasis = null;
|
|
|
|
item.style.order = null;
|
|
|
|
}
|
|
|
|
this.element.style.maxHeight = null;
|
|
|
|
return;
|
|
|
|
}
|
2020-09-29 21:20:34 +00:00
|
|
|
|
|
|
|
const columns = new Array(this.args.columns).fill(null).map(() => ({
|
|
|
|
height: 0,
|
|
|
|
elements: [],
|
|
|
|
}));
|
|
|
|
|
|
|
|
// First pass: assign each element to a column based on the running heights of each column
|
|
|
|
for (let item of items) {
|
|
|
|
const styles = window.getComputedStyle(item);
|
|
|
|
const marginTop = parseFloat(styles.marginTop);
|
|
|
|
const marginBottom = parseFloat(styles.marginBottom);
|
|
|
|
const height = item.clientHeight;
|
|
|
|
|
|
|
|
// Pick the shortest column accounting for margins
|
2021-12-28 14:45:20 +00:00
|
|
|
const column = columns[minIndex(columns, (c) => c.height)];
|
2020-09-29 21:20:34 +00:00
|
|
|
|
|
|
|
// Add the new element's height to the column height
|
|
|
|
column.height += marginTop + height + marginBottom;
|
|
|
|
column.elements.push(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second pass: assign an order to each element based on their column and position in the column
|
|
|
|
columns
|
|
|
|
.mapBy('elements')
|
|
|
|
.flat()
|
|
|
|
.forEach((dc, index) => {
|
|
|
|
dc.style.order = index;
|
|
|
|
});
|
|
|
|
|
2020-10-08 05:18:43 +00:00
|
|
|
// Guarantee column wrapping as predicted (if the first item of a column is shorter than the difference
|
2020-09-29 21:20:34 +00:00
|
|
|
// beteen the height of the column and the previous column, then flexbox will naturally place the first
|
|
|
|
// item at the end of the previous column).
|
|
|
|
columns.forEach((column, index) => {
|
2021-12-28 16:08:12 +00:00
|
|
|
const nextHeight =
|
|
|
|
index < columns.length - 1 ? columns[index + 1].height : 0;
|
2020-09-29 21:20:34 +00:00
|
|
|
const item = column.elements.lastObject;
|
|
|
|
if (item) {
|
2021-12-28 16:08:12 +00:00
|
|
|
item.style.flexBasis =
|
|
|
|
item.clientHeight + Math.max(0, nextHeight - column.height) + 'px';
|
2020-09-29 21:20:34 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Set the max height of the container to the height of the tallest column
|
|
|
|
this.element.style.maxHeight = max(columns.mapBy('height')) + 1 + 'px';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|