Electra — Sarah Brower (function() { let resizeGallery = function(document) { let getLeft = function(node) { return parseInt($(node).css("left")); } let getWidth = function(node) { return parseInt($(node).css("width")); } let getImageNodesByCol = function(document) { var nodes = document.getElementsByClassName("slide sqs-gallery-design-autocolumns-slide"); var nodesByCol = []; var offsets = []; for (var nodeIndex = 0; nodeIndex < nodes.length; nodeIndex++) { node = nodes[nodeIndex]; offset = getLeft(node); if (offsets.length == 0 || offset > offsets[offsets.length - 1]) { offsets.push(offset); nodesByCol.push([node]); } else { for (var colIdx = 0; colIdx < offsets.length; colIdx++) { if (offset === offsets[colIdx]) { nodesByCol[colIdx].push(node) break; } } } } return nodesByCol; } let getPadding = function(nodesByCol) { return getLeft(nodesByCol[1][0]) - getWidth(nodesByCol[0][0]) - getLeft(nodesByCol[0][0]); } let getNodeImgDimensions = function(node) { var dimStr = $(node).find("img").attr("data-image-dimensions").split("x"); return [parseInt(dimStr[0]), parseInt(dimStr[1])]; } let getNodeAspectRatio = function(node) { var dimensions = getNodeImgDimensions(node); return dimensions[0] / dimensions[1]; } let getAspectRatioByColumn = function(nodesByCol) { var numCols = nodesByCol.length; var aspectRatioByColumn = new Array(numCols); for (var col = 0; col < numCols; col++) { var sumNormalizedHeight = 0; for (var nodeIndex = 0; nodeIndex < nodesByCol[col].length; nodeIndex++) { sumNormalizedHeight += 1.0 / getNodeAspectRatio(nodesByCol[col][nodeIndex]); } aspectRatioByColumn[col] = 1.0 / sumNormalizedHeight; } return aspectRatioByColumn; } let getColumnSizes = function(nodesByCol, totalWidth, padding) { var aspectRatioByColumn = getAspectRatioByColumn(nodesByCol); var numCols = nodesByCol.length; var sumAdjAspectRatio = 0.0; var sumAspectRatio = 0.0; for (var col = 0; col < numCols; col++) { sumAspectRatio += aspectRatioByColumn[col]; sumAdjAspectRatio += aspectRatioByColumn[col] * (nodesByCol[col].length - 1); } var adjustedWidth = totalWidth - (numCols - 1)*padding; var finalHeight = Math.floor((adjustedWidth + sumAdjAspectRatio*padding) / sumAspectRatio); var widthByCol = [] for (var col = 0; col < numCols; col++) { var heightNoPad = finalHeight - padding*(nodesByCol[col].length - 1); widthByCol.push(Math.round(heightNoPad * aspectRatioByColumn[col])); } return [finalHeight, widthByCol]; } let getNodeValuesByColumn = function(nodesByCol, widthByCol, totalHeight, padding) { var nodeValsByCol = new Array(widthByCol.length); var leftOffset = 0; for (var col = 0; col < nodesByCol.length; col++) { nodeValsByCol[col] = new Array(nodesByCol[col].length); var topOffset = 0.0; for (var nodeIdx = 0; nodeIdx < nodesByCol[col].length; nodeIdx++) { var aspectRatio = getNodeAspectRatio(nodesByCol[col][nodeIdx]); var nodeHeight = widthByCol[col] / aspectRatio; if (nodeIdx == nodesByCol[col].length - 1) { topOffset = (totalHeight - nodeHeight); } nodeValsByCol[col][nodeIdx] = [topOffset, leftOffset, widthByCol[col], nodeHeight]; topOffset += nodeHeight + padding; } leftOffset += widthByCol[col] + padding; } return nodeValsByCol; } var nodesByCol = getImageNodesByCol(document); if (nodesByCol.length <= 1) { return; } var padding = getPadding(nodesByCol); var totalWidth = $(".gallery-wrapper").width(); const [totalHeight, widthByCol] = getColumnSizes(nodesByCol, totalWidth, padding); var nodeValsByCol = getNodeValuesByColumn(nodesByCol, widthByCol, totalHeight, padding); for (var col = 0; col < nodesByCol.length; col++) { for (var nodeIdx = 0; nodeIdx < nodesByCol[col].length; nodeIdx++) { node = $(nodesByCol[col][nodeIdx]); nodeVals = nodeValsByCol[col][nodeIdx]; node.css("top", nodeVals[0] + "px"); node.css("left", nodeVals[1] + "px"); node.css("width", nodeVals[2] + "px"); node.css("height", nodeVals[3] + "px"); } } $(".sqs-gallery-design-autocolumns").css("height", totalHeight + "px"); } let resizeGalleryWithScrollbar = function(document) { var totalWidthStart = $(".gallery-wrapper").width(); resizeGallery(window.document); // Kind of an ugly hack, but if the resizing eliminated the scrollbar, the width changes and we need // to recalculate. if (totalWidthStart != $(".gallery-wrapper").width()) { resizeGallery(window.document); } } if (!Y.SQS.Gallery.ResizeEmitter.prototype._oldHandleResize) { Y.SQS.Gallery.ResizeEmitter.prototype._oldHandleResize = Y.SQS.Gallery.ResizeEmitter.prototype._handleResize; } Y.SQS.Gallery.ResizeEmitter.prototype._handleResize = function() { this._oldHandleResize(); resizeGalleryWithScrollbar(window.document); } if (!Y.SQS.Gallery.Gallery2.prototype._oldInitializer) { Y.SQS.Gallery.Gallery2.prototype._oldInitializer = Y.SQS.Gallery.Gallery2.prototype.initializer; } Y.SQS.Gallery.Gallery2.prototype.initializer = function() { this._oldInitializer(); resizeGalleryWithScrollbar(window.document); } })();