Files
netdisco/share/views/ajax/device/netmap.tt
2017-12-19 23:24:47 +00:00

106 lines
3.5 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script>
$.getJSON('[% uri_for('/ajax/data/device/netmap') %]', {q: '[% params.q %]'}, function(mapdata) {
// custom resize function as there is no event to fire and we need
// to react to the sidebar.
function resizeGraphContainer() {
setTimeout(function(){
var netmap_pane = jQuery('#netmap_pane');
graph.width( parseInt(netmap_pane.parent().css('width')) ).resume();
graph.height( window.innerHeight - 100 ).resume();
}, 500)
}
jQuery(document).ready(function() {
window.graph = netGobrechtsD3Force('netmap_pane')
.debug(true)
.lassoMode(true)
.dragMode(true)
.nodeEventToOpenLink('click')
.nodeLinkTarget('none')
.labelDistance(2)
.zoomMode(true)
.pinMode(true)
.nodeEventToStopPinMode('none')
.showLinkDirection(false)
// .preventLabelOverlappingOnForceEnd(true)
// .zoomToFitOnForceEnd(true)
.width( parseInt(jQuery('#netmap_pane').parent().css('width')) )
.height( window.innerHeight - 100 )
.showSelfLinks(true)
.minZoomFactor(0.1)
.maxZoomFactor(10)
.charge(-550);
jQuery('#nd_sidebar-toggle-img-in').on("click", resizeGraphContainer);
jQuery('#nd_sidebar-toggle-img-out').on("click", resizeGraphContainer);
jQuery(window).on("resize", resizeGraphContainer);
graph.inspect().main.force.on('end.setupfornetdisco', function() {
graph.inspect().main.nodes.each(function(n) { n.fixed = true });
// FIXME
$.post(
'[% uri_for('/ajax/data/device/netmappositions') %]'
,'positions=' + JSON.stringify(graph.positions())
);
graph['nd2'] = {};
graph['nd2']['dragging'] = false;
graph.inspect().main.nodes.on('mouseup.dragall', function(n) {
graph['nd2']['dragging'] = false;
});
graph.inspect().main.nodes.on('mousedown.dragall', function(n) {
if (this.nodeName !== 'circle') { return }
graph['nd2']['dragging'] = true;
graph['nd2']['dragStartX'] = n.x;
graph['nd2']['dragStartY'] = n.y;
graph['nd2']['draggedNode'] = n.index;
});
graph.inspect().main.nodes.on('mousemove.dragall', function(draggedNode) {
var evt = window.event;
if (!("buttons" in evt) || (evt.buttons !== 1)) { return }
if (!(graph['nd2']['dragging']) || !(graph['nd2']['draggedNode'])) { return }
if (draggedNode.index !== graph['nd2']['draggedNode']) { return }
var dx = (draggedNode.x - graph['nd2']['dragStartX']),
dy = (draggedNode.y - graph['nd2']['dragStartY']);
graph['nd2']['dragStartX'] += dx;
graph['nd2']['dragStartY'] += dy;
graph.inspect().main.nodes
.filter(function(n) { return (n.selected && (n.index !== draggedNode.index)) })
.each(function(n) {
n.x += dx; n.y += dy;
n.px += dx; n.py += dy;
});
});
graph.inspect().main.force.on('end.setupfornetdisco', null);
});
var netmapdata = {'data': {
'nodes': mapdata['v4']['nodes'].map(function(node) {
var index = node['index'];
return mapdata['v3']['nodes'][index];
}),
'links': mapdata['v3']['links']
}};
graph.start();
// center on our selected device
// graph.inspect().main.force.on('end.centernode', function() {
// var node = graph.nodeDataById( mapdata['v3']['centernode'] );
// graph.zoomSmooth(node.x, node.y, node.radius * 200);
// graph.inspect().main.force.on('end.centernode', null);
// });
});
});
// vim: ft=javascript
</script>