@m3ui-vue/m3ui-vue 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/dist/API.md +636 -20
  2. package/dist/MMenuItem-DFyXflk9.js +707 -0
  3. package/dist/MMenuItem-DFyXflk9.js.map +1 -0
  4. package/dist/MSlider-CdNIH7jc.js +1376 -0
  5. package/dist/MSlider-CdNIH7jc.js.map +1 -0
  6. package/dist/MTextField-B4X92vH_.js +671 -0
  7. package/dist/MTextField-B4X92vH_.js.map +1 -0
  8. package/dist/api.json +1 -1
  9. package/dist/chart.js +1 -1
  10. package/dist/chart.js.map +1 -1
  11. package/dist/code-editor.js +1 -1
  12. package/dist/components/MIconButton.vue.d.ts +1 -1
  13. package/dist/components/MMap.vue.d.ts +74 -0
  14. package/dist/components/MMapBaseLayers.vue.d.ts +15 -0
  15. package/dist/components/MMapControl.vue.d.ts +23 -0
  16. package/dist/components/MMapFeatureInfo.vue.d.ts +25 -0
  17. package/dist/components/MMapFilter.vue.d.ts +19 -0
  18. package/dist/components/MMapGeoJson.vue.d.ts +34 -0
  19. package/dist/components/MMapHover.vue.d.ts +17 -0
  20. package/dist/components/MMapIdentify.vue.d.ts +37 -0
  21. package/dist/components/MMapLayers.vue.d.ts +31 -0
  22. package/dist/components/MMapMarker.vue.d.ts +42 -0
  23. package/dist/components/MMapMeasure.vue.d.ts +43 -0
  24. package/dist/components/MMapPopup.vue.d.ts +43 -0
  25. package/dist/components/MMapSearch.vue.d.ts +28 -0
  26. package/dist/components/MShader.vue.d.ts +9 -3
  27. package/dist/components/_MMapLayerItem.vue.d.ts +25 -0
  28. package/dist/composables/_hydration.d.ts +4 -0
  29. package/dist/composables/_mapGeometry.d.ts +13 -0
  30. package/dist/composables/_ssrState.d.ts +30 -0
  31. package/dist/composables/_storageKeys.d.ts +3 -0
  32. package/dist/composables/_useThemeTokens.d.ts +7 -0
  33. package/dist/composables/mapAdapters.d.ts +111 -0
  34. package/dist/composables/mapSearch.d.ts +31 -0
  35. package/dist/composables/themeInitScript.d.ts +22 -0
  36. package/dist/composables/useColorPalette.d.ts +176 -6
  37. package/dist/composables/useLocale.d.ts +30 -0
  38. package/dist/composables/useMap.d.ts +47 -0
  39. package/dist/composables/useMapFilter.d.ts +10 -0
  40. package/dist/composables/useMapLayers.d.ts +30 -0
  41. package/dist/composables/useTheme.d.ts +2 -0
  42. package/dist/index.d.ts +4 -1
  43. package/dist/locales/index.js +217 -7
  44. package/dist/locales/index.js.map +1 -1
  45. package/dist/m3ui-vue.css +1 -1
  46. package/dist/m3ui.js +6742 -7961
  47. package/dist/m3ui.js.map +1 -1
  48. package/dist/map.d.ts +24 -0
  49. package/dist/map.js +1931 -0
  50. package/dist/map.js.map +1 -0
  51. package/dist/plugin.d.ts +25 -2
  52. package/dist/rich-text-editor.js +27 -26
  53. package/dist/rich-text-editor.js.map +1 -1
  54. package/dist/styles.css +1 -1
  55. package/dist/{useLocale-CuvWRZ1h.js → useLocale-b5-hzsL3.js} +32 -2
  56. package/dist/useLocale-b5-hzsL3.js.map +1 -0
  57. package/package.json +11 -2
  58. package/src/__tests__/MFab.test.ts +52 -6
  59. package/src/__tests__/MMap.test.ts +292 -0
  60. package/src/__tests__/MMapLayers.test.ts +257 -0
  61. package/src/__tests__/MMapTools.test.ts +233 -0
  62. package/src/__tests__/MShader.test.ts +16 -0
  63. package/src/__tests__/_fakeMaplibre.ts +158 -0
  64. package/src/__tests__/hydration.test.ts +119 -0
  65. package/src/__tests__/ssr.test.ts +171 -0
  66. package/src/__tests__/ssrState.test.ts +155 -0
  67. package/src/__tests__/themeInitScript.test.ts +122 -0
  68. package/src/components/MChart.vue +3 -2
  69. package/src/components/MFab.vue +29 -7
  70. package/src/components/MIconButton.vue +1 -1
  71. package/src/components/MLightbox.vue +10 -1
  72. package/src/components/MMap.vue +522 -0
  73. package/src/components/MMapBaseLayers.vue +67 -0
  74. package/src/components/MMapControl.vue +61 -0
  75. package/src/components/MMapFeatureInfo.vue +41 -0
  76. package/src/components/MMapFilter.vue +80 -0
  77. package/src/components/MMapGeoJson.vue +136 -0
  78. package/src/components/MMapHover.vue +131 -0
  79. package/src/components/MMapIdentify.vue +103 -0
  80. package/src/components/MMapLayers.vue +111 -0
  81. package/src/components/MMapMarker.vue +102 -0
  82. package/src/components/MMapMeasure.vue +452 -0
  83. package/src/components/MMapPopup.vue +99 -0
  84. package/src/components/MMapSearch.vue +162 -0
  85. package/src/components/MSearchBar.vue +4 -1
  86. package/src/components/MShader.vue +11 -3
  87. package/src/components/_MMapLayerItem.vue +77 -0
  88. package/src/composables/_hydration.ts +10 -0
  89. package/src/composables/_mapGeometry.ts +75 -0
  90. package/src/composables/_ssrState.ts +80 -0
  91. package/src/composables/_storageKeys.ts +6 -0
  92. package/src/composables/_useThemeTokens.ts +42 -0
  93. package/src/composables/mapAdapters.ts +208 -0
  94. package/src/composables/mapSearch.ts +56 -0
  95. package/src/composables/themeInitScript.ts +51 -0
  96. package/src/composables/useColorPalette.ts +32 -14
  97. package/src/composables/useLocale.ts +62 -0
  98. package/src/composables/useMap.ts +98 -0
  99. package/src/composables/useMapFilter.ts +45 -0
  100. package/src/composables/useMapLayers.ts +124 -0
  101. package/src/composables/useTheme.ts +18 -7
  102. package/src/composables/useWindowSizeClass.ts +23 -4
  103. package/src/index.ts +4 -1
  104. package/src/locales/de.ts +31 -1
  105. package/src/locales/es.ts +31 -1
  106. package/src/locales/fr.ts +31 -1
  107. package/src/locales/ja.ts +31 -1
  108. package/src/locales/ko.ts +31 -1
  109. package/src/locales/pt.ts +31 -1
  110. package/src/locales/zh.ts +31 -1
  111. package/src/map.ts +32 -0
  112. package/src/plugin.ts +77 -13
  113. package/dist/MTextField-BPbFeI9M.js +0 -1368
  114. package/dist/MTextField-BPbFeI9M.js.map +0 -1
  115. package/dist/useLocale-CuvWRZ1h.js.map +0 -1
