|
| 1 | +import LayerTypes from '@/api/layers/LayerTypes.enum' |
| 2 | +import { computed } from 'vue' |
| 3 | +import { useStore } from 'vuex' |
| 4 | + |
| 5 | +/** Composable that gives utility function to calculate/get layers' and features' z-index */ |
| 6 | +export function useLayerZIndexCalculation() { |
| 7 | + const store = useStore() |
| 8 | + const is3dActive = computed(() => store.state.cesium.active) |
| 9 | + const backgroundLayers = computed(() => { |
| 10 | + if (is3dActive.value) { |
| 11 | + return store.getters.backgroundLayersFor3D |
| 12 | + } |
| 13 | + return [store.state.layers.currentBackgroundLayer] |
| 14 | + }) |
| 15 | + const selectedFeatures = computed(() => store.state.features.selectedFeatures) |
| 16 | + const pinnedLocation = computed(() => store.state.map.pinnedLocation) |
| 17 | + const previewLocation = computed(() => store.state.map.previewedPinnedLocation) |
| 18 | + const crossHair = computed(() => store.state.position.crossHair) |
| 19 | + |
| 20 | + const startingZIndexForVisibleLayers = computed(() => { |
| 21 | + if (backgroundLayers.value && backgroundLayers.value.length > 0) { |
| 22 | + return backgroundLayers.value.length - 1 |
| 23 | + } |
| 24 | + return 0 |
| 25 | + }) |
| 26 | + const visibleLayersHoldingAZIndex = computed(() => { |
| 27 | + const visibleLayersWithZIndex = [...backgroundLayers.value] |
| 28 | + if (is3dActive.value) { |
| 29 | + // in 3D, GeoJSON and KML layers are not given a ZIndex as Cesium handles them differently |
| 30 | + // (as primitive layers, on top of all other layers) |
| 31 | + visibleLayersWithZIndex.push( |
| 32 | + ...store.getters.visibleLayers.filter( |
| 33 | + (visibleLayer) => |
| 34 | + [LayerTypes.KML, LayerTypes.GEOJSON].indexOf(visibleLayer.type) === -1 |
| 35 | + ) |
| 36 | + ) |
| 37 | + } else { |
| 38 | + visibleLayersWithZIndex.push(...store.getters.visibleLayers) |
| 39 | + } |
| 40 | + return visibleLayersWithZIndex |
| 41 | + }) |
| 42 | + |
| 43 | + // from here: things that will be exported |
| 44 | + |
| 45 | + const startingZIndexForThingsOnTopOfLayers = computed( |
| 46 | + () => visibleLayersHoldingAZIndex.value.length |
| 47 | + ) |
| 48 | + const zIndexHighlightedFeatures = computed(() => startingZIndexForThingsOnTopOfLayers.value) |
| 49 | + const zIndexDroppedPin = computed(() => { |
| 50 | + let zIndex = zIndexHighlightedFeatures.value |
| 51 | + if (selectedFeatures.value.length > 0) { |
| 52 | + zIndex++ |
| 53 | + } |
| 54 | + return zIndex |
| 55 | + }) |
| 56 | + const zIndexPreviewPosition = computed(() => { |
| 57 | + let zIndex = zIndexDroppedPin.value |
| 58 | + if (pinnedLocation.value) { |
| 59 | + zIndex++ |
| 60 | + } |
| 61 | + return zIndex |
| 62 | + }) |
| 63 | + const zIndexCrossHair = computed(() => { |
| 64 | + let zIndex = zIndexPreviewPosition.value |
| 65 | + if (previewLocation.value) { |
| 66 | + zIndex++ |
| 67 | + } |
| 68 | + return zIndex |
| 69 | + }) |
| 70 | + const nextAvailableZIndex = computed(() => { |
| 71 | + let zIndex = zIndexCrossHair.value |
| 72 | + if (crossHair.value) { |
| 73 | + zIndex++ |
| 74 | + } |
| 75 | + return zIndex |
| 76 | + }) |
| 77 | + |
| 78 | + /** |
| 79 | + * Gives the z-index of a layer, taking into account if the map is shown in 3D or not. This |
| 80 | + * works for BG layer too. |
| 81 | + * |
| 82 | + * @param {AbstractLayer} layer A background or visible layer |
| 83 | + * @returns {Number} The Z-Index for this layer |
| 84 | + */ |
| 85 | + function getZIndexForLayer(layer) { |
| 86 | + if (!layer) { |
| 87 | + return -1 |
| 88 | + } |
| 89 | + // checking first of this is a BG layer |
| 90 | + const matchingBackgroundLayer = backgroundLayers.value.find( |
| 91 | + (bgLayer) => bgLayer.getID() === layer.getID() |
| 92 | + ) |
| 93 | + if (matchingBackgroundLayer) { |
| 94 | + return backgroundLayers.value.indexOf(matchingBackgroundLayer) |
| 95 | + } |
| 96 | + // if not found in the BG, we check the visible layers |
| 97 | + const matchingLayerInVisibleLayers = visibleLayersHoldingAZIndex.value.find( |
| 98 | + (visibleLayer) => visibleLayer.getID() === layer.getID() |
| 99 | + ) |
| 100 | + if (!matchingLayerInVisibleLayers) { |
| 101 | + return -1 |
| 102 | + } |
| 103 | + const indexOfLayerInVisibleLayers = visibleLayersHoldingAZIndex.value.indexOf( |
| 104 | + matchingLayerInVisibleLayers |
| 105 | + ) |
| 106 | + // checking if there are some group of layers before, meaning more than one z-index for this entry |
| 107 | + const subLayersPresentUnderThisLayer = visibleLayersHoldingAZIndex.value |
| 108 | + // only keeping previous layers (if layer is first, an empty array will be returned by slice(0, 0)) |
| 109 | + .slice(0, Math.max(indexOfLayerInVisibleLayers - 1, 0)) |
| 110 | + // only keeping groups of layers |
| 111 | + .filter((previousLayer) => previousLayer.type === LayerTypes.GROUP) |
| 112 | + // counting how many layers they have inside each group |
| 113 | + .map((previousGroup) => previousGroup.layers.length) |
| 114 | + // sum |
| 115 | + .reduce((a, b) => a + b, 0) |
| 116 | + return ( |
| 117 | + startingZIndexForVisibleLayers.value + |
| 118 | + subLayersPresentUnderThisLayer + |
| 119 | + indexOfLayerInVisibleLayers |
| 120 | + ) |
| 121 | + } |
| 122 | + |
| 123 | + return { |
| 124 | + zIndexHighlightedFeatures, |
| 125 | + zIndexDroppedPin, |
| 126 | + zIndexPreviewPosition, |
| 127 | + zIndexCrossHair, |
| 128 | + nextAvailableZIndex, |
| 129 | + getZIndexForLayer, |
| 130 | + } |
| 131 | +} |
0 commit comments