Skip to content

Commit 8c241ff

Browse files
committed
refactor(ol-interaction-clusterselect): migrate to script setup
1 parent 7738f1f commit 8c241ff

File tree

4 files changed

+130
-149
lines changed

4 files changed

+130
-149
lines changed

docs/componentsguide/interactions/clusterselect/index.md

Lines changed: 38 additions & 53 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
```

src/components/interaction/ClusterSelectInteraction.vue

Lines changed: 0 additions & 93 deletions
This file was deleted.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<template lang="">
2+
<slot> </slot>
3+
</template>
4+
5+
<script setup>
6+
import { provide, inject, watch, onMounted, onUnmounted, computed } from "vue";
7+
8+
import Select from "ol-ext/interaction/SelectCluster";
9+
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
10+
11+
const emit = defineEmits(["select"]);
12+
const props = defineProps({
13+
multi: {
14+
type: Boolean,
15+
default: false,
16+
},
17+
condition: {
18+
type: Function,
19+
},
20+
filter: {
21+
type: Function,
22+
},
23+
pointRadius: {
24+
type: Number,
25+
default: 7,
26+
},
27+
animate: {
28+
type: Boolean,
29+
default: true,
30+
},
31+
animationDuration: {
32+
type: Number,
33+
default: 500,
34+
},
35+
spiral: {
36+
type: Boolean,
37+
default: true,
38+
},
39+
selectCluster: {
40+
type: Boolean,
41+
default: true,
42+
},
43+
autoClose: {
44+
type: Boolean,
45+
default: true,
46+
},
47+
circleMaxObjects: {
48+
type: Number,
49+
},
50+
maxObjects: {
51+
type: Number,
52+
},
53+
featureStyle: {
54+
type: Function,
55+
},
56+
});
57+
58+
const map = inject("map");
59+
60+
const { properties } = usePropsAsObjectProperties(props);
61+
62+
const select = computed(() => {
63+
const s = new Select(properties);
64+
s.on("select", (event) => {
65+
emit("select", event);
66+
});
67+
68+
return s;
69+
});
70+
71+
watch(select, (newVal, oldVal) => {
72+
map.removeInteraction(oldVal);
73+
map.addInteraction(newVal);
74+
75+
map.changed();
76+
});
77+
78+
onMounted(() => {
79+
map.addInteraction(select.value);
80+
});
81+
82+
onUnmounted(() => {
83+
map.removeInteraction(select.value);
84+
});
85+
86+
provide("stylable", select);
87+
</script>
88+
89+
<style lang=""></style>

src/components/interaction/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import OlClusterSelectInteraction from "./OlClusterSelectInteraction.vue";
12
import SelectInteraction from "./SelectInteraction.vue";
2-
import ClusterSelectInteraction from "./ClusterSelectInteraction.vue";
33
import DrawInteraction from "./DrawInteraction.vue";
44
import SnapInteraction from "./SnapInteraction.vue";
55
import ModifyInteraction from "./ModifyInteraction.vue";
@@ -13,8 +13,8 @@ function install(app) {
1313

1414
install.installed = true;
1515

16+
app.component("ol-interaction-clusterselect", OlClusterSelectInteraction);
1617
app.component(SelectInteraction.name, SelectInteraction);
17-
app.component(ClusterSelectInteraction.name, ClusterSelectInteraction);
1818
app.component(DrawInteraction.name, DrawInteraction);
1919
app.component(SnapInteraction.name, SnapInteraction);
2020
app.component(ModifyInteraction.name, ModifyInteraction);
@@ -29,8 +29,8 @@ export default install;
2929

3030
export {
3131
install,
32+
OlClusterSelectInteraction,
3233
SelectInteraction,
33-
ClusterSelectInteraction,
3434
DrawInteraction,
3535
SnapInteraction,
3636
ModifyInteraction,

0 commit comments

Comments
 (0)