mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 08:18:42 +00:00
* feat: redesign sidebar workspace navigation * feat: redesign the sidebar footer and account menu Match the account menu to the sidebar prototype and reuse the admin navigation in a submenu. Keep Add new above the footer while the application list scrolls. * fix: show the sortable indicator only when dragging * feat: refine workspace sidebar application navigation * feat: reorganize workspace sidebar shortcuts * fix: isolate workspace sidebar menus and actions * fix: cancel sortable drops on hidden lists * fix(enterprise): allow retrying Direct support * feat: simplify the workspace overview header * fix: prevent horizontal overflow around the sidebar Constrain animated tree groups to the width of the sidebar so long labels truncate. On narrow screens, let the sidebar expand over the content instead of reducing the usable page width. * fix: fit workspace cards and creation menus on mobile * test: keep the current table selected after reload * fix: undo and redo edits across expanded sidebar applications * fix: keep sidebar groups out of the tab order * fix: highlight the active database in the tables tour * feat: label workspace integrations in the settings menu * fix: keep the workspace sidebar and guided tour usable on phones * fix: undo sidebar row imports in the destination database * fix: keep the guided tour highlight on the current step * fix: show an empty state in the workspace picker * fix: confirm workspace deletion before moving it to trash * feat: open registered workspace settings tabs from the sidebar * fix: keep the sidebar logo the same size across workspaces --------- Co-authored-by: peter_baserow <peter@baserow.io>
628 lines
17 KiB
Vue
628 lines
17 KiB
Vue
<template>
|
|
<Teleport to="body">
|
|
<div
|
|
ref="contextEl"
|
|
v-auto-overflow-scroll="open && overflowScroll"
|
|
v-bind="$attrs"
|
|
class="context"
|
|
:class="{
|
|
'visibility-hidden': !open || !updatedOnce,
|
|
'context--overflow-scroll': overflowScroll,
|
|
}"
|
|
>
|
|
<slot v-if="openedOnce"></slot>
|
|
</div>
|
|
</Teleport>
|
|
</template>
|
|
|
|
<script>
|
|
import { clamp } from '@baserow/modules/core/utils/number'
|
|
import {
|
|
collectTeleportRootsForOutsideClick,
|
|
getTeleportedElementFromRef,
|
|
isDomElement,
|
|
isElement,
|
|
onClickOutside,
|
|
} from '@baserow/modules/core/utils/dom'
|
|
|
|
export default {
|
|
name: 'Context',
|
|
provide() {
|
|
return {
|
|
registerChild: this.registerChild,
|
|
unregisterChild: this.unregisterChild,
|
|
}
|
|
},
|
|
inject: {
|
|
parentRegisterChild: {
|
|
from: 'registerChild',
|
|
default: null,
|
|
},
|
|
parentUnregisterChild: {
|
|
from: 'unregisterChild',
|
|
default: null,
|
|
},
|
|
},
|
|
inheritAttrs: false,
|
|
props: {
|
|
hideOnClickOutside: {
|
|
type: Boolean,
|
|
default: true,
|
|
required: false,
|
|
},
|
|
hideOnScroll: {
|
|
type: Boolean,
|
|
default: false,
|
|
required: false,
|
|
},
|
|
hideOnResize: {
|
|
type: Boolean,
|
|
default: false,
|
|
required: false,
|
|
},
|
|
overflowScroll: {
|
|
type: Boolean,
|
|
default: false,
|
|
required: false,
|
|
},
|
|
maxHeightIfOutsideViewport: {
|
|
type: Boolean,
|
|
default: () => false,
|
|
required: false,
|
|
},
|
|
horizontalViewportMargin: {
|
|
type: Number,
|
|
default: null,
|
|
},
|
|
/**
|
|
* When `checkForEdges` is called, this prop allows us to force the
|
|
* position to remain fixed, even if the context could result in it
|
|
* getting clipped.
|
|
*/
|
|
forcePosition: {
|
|
type: Boolean,
|
|
default: false,
|
|
required: false,
|
|
},
|
|
},
|
|
emits: ['hidden', 'shown'],
|
|
data() {
|
|
return {
|
|
open: false,
|
|
opener: null,
|
|
updatedOnce: false,
|
|
// If opened once, should stay in DOM to keep nested content
|
|
openedOnce: false,
|
|
maxHeightOffset: 10,
|
|
children: [],
|
|
}
|
|
},
|
|
mounted() {
|
|
if (this.parentRegisterChild) {
|
|
this.parentRegisterChild(this)
|
|
}
|
|
},
|
|
beforeUnmount() {
|
|
if (this.parentUnregisterChild) {
|
|
this.parentUnregisterChild(this)
|
|
}
|
|
this.hide(false)
|
|
},
|
|
methods: {
|
|
/**
|
|
* Toggles the open state of the context menu.
|
|
*
|
|
* @param target The original element that changed the state of the
|
|
* context, this will be used to calculate the correct position.
|
|
* @param vertical `bottom` positions the context under the target.
|
|
* `top` positions the context above the target.
|
|
* `over-bottom` positions the context over and under the target.
|
|
* `over-top` positions the context over and above the target.
|
|
* `over` positions the context between top and bottom of the
|
|
* target.
|
|
* @param horizontal `left` aligns the context with the left side of the target.
|
|
* `right` aligns the context with the right side of the target.
|
|
* @param verticalOffset
|
|
* The offset indicates how many pixels the context is moved
|
|
* top from the original calculated position.
|
|
* @param horizontalOffset
|
|
* The offset indicates how many pixels the context is moved
|
|
* left from the original calculated position.
|
|
* @param value True if context must be shown, false if not and undefine
|
|
* will invert the current state.
|
|
*/
|
|
toggle(
|
|
target,
|
|
vertical = 'bottom',
|
|
horizontal = 'left',
|
|
verticalOffset = 10,
|
|
horizontalOffset = 0,
|
|
value
|
|
) {
|
|
if (value === undefined) {
|
|
value = !this.open
|
|
}
|
|
|
|
if (value) {
|
|
return this.show(
|
|
target,
|
|
vertical,
|
|
horizontal,
|
|
verticalOffset,
|
|
horizontalOffset
|
|
)
|
|
} else {
|
|
this.hide()
|
|
}
|
|
},
|
|
/**
|
|
* Calculate the position, show the context menu and register a click event on the
|
|
* body to check if the user has clicked outside the context.
|
|
*/
|
|
async show(
|
|
target,
|
|
vertical,
|
|
horizontal,
|
|
verticalOffset = 10,
|
|
horizontalOffset = 0
|
|
) {
|
|
const isElementOrigin = isDomElement(target)
|
|
const updatePosition = () => {
|
|
if (!this.open) {
|
|
return
|
|
}
|
|
const el = this.$refs.contextEl
|
|
const css = isElementOrigin
|
|
? this.calculatePositionElement(
|
|
target,
|
|
vertical,
|
|
horizontal,
|
|
verticalOffset,
|
|
horizontalOffset
|
|
)
|
|
: this.calculatePositionFixed(
|
|
target,
|
|
vertical,
|
|
horizontal,
|
|
verticalOffset,
|
|
horizontalOffset
|
|
)
|
|
|
|
// If the context menu doesn't fit inside the viewport from the opposite.
|
|
// direction, then it will break out of it. We will therefore close it. This can
|
|
// happen the height or width of the viewport decreases.
|
|
if (
|
|
(css.bottom && css.bottom < this.getWindowScrollHeight()) ||
|
|
(css.right && css.right < 0) ||
|
|
(css.top &&
|
|
css.top > window.innerHeight + this.getWindowScrollHeight())
|
|
) {
|
|
this.hide()
|
|
return
|
|
}
|
|
|
|
for (const key in css) {
|
|
const cssValue =
|
|
css[key] !== null ? Math.ceil(css[key]) + 'px' : 'auto'
|
|
el.style[key] = cssValue
|
|
}
|
|
|
|
// The max height can optionally be automatically to prevent the context from
|
|
// breaking out of the viewport.
|
|
if (this.maxHeightIfOutsideViewport) {
|
|
const maxHeight =
|
|
css.top || css.bottom
|
|
? `calc(100vh - ${
|
|
(css.top || css.bottom) +
|
|
this.maxHeightOffset -
|
|
this.getWindowScrollHeight()
|
|
}px)`
|
|
: 'none'
|
|
el.style['max-height'] = maxHeight
|
|
}
|
|
|
|
this.updatedOnce = true
|
|
}
|
|
|
|
// If we store the element who opened the context menu we can exclude the element
|
|
// when clicked outside of this element.
|
|
this.opener = isElementOrigin ? target : null
|
|
this.open = true
|
|
this.openedOnce = true
|
|
|
|
// Delay the position update to the next tick to let the Context content
|
|
// be available in DOM for accurate positioning.
|
|
await this.$nextTick()
|
|
updatePosition()
|
|
if (!this.open) {
|
|
return
|
|
}
|
|
|
|
// Cancel any previous handlers before setting up new ones
|
|
this._cleanupEventHandlers()
|
|
|
|
const el = this.getTeleportedElement()
|
|
|
|
const ignoreElements = () => {
|
|
const childRoots = []
|
|
for (const child of this.children) {
|
|
childRoots.push(...collectTeleportRootsForOutsideClick(child))
|
|
}
|
|
return [
|
|
this.opener,
|
|
...new Set(childRoots.filter((el) => el instanceof HTMLElement)),
|
|
].filter(Boolean)
|
|
}
|
|
|
|
this._cancelOnClickOutside = onClickOutside(
|
|
el,
|
|
() => {
|
|
if (this.open && this.hideOnClickOutside) {
|
|
this.hide()
|
|
}
|
|
},
|
|
{
|
|
ignoreElements,
|
|
}
|
|
)
|
|
|
|
this._updatePositionViaScrollEvent = (event) => {
|
|
if (this.hideOnScroll) {
|
|
this.hide()
|
|
} else if (
|
|
!isElement(this.getTeleportedElement(), event.target) &&
|
|
!this.children.some((child) =>
|
|
collectTeleportRootsForOutsideClick(child).some((root) =>
|
|
isElement(root, event.target)
|
|
)
|
|
)
|
|
) {
|
|
updatePosition()
|
|
}
|
|
}
|
|
window.addEventListener(
|
|
'scroll',
|
|
this._updatePositionViaScrollEvent,
|
|
true
|
|
)
|
|
|
|
this._updatePositionViaResizeEvent = () => {
|
|
if (this.hideOnResize) {
|
|
this.hide()
|
|
} else {
|
|
updatePosition()
|
|
}
|
|
}
|
|
window.addEventListener('resize', this._updatePositionViaResizeEvent)
|
|
|
|
this.$emit('shown')
|
|
},
|
|
/**
|
|
* Toggles context menu next to mouse when click event has happened
|
|
*/
|
|
toggleNextToMouse(
|
|
clickEvent,
|
|
vertical = 'bottom',
|
|
horizontal = 'left',
|
|
verticalOffset = 10,
|
|
horizontalOffset = 0,
|
|
value = true
|
|
) {
|
|
this.toggle(
|
|
{
|
|
top: clickEvent.pageY,
|
|
left: clickEvent.pageX,
|
|
},
|
|
vertical,
|
|
horizontal,
|
|
verticalOffset,
|
|
horizontalOffset,
|
|
value
|
|
)
|
|
},
|
|
/**
|
|
* Shows context menu next to mouse when click event has happened
|
|
*/
|
|
showNextToMouse(
|
|
clickEvent,
|
|
vertical = 'bottom',
|
|
horizontal = 'left',
|
|
verticalOffset = 10,
|
|
horizontalOffset = 0
|
|
) {
|
|
this.show(
|
|
{
|
|
top: clickEvent.pageY,
|
|
left: clickEvent.pageX,
|
|
},
|
|
vertical,
|
|
horizontal,
|
|
verticalOffset,
|
|
horizontalOffset
|
|
)
|
|
},
|
|
/**
|
|
* Forces the child elements to render by setting `openedOnce` to `true`. This
|
|
* could be useful when children of the context must be accessed before the context
|
|
* has been opened.
|
|
*/
|
|
forceRender() {
|
|
this.openedOnce = true
|
|
},
|
|
/**
|
|
* Hide the context menu and make sure the body event is removed.
|
|
*/
|
|
hide(emit = true) {
|
|
this.opener = null
|
|
this.open = false
|
|
|
|
if (emit) {
|
|
this.$emit('hidden')
|
|
}
|
|
|
|
this._cleanupEventHandlers()
|
|
},
|
|
_cleanupEventHandlers() {
|
|
if (this._cancelOnClickOutside) {
|
|
this._cancelOnClickOutside()
|
|
this._cancelOnClickOutside = null
|
|
}
|
|
if (this._updatePositionViaScrollEvent) {
|
|
window.removeEventListener(
|
|
'scroll',
|
|
this._updatePositionViaScrollEvent,
|
|
true
|
|
)
|
|
this._updatePositionViaScrollEvent = null
|
|
}
|
|
if (this._updatePositionViaResizeEvent) {
|
|
window.removeEventListener('resize', this._updatePositionViaResizeEvent)
|
|
this._updatePositionViaResizeEvent = null
|
|
}
|
|
},
|
|
/**
|
|
* Calculates the absolute position of the context based on the original clicked
|
|
* element. If the target element is not visible, it might mean that we can't
|
|
* figure out the correct position, so in that case we force the element to be
|
|
* visible.
|
|
*/
|
|
calculatePositionElement(
|
|
target,
|
|
vertical,
|
|
horizontal,
|
|
verticalOffset,
|
|
horizontalOffset
|
|
) {
|
|
const visible =
|
|
window.getComputedStyle(target).getPropertyValue('display') !== 'none'
|
|
|
|
// If the target is not visible then we can't calculate the position, so we
|
|
// temporarily need to show the element forcefully.
|
|
if (!visible) {
|
|
target.classList.add('forced-block')
|
|
}
|
|
|
|
const targetRect = target.getBoundingClientRect()
|
|
const positions = this.calculatePositions(
|
|
horizontal,
|
|
vertical,
|
|
targetRect.top,
|
|
targetRect.right,
|
|
targetRect.bottom,
|
|
targetRect.left,
|
|
verticalOffset,
|
|
horizontalOffset
|
|
)
|
|
|
|
if (!visible) {
|
|
target.classList.remove('forced-block')
|
|
}
|
|
|
|
return positions
|
|
},
|
|
/**
|
|
* Calculates the desired position based on the provided coordinates. For now this
|
|
* is only used by the row context menu, but because of the reserved space of the
|
|
* grid on the right and bottom there is always room for the context. Therefore we
|
|
* do not need to check if the context fits.
|
|
*/
|
|
calculatePositionFixed(
|
|
coordinates,
|
|
vertical,
|
|
horizontal,
|
|
verticalOffset,
|
|
horizontalOffset
|
|
) {
|
|
const targetTop = coordinates.top
|
|
const targetLeft = coordinates.left
|
|
// The bottom and right must be equal to the top and left because when calculating
|
|
// the position fixed, it's a mouseclick which just has an x and y coordinate and
|
|
// is not an element with a width and height.
|
|
const targetBottom = coordinates.top
|
|
const targetRight = coordinates.left
|
|
|
|
const positions = this.calculatePositions(
|
|
horizontal,
|
|
vertical,
|
|
targetTop,
|
|
targetRight,
|
|
targetBottom,
|
|
targetLeft,
|
|
verticalOffset,
|
|
horizontalOffset
|
|
)
|
|
|
|
return positions
|
|
},
|
|
/**
|
|
* Calculates the optimal positions based on the chosen position, target and offset.
|
|
*/
|
|
calculatePositions(
|
|
horizontal,
|
|
vertical,
|
|
targetTop,
|
|
targetRight,
|
|
targetBottom,
|
|
targetLeft,
|
|
verticalOffset,
|
|
horizontalOffset
|
|
) {
|
|
const { vertical: verticalAdjusted, horizontal: horizontalAdjusted } =
|
|
this.checkForEdges(
|
|
{
|
|
top: targetTop,
|
|
left: targetLeft,
|
|
bottom: targetBottom,
|
|
right: targetRight,
|
|
},
|
|
vertical,
|
|
horizontal,
|
|
verticalOffset,
|
|
horizontalOffset
|
|
)
|
|
|
|
const positions = { top: null, right: null, bottom: null, left: null }
|
|
|
|
// Calculate the correct positions for horizontal and vertical values.
|
|
if (horizontalAdjusted === 'left') {
|
|
positions.left = targetLeft + horizontalOffset
|
|
}
|
|
|
|
if (horizontalAdjusted === 'right') {
|
|
positions.right = window.innerWidth - targetRight - horizontalOffset
|
|
}
|
|
|
|
if (this.horizontalViewportMargin !== null) {
|
|
const margin = this.horizontalViewportMargin
|
|
const availableWidth =
|
|
window.innerWidth - this.$refs.contextEl.offsetWidth
|
|
const left = positions.left ?? availableWidth - positions.right
|
|
positions.left =
|
|
window.scrollX + clamp(left, margin, availableWidth - margin)
|
|
positions.right = null
|
|
}
|
|
|
|
if (verticalAdjusted === 'bottom') {
|
|
positions.top =
|
|
targetBottom + verticalOffset + this.getWindowScrollHeight()
|
|
}
|
|
|
|
if (verticalAdjusted === 'over-bottom' || verticalAdjusted === 'over') {
|
|
positions.top =
|
|
targetTop + verticalOffset + this.getWindowScrollHeight()
|
|
}
|
|
|
|
if (verticalAdjusted === 'top') {
|
|
positions.bottom =
|
|
window.innerHeight -
|
|
targetTop +
|
|
verticalOffset +
|
|
this.getWindowScrollHeight()
|
|
}
|
|
|
|
if (verticalAdjusted === 'over-top' || verticalAdjusted === 'over') {
|
|
positions.bottom = window.innerHeight - targetBottom + verticalOffset
|
|
}
|
|
|
|
// Round position otherwise sometimes it breaks, especially when using
|
|
// Browser zoom
|
|
return Object.fromEntries(
|
|
Object.entries(positions).map(([key, value]) => [
|
|
key,
|
|
Number.isFinite(value) ? Math.round(value) : value,
|
|
])
|
|
)
|
|
},
|
|
/**
|
|
* Checks if we need to adjust the horizontal/vertical value of where the context
|
|
* menu will be placed. This might happen if the screen size would cause the context
|
|
* to clip out of the screen if positioned in a certain position.
|
|
*
|
|
* @returns {{horizontal: string, vertical: string}}
|
|
*/
|
|
checkForEdges(
|
|
targetRect,
|
|
vertical,
|
|
horizontal,
|
|
verticalOffset,
|
|
horizontalOffset
|
|
) {
|
|
const el = this.$refs.contextEl
|
|
const contextRect = el.getBoundingClientRect()
|
|
const scrollHeight = el.scrollHeight
|
|
const canTop =
|
|
targetRect.top -
|
|
scrollHeight -
|
|
verticalOffset +
|
|
this.getWindowScrollHeight() >
|
|
0
|
|
const canBottom =
|
|
window.innerHeight +
|
|
this.getWindowScrollHeight() -
|
|
targetRect.bottom -
|
|
scrollHeight -
|
|
this.maxHeightOffset -
|
|
1 -
|
|
verticalOffset >
|
|
0
|
|
const canRight =
|
|
targetRect.right - contextRect.width - horizontalOffset > 0
|
|
const canLeft =
|
|
window.innerWidth -
|
|
targetRect.left -
|
|
contextRect.width -
|
|
horizontalOffset >
|
|
0
|
|
|
|
// If forcePosition is true, don't adjust the position based on edges.
|
|
if (this.forcePosition) {
|
|
return { vertical, horizontal }
|
|
}
|
|
|
|
// If bottom, top, left or right doesn't fit, but their opposite does we switch to
|
|
// that.
|
|
if (vertical === 'bottom' && !canBottom && canTop) {
|
|
vertical = 'top'
|
|
}
|
|
|
|
if (vertical === 'top' && !canTop && canBottom) {
|
|
vertical = 'bottom'
|
|
}
|
|
|
|
if (horizontal === 'left' && !canLeft && canRight) {
|
|
horizontal = 'right'
|
|
}
|
|
|
|
if (horizontal === 'right' && !canRight && canLeft) {
|
|
horizontal = 'left'
|
|
}
|
|
|
|
return { vertical, horizontal }
|
|
},
|
|
getWindowScrollHeight() {
|
|
return window?.scrollY || 0
|
|
},
|
|
isOpen() {
|
|
return this.open
|
|
},
|
|
/**
|
|
* Root element rendered inside `<Teleport>` (not the anchor `this.$el`).
|
|
*
|
|
* @returns {HTMLElement|null}
|
|
*/
|
|
getTeleportedElement() {
|
|
return getTeleportedElementFromRef(this.$refs, 'contextEl')
|
|
},
|
|
registerChild(child) {
|
|
this.children.push(child)
|
|
},
|
|
unregisterChild(child) {
|
|
const index = this.children.indexOf(child)
|
|
if (index !== -1) {
|
|
this.children.splice(index, 1)
|
|
}
|
|
},
|
|
},
|
|
}
|
|
</script>
|