diff --git a/web/main.js b/web/main.js index e07318b..797c527 100644 --- a/web/main.js +++ b/web/main.js @@ -7,23 +7,26 @@ import getLocation from './location.js' const { scheme, host } = getLocation(window); const client = createClient(scheme, host); -const parse = function(blob) { +const parse = (blob) => { const parser = new DOMParser(); const doc = parser.parseFromString(blob, "text/xml"); return doc.documentElement; } -const loadDocument = function(config) { +const loadDocument = (config) => { return client.showWorkflow("/workflow/default", config); } -const loadGraph = function() { +let panzoomInstance; + +const loadGraph = () => { loadDocument().then((resp) => { try { const viewport = document.querySelector("#viewport"); viewport.innerHTML = ""; viewport.appendChild(parse(resp.data)); - panzoom(viewport, { minZoom: 0.01, zoomSpeed: 0.3, autocenter: true }).zoomAbs(0, 0, 0.1); + panzoomInstance = panzoom(viewport, { minZoom: 0.01, zoomSpeed: 0.3, autocenter: false }); + panzoomInstance.zoomAbs(0, 0, 0.1); } catch (error) { console.error(error); alert("An error occurred while loading the graph."); @@ -34,7 +37,7 @@ const loadGraph = function() { }) }; -const saveGraph = function() { +const saveGraph = () => { loadDocument({ responseType: "blob" }).then((resp) => { saveAs(resp.data, "workflow.svg", {type: "image/svg+xml"}); }).catch((error) => { @@ -43,11 +46,49 @@ const saveGraph = function() { }) } +const search = (text) => { + const svg = document.querySelector('svg'); + const textElements = svg.querySelectorAll('*'); + const matchingNodesSet = new Set(); + const lctext = text.toLowerCase(); + + for (let element of textElements) { + if (element.textContent.toLowerCase().includes(lctext)) { + let parent = element.parentElement; + while (parent) { + if (parent.classList.contains('node')) { + matchingNodesSet.add(parent); + break; + } + parent = parent.parentElement; + } + } + } + + return Array.from(matchingNodesSet); +} + +const zoomElement = (element) => { + const svg = document.querySelector('svg'); + const rect = element.getBBox(); + const point = svg.createSVGPoint(); + point.x = rect.x + rect.width / 2; + point.y = rect.y + rect.height / 2; + const globalPoint = point.matrixTransform(element.getScreenCTM()); + const viewportWidth = window.innerWidth || document.documentElement.clientWidth; + const viewportHeight = window.innerHeight || document.documentElement.clientHeight; + const panX = (viewportWidth / 2 - globalPoint.x); + const panY = (viewportHeight / 2 - globalPoint.y); + panzoomInstance.moveBy(panX, panY, false); +} + +// Write the initial layout of the application. document.querySelector('#app').innerHTML = `