Skip to content

Commit

Permalink
feat: Add snap to grid effect on layout
Browse files Browse the repository at this point in the history
Signed-off-by: felix.gateru <[email protected]>
  • Loading branch information
felixgateru committed Feb 27, 2024
1 parent d34175f commit 0250027
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
8 changes: 8 additions & 0 deletions ui/web/static/css/dashboards.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ SPDX-License-Identifier: Apache-2.0 */

.grid {
position: relative;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(18rem, 1fr));
grid-gap: 1rem;
padding: 1rem;
}

.item {
Expand Down Expand Up @@ -108,3 +112,7 @@ SPDX-License-Identifier: Apache-2.0 */
.min-vh-50 {
min-height: 50vh;
}

.resize-helper {
border: 10px dotted #000; /* Customize as needed */
}
23 changes: 14 additions & 9 deletions ui/web/static/js/singleDashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
var gridClass = ".grid";
var grid = initGrid(layout);
const gridSize = 50;

// Editable canvas is used to make the canvas editable allowing the user to add widgets and be able to move the
// widgets around the canvas
Expand Down Expand Up @@ -261,9 +262,13 @@ const previousSizes = new Map();
const resizeObserver = new ResizeObserver((entries) => {
for (let entry of entries) {
const { target } = entry;

let snappedWidth = Math.round(target.clientWidth / gridSize) * gridSize;
let snappedHeight = Math.round(target.clientHeight / gridSize) * gridSize;

const previousSize = previousSizes.get(target) || {
width: target.clientWidth,
height: target.clientHeight,
width: snappedWidth,
height: snappedHeight,
};
const contentEl = target.querySelector(".item-content");
const gridRightPosition = target.parentNode.getBoundingClientRect().right;
Expand All @@ -277,23 +282,23 @@ const resizeObserver = new ResizeObserver((entries) => {
target.style.maxHeight = "none";
}

if (widgetRightPosition < gridRightPosition - 5) {
if (widgetRightPosition < gridRightPosition - gridSize) {
// Calculate the change in width and height
var widthChange = target.clientWidth - previousSize.width;
var heightChange = target.clientHeight - previousSize.height;
var widthChange = snappedWidth - previousSize.width;
var heightChange = snappedHeight - previousSize.height;
var itemContentWidth =
parseInt(window.getComputedStyle(contentEl).getPropertyValue("width")) + widthChange;
var itemContentHeight =
parseInt(window.getComputedStyle(contentEl).getPropertyValue("height")) + heightChange;

// Update the previous size for the next callback
previousSizes.set(target, {
width: target.clientWidth,
height: target.clientHeight,
width: snappedWidth,
height: snappedHeight,
});

target.style.width = target.clientWidth + "px";
target.style.height = target.clientHeight + "px";
target.style.width = snappedWidth + "px";
target.style.height = snappedHeight + "px";

contentEl.style.width = itemContentWidth + "px";
contentEl.style.height = itemContentHeight + "px";
Expand Down
3 changes: 3 additions & 0 deletions ui/web/templates/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
crossorigin="anonymous"
referrerpolicy="no-referrer"
></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" />
<script src="//code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
</head>
<body>
{{ template "navbar" . }}
Expand Down

0 comments on commit 0250027

Please sign in to comment.