Skip to content

Commit ba0c2de

Browse files
author
Mark Robinson
committed
Loading of svg dynamically with selection/hover effects on graph
1 parent cf76af4 commit ba0c2de

File tree

2 files changed

+52
-51
lines changed

2 files changed

+52
-51
lines changed

src/main/resources/static/js/workflow.js

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ requirejs.config({
4040
/**
4141
* Make the graph pannable and zoomable
4242
*/
43-
require(['jquery', 'bootstrap.modal', 'svg-pan-zoom', 'hammerjs'],
43+
require(['jquery', 'bootstrap.modal', 'svg-pan-zoom', 'hammerjs', 'jquery.svg'],
4444
function ($, modal, svgPanZoom, hammerjs) {
4545
/**
4646
* Custom hammer event handler to add mobile support
@@ -100,57 +100,62 @@ require(['jquery', 'bootstrap.modal', 'svg-pan-zoom', 'hammerjs'],
100100
};
101101

102102
// Loading from external URL needs to be done to enable events
103-
$(selector).svg({loadURL: 'lion.svg'});
103+
$("#graph").svg({
104+
loadURL: $("#graph").attr("data-svgurl"),
105+
onLoad: enablePanZoom
106+
});
104107

105108
/**
106109
* Enable svg-pan-zoom on the graph
107110
*/
108-
var graph = svgPanZoom('#graph', {
109-
zoomEnabled: true,
110-
controlIconsEnabled: true,
111-
customEventsHandler: eventHandler,
112-
preventMouseEventsDefault: false
113-
});
111+
function enablePanZoom() {
112+
var graph = svgPanZoom('#graph svg', {
113+
zoomEnabled: true,
114+
controlIconsEnabled: true,
115+
customEventsHandler: eventHandler,
116+
preventMouseEventsDefault: false
117+
});
114118

115-
// Resizing window also resizes the graph
116-
$(window).resize(function(){
117-
graph.resize();
118-
graph.fit();
119-
graph.center();
120-
});
119+
// Resizing window also resizes the graph
120+
$(window).resize(function(){
121+
graph.resize();
122+
graph.fit();
123+
graph.center();
124+
});
121125

122-
// Enable svg-pan-zoom on fullscreen modal when opened
123-
$('#fullScreenGraphModal').on('shown.bs.modal', function (e) {
124-
// Timeout allows for modal to show
125-
setTimeout(function() {
126-
var fullGraph = svgPanZoom('#graphFullscreen', {
127-
zoomEnabled: true,
128-
controlIconsEnabled: true,
129-
customEventsHandler: eventHandler
130-
});
126+
// Enable svg-pan-zoom on fullscreen modal when opened
127+
$('#fullScreenGraphModal').on('shown.bs.modal', function (e) {
128+
// Timeout allows for modal to show
129+
setTimeout(function() {
130+
var fullGraph = svgPanZoom('#graphFullscreen', {
131+
zoomEnabled: true,
132+
controlIconsEnabled: true,
133+
customEventsHandler: eventHandler
134+
});
131135

132-
// Set to same zoom/pan as other graph
133-
fullGraph.zoom(graph.getZoom());
134-
fullGraph.pan(graph.getPan());
136+
// Set to same zoom/pan as other graph
137+
fullGraph.zoom(graph.getZoom());
138+
fullGraph.pan(graph.getPan());
135139

136-
// Link the two graphs panning and zooming
137-
fullGraph.setOnZoom(function(level){
138-
graph.zoom(level);
139-
graph.pan(fullGraph.getPan());
140-
});
140+
// Link the two graphs panning and zooming
141+
fullGraph.setOnZoom(function(level){
142+
graph.zoom(level);
143+
graph.pan(fullGraph.getPan());
144+
});
141145

142-
fullGraph.setOnPan(function(point){
143-
graph.pan(point);
144-
});
146+
fullGraph.setOnPan(function(point){
147+
graph.pan(point);
148+
});
145149

146-
// Resizing window also resizes the graph
147-
$(window).resize(function(){
148-
fullGraph.resize();
149-
fullGraph.fit();
150-
fullGraph.center();
151-
});
152-
}, 100);
153-
});
150+
// Resizing window also resizes the graph
151+
$(window).resize(function(){
152+
fullGraph.resize();
153+
fullGraph.fit();
154+
fullGraph.center();
155+
});
156+
}, 100);
157+
});
158+
};
154159
});
155160

156161
/**
@@ -251,6 +256,7 @@ require(['jquery', 'jquery.svg', 'jquery.svgdom'],
251256
*/
252257
$("tr").not('thead tr').on({
253258
click: function() {
259+
$("polygon.selected, tr.selected").removeClass("selected");
254260
getGraphBox(this).toggleClass("selected");
255261
$(this).toggleClass("selected");
256262
},
@@ -280,22 +286,19 @@ require(['jquery', 'jquery.svg', 'jquery.svgdom'],
280286
/**
281287
* When a graph box is hovered over/clicked, highlight
282288
*/
283-
var svg = $('#graph').svg('get');
284-
$($(".node"), svg.root()).on({
289+
$(document).on({
285290
click: function() {
286-
alert("click");
291+
$("polygon.selected, tr.selected").removeClass("selected");
287292
getTableRow(this).toggleClass("selected");
288293
$(this).find("polygon").toggleClass("selected");
289294
},
290295
mouseenter: function() {
291-
alert("mouseenter");
292296
getTableRow(this).addClass("hover");
293297
$(this).find("polygon").addClass("hover");
294298
},
295299
mouseleave: function() {
296-
alert("mouseleave");
297300
getTableRow(this).removeClass("hover");
298301
$(this).find("polygon").removeClass("hover");
299302
}
300-
});
303+
}, ".node");
301304
});

src/main/resources/templates/workflow.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ <h2>Workflow: <span th:text="${workflow.label}">Workflow Name</span></h2>
152152
<div class="col-md-12">
153153
<div id="visualisation" class="jumbotron">
154154
<span id="fullscreen-open" data-toggle="modal" data-target="#fullScreenGraphModal" class="glyphicon glyphicon-resize-full"></span>
155-
<object id="graph" th:data="@{'/workflows/' + ${workflow.id} + '/graph/svg'}" type="image/svg+xml">
156-
<img th:src="@{'/workflows/' + ${workflow.id} + '/graph/png'}" alt="workflow graph" />
157-
</object>
155+
<div id="graph" data-th-attr="data-svgurl=@{'/workflows/' + ${workflow.id} + '/graph/svg'}"></div>
158156
</div>
159157
<div th:if="${workflow.dockerLink != null}">
160158
<h2 style="float:left;">Requires: </h2>

0 commit comments

Comments
 (0)