-
Notifications
You must be signed in to change notification settings - Fork 104
/
zoom.js
134 lines (109 loc) · 4.21 KB
/
zoom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import * as d3 from "d3-selection";
import {zoom, zoomTransform, zoomIdentity} from "d3-zoom";
import {interpolate} from "d3-interpolate";
export default function(enable) {
this._options.zoom = enable;
if (this._options.zoom && !this._zoomBehavior) {
createZoomBehavior.call(this);
} else if (!this._options.zoom && this._zoomBehavior) {
this._zoomSelection.on(".zoom", null);
this._zoomBehavior = null;
}
return this;
}
export function createZoomBehavior() {
var graphvizInstance = this;
function zoomed(event) {
var g = d3.select(svg.node().querySelector("g"));
var transform = g.node().transform;
var matrix = transform.baseVal.consolidate().matrix;
if (matrix.b == 0) {
// drawing orientation is portrait
g.attr('transform', event.transform);
}
else {
// drawing orientation is landscape
const t = event.transform;
const transformStr = `rotate(-90) translate(${-t.y} ${t.x}) scale(${t.k})`;
g.attr('transform', transformStr);
}
graphvizInstance._dispatch.call('zoom', graphvizInstance);
}
var root = this._selection;
var svg = d3.select(root.node().querySelector("svg"));
if (svg.size() == 0) {
return this;
}
this._zoomSelection = svg;
var zoomBehavior = zoom()
.scaleExtent(this._options.zoomScaleExtent)
.translateExtent(this._options.zoomTranslateExtent)
.interpolate(interpolate)
.on("zoom", zoomed);
this._zoomBehavior = zoomBehavior;
var g = d3.select(svg.node().querySelector("g"));
svg.call(zoomBehavior);
if (!this._active) {
translateZoomBehaviorTransform.call(this, g);
}
this._originalTransform = zoomTransform(svg.node());
return this;
};
export function getTranslatedZoomTransform(selection) {
// Get the current zoom transform for the top level svg and
// translate it uniformly with the given selection, using the
// difference between the translation specified in the selection's
// data and it's saved previous translation. The selection is
// normally the top level g element of the graph.
var oldTranslation = this._translation;
var oldScale = this._scale;
var newTranslation = selection.datum().translation;
var newScale = selection.datum().scale;
var t = zoomTransform(this._zoomSelection.node());
if (oldTranslation) {
t = t.scale(1 / oldScale);
t = t.translate(-oldTranslation.x, -oldTranslation.y);
}
t = t.translate(newTranslation.x, newTranslation.y);
t = t.scale(newScale);
return t;
}
export function translateZoomBehaviorTransform(selection) {
// Translate the current zoom transform for the top level svg
// uniformly with the given selection, using the difference
// between the translation specified in the selection's data and
// it's saved previous translation. The selection is normally the
// top level g element of the graph.
this._zoomBehavior.transform(this._zoomSelection, getTranslatedZoomTransform.call(this, selection));
// Save the selections's new translation and scale.
this._translation = selection.datum().translation;
this._scale = selection.datum().scale;
// Set the original zoom transform to the translation and scale specified in
// the selection's data.
this._originalTransform = zoomIdentity.translate(selection.datum().translation.x, selection.datum().translation.y).scale(selection.datum().scale);
}
export function resetZoom(transition) {
// Reset the zoom transform to the original zoom transform.
var selection = this._zoomSelection;
if (transition) {
selection = selection
.transition(transition);
}
selection
.call(this._zoomBehavior.transform, this._originalTransform);
return this;
}
export function zoomScaleExtent(extent) {
this._options.zoomScaleExtent = extent;
return this;
}
export function zoomTranslateExtent(extent) {
this._options.zoomTranslateExtent = extent;
return this;
}
export function zoomBehavior() {
return this._zoomBehavior || null;
}
export function zoomSelection() {
return this._zoomSelection || null;
}