-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmasonry-layout.js
More file actions
131 lines (108 loc) · 3.95 KB
/
masonry-layout.js
File metadata and controls
131 lines (108 loc) · 3.95 KB
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
// Masonry layout implementation
function applyMasonryLayout() {
const container = document.getElementById('windowsContainer');
if (!container || !container.classList.contains('masonry-container')) {
console.log('Masonry: Container not found or not masonry');
return;
}
const cards = Array.from(container.querySelectorAll('.window-card'));
if (cards.length === 0) {
console.log('Masonry: No cards found');
return;
}
console.log('Masonry: Applying layout to', cards.length, 'cards');
// Get number of columns based on view mode
let columns = 3;
if (container.classList.contains('list-view')) {
columns = 1;
} else if (container.classList.contains('full-grid-view')) {
columns = 4;
}
// Responsive columns
if (window.innerWidth <= 768) {
columns = 1;
} else if (window.innerWidth <= 1200 && columns > 2) {
columns = 2;
}
const gap = 24;
const containerWidth = container.offsetWidth;
const columnWidth = (containerWidth - (gap * (columns - 1))) / columns;
console.log('Masonry: Container width:', containerWidth, 'Column width:', columnWidth, 'Columns:', columns);
// Initialize column heights
const columnHeights = new Array(columns).fill(0);
// Set container to relative positioning
container.style.position = 'relative';
// Process cards in sequence to avoid race conditions
cards.forEach((card, index) => {
// Reset any existing positioning
card.style.position = 'absolute';
card.style.width = columnWidth + 'px';
card.style.transition = 'all 0.3s ease';
// Get card height after width is set
const cardHeight = card.offsetHeight;
// Find shortest column
let shortestColumn = 0;
let minHeight = columnHeights[0];
for (let i = 1; i < columns; i++) {
if (columnHeights[i] < minHeight) {
minHeight = columnHeights[i];
shortestColumn = i;
}
}
// Set card position
card.style.left = (shortestColumn * (columnWidth + gap)) + 'px';
card.style.top = columnHeights[shortestColumn] + 'px';
console.log('Masonry: Card', index, 'height:', cardHeight, 'column:', shortestColumn, 'top:', columnHeights[shortestColumn]);
// Update column height
columnHeights[shortestColumn] += cardHeight + gap;
});
// Set container height
const maxHeight = Math.max(...columnHeights);
container.style.height = maxHeight + 'px';
console.log('Masonry: Final container height:', maxHeight);
}
// Debounced resize function
let resizeTimeout;
function debouncedResize() {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(applyMasonryLayout, 250);
}
// Initialize masonry when page loads
document.addEventListener('DOMContentLoaded', () => {
console.log('Masonry: DOM loaded, setting up listeners');
// Apply initial layout
setTimeout(applyMasonryLayout, 500);
// Listen for custom event from manager-masonry.js
document.addEventListener('windowsRendered', () => {
console.log('Masonry: Windows rendered event received');
setTimeout(applyMasonryLayout, 100);
});
// Reapply on window resize
window.addEventListener('resize', debouncedResize);
// Watch for layout changes
const observer = new MutationObserver((mutations) => {
let shouldReapply = false;
mutations.forEach(mutation => {
if (mutation.type === 'attributes' &&
(mutation.attributeName === 'class' || mutation.attributeName === 'style')) {
shouldReapply = true;
}
});
if (shouldReapply) {
console.log('Masonry: Layout change detected, reapplying');
setTimeout(applyMasonryLayout, 100);
}
});
// Start observing after a delay
setTimeout(() => {
const container = document.getElementById('windowsContainer');
if (container) {
observer.observe(container, {
attributes: true,
attributeFilter: ['class', 'style'],
subtree: false
});
console.log('Masonry: Observer started');
}
}, 1000);
});