#49 feat: componente genérico CytoscapeChart con evento select

This commit is contained in:
2026-08-15 17:16:53 -05:00
parent 8335ad2313
commit efa666dcb3
4 changed files with 179 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
<template>
<div
ref="containerRef"
class="cytoscape-chart"
:style="{ height: `${height}px` }"
/>
</template>
<script setup>
import { onBeforeUnmount, onMounted, ref, watch } from 'vue'
import cytoscape from 'cytoscape'
const props = defineProps({
elements: {
type: Array,
default: () => [],
},
styles: {
type: Array,
default: () => [],
},
layout: {
type: Object,
default: () => ({ name: 'preset' }),
},
height: {
type: Number,
default: 400,
},
})
const emit = defineEmits(['select'])
const containerRef = ref(null)
let cy = null
function runLayout () {
cy.layout(props.layout).run()
}
onMounted(() => {
cy = cytoscape({
container: containerRef.value,
elements: props.elements,
style: props.styles,
layout: props.layout,
})
cy.on('tap', 'node', event => {
emit('select', event.target.data())
})
})
watch(
() => props.elements,
elements => {
if (!cy) return
cy.elements().remove()
cy.add(elements)
runLayout()
},
{ deep: true }
)
onBeforeUnmount(() => {
if (cy) cy.destroy()
cy = null
})
</script>
<style scoped>
.cytoscape-chart {
width: 100%;
min-height: 200px;
}
</style>