Skip to content

Commit

Permalink
feat(@dpc-sdp/ripple-ui-maps): ♻️ use custom map controls
Browse files Browse the repository at this point in the history
  • Loading branch information
dylankelly committed Dec 1, 2023
1 parent 425340e commit 150e9ac
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 44 deletions.
3 changes: 2 additions & 1 deletion packages/ripple-ui-core/src/components/icon/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ export const RplIconGroups = {
'icon-view',
'icon-zoom-in',
'icon-zoom-out'
]
],
map: ['icon-enlarge', 'icon-map-zoom-in', 'icon-map-zoom-out', 'icon-home']
} as const

export default {
Expand Down
6 changes: 5 additions & 1 deletion packages/ripple-ui-core/src/components/icon/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,9 @@ export default {
'icon-wechat': () => import('./custom/icon-wechat.svg?component'),
'icon-youtube': () => import('./custom/icon-youtube.svg?component'),
'icon-zoom-in': () => import('./custom/icon-zoom-in.svg?component'),
'icon-zoom-out': () => import('./custom/icon-zoom-out.svg?component')
'icon-zoom-out': () => import('./custom/icon-zoom-out.svg?component'),
'icon-map-zoom-in': () => import('./custom/icon-map-zoom-in.svg?component'),
'icon-map-zoom-out': () => import('./custom/icon-map-zoom-out.svg?component'),
'icon-enlarge': () => import('./custom/icon-enlarge.svg?component'),
'icon-home': () => import('./custom/icon-home.svg?component')
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions packages/ripple-ui-maps/src/components/map/RplMap.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
.rpl-map__control {
position: absolute;
right: var(--rpl-sp-2);

z-index: var(--rpl-layer-1);
@media (--rpl-bp-l) {
right: var(--rpl-sp-3);
}
Expand Down Expand Up @@ -86,12 +86,12 @@
bottom: var(--rpl-sp-3);
}

.ol-zoom-in {
.rpl-map__control-zoom-in {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
}

.ol-zoom-out {
.rpl-map__control-zoom-out {
border-top-left-radius: 0;
border-top-right-radius: 0;
border-top: none;
Expand Down
63 changes: 36 additions & 27 deletions packages/ripple-ui-maps/src/components/map/RplMap.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup lang="ts">
import { useRippleEvent, rplEventPayload } from '@dpc-sdp/ripple-ui-core'
import { RplIcon } from '@dpc-sdp/ripple-ui-core/vue'
import type { IRplMapFeature } from './../../types'
import { onMounted, ref, inject, computed } from 'vue'
import { Map } from 'ol'
Expand All @@ -11,11 +12,8 @@ import { fromLonLat } from 'ol/proj'
import RplMapPopUp from './../popup/RplMapPopUp.vue'
import RplMapCluster from './../cluster/RplMapCluster.vue'
import markerIconDefaultSrc from './../feature-pin/icon-pin.svg?url'
// import zoomInIcon from './../../assets/icons/icon-map-zoom-in.svg?raw'
// import zoomOutIcon from './../../assets/icons/icon-map-zoom-out.svg?raw'
// import enlargeIcon from './../../assets/icons/icon-enlarge.svg?raw'
import homeIcon from './../../assets/icons/icon-home.svg?component'
import useMapControlLabel from './../../composables/useMapControlLabel'
import useMapControls from './../../composables/useMapControls.ts'
import {
getfeaturesAtMapPixel,
zoomToClusterExtent,
Expand Down Expand Up @@ -72,6 +70,14 @@ const center = computed(() => {
}
})
const {
onHomeClick,
onZoomInClick,
onZoomOutClick,
onFullScreenClick,
isFullScreenRef
} = useMapControls(mapRef, center, props.initialZoom)
const mapFeatures = computed(() => {
if (Array.isArray(props.features)) {
return props.features.map((itm, idx) => {
Expand All @@ -92,11 +98,6 @@ function onPopUpClose() {
popup.value.isOpen = false
}
const { createHTMLElementFromString } = useMapControlLabel()
// const zoomInLabel = createHTMLElementFromString(zoomInIcon)
// const zoomOutLabel = createHTMLElementFromString(zoomOutIcon)
// const fullScreenLabel = createHTMLElementFromString(enlargeIcon)
function onMapSingleClick(evt) {
const map = mapRef.value.map
const point = getfeaturesAtMapPixel(map, evt.pixel)
Expand Down Expand Up @@ -133,10 +134,6 @@ function onMapMove(evt) {
}
}
}
function onHomeClick() {
centerMap(mapRef.value.map, center.value, { y: 0, x: 0 }, props.initialZoom)
}
</script>

<template>
Expand Down Expand Up @@ -225,22 +222,34 @@ function onHomeClick() {
</RplMapPopUp>
</ol-overlay>
</slot>

<!-- <ol-zoom-control
className="rpl-map__control rpl-map__control-zoom"
:zoomInLabel="zoomInLabel"
:zoomOutLabel="zoomOutLabel"
/>
<div className="rpl-map__control rpl-map__control-home">
<div class="rpl-map__control rpl-map__control-fullscreen">
<button title="View map fullscreen" @click="onFullScreenClick">
<RplIcon v-if="isFullScreenRef" name="icon-cancel"></RplIcon>
<RplIcon v-else name="icon-enlarge" size="m"></RplIcon>
</button>
</div>
<div class="rpl-map__control rpl-map__control-home">
<button title="Centre map" @click="onHomeClick">
<homeIcon></homeIcon>
<RplIcon name="icon-home"></RplIcon>
</button>
</div>
<div class="rpl-map__control rpl-map__control-zoom">
<button
title="Zoom map in"
class="rpl-map__control-zoom-in"
@click="onZoomInClick"
>
<RplIcon name="icon-map-zoom-in"></RplIcon>
</button>

<button
title="Zoom map out"
class="rpl-map__control-zoom-out"
@click="onZoomOutClick"
>
<RplIcon name="icon-map-zoom-out"></RplIcon>
</button>
</div>
<ol-fullscreen-control
:label="fullScreenLabel"
:labelActive="fullScreenLabel"
className="rpl-map__control rpl-map__control-fullscreen"
/> -->
</ol-map>
</div>
</template>
Expand Down
12 changes: 0 additions & 12 deletions packages/ripple-ui-maps/src/composables/useMapControlLabel.ts

This file was deleted.

135 changes: 135 additions & 0 deletions packages/ripple-ui-maps/src/composables/useMapControls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { easeOut } from 'ol/easing'
import { centerMap } from './../components/map/utils.ts'
import { ref } from 'vue'
export default (mapRef, center, initialZoom) => {
const isFullScreenRef = ref(false)

/**
* @param {number} delta Zoom delta.
* @private
*/
function zoomByDelta(delta, duration = 250) {
const view = mapRef.value.map.getView()
if (!view) {
// the map does not have a view, so we can't act
// upon it
return
}
const currentZoom = view.getZoom()
if (currentZoom !== undefined) {
const newZoom = view.getConstrainedZoom(currentZoom + delta)
if (duration > 0) {
if (view.getAnimating()) {
view.cancelAnimations()
}
view.animate({
zoom: newZoom,
duration: duration,
easing: easeOut
})
} else {
view.setZoom(newZoom)
}
}
}

/**
* @param {Document} doc The root document to check.
* @return {boolean} Fullscreen is supported by the current platform.
*/
function isFullScreenSupported(doc) {
const body = doc.body
return !!(
body['webkitRequestFullscreen'] ||
(body.requestFullscreen && doc.fullscreenEnabled)
)
}

/**
* @param {Document} doc The root document to check.
* @return {boolean} Element is currently in fullscreen.
*/
function isFullScreen(doc) {
return !!(doc['webkitIsFullScreen'] || doc.fullscreenElement)
}

/**
* Request to fullscreen an element.
* @param {HTMLElement} element Element to request fullscreen
*/
function requestFullScreen(element) {
if (element.requestFullscreen) {
element.requestFullscreen()
} else if (element['webkitRequestFullscreen']) {
element['webkitRequestFullscreen']()
}
}

/**
* Request to fullscreen an element with keyboard input.
* @param {HTMLElement} element Element to request fullscreen
*/
function requestFullScreenWithKeys(element) {
if (element['webkitRequestFullscreen']) {
element['webkitRequestFullscreen']()
} else {
requestFullScreen(element)
}
}

/**
* Exit fullscreen.
* @param {Document} doc The document to exit fullscren from
*/
function exitFullScreen(doc) {
if (doc.exitFullscreen) {
doc.exitFullscreen()
} else if (doc['webkitExitFullscreen']) {
doc['webkitExitFullscreen']()
}
}

function handleFullScreen(withKeys = false) {
const map = mapRef.value.map
if (!map) {
return
}
const doc = map.getOwnerDocument()
if (!isFullScreenSupported(doc)) {
return
}
if (isFullScreen(doc)) {
exitFullScreen(doc)
isFullScreenRef.value = false
} else {
const element = map.getTargetElement()
if (withKeys) {
requestFullScreenWithKeys(element)
} else {
requestFullScreen(element)
}
isFullScreenRef.value = true
}
}

function onHomeClick() {
centerMap(mapRef.value.map, center.value, { y: 0, x: 0 }, initialZoom)
}
function onZoomInClick() {
zoomByDelta(1)
}
function onZoomOutClick() {
zoomByDelta(-1)
}
function onFullScreenClick() {
handleFullScreen()
}

return {
onHomeClick,
onZoomInClick,
onZoomOutClick,
onFullScreenClick,
isFullScreenRef
}
}

0 comments on commit 150e9ac

Please sign in to comment.