-
Notifications
You must be signed in to change notification settings - Fork 670
Map: OSM provider - Add marker tooltips #35164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlisherAmonulloev
wants to merge
7
commits into
feature/26_2_osm-provider-for-dxmap/main
Choose a base branch
from
26_2_osm-provider-for-dxmap/marker-tooltips
base: feature/26_2_osm-provider-for-dxmap/main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f38380b
Map: OSM provider - Add marker tooltips
AlisherAmonulloev 6ca93f5
Map: OSM provider - Showcase marker tooltips in Storybook
AlisherAmonulloev e4e9d1a
Map: OSM provider - Rename Storybook story to Overview
AlisherAmonulloev bd2a5e0
Map: Update ThemeBuilder dependencies for marker tooltips
AlisherAmonulloev 17a93d0
Map: OSM provider - Use standard Popover for marker tooltips
AlisherAmonulloev e08255e
Map: OSM provider - Skip render updates for hidden tooltips
AlisherAmonulloev bb82862
Map: OSM provider - Preserve focus settings when customizing tooltips
AlisherAmonulloev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
216 changes: 216 additions & 0 deletions
216
packages/devextreme/js/__internal/ui/map/provider.dynamic.osm.openlayers.tooltip.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| import { normalizeKeyName } from '@js/common/core/events/utils'; | ||
| import domAdapter from '@js/core/dom_adapter'; | ||
| import $ from '@js/core/renderer'; | ||
| import type { Properties } from '@js/ui/popover'; | ||
| import Popover from '@js/ui/popover'; | ||
| import { ALL_FOCUSABLE_ELEMENTS_SELECTOR } from '@ts/core/utils/m_selectors'; | ||
| import type InternalPopover from '@ts/ui/popover/popover'; | ||
|
|
||
| import { DEFAULT_MARKER_CLASS } from './provider.dynamic.osm.openlayers.marker'; | ||
| import type { MapLike } from './provider.dynamic.osm.openlayers.utils'; | ||
|
|
||
| const POPOVER_CLASS = 'dx-map-marker-popover'; | ||
| const TOOLTIP_MAX_WIDTH = 280; | ||
|
|
||
| type MarkerPopover = Popover & Pick<InternalPopover, | ||
| '_renderDimensions' | '_setContentHeight' | '_renderPosition'>; | ||
|
|
||
| export class OpenLayersMarkerTooltip { | ||
| readonly element: HTMLElement; | ||
|
|
||
| private readonly _host: HTMLElement; | ||
|
|
||
| private readonly _popover: MarkerPopover; | ||
|
|
||
| private readonly _inertElements = new Set<HTMLElement>(); | ||
|
|
||
| private readonly _tabIndexes = new Map<HTMLElement, string | null>(); | ||
|
|
||
| private _focusEnabled = true; | ||
|
|
||
| private _positioning = false; | ||
|
|
||
| private _positionUpdatePending = false; | ||
|
|
||
| private _disposed = false; | ||
|
|
||
| constructor( | ||
| private readonly _map: MapLike, | ||
| private readonly _container: Element, | ||
| private readonly _marker: HTMLElement, | ||
| text: string, | ||
| rtlEnabled: boolean, | ||
| ) { | ||
| const { ownerDocument } = _container; | ||
| const host = ownerDocument.createElement('div'); | ||
| Object.assign(host.style, { position: 'absolute', inset: '0', contain: 'layout paint' }); | ||
| _map.getOverlayContainer().appendChild(host); | ||
| this._host = host; | ||
| const element = ownerDocument.createElement('div'); | ||
| host.appendChild(element); | ||
| const content = ownerDocument.createElement('div'); | ||
| content.innerHTML = text; | ||
| const target = _marker.classList.contains(DEFAULT_MARKER_CLASS) | ||
| ? _marker.firstElementChild ?? _marker | ||
| : _marker; | ||
| const focusTargets = _marker.querySelectorAll<HTMLElement>(ALL_FOCUSABLE_ELEMENTS_SELECTOR); | ||
|
|
||
| this._popover = new Popover<Properties>(element, { | ||
| container: host, | ||
| // @ts-expect-error Popover also supports renderer collections as targets. | ||
| target: focusTargets.length ? $(Array.from(focusTargets)) : _marker, | ||
| position: { | ||
| of: target, | ||
| my: { x: 'center', y: 'bottom' }, | ||
| at: { x: 'center', y: 'top' }, | ||
| collision: 'flip', | ||
| boundary: _container, | ||
| }, | ||
| animation: undefined, | ||
| deferRendering: false, | ||
| contentTemplate: (): HTMLElement => content, | ||
| maxWidth: TOOLTIP_MAX_WIDTH, | ||
| showTitle: false, | ||
| showCloseButton: false, | ||
| hideOnOutsideClick: false, | ||
| hideOnParentScroll: false, | ||
| rtlEnabled, | ||
| elementAttr: { class: POPOVER_CLASS }, | ||
| wrapperAttr: { class: POPOVER_CLASS }, | ||
| }) as MarkerPopover; | ||
| this.element = $(this._popover.content()).parent().get(0) as HTMLElement; | ||
| this._popover.on('showing', this._syncFocusState); | ||
| this._popover.on('positioned', this._restoreContentSize); | ||
| this._popover.on('positioned', this._syncFocusState); | ||
| this._popover.on('shown', this._syncFocusState); | ||
| this._popover.on('hidden', this._syncFocusState); | ||
| this.element.addEventListener('click', this._stopPropagation); | ||
| this.element.addEventListener('dblclick', this._stopPropagation); | ||
| this.element.addEventListener('pointerdown', this._stopPropagation); | ||
| this.element.addEventListener('keydown', this._stopMapKeyPropagation); | ||
| _map.on('postrender', this.syncPosition); | ||
| } | ||
|
|
||
| show(): void { | ||
| if (!this._disposed) { | ||
| this._popover.option('visible', true); | ||
| } | ||
| } | ||
|
|
||
| setFocusEnabled(enabled: boolean): void { | ||
| if (enabled !== this._focusEnabled) { | ||
| this._focusEnabled = enabled; | ||
| this._popover.option('_preventDialogContainerFocus', !enabled); | ||
| const focusEnabled = enabled && this.element.getAttribute('role') === 'dialog'; | ||
| this._popover.option({ | ||
| focusStateEnabled: focusEnabled, | ||
| tabFocusLoopEnabled: focusEnabled, | ||
| }); | ||
| } | ||
| this._syncFocusState(); | ||
| } | ||
|
|
||
| private readonly _restoreContentSize = (): Promise<void> | undefined => { | ||
| if (this._positioning || this._positionUpdatePending) { | ||
| return undefined; | ||
| } | ||
|
|
||
| this._positionUpdatePending = true; | ||
| return Promise.resolve().then(() => { | ||
| this._positionUpdatePending = false; | ||
| if (this._disposed) { | ||
| return; | ||
| } | ||
|
|
||
| this._popover._renderDimensions(); | ||
| this._popover._setContentHeight(true); | ||
| this.syncPosition(); | ||
| }); | ||
| }; | ||
|
|
||
| readonly syncPosition = (): void => { | ||
| if (!this._popover.option('visible')) { | ||
| return; | ||
| } | ||
|
|
||
| this._positioning = true; | ||
| try { | ||
| this._popover._renderPosition(false); | ||
| } finally { | ||
| this._positioning = false; | ||
| } | ||
| this._syncFocusState(); | ||
| }; | ||
|
|
||
| private readonly _stopPropagation = (event: Event): void => event.stopPropagation(); | ||
|
|
||
| private readonly _stopMapKeyPropagation = (event: KeyboardEvent): void => { | ||
| const key = normalizeKeyName(event); | ||
| if (event.defaultPrevented || (key !== 'escape' && key !== 'tab')) { | ||
| event.stopPropagation(); | ||
| } | ||
| }; | ||
|
|
||
| private readonly _syncFocusState = (): void => { | ||
| this._inertElements.forEach((element) => { element.inert = false; }); | ||
| this._inertElements.clear(); | ||
| if (this._focusEnabled) { | ||
| this._tabIndexes.forEach((tabIndex, element) => { | ||
| if (tabIndex === null) { | ||
| element.removeAttribute('tabindex'); | ||
| } else { | ||
| element.setAttribute('tabindex', tabIndex); | ||
| } | ||
| }); | ||
| this._tabIndexes.clear(); | ||
| } | ||
| const focusTargets = this.element | ||
| .querySelectorAll<HTMLElement>(ALL_FOCUSABLE_ELEMENTS_SELECTOR); | ||
| if (!this._focusEnabled) { | ||
| focusTargets.forEach((element) => { | ||
| if (!this._tabIndexes.has(element)) { | ||
| this._tabIndexes.set(element, element.getAttribute('tabindex')); | ||
| } | ||
| element.setAttribute('tabindex', '-1'); | ||
| }); | ||
| } | ||
| const boundary = this._container.getBoundingClientRect(); | ||
| const activeElement = domAdapter.getActiveElement(this.element); | ||
| const elements = this._popover.option('visible') && this.element.getClientRects().length | ||
| ? [this._marker, this.element, ...focusTargets] | ||
| : [this._marker]; | ||
|
|
||
| elements.forEach((element) => { | ||
| const rect = element.getBoundingClientRect(); | ||
| const outside = element === this.element | ||
| ? rect.bottom <= boundary.top || rect.top >= boundary.bottom | ||
| || rect.right <= boundary.left || rect.left >= boundary.right | ||
| : rect.top < boundary.top || rect.bottom > boundary.bottom | ||
| || rect.left < boundary.left || rect.right > boundary.right; | ||
|
|
||
| if (outside) { | ||
| if (element.contains(activeElement)) { | ||
| (this._container as HTMLElement).focus({ preventScroll: true }); | ||
| } | ||
| if (!element.inert) { | ||
| element.inert = true; | ||
| this._inertElements.add(element); | ||
| } | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| dispose(): void { | ||
| this._disposed = true; | ||
| this._map.un('postrender', this.syncPosition); | ||
| this.element.removeEventListener('click', this._stopPropagation); | ||
| this.element.removeEventListener('dblclick', this._stopPropagation); | ||
| this.element.removeEventListener('pointerdown', this._stopPropagation); | ||
| this.element.removeEventListener('keydown', this._stopMapKeyPropagation); | ||
| this._popover.dispose(); | ||
| this._host.remove(); | ||
| this._inertElements.forEach((element) => { element.inert = false; }); | ||
| this._inertElements.clear(); | ||
| this._tabIndexes.clear(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.