|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Chat Assistant Add-on |
| 4 | + * %% |
| 5 | + * Copyright (C) 2025 Flowing Code |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +(function () { |
| 21 | + window.Vaadin.Flow.fcChatAssistantConnector = { |
| 22 | + observePopoverResize: (popover) => { |
| 23 | + // Skip the following logic on mobile devices by checking viewport width. |
| 24 | + if (window.innerWidth <= 768) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + if (popover.$connector) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + popover.$connector = {}; |
| 33 | + |
| 34 | + // Find the resizable container inside the popover |
| 35 | + const resizableContainer = popover.querySelector('.chat-assistant-resizable-vertical-layout'); |
| 36 | + if (!resizableContainer) return; |
| 37 | + |
| 38 | + popover.addEventListener('opened-changed', e => { |
| 39 | + if (e.detail.value) { |
| 40 | + const popoverOverlay = resizableContainer.parentElement; |
| 41 | + const overlay = popoverOverlay.shadowRoot?.querySelector('[part="overlay"]'); |
| 42 | + // Track overlay position changes and keep container inside viewport |
| 43 | + trackOverlayPosition(overlay, resizableContainer, () => clampToViewport(resizableContainer)); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + // On drag/resize start (mouse), reset size restrictions so user can freely resize |
| 48 | + resizableContainer.addEventListener("mousedown", e => { |
| 49 | + resizableContainer.style.maxHeight = ''; |
| 50 | + resizableContainer.style.maxWidth = ''; |
| 51 | + }); |
| 52 | + // On drag/resize start (touch), reset size restrictions so user can freely resize |
| 53 | + resizableContainer.addEventListener("touchstart", e => { |
| 54 | + resizableContainer.style.maxHeight = ''; |
| 55 | + resizableContainer.style.maxWidth = ''; |
| 56 | + }); |
| 57 | + |
| 58 | + // Debounce calls to avoid excessive recalculations on rapid resize |
| 59 | + const debouncedClamp = debounce(() => clampToViewport(resizableContainer)); |
| 60 | + |
| 61 | + new ResizeObserver(() => { |
| 62 | + const popoverOverlay = resizableContainer.parentElement; |
| 63 | + const overlay = popoverOverlay.shadowRoot?.querySelector('[part="overlay"]'); |
| 64 | + if (!overlay) return; |
| 65 | + |
| 66 | + debouncedClamp(); |
| 67 | + }).observe(resizableContainer); |
| 68 | + |
| 69 | + |
| 70 | + function debounce(callback) { |
| 71 | + let rafId; |
| 72 | + return () => { |
| 73 | + cancelAnimationFrame(rafId); |
| 74 | + rafId = requestAnimationFrame(callback); |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Restricts the size and position of a resizable container so that it remains fully visible |
| 80 | + * within the browser's viewport, applying a small padding to keep it from touching the edges. |
| 81 | + * |
| 82 | + * This function calculates how much space is available on each side of the container |
| 83 | + * (top, bottom, left, right) relative to the viewport. If the container would overflow |
| 84 | + * on a given side, it adjusts `maxWidth`/`maxHeight` and aligns it to the opposite side |
| 85 | + * with a fixed padding. |
| 86 | + * |
| 87 | + * - If there isn't enough space on the right, it clamps width and aligns to the left. |
| 88 | + * - If there isn't enough space on the left, it clamps width and aligns to the right. |
| 89 | + * - If there isn't enough space at the bottom, it clamps height and aligns to the top. |
| 90 | + * - If there isn't enough space at the top, it clamps height and aligns to the bottom. |
| 91 | + * |
| 92 | + * @param {HTMLElement} resizableContainer - The element whose size and position should be clamped to the viewport. |
| 93 | + */ |
| 94 | + function clampToViewport(resizableContainer) { |
| 95 | + const boundingClientRect = resizableContainer.getBoundingClientRect(); |
| 96 | + |
| 97 | + const containerWidthRight = boundingClientRect.width + (window.innerWidth - boundingClientRect.right); |
| 98 | + const containerWidthLeft = boundingClientRect.left + boundingClientRect.width; |
| 99 | + const containerHeightBottom = boundingClientRect.height + (window.innerHeight - boundingClientRect.bottom); |
| 100 | + const containerHeightTop = boundingClientRect.top + boundingClientRect.height; |
| 101 | + |
| 102 | + const padding = 5; |
| 103 | + const paddingPx = padding + "px"; |
| 104 | + |
| 105 | + if (containerWidthRight >= window.innerWidth) { |
| 106 | + resizableContainer.style.maxWidth = (boundingClientRect.right - padding) + "px"; |
| 107 | + resizableContainer.style.left = paddingPx; |
| 108 | + } else if (containerWidthLeft >= window.innerWidth) { |
| 109 | + resizableContainer.style.maxWidth = (window.innerWidth - boundingClientRect.left - padding) + "px"; |
| 110 | + resizableContainer.style.right = paddingPx; |
| 111 | + } |
| 112 | + |
| 113 | + if (containerHeightBottom >= window.innerHeight) { |
| 114 | + resizableContainer.style.maxHeight = (boundingClientRect.bottom - padding) + "px"; |
| 115 | + resizableContainer.style.top = paddingPx; |
| 116 | + } else if (containerHeightTop >= window.innerHeight) { |
| 117 | + resizableContainer.style.maxHeight = (window.innerHeight - boundingClientRect.top - padding) + "px"; |
| 118 | + resizableContainer.style.bottom = paddingPx; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Continuously tracks the position of an overlay element and triggers a callback |
| 124 | + * when the overlay's position has stabilized (i.e., changes are within the given buffer). |
| 125 | + * |
| 126 | + * This function uses `requestAnimationFrame` to check the overlay's position every frame. |
| 127 | + * If the overlay moves more than `positionBuffer` pixels horizontally or vertically, |
| 128 | + * tracking continues without calling the callback. |
| 129 | + * Once the position changes are smaller than `positionBuffer`, the callback is invoked. |
| 130 | + * |
| 131 | + * @param {HTMLElement} overlay - The overlay element to track. Must support `.checkVisibility()`. |
| 132 | + * @param {HTMLElement} resizableContainer - The container related to the overlay (not used directly here, |
| 133 | + * but often used by the callback to adjust size). |
| 134 | + * @param {Function} callback - Function to call when the overlay position is stable. |
| 135 | + * @param {number} [positionBuffer=10] - The minimum pixel movement threshold before considering the overlay stable. |
| 136 | + */ |
| 137 | + function trackOverlayPosition(overlay, resizableContainer, callback, positionBuffer = 10) { |
| 138 | + let lastTop = 0; |
| 139 | + let lastLeft = 0; |
| 140 | + let frameId; |
| 141 | + |
| 142 | + function checkPosition() { |
| 143 | + if (!isVisible(overlay)) { |
| 144 | + cancelAnimationFrame(frameId); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + const rect = overlay.getBoundingClientRect(); |
| 149 | + const deltaTop = Math.abs(rect.top - lastTop); |
| 150 | + const deltaLeft = Math.abs(rect.left - lastLeft); |
| 151 | + if (deltaTop > positionBuffer || deltaLeft > positionBuffer) { |
| 152 | + lastTop = rect.top; |
| 153 | + lastLeft = rect.left; |
| 154 | + } else { |
| 155 | + callback(); |
| 156 | + } |
| 157 | + |
| 158 | + frameId = requestAnimationFrame(checkPosition); |
| 159 | + } |
| 160 | + |
| 161 | + frameId = requestAnimationFrame(checkPosition); |
| 162 | + } |
| 163 | + |
| 164 | + function isVisible(el) { |
| 165 | + if (!el) return false; |
| 166 | + |
| 167 | + if (typeof el.checkVisibility === 'function') { |
| 168 | + // Use native checkVisibility if available |
| 169 | + return el.checkVisibility(); |
| 170 | + } |
| 171 | + |
| 172 | + // Fallback: check CSS display and visibility |
| 173 | + const style = getComputedStyle(el); |
| 174 | + return style.display !== 'none' && style.visibility !== 'hidden'; |
| 175 | + } |
| 176 | + }, |
| 177 | + } |
| 178 | +})(); |
0 commit comments