|
3 | 3 | </template>
|
4 | 4 | <script setup lang="ts">
|
5 | 5 | import type { Extent } from "ol/extent";
|
6 |
| -import useView from "@/composables/useView"; |
7 |
| -import type { ProjectionLike } from "ol/proj"; |
| 6 | +import { inject, watch, onMounted } from "vue"; |
| 7 | +import Projection from "ol/proj/Projection"; |
| 8 | +import type { AnimationOptions, FitOptions } from "ol/View"; |
| 9 | +import View, { type ViewOptions } from "ol/View"; |
| 10 | +import type Map from "ol/Map"; |
| 11 | +import type { Coordinate } from "ol/coordinate"; |
| 12 | +import type { Size } from "ol/size"; |
| 13 | +import type { Pixel } from "ol/pixel"; |
| 14 | +import type BaseEvent from "ol/events/Event"; |
| 15 | +import type { SimpleGeometry } from "ol/geom"; |
| 16 | +import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties"; |
| 17 | +import projectionFromProperties from "@/helpers/projection"; |
8 | 18 |
|
9 |
| -const props = withDefaults( |
10 |
| - defineProps<{ |
11 |
| - center?: unknown[]; |
12 |
| - constrainRotation?: boolean; |
13 |
| - enableRotation?: boolean; |
14 |
| - extent?: Extent; |
15 |
| - constrainOnlyCenter?: boolean; |
16 |
| - smoothExtentConstraint?: boolean; |
17 |
| - maxResolution?: number; |
18 |
| - minResolution?: number; |
19 |
| - maxZoom?: number; |
20 |
| - minZoom?: number; |
21 |
| - multiWorld?: boolean; |
22 |
| - constrainResolution?: boolean; |
23 |
| - smoothResolutionConstraint?: boolean; |
24 |
| - showFullExtent?: boolean; |
25 |
| - projection?: ProjectionLike; |
26 |
| - resolution?: number; |
27 |
| - resolutions?: unknown[]; |
28 |
| - rotation?: number; |
29 |
| - zoom?: number; |
30 |
| - zoomFactor?: number; |
31 |
| - padding?: number[]; |
32 |
| - }>(), |
33 |
| - { |
34 |
| - center: () => [0, 0], |
35 |
| - constrainRotation: true, |
36 |
| - enableRotation: true, |
37 |
| - constrainOnlyCenter: false, |
38 |
| - smoothExtentConstraint: true, |
39 |
| - maxZoom: 28, |
40 |
| - minZoom: 0, |
41 |
| - multiWorld: false, |
42 |
| - constrainResolution: false, |
43 |
| - smoothResolutionConstraint: true, |
44 |
| - showFullExtent: false, |
45 |
| - projection: "EPSG:3857", |
46 |
| - zoom: 0, |
47 |
| - zoomFactor: 2, |
48 |
| - padding: () => [0, 0, 0, 0], |
49 |
| - } |
50 |
| -); |
| 19 | +const props = withDefaults(defineProps<ViewOptions>(), { |
| 20 | + center: () => [0, 0], |
| 21 | + constrainRotation: true, |
| 22 | + enableRotation: true, |
| 23 | + constrainOnlyCenter: false, |
| 24 | + smoothExtentConstraint: true, |
| 25 | + maxZoom: 28, |
| 26 | + minZoom: 0, |
| 27 | + multiWorld: false, |
| 28 | + constrainResolution: false, |
| 29 | + smoothResolutionConstraint: true, |
| 30 | + showFullExtent: false, |
| 31 | + projection: "EPSG:3857", |
| 32 | + zoom: 0, |
| 33 | + zoomFactor: 2, |
| 34 | + padding: () => [0, 0, 0, 0], |
| 35 | +}); |
51 | 36 |
|
52 | 37 | const emit = defineEmits([
|
53 | 38 | "centerChanged",
|
54 | 39 | "zoomChanged",
|
55 | 40 | "resolutionChanged",
|
56 | 41 | "rotationChanged",
|
57 | 42 | ]);
|
58 |
| -const view = useView(props, emit); |
| 43 | +
|
| 44 | +const map = inject<Map>("map"); |
| 45 | +const { properties } = usePropsAsObjectProperties(props); |
| 46 | +
|
| 47 | +const createProp = () => { |
| 48 | + return { |
| 49 | + ...properties, |
| 50 | + projection: projectionFromProperties(properties.projection), |
| 51 | + }; |
| 52 | +}; |
| 53 | +const view = new View(createProp()); |
| 54 | +
|
| 55 | +onMounted(() => { |
| 56 | + map?.setView(view); |
| 57 | +}); |
| 58 | +
|
| 59 | +view.on("change:center", () => { |
| 60 | + emit("centerChanged", getCenter()); |
| 61 | + emit("zoomChanged", getZoom()); |
| 62 | +}); |
| 63 | +
|
| 64 | +view.on("change:resolution", () => emit("resolutionChanged", getResolution())); |
| 65 | +
|
| 66 | +view.on("change:rotation", () => emit("rotationChanged", getRotation())); |
| 67 | +
|
| 68 | +watch(properties, () => { |
| 69 | + const pr = createProp(); |
| 70 | + view.setProperties(pr); |
| 71 | + view.applyOptions_(pr); |
| 72 | +}); |
| 73 | +
|
| 74 | +const adjustCenter = (deltaCoordinates: Coordinate) => |
| 75 | + view.adjustCenter(deltaCoordinates); |
| 76 | +const adjustResolution = (ratio: number, optAnchor?: Coordinate) => |
| 77 | + view.adjustResolution(ratio, optAnchor); |
| 78 | +const adjustRotation = (delta: number, optAnchor?: Coordinate) => |
| 79 | + view.adjustRotation(delta, optAnchor); |
| 80 | +const adjustZoom = (delta: number, optAnchor?: Coordinate) => |
| 81 | + view.adjustZoom(delta, optAnchor); |
| 82 | +const animate = (...args: (AnimationOptions | ((arg0: boolean) => void))[]) => |
| 83 | + view.animate(...args); |
| 84 | +const beginInteraction = () => view.beginInteraction(); |
| 85 | +const calculateExtent = (optSize?: Size | undefined): Extent => |
| 86 | + view.calculateExtent(optSize); |
| 87 | +const cancelAnimations = () => view.cancelAnimations(); |
| 88 | +const centerOn = (coordinate: Coordinate, size: Size, position: Pixel) => |
| 89 | + view.centerOn(coordinate, size, position); |
| 90 | +const changed = () => view.changed(); |
| 91 | +const dispatchEvent = (event: string | BaseEvent): boolean | undefined => |
| 92 | + view.dispatchEvent(event); |
| 93 | +const endInteraction = ( |
| 94 | + optDuration?: number | undefined, |
| 95 | + optResolutionDirection?: number | undefined, |
| 96 | + optAnchor?: Coordinate | undefined |
| 97 | +) => view.endInteraction(optDuration, optResolutionDirection, optAnchor); |
| 98 | +const fit = ( |
| 99 | + geometryOrExtent: Extent | SimpleGeometry, |
| 100 | + options?: FitOptions | undefined |
| 101 | +) => view.fit(geometryOrExtent, options); |
| 102 | +const get = (key: string): any => view.get(key); |
| 103 | +const getAnimating = (): boolean => view.getAnimating(); |
| 104 | +const getCenter = (): Coordinate | undefined => view.getCenter(); |
| 105 | +const getInteracting = (): boolean => view.getInteracting(); |
| 106 | +const getKeys = (): string[] => view.getKeys(); |
| 107 | +const getMaxResolution = (): number => view.getMaxResolution(); |
| 108 | +const getMaxZoom = (): number => view.getMaxZoom(); |
| 109 | +const getMinResolution = (): number => view.getMinResolution(); |
| 110 | +const getMinZoom = (): number => view.getMinZoom(); |
| 111 | +const getProjection = (): Projection => view.getProjection(); |
| 112 | +const getProperties = () => view.getProperties(); |
| 113 | +const getResolution = () => view.getResolution(); |
| 114 | +const getResolutionForExtent = (extent: Extent, size?: Size | undefined) => |
| 115 | + view.getResolutionForExtent(extent, size); |
| 116 | +const getResolutionForZoom = (zoom: number): number => |
| 117 | + view.getResolutionForZoom(zoom); |
| 118 | +const getResolutions = (): number[] | undefined => view.getResolutions(); |
| 119 | +const getRevision = (): number => view.getRevision(); |
| 120 | +const getRotation = (): number => view.getRotation(); |
| 121 | +const getZoom = (): number | undefined => view.getZoom(); |
| 122 | +const getZoomForResolution = (resolution: number): number | undefined => |
| 123 | + view.getZoomForResolution(resolution); |
| 124 | +
|
| 125 | +const setCenter = (center: Coordinate | undefined) => view.setCenter(center); |
| 126 | +const setConstrainResolution = (enabled: boolean) => |
| 127 | + view.setConstrainResolution(enabled); |
| 128 | +const setMaxZoom = (zoom: number) => view.setMaxZoom(zoom); |
| 129 | +const setMinZoom = (zoom: number) => view.setMinZoom(zoom); |
| 130 | +const setResolution = (resolution: number | undefined) => |
| 131 | + view.setResolution(resolution); |
| 132 | +const setRotation = (rotation: number) => view.setRotation(rotation); |
| 133 | +const setZoom = (zoom: number) => view.setZoom(zoom); |
59 | 134 |
|
60 | 135 | defineExpose({
|
61 |
| - ...view, |
| 136 | + view, |
| 137 | + adjustCenter, |
| 138 | + adjustResolution, |
| 139 | + adjustRotation, |
| 140 | + adjustZoom, |
| 141 | + animate, |
| 142 | + beginInteraction, |
| 143 | + calculateExtent, |
| 144 | + cancelAnimations, |
| 145 | + centerOn, |
| 146 | + changed, |
| 147 | + dispatchEvent, |
| 148 | + endInteraction, |
| 149 | + fit, |
| 150 | + get, |
| 151 | + getAnimating, |
| 152 | + getCenter, |
| 153 | + getInteracting, |
| 154 | + getKeys, |
| 155 | + getMaxResolution, |
| 156 | + getMaxZoom, |
| 157 | + getMinResolution, |
| 158 | + getMinZoom, |
| 159 | + getProjection, |
| 160 | + getProperties, |
| 161 | + getResolution, |
| 162 | + getResolutionForExtent, |
| 163 | + getResolutionForZoom, |
| 164 | + getResolutions, |
| 165 | + getRevision, |
| 166 | + getRotation, |
| 167 | + getZoom, |
| 168 | + getZoomForResolution, |
| 169 | + setCenter, |
| 170 | + setConstrainResolution, |
| 171 | + setMaxZoom, |
| 172 | + setMinZoom, |
| 173 | + setResolution, |
| 174 | + setRotation, |
| 175 | + setZoom, |
62 | 176 | });
|
63 | 177 | </script>
|
64 | 178 |
|
|
0 commit comments