Skip to content

Commit 7d8a25f

Browse files
committed
refactor(ol-animated-clusterlayer): migrate to script setup
1 parent 5c917d0 commit 7d8a25f

File tree

11 files changed

+217
-201
lines changed

11 files changed

+217
-201
lines changed

docs/componentsguide/layers/animatedclusterlayer/index.md

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import AnimatedClusterDemo from "@demos/AnimatedClusterDemo.vue"
1717
<ol-map
1818
:loadTilesWhileAnimating="true"
1919
:loadTilesWhileInteracting="true"
20-
style="height:400px"
20+
style="height: 400px"
2121
>
2222
<ol-view
2323
ref="view"
@@ -73,62 +73,47 @@ import AnimatedClusterDemo from "@demos/AnimatedClusterDemo.vue"
7373
</ol-map>
7474
</template>
7575
76-
<script>
76+
<script setup>
7777
import { ref } from "vue";
7878
7979
import markerIcon from "@/assets/marker.png";
8080
81-
export default {
82-
setup() {
83-
const center = ref([40, 40]);
84-
const projection = ref("EPSG:4326");
85-
const zoom = ref(5);
86-
const rotation = ref(0);
87-
88-
const overrideStyleFunction = (feature, style) => {
89-
let clusteredFeatures = feature.get("features");
90-
let size = clusteredFeatures.length;
91-
92-
let color = size > 20 ? "192,0,0" : size > 8 ? "255,128,0" : "0,128,0";
93-
var radius = Math.max(8, Math.min(size, 20));
94-
let dash = (2 * Math.PI * radius) / 6;
95-
let calculatedDash = [0, dash, dash, dash, dash, dash, dash];
96-
97-
style.getImage().getStroke().setLineDash(dash);
98-
style
99-
.getImage()
100-
.getStroke()
101-
.setColor("rgba(" + color + ",0.5)");
102-
style.getImage().getStroke().setLineDash(calculatedDash);
103-
style
104-
.getImage()
105-
.getFill()
106-
.setColor("rgba(" + color + ",1)");
107-
108-
style.getImage().setRadius(radius);
109-
110-
style.getText().setText(size.toString());
111-
};
112-
113-
const getRandomInRange = (from, to, fixed) => {
114-
return (Math.random() * (to - from) + from).toFixed(fixed) * 1;
115-
};
116-
117-
const featureSelected = (event) => {
118-
console.log(event);
119-
};
120-
121-
return {
122-
center,
123-
projection,
124-
zoom,
125-
rotation,
126-
markerIcon,
127-
getRandomInRange,
128-
overrideStyleFunction,
129-
featureSelected,
130-
};
131-
},
81+
const center = ref([40, 40]);
82+
const projection = ref("EPSG:4326");
83+
const zoom = ref(5);
84+
const rotation = ref(0);
85+
86+
const overrideStyleFunction = (feature, style) => {
87+
const clusteredFeatures = feature.get("features");
88+
const size = clusteredFeatures.length;
89+
90+
const color = size > 20 ? "192,0,0" : size > 8 ? "255,128,0" : "0,128,0";
91+
const radius = Math.max(8, Math.min(size, 20));
92+
const dash = (2 * Math.PI * radius) / 6;
93+
const calculatedDash = [0, dash, dash, dash, dash, dash, dash];
94+
95+
style.getImage().getStroke().setLineDash(dash);
96+
style
97+
.getImage()
98+
.getStroke()
99+
.setColor("rgba(" + color + ",0.5)");
100+
style.getImage().getStroke().setLineDash(calculatedDash);
101+
style
102+
.getImage()
103+
.getFill()
104+
.setColor("rgba(" + color + ",1)");
105+
106+
style.getImage().setRadius(radius);
107+
108+
style.getText().setText(size.toString());
109+
};
110+
111+
const getRandomInRange = (from, to, fixed) => {
112+
return (Math.random() * (to - from) + from).toFixed(fixed) * 1;
113+
};
114+
115+
const featureSelected = (event) => {
116+
console.log(event);
132117
};
133118
</script>
134119
```
@@ -144,14 +129,14 @@ A CSS class name to set to the layer element.
144129

145130
### opacity
146131

147-
- **Type**: `number `
132+
- **Type**: `number`
148133
- **Default**: `1`
149134

150135
Opacity (0, 1).
151136

152137
### visible
153138

154-
- **Type**: `boolean `
139+
- **Type**: `boolean`
155140
- **Default**: `true`
156141

157142
Visibility.

docs/componentsguide/layers/imagelayer/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Opacity (0, 1).
9494

9595
### visible
9696

97-
- **Type**: `boolean `
97+
- **Type**: `boolean`
9898
- **Default**: `true`
9999

100100
Visibility.

docs/componentsguide/layers/tilelayer/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Opacity (0, 1).
9191

9292
### visible
9393

94-
- **Type**: `boolean `
94+
- **Type**: `boolean`
9595
- **Default**: `true`
9696

9797
Visibility.

docs/componentsguide/layers/vectorimagelayer/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Opacity (0, 1).
8989

9090
### visible
9191

92-
- **Type**: `boolean `
92+
- **Type**: `boolean`
9393
- **Default**: `true`
9494

9595
Visibility.

docs/componentsguide/layers/vectorlayer/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Opacity (0, 1).
8989

9090
### visible
9191

92-
- **Type**: `boolean `
92+
- **Type**: `boolean`
9393
- **Default**: `true`
9494

9595
Visibility.

docs/componentsguide/layers/webgltilelayer/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Opacity (0, 1).
6262

6363
### visible
6464

65-
- **Type**: `boolean `
65+
- **Type**: `boolean`
6666
- **Default**: `true`
6767

6868
Visibility.

src/components/layers/AnimatedClusterLayer.vue

Lines changed: 0 additions & 86 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<template lang="">
2+
<div>
3+
<slot></slot>
4+
</div>
5+
</template>
6+
7+
<script setup>
8+
import { inject, provide, onUnmounted, onMounted, watch, computed } from "vue";
9+
import { Cluster } from "ol/source";
10+
import { easeOut } from "ol/easing";
11+
12+
import AnimatedCluster from "ol-ext/layer/AnimatedCluster";
13+
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
14+
import useBaseLayerProps from "@/composables/useBaseLayerProps";
15+
16+
const props = defineProps({
17+
...useBaseLayerProps(),
18+
animationDuration: {
19+
type: Number,
20+
default: 700,
21+
},
22+
distance: {
23+
type: Number,
24+
default: 20,
25+
},
26+
animationMethod: {
27+
type: Function,
28+
default: easeOut,
29+
},
30+
updateWhileAnimating: {
31+
type: Boolean,
32+
default: false,
33+
},
34+
updateWhileInteracting: {
35+
type: Boolean,
36+
default: false,
37+
},
38+
});
39+
40+
const map = inject("map");
41+
42+
const { properties } = usePropsAsObjectProperties(props);
43+
44+
const vectorLayer = computed(() => {
45+
const ac = new AnimatedCluster({
46+
...properties,
47+
source: new Cluster({
48+
distance: properties.distance,
49+
geometryFunction: (feature) => feature.getGeometry(),
50+
}),
51+
});
52+
53+
return ac;
54+
});
55+
56+
const source = computed(() => vectorLayer.value.getSource());
57+
58+
watch(properties, () => {
59+
vectorLayer.value.setProperties(properties);
60+
vectorLayer.value.changed();
61+
});
62+
63+
onMounted(() => {
64+
map.addLayer(vectorLayer.value);
65+
vectorLayer.value.changed();
66+
map.changed();
67+
});
68+
69+
onUnmounted(() => {
70+
map.removeLayer(vectorLayer.value);
71+
});
72+
73+
provide("vectorLayer", source);
74+
provide("stylable", vectorLayer);
75+
76+
defineExpose({
77+
vectorLayer,
78+
map,
79+
});
80+
</script>
81+
82+
<style lang=""></style>

src/components/layers/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import WebGLTileLayer from "./WebGLTileLayer.vue";
33
import ImageLayer from "./ImageLayer.vue";
44
import VectorImageLayer from "./VectorImageLayer.vue";
55
import VectorLayer from "./VectorLayer.vue";
6-
import AnimatedClusterLayer from "./AnimatedClusterLayer.vue";
6+
import OlAnimatedClusterLayer from "./OlAnimatedClusterLayer.vue";
77
import WebglPointsLayer from "./WebglPointsLayer.vue";
88
import HeatmapLayer from "./HeatmapLayer.vue";
99

@@ -14,12 +14,12 @@ function install(app) {
1414

1515
install.installed = true;
1616

17+
app.component("ol-animated-clusterlayer", OlAnimatedClusterLayer);
1718
app.component(TileLayer.name, TileLayer);
1819
app.component(WebGLTileLayer.name, WebGLTileLayer);
1920
app.component(ImageLayer.name, ImageLayer);
2021
app.component(VectorImageLayer.name, VectorImageLayer);
2122
app.component(VectorLayer.name, VectorLayer);
22-
app.component(AnimatedClusterLayer.name, AnimatedClusterLayer);
2323
app.component(WebglPointsLayer.name, WebglPointsLayer);
2424
app.component(HeatmapLayer.name, HeatmapLayer);
2525
}
@@ -28,12 +28,12 @@ export default install;
2828

2929
export {
3030
install,
31+
OlAnimatedClusterLayer,
3132
TileLayer,
3233
WebGLTileLayer,
3334
ImageLayer,
3435
VectorImageLayer,
3536
VectorLayer,
36-
AnimatedClusterLayer,
3737
WebglPointsLayer,
3838
HeatmapLayer,
3939
};

0 commit comments

Comments
 (0)