|
| 1 | +<script lang="ts" setup> |
| 2 | +import { Eye, EyeOff } from 'lucide-vue-next' |
| 3 | +import { CollectionType, LayerLink } from '../types' |
| 4 | +import type { Ref } from 'vue' |
| 5 | +
|
| 6 | +let { collection, onChangeActive, activeValue } = defineProps<{ |
| 7 | + collection: CollectionType & { href: string } |
| 8 | + activeValue?: LayerLink |
| 9 | + onChangeActive(link?: LayerLink): void |
| 10 | +}>() |
| 11 | +
|
| 12 | +let baseURL = collection.href.replace('/collection.json', '') |
| 13 | +
|
| 14 | +let summaries = computed(() => { |
| 15 | + return collection?.summaries |
| 16 | +}) |
| 17 | +
|
| 18 | +let propertyOrder = computed(() => Object.keys(summaries.value)) |
| 19 | +
|
| 20 | +let variables: Ref<Record<string, string | undefined>> = ref({}) |
| 21 | +
|
| 22 | +function resetVariables() { |
| 23 | + variables.value = Object.keys(summaries.value ?? {}).reduce((acc, key) => { |
| 24 | + return { |
| 25 | + ...acc, |
| 26 | + [key]: undefined, |
| 27 | + } |
| 28 | + }, {} as Record<string, string | undefined>) |
| 29 | +} |
| 30 | +
|
| 31 | +resetVariables() |
| 32 | +
|
| 33 | +function getValidOptionsForProperty(property: string) { |
| 34 | + if (!variables.value) return [] |
| 35 | +
|
| 36 | + let currentIndex = propertyOrder.value.indexOf(property) |
| 37 | + let relevantProperties = propertyOrder.value.slice(0, currentIndex) |
| 38 | +
|
| 39 | + let validItems = collection.links |
| 40 | + .filter((link) => link.rel === 'item') |
| 41 | + .filter((link) => { |
| 42 | + if (!variables.value) return false |
| 43 | + return Object.entries(variables.value) |
| 44 | + .filter(([key]) => relevantProperties.includes(key)) |
| 45 | + .every( |
| 46 | + ([key, option]) => |
| 47 | + !option || |
| 48 | + link.properties?.[key as keyof typeof link.properties] === option, |
| 49 | + ) |
| 50 | + }) |
| 51 | +
|
| 52 | + return [ |
| 53 | + ...new Set( |
| 54 | + validItems.map( |
| 55 | + (item) => item?.properties?.[property as keyof typeof item.properties], |
| 56 | + ), |
| 57 | + ), |
| 58 | + ] |
| 59 | +} |
| 60 | +
|
| 61 | +function selectOption(property: string, option: string) { |
| 62 | + if (!variables.value) return |
| 63 | +
|
| 64 | + if (variables.value[property] === option) { |
| 65 | + variables.value[property] = undefined |
| 66 | + } else { |
| 67 | + variables.value[property] = option |
| 68 | + } |
| 69 | +
|
| 70 | + let currentIndex = propertyOrder.value.indexOf(property) |
| 71 | + for (let i = currentIndex + 1; i < propertyOrder.value.length; i++) { |
| 72 | + let nextProperty = propertyOrder.value[i] |
| 73 | + if (!variables.value) break |
| 74 | +
|
| 75 | + variables.value[nextProperty] = undefined |
| 76 | +
|
| 77 | + let validOptions = getValidOptionsForProperty(nextProperty) |
| 78 | + if (validOptions.length === 1) { |
| 79 | + variables.value[nextProperty] = validOptions[0] |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | +
|
| 84 | +watchEffect(() => { |
| 85 | + if (!summaries.value || !variables.value) return |
| 86 | +
|
| 87 | + let foundLink = collection.links |
| 88 | + .filter((l) => l.rel === 'item') |
| 89 | + .find((link) => { |
| 90 | + return Object.entries(variables.value ?? {}).every( |
| 91 | + ([key, option]) => |
| 92 | + link.properties?.[key as keyof typeof link.properties] === option, |
| 93 | + ) |
| 94 | + }) |
| 95 | +
|
| 96 | + if (foundLink) { |
| 97 | + onChangeActive({ |
| 98 | + type: 'item', |
| 99 | + href: baseURL + '/' + foundLink.href, |
| 100 | + }) |
| 101 | + } else { |
| 102 | + onChangeActive(undefined) |
| 103 | + } |
| 104 | +}) |
| 105 | +
|
| 106 | +function toggleActive(value: boolean) { |
| 107 | + if (!value) { |
| 108 | + onChangeActive(undefined) |
| 109 | + resetVariables() |
| 110 | + } else { |
| 111 | + if (summaries.value) { |
| 112 | + // do nothing |
| 113 | + } else { |
| 114 | + const geoserverLink = collection.assets?.['geoserver_link'] as |
| 115 | + | { href: string } |
| 116 | + | undefined |
| 117 | + if (geoserverLink) { |
| 118 | + onChangeActive({ |
| 119 | + type: 'raster', |
| 120 | + href: geoserverLink.href, |
| 121 | + }) |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | +</script> |
| 127 | + |
| 128 | +<template> |
| 129 | + <div v-if="!summaries"> |
| 130 | + <div class="flex items-center gap-3 bg-white p-3 shadow"> |
| 131 | + <button |
| 132 | + @click="toggleActive(!activeValue)" |
| 133 | + class="size-8 flex items-center justify-center shrink-0 rounded-md hover:bg-gray-100" |
| 134 | + > |
| 135 | + <Eye class="size-4" v-if="!!activeValue" /> |
| 136 | + <EyeOff class="size-4" v-if="!activeValue" /> |
| 137 | + </button> |
| 138 | + <div class="text-sm font-medium">{{ collection.title }}</div> |
| 139 | + </div> |
| 140 | + </div> |
| 141 | + <v-expansion-panel v-if="summaries"> |
| 142 | + <v-expansion-panel-title> |
| 143 | + <div class="flex items-center gap-3"> |
| 144 | + <button |
| 145 | + @click.stop="!!activeValue && toggleActive(false)" |
| 146 | + class="z-10 size-8 flex items-center justify-center shrink-0 rounded-md hover:bg-gray-100" |
| 147 | + > |
| 148 | + <Eye class="size-4" v-if="!!activeValue" /> |
| 149 | + <EyeOff class="size-4" v-if="!activeValue" /> |
| 150 | + </button> |
| 151 | + <div>{{ collection.title }}</div> |
| 152 | + </div> |
| 153 | + </v-expansion-panel-title> |
| 154 | + <v-expansion-panel-text> |
| 155 | + <div class="grid grid-flow-row gap-1.5 py-3"> |
| 156 | + <div |
| 157 | + v-for="(options, key) in summaries" |
| 158 | + :key="key" |
| 159 | + class="empty:hidden" |
| 160 | + > |
| 161 | + <v-select |
| 162 | + :label="key" |
| 163 | + :items="getValidOptionsForProperty(key)" |
| 164 | + :model-value="variables?.[key]" |
| 165 | + @update:model-value="selectOption(key, $event)" |
| 166 | + /> |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + </v-expansion-panel-text> |
| 170 | + </v-expansion-panel> |
| 171 | +</template> |
0 commit comments