@@ -0,0 +1,41 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * Identify results (from `MMapIdentify`'s `identify` event, or `identifyFeatures()`): a card per feature with its
4
+ * layer's name and its formatted properties. `MMapIdentify` shows it in a popup; use it in a panel of your own (an
5
+ * `MWindow`, an `MSideSheet`) the same way. Values are shown as text. Available from the `/map` entry point.
6
+ *
7
+ * @slot value A property's value (scope: `{ field, result }`), e.g. to turn URLs into links
8
+ */
9
+ import MIcon from './MIcon.vue'
10
+ import { useLocale } from '../composables/useLocale'
11
+ import type { MapIdentifyResult } from '../composables/mapAdapters'
12
+
13
+ defineProps<{
14
+ /** What was found */
15
+ results: MapIdentifyResult[]
16
+ }>()
17
+
18
+ const t = useLocale()
19
+ </script>
20
+
21
+ <template>
22
+ <div class="flex flex-col gap-2">
23
+ <div v-for="(result, i) in results" :key="i" class="overflow-hidden rounded-2xl bg-surface-container">
24
+ <div class="flex items-center gap-2 bg-primary-container/40 px-4 py-2">
25
+ <MIcon name="layers" :size="16" class="text-on-primary-container" />
26
+ <span class="text-label-large text-on-primary-container">{{ result.layer.title }}</span>
27
+ </div>
28
+ <dl class="flex flex-col px-4 py-1">
29
+ <div v-for="field in result.fields" :key="field.key" class="flex flex-col gap-0.5 border-b border-outline-variant py-1.5 last:border-b-0">
30
+ <dt class="text-label-small text-on-surface-variant">{{ field.label }}</dt>
31
+ <dd class="text-body-medium text-on-surface">
32
+ <slot name="value" :field="field" :result="result">
33
+ <span v-if="field.value" class="break-words">{{ field.value }}</span>
34
+ <span v-else class="text-body-small italic text-on-surface-variant">{{ t.mapNoValue }}</span>
35
+ </slot>
36
+ </dd>
37
+ </div>
38
+ </dl>
39
+ </div>
40
+ </div>
41
+ </template>
@@ -0,0 +1,80 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * A simple filter for one layer of an `MMap`: chips for the values of one of its properties; the layer shows only the
4
+ * features with the selected ones (all of them with none selected). The values are `values`, or else the distinct
5
+ * ones among the layer's features loaded in the map. The style's own filter on the layer stays. For anything more
6
+ * involved, build your own on `useMapFilter()`. Available from the `/map` entry point.
7
+ */
8
+ import { computed, onBeforeUnmount, ref, watch } from 'vue'
9
+ import MChip from './MChip.vue'
10
+ import { useMap } from '../composables/useMap'
11
+ import { useMapFilter } from '../composables/useMapFilter'
12
+
13
+ const props = defineProps<{
14
+ /** The layer's id */
15
+ layer: string
16
+ /** The property to filter by */
17
+ property: string
18
+ /** The values to offer (labels in `labels`); by default the ones found in the layer's loaded features */
19
+ values?: (string | number)[]
20
+ /** Labels for the values, by value */
21
+ labels?: Record<string, string>
22
+ /** The selected values (v-model) */
23
+ modelValue?: (string | number)[]
24
+ }>()
25
+
26
+ const emit = defineEmits<{ 'update:modelValue': [values: (string | number)[]] }>()
27
+
28
+ const { onReady, styleVersion } = useMap()
29
+ const { setFilter, valuesOf } = useMapFilter()
30
+
31
+ const found = ref<(string | number)[]>([])
32
+ const options = computed(() => props.values ?? found.value)
33
+ const local = ref<(string | number)[]>([])
34
+ const selected = computed(() => props.modelValue ?? local.value)
35
+
36
+ function apply() {
37
+ const values = selected.value
38
+ setFilter(props.layer, values.length ? ['in', ['get', props.property], ['literal', values]] : null)
39
+ }
40
+
41
+ function toggle(value: string | number) {
42
+ const next = selected.value.includes(value) ? selected.value.filter((v) => v !== value) : [...selected.value, value]
43
+ local.value = next
44
+ emit('update:modelValue', next)
45
+ }
46
+
47
+ function refreshValues(map: { once: (type: 'idle', fn: () => void) => unknown }) {
48
+ const read = () => { if (!props.values) found.value = valuesOf(props.layer, props.property) }
49
+ read()
50
+ // And once the layer's data is in, if it wasn't yet (tiles load after the style)
51
+ map.once('idle', read)
52
+ }
53
+
54
+ onReady((map) => {
55
+ refreshValues(map)
56
+ apply()
57
+ })
58
+ watch(selected, apply, { deep: true })
59
+ watch(styleVersion, () => apply())
60
+ watch(() => [props.layer, props.property], ([layer], [old]) => {
61
+ if (layer !== old) setFilter(old as string, null)
62
+ apply()
63
+ })
64
+ onBeforeUnmount(() => setFilter(props.layer, null))
65
+ </script>
66
+
67
+ <template>
68
+ <div class="flex flex-wrap gap-2">
69
+ <MChip
70
+ v-for="value in options"
71
+ :key="value"
72
+ type="filter"
73
+ clickable
74
+ :selected="selected.includes(value)"
75
+ @click="toggle(value)"
76
+ >
77
+ {{ labels?.[String(value)] ?? value }}
78
+ </MChip>
79
+ </div>
80
+ </template>
@@ -0,0 +1,136 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * GeoJSON on an `MMap`: a source from `data` (an object, or a URL MapLibre fetches) drawn with `layers`. Without
4
+ * `layers` it draws every geometry in the theme's primary, following palette and dark mode: polygons filled with an
5
+ * outline, lines, and points as circles. Replace `data` (a new object) to update it. It's added again after a style
6
+ * change. Available from the `/map` entry point.
7
+ *
8
+ * @event click A click on one of its features (MapLibre's `MapLayerMouseEvent`: `e.features` holds them)
9
+ */
10
+ import { computed, onBeforeUnmount, useId, watch } from 'vue'
11
+ import type * as Maplibre from 'maplibre-gl'
12
+ import { useMap } from '../composables/useMap'
13
+ import { useThemeTokens } from '../composables/_useThemeTokens'
14
+
15
+ // Omit over each member of the union (a plain Omit keeps only what all layer types share, losing paint and layout)
16
+ type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never
17
+
18
+ /** A layer of `MMapGeoJson`: any MapLibre layer without its `source` (and `id`, made up when left out). */
19
+ export type MapGeoJsonLayer = DistributiveOmit<Exclude<Maplibre.LayerSpecification, Maplibre.BackgroundLayerSpecification>, 'id' | 'source'> & { id?: string }
20
+
21
+ // It renders nothing: no element for attributes to land on
22
+ defineOptions({ inheritAttrs: false })
23
+
24
+ const props = withDefaults(
25
+ defineProps<{
26
+ /** A GeoJSON object, or the URL of one */
27
+ data: Maplibre.GeoJSONSourceSpecification['data']
28
+ /** The source's id (and the prefix of its layers' made-up ids). Unique on the map; generated when left out */
29
+ id?: string
30
+ /** How to draw it: MapLibre layers without `source`. Defaults to theme-colored fill, outline, line and circle layers */
31
+ layers?: MapGeoJsonLayer[]
32
+ /** Insert the layers below this layer id (e.g. under the style's labels) */
33
+ before?: string
34
+ /** Show a pointer over its features (set it when you handle `click`) */
35
+ interactive?: boolean
36
+ /** More options for the GeoJSON source (`cluster`, `promoteId`, `tolerance`…) */
37
+ sourceOptions?: Partial<Omit<Maplibre.GeoJSONSourceSpecification, 'type' | 'data'>>
38
+ }>(),
39
+ { interactive: false },
40
+ )
41
+
42
+ const emit = defineEmits<{ click: [event: Maplibre.MapLayerMouseEvent] }>()
43
+
44
+ const sourceId = props.id ?? `m3-geojson-${useId()}`
45
+ const { onReady, styleVersion } = useMap()
46
+ const theme = useThemeTokens(['primary', 'surface'] as const)
47
+
48
+ const is = (...types: string[]): Maplibre.FilterSpecification => ['match', ['geometry-type'], types, true, false]
49
+
50
+ /** The theme-colored default layers. */
51
+ const defaultLayers = computed<MapGeoJsonLayer[]>(() => {
52
+ const { primary, surface } = theme.value
53
+ return [
54
+ { id: `${sourceId}-fill`, type: 'fill', filter: is('Polygon', 'MultiPolygon'), paint: { 'fill-color': primary, 'fill-opacity': 0.24 } },
55
+ { id: `${sourceId}-line`, type: 'line', filter: is('Polygon', 'MultiPolygon', 'LineString', 'MultiLineString'), paint: { 'line-color': primary, 'line-width': 2 } },
56
+ {
57
+ id: `${sourceId}-circle`,
58
+ type: 'circle',
59
+ filter: is('Point', 'MultiPoint'),
60
+ paint: { 'circle-color': primary, 'circle-radius': 6, 'circle-stroke-color': surface, 'circle-stroke-width': 2 },
61
+ },
62
+ ]
63
+ })
64
+
65
+ const layers = computed(() =>
66
+ (props.layers ?? defaultLayers.value).map((l, i) => ({ ...l, id: l.id ?? `${sourceId}-${i}`, source: sourceId }) as Maplibre.LayerSpecification),
67
+ )
68
+
69
+ let map: Maplibre.Map | undefined
70
+ let added: string[] = []
71
+
72
+ const onClick = (e: Maplibre.MapLayerMouseEvent) => emit('click', e)
73
+ const onEnter = () => { if (props.interactive && map) map.getCanvas().style.cursor = 'pointer' }
74
+ const onLeave = () => { if (props.interactive && map) map.getCanvas().style.cursor = '' }
75
+
76
+ function removeLayers() {
77
+ if (!map) return
78
+ for (const id of added) {
79
+ map.off('click', id, onClick)
80
+ map.off('mouseenter', id, onEnter)
81
+ map.off('mouseleave', id, onLeave)
82
+ if (map.getLayer(id)) map.removeLayer(id)
83
+ }
84
+ added = []
85
+ }
86
+
87
+ function addLayers() {
88
+ if (!map) return
89
+ removeLayers()
90
+ for (const layer of layers.value) {
91
+ map.addLayer(layer, props.before && map.getLayer(props.before) ? props.before : undefined)
92
+ map.on('click', layer.id, onClick)
93
+ map.on('mouseenter', layer.id, onEnter)
94
+ map.on('mouseleave', layer.id, onLeave)
95
+ added.push(layer.id)
96
+ }
97
+ }
98
+
99
+ /** The source and its layers, unless they're there already (a style change drops them). */
100
+ function add() {
101
+ if (!map) return
102
+ if (!map.getSource(sourceId)) {
103
+ map.addSource(sourceId, { type: 'geojson', data: props.data, ...props.sourceOptions })
104
+ added = []
105
+ }
106
+ if (!added.length || !map.getLayer(added[0]!)) addLayers()
107
+ }
108
+
109
+ onReady((m) => {
110
+ map = m
111
+ add()
112
+ })
113
+
114
+ watch(styleVersion, () => add())
115
+ watch(() => props.data, (data) => (map?.getSource(sourceId) as Maplibre.GeoJSONSource | undefined)?.setData(data))
116
+ // Layers by value: an inline `:layers="[...]"` is a new array on every parent render
117
+ watch(() => JSON.stringify(layers.value), () => addLayers())
118
+ watch(() => props.before, () => addLayers())
119
+
120
+ onBeforeUnmount(() => {
121
+ if (!map) return
122
+ removeLayers()
123
+ if (map.getSource(sourceId)) map.removeSource(sourceId)
124
+ map = undefined
125
+ })
126
+
127
+ defineExpose({
128
+ /** The source's id */
129
+ sourceId,
130
+ /** The ids of its layers on the map */
131
+ layerIds: () => [...added],
132
+ })
133
+ </script>
134
+
135
+ <!-- Nothing of its own: it draws on the map -->
136
+ <template></template>
@@ -0,0 +1,131 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * Hover info for an `MMap` with an `adapter`: while on (v-model), the layers with `hover` fields show them for the
4
+ * feature under the pointer, in a small popup that follows it. With `control` (on by default) it adds its own on/off
5
+ * button to the map's controls. Pointer devices only: touch has no hover. Available from the `/map` entry point.
6
+ *
7
+ * @event update:modelValue Turned on or off with its button
8
+ */
9
+ import { computed, onBeforeUnmount, ref, shallowRef, watch } from 'vue'
10
+ import type * as Maplibre from 'maplibre-gl'
11
+ import MIcon from './MIcon.vue'
12
+ import MMapControl from './MMapControl.vue'
13
+ import MMapPopup from './MMapPopup.vue'
14
+ import { useLocale } from '../composables/useLocale'
15
+ import { useMap } from '../composables/useMap'
16
+ import { useMapLayers } from '../composables/useMapLayers'
17
+ import { formatFieldValue, fieldLabel, type MapLayerInfo } from '../composables/mapAdapters'
18
+ import type { MapControlPosition } from './MMap.vue'
19
+
20
+ const props = withDefaults(
21
+ defineProps<{
22
+ /** On (v-model) */
23
+ modelValue?: boolean
24
+ /** Its on/off button: `false`, or a corner (`true` is top-right) */
25
+ control?: boolean | MapControlPosition
26
+ }>(),
27
+ { modelValue: false, control: true },
28
+ )
29
+
30
+ const emit = defineEmits<{ 'update:modelValue': [on: boolean] }>()
31
+
32
+ const t = useLocale()
33
+ const { onReady } = useMap()
34
+ const { model } = useMapLayers()
35
+
36
+ const hoverLayers = computed(() => (model.value?.layers ?? []).filter((l) => l.hover && l.hover.fields.length))
37
+ const shown = shallowRef<{ layer: MapLayerInfo; fields: { key: string; label: string; value: string }[] } | null>(null)
38
+ const lngLat = ref<[number, number]>([0, 0])
39
+ const open = ref(false)
40
+
41
+ let map: Maplibre.Map | undefined
42
+ let bound: string[] = []
43
+
44
+ function move(e: Maplibre.MapLayerMouseEvent) {
45
+ const feature = e.features?.[0]
46
+ const layer = hoverLayers.value.find((l) => l.id === feature?.layer.id)
47
+ if (!feature || !layer || !layer.hover) return
48
+ const props = feature.properties ?? {}
49
+ shown.value = {
50
+ layer,
51
+ fields: layer.hover.fields
52
+ .map((f) => ({ key: f.key, label: fieldLabel(f.key, f), value: formatFieldValue(props[f.key], f) }))
53
+ .filter((f) => f.value),
54
+ }
55
+ lngLat.value = [e.lngLat.lng, e.lngLat.lat]
56
+ open.value = true
57
+ }
58
+ function leave() {
59
+ open.value = false
60
+ }
61
+
62
+ function bind() {
63
+ if (!map) return
64
+ for (const id of bound) {
65
+ map.off('mousemove', id, move)
66
+ map.off('mouseleave', id, leave)
67
+ }
68
+ bound = props.modelValue ? hoverLayers.value.map((l) => l.id) : []
69
+ for (const id of bound) {
70
+ map.on('mousemove', id, move)
71
+ map.on('mouseleave', id, leave)
72
+ }
73
+ if (!props.modelValue) open.value = false
74
+ }
75
+
76
+ onReady((m) => {
77
+ map = m
78
+ bind()
79
+ })
80
+ watch([() => props.modelValue, hoverLayers], bind)
81
+ onBeforeUnmount(() => {
82
+ for (const id of bound) {
83
+ map?.off('mousemove', id, move)
84
+ map?.off('mouseleave', id, leave)
85
+ }
86
+ })
87
+
88
+ const corner = computed(() => (props.control === true ? 'top-right' : props.control || 'top-right'))
89
+ </script>
90
+
91
+ <template>
92
+ <div class="hidden">
93
+ <MMapControl v-if="control && hoverLayers.length" :position="corner">
94
+ <button
95
+ type="button"
96
+ class="grid place-items-center"
97
+ :title="t.mapHoverInfo"
98
+ :aria-label="t.mapHoverInfo"
99
+ :aria-pressed="modelValue"
100
+ @click="emit('update:modelValue', !modelValue)"
101
+ >
102
+ <MIcon name="ads_click" :size="20" />
103
+ </button>
104
+ </MMapControl>
105
+ <MMapPopup
106
+ v-if="shown"
107
+ v-model:open="open"
108
+ :lng-lat="lngLat"
109
+ :close-button="false"
110
+ :close-on-click="false"
111
+ :offset="12"
112
+ max-width="280px"
113
+ class="m3-map-hover"
114
+ >
115
+ <p class="-mx-4 -mt-3 mb-1 bg-primary-container px-3 py-1.5 text-label-medium text-on-primary-container">{{ shown.layer.title }}</p>
116
+ <dl class="flex flex-col">
117
+ <div v-for="field in shown.fields" :key="field.key" class="border-b border-outline-variant py-1 last:border-b-0">
118
+ <dt class="text-label-small text-on-surface-variant">{{ field.label }}</dt>
119
+ <dd class="text-body-small text-on-surface">{{ field.value }}</dd>
120
+ </div>
121
+ </dl>
122
+ </MMapPopup>
123
+ </div>
124
+ </template>
125
+
126
+ <style>
127
+ /* The hover popup ignores the pointer, so it never steals the mousemove that keeps it open */
128
+ .maplibregl-popup:has(.m3-map-hover) {
129
+ pointer-events: none;
130
+ }
131
+ </style>
@@ -0,0 +1,103 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * Click-to-identify for an `MMap` with an `adapter`: a click on the map lists the features of the layers marked to
4
+ * identify under it (with their formatted properties) in a popup, with a marker where the user clicked. The pointer
5
+ * turns into a hand over those layers. The `identify` event carries the results, to show them in a panel of your own
6
+ * instead (`:popup="false"`, then `MMapFeatureInfo` in an `MWindow`). Available from the `/map` entry point.
7
+ *
8
+ * @slot default The popup's content instead of `MMapFeatureInfo` (scope: `{ results, lngLat, close }`)
9
+ * @event identify The user clicked the map: what was found there (empty when nothing) and where (`[lng, lat]`)
10
+ */
11
+ import { onBeforeUnmount, ref, shallowRef, watch } from 'vue'
12
+ import type * as Maplibre from 'maplibre-gl'
13
+ import MMapPopup from './MMapPopup.vue'
14
+ import MMapMarker from './MMapMarker.vue'
15
+ import MMapFeatureInfo from './MMapFeatureInfo.vue'
16
+ import { useMap } from '../composables/useMap'
17
+ import { useMapLayers } from '../composables/useMapLayers'
18
+ import { identifyFeatures, type MapIdentifyResult } from '../composables/mapAdapters'
19
+
20
+ // Attributes (class, style…) go on the popup's content
21
+ defineOptions({ inheritAttrs: false })
22
+
23
+ const props = withDefaults(
24
+ defineProps<{
25
+ /** Listen to clicks. It also pauses by itself while another tool owns them (`MMapMeasure` measuring) */
26
+ enabled?: boolean
27
+ /** Show the results in a popup at the click */
28
+ popup?: boolean
29
+ /** Mark where the user clicked while there are results */
30
+ marker?: boolean
31
+ }>(),
32
+ { enabled: true, popup: true, marker: true },
33
+ )
34
+
35
+ const emit = defineEmits<{ identify: [results: MapIdentifyResult[], lngLat: [number, number]] }>()
36
+
37
+ const { on, onReady, activeTool } = useMap()
38
+ const { model } = useMapLayers()
39
+
40
+ const results = shallowRef<MapIdentifyResult[]>([])
41
+ const lngLat = ref<[number, number]>([0, 0])
42
+ const open = ref(false)
43
+
44
+ on('click', (e) => {
45
+ // Another tool (measuring…) owns the clicks
46
+ if (!props.enabled || activeTool.value) return
47
+ const map = e.target as Maplibre.Map
48
+ results.value = identifyFeatures(map, e.point, model.value)
49
+ lngLat.value = [e.lngLat.lng, e.lngLat.lat]
50
+ open.value = results.value.length > 0
51
+ emit('identify', results.value, lngLat.value)
52
+ })
53
+
54
+ function close() {
55
+ open.value = false
56
+ }
57
+ watch(() => props.enabled, (enabled) => { if (!enabled) close() })
58
+
59
+ // A hand over the layers that answer to a click
60
+ let map: Maplibre.Map | undefined
61
+ let hovered: string[] = []
62
+ const enter = () => { if (props.enabled && !activeTool.value && map) map.getCanvas().style.cursor = 'pointer' }
63
+ const leave = () => { if (map) map.getCanvas().style.cursor = '' }
64
+ function bindCursor() {
65
+ if (!map) return
66
+ for (const id of hovered) {
67
+ map.off('mouseenter', id, enter)
68
+ map.off('mouseleave', id, leave)
69
+ }
70
+ hovered = (model.value?.layers ?? []).filter((l) => l.identify).map((l) => l.id)
71
+ for (const id of hovered) {
72
+ map.on('mouseenter', id, enter)
73
+ map.on('mouseleave', id, leave)
74
+ }
75
+ }
76
+ onReady((m) => {
77
+ map = m
78
+ bindCursor()
79
+ })
80
+ watch(model, bindCursor)
81
+ onBeforeUnmount(() => {
82
+ hovered.forEach((id) => {
83
+ map?.off('mouseenter', id, enter)
84
+ map?.off('mouseleave', id, leave)
85
+ })
86
+ })
87
+
88
+ defineExpose({ close })
89
+ </script>
90
+
91
+ <template>
92
+ <!-- One root (the marker and the popup move their content into the map) -->
93
+ <div class="hidden">
94
+ <MMapMarker v-if="marker && open" :lng-lat="lngLat" />
95
+ <MMapPopup v-if="popup && results.length" v-model:open="open" :lng-lat="lngLat" max-width="360px" :offset="marker ? 40 : 8">
96
+ <div class="max-h-80 overflow-y-auto" v-bind="$attrs">
97
+ <slot :results="results" :lng-lat="lngLat" :close="close">
98
+ <MMapFeatureInfo :results="results" />
99
+ </slot>
100
+ </div>
101
+ </MMapPopup>
102
+ </div>
103
+ </template>
@@ -0,0 +1,111 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * The layer list of an `MMap` with an `adapter`: a search field, the style's groups (collapsible, with a visibility
4
+ * toggle for the whole group and a count) and its layers, each with a visibility toggle and, expanded, its
5
+ * description, an opacity slider, its legend and a download link. Only the content: put it in an `MWindow`, an
6
+ * `MSideSheet`, an `MBottomSheet` or an `MMapControl`. Available from the `/map` entry point.
7
+ *
8
+ * @slot actions Extra actions in an expanded layer (scope: `{ layer, visible }`)
9
+ */
10
+ import { computed, ref } from 'vue'
11
+ import MIcon from './MIcon.vue'
12
+ import MTextField from './MTextField.vue'
13
+ import MMapLayerItem from './_MMapLayerItem.vue'
14
+ import { useLocale } from '../composables/useLocale'
15
+ import { useMapLayers } from '../composables/useMapLayers'
16
+ import type { MapLayerInfo } from '../composables/mapAdapters'
17
+
18
+ const props = withDefaults(
19
+ defineProps<{
20
+ /** Show the search field */
21
+ search?: boolean
22
+ /** Groups start collapsed */
23
+ collapsed?: boolean
24
+ }>(),
25
+ { search: true, collapsed: false },
26
+ )
27
+
28
+ const t = useLocale()
29
+ const { model, layersOf, isVisible, groupState, setGroupVisible } = useMapLayers()
30
+
31
+ const query = ref('')
32
+ const matches = (layer: MapLayerInfo) => !query.value || layer.title.toLowerCase().includes(query.value.toLowerCase())
33
+
34
+ const groups = computed(() =>
35
+ (model.value?.groups ?? [])
36
+ .map((g) => ({ ...g, all: layersOf(g.name), layers: layersOf(g.name).filter(matches) }))
37
+ .filter((g) => g.layers.length),
38
+ )
39
+ const ungrouped = computed(() => layersOf(null).filter(matches))
40
+
41
+ // Collapsed groups and expanded layers (by default the other way around, with `collapsed`)
42
+ const toggledGroups = ref(new Set<string>())
43
+ const isOpen = (group: string) => toggledGroups.value.has(group) === props.collapsed
44
+ function toggleGroup(group: string) {
45
+ const next = new Set(toggledGroups.value)
46
+ if (!next.delete(group)) next.add(group)
47
+ toggledGroups.value = next
48
+ }
49
+ const expanded = ref(new Set<string>())
50
+ function toggleExpanded(id: string) {
51
+ const next = new Set(expanded.value)
52
+ if (!next.delete(id)) next.add(id)
53
+ expanded.value = next
54
+ }
55
+
56
+ const visibleCount = (layers: MapLayerInfo[]) => layers.filter((l) => isVisible(l.id)).length
57
+ </script>
58
+
59
+ <template>
60
+ <div class="flex flex-col gap-3">
61
+ <MTextField v-if="search" v-model="query" :label="t.mapSearchLayers" leading-icon="search" variant="outlined" clearable />
62
+
63
+ <section v-for="group in groups" :key="group.name" class="flex flex-col">
64
+ <div class="m3-map-layers-row flex cursor-pointer items-center gap-2 rounded-xl px-2 py-1.5" @click="toggleGroup(group.name)">
65
+ <button
66
+ type="button"
67
+ class="grid size-8 shrink-0 cursor-pointer place-items-center rounded-full transition-colors duration-200"
68
+ :class="groupState(group.name) === 'visible' ? 'bg-primary-container text-on-primary-container'
69
+ : groupState(group.name) === 'partial' ? 'bg-secondary-container text-on-secondary-container'
70
+ : 'bg-on-surface/8 text-on-surface-variant'"
71
+ :aria-label="groupState(group.name) === 'hidden' ? t.mapShowLayer : t.mapHideLayer"
72
+ :aria-pressed="groupState(group.name) !== 'hidden'"
73
+ @click.stop="setGroupVisible(group.name, groupState(group.name) !== 'visible')"
74
+ >
75
+ <MIcon :name="groupState(group.name) === 'hidden' ? 'visibility_off' : 'visibility'" :size="18" />
76
+ </button>
77
+ <span class="flex-1 select-none truncate text-label-large text-on-surface">{{ group.title }}</span>
78
+ <span class="rounded-full bg-surface-container-highest px-2 py-0.5 text-label-small tabular-nums text-on-surface-variant">
79
+ {{ visibleCount(group.all) }}/{{ group.all.length }}
80
+ </span>
81
+ <MIcon name="expand_more" :size="18" class="text-on-surface-variant transition-transform duration-300" :class="isOpen(group.name) ? 'rotate-180' : ''" />
82
+ </div>
83
+ <div class="grid transition-[grid-template-rows] duration-300 ease-[cubic-bezier(0.2,0,0,1)]" :style="{ gridTemplateRows: isOpen(group.name) ? '1fr' : '0fr' }">
84
+ <div class="flex min-h-0 flex-col gap-1.5 overflow-hidden" :class="isOpen(group.name) ? 'pt-1.5' : ''">
85
+ <MMapLayerItem v-for="layer in group.layers" :key="layer.id" :layer="layer" :expanded="expanded.has(layer.id)" @toggle-expanded="toggleExpanded(layer.id)">
86
+ <template v-if="$slots.actions" #actions="scope"><slot name="actions" v-bind="scope" /></template>
87
+ </MMapLayerItem>
88
+ </div>
89
+ </div>
90
+ </section>
91
+
92
+ <div v-if="ungrouped.length" class="flex flex-col gap-1.5">
93
+ <MMapLayerItem v-for="layer in ungrouped" :key="layer.id" :layer="layer" :expanded="expanded.has(layer.id)" @toggle-expanded="toggleExpanded(layer.id)">
94
+ <template v-if="$slots.actions" #actions="scope"><slot name="actions" v-bind="scope" /></template>
95
+ </MMapLayerItem>
96
+ </div>
97
+
98
+ <div v-if="!groups.length && !ungrouped.length" class="flex flex-col items-center gap-3 py-8">
99
+ <div class="grid size-14 place-items-center rounded-full bg-surface-container-highest">
100
+ <MIcon name="layers_clear" :size="28" class="text-on-surface-variant" />
101
+ </div>
102
+ <p class="text-body-medium text-on-surface-variant">{{ t.mapNoLayers }}</p>
103
+ </div>
104
+ </div>
105
+ </template>
106
+
107
+ <style>
108
+ .m3-map-layers-row:hover {
109
+ background-color: color-mix(in srgb, var(--color-on-surface) 6%, transparent);
110
+ }
111
+ </style>