1 |
1 |
/**
|
2 |
2 |
* Class representing minimap of the viewport displayed in sidebar. It is used to have a quick overview of the current viewport layout.
|
3 |
|
* @constructor
|
4 |
3 |
*/
|
5 |
|
function Minimap() {
|
6 |
|
var rootElement;
|
7 |
|
var viewportElement;
|
|
4 |
class Minimap {
|
|
5 |
/**
|
|
6 |
* @constructor
|
|
7 |
* @param {string} useElement ID of SVG element to display in minimap.
|
|
8 |
* @param {float} scale Ratio to scale down the displayed element.
|
|
9 |
*/
|
|
10 |
constructor(useElement, scale = 0.1) {
|
|
11 |
this._width = 350;
|
|
12 |
this._height = 200;
|
|
13 |
this._useElement = useElement;
|
|
14 |
this._scale = scale;
|
|
15 |
}
|
|
16 |
|
|
17 |
/**
|
|
18 |
* Creates a new DOM element representing the minimap in memory.
|
|
19 |
* @public
|
|
20 |
* @returns {SVGElement} SVG DOM element.
|
|
21 |
*/
|
|
22 |
render() {
|
|
23 |
this._viewportElement = DOM.s('rect', {
|
|
24 |
class: 'minimap-viewport',
|
|
25 |
onMouseDown: this._onViewportMouseDown.bind(this),
|
|
26 |
});
|
8 |
27 |
|
9 |
|
var width = 350;
|
10 |
|
var height = 200;
|
11 |
|
var scale = 0.1;
|
12 |
|
var useElement = '#graph';
|
|
28 |
this._rootElement = DOM.s('svg', {
|
|
29 |
class: 'minimap',
|
|
30 |
id: 'minimapComponent',
|
|
31 |
viewBox: `-100 -50 ${this._width} ${this._height}`,
|
|
32 |
onMouseDown: this._onRootMouseDown.bind(this),
|
|
33 |
}, [
|
|
34 |
DOM.s('use', {
|
|
35 |
transform: `scale(${this._scale})`,
|
|
36 |
href: this._useElement,
|
|
37 |
}),
|
|
38 |
this._viewportElement,
|
|
39 |
]);
|
|
40 |
|
|
41 |
return this._rootElement;
|
|
42 |
}
|
13 |
43 |
|
14 |
44 |
/**
|
15 |
45 |
* Resizes the minimap viewport using current viewport size and minimap scale.
|
|
46 |
* @public
|
16 |
47 |
* @param {Dimensions} dimensions Object holding current dimensions of the viewport component.
|
17 |
48 |
*/
|
18 |
|
this.setViewportSize = function(dimensions) {
|
19 |
|
viewportElement.setAttribute('width', dimensions.width * scale);
|
20 |
|
viewportElement.setAttribute('height', dimensions.height * scale);
|
21 |
|
};
|
|
49 |
set viewportSize(dimensions) {
|
|
50 |
this._viewportElement.setAttribute('width', dimensions.width * this._scale);
|
|
51 |
this._viewportElement.setAttribute('height', dimensions.height * this._scale);
|
|
52 |
}
|
22 |
53 |
|
23 |
54 |
/**
|
|
55 |
* @public
|
24 |
56 |
* @returns {Coordinates} Current coordinates of the viewport.
|
25 |
57 |
*/
|
26 |
|
this.getViewportPosition = function() {
|
|
58 |
get viewportPosition() {
|
27 |
59 |
return new Coordinates(
|
28 |
|
+viewportElement.getAttribute('x'),
|
29 |
|
+viewportElement.getAttribute('y'),
|
|
60 |
+this._viewportElement.getAttribute('x'),
|
|
61 |
+this._viewportElement.getAttribute('y'),
|
30 |
62 |
);
|
31 |
|
};
|
|
63 |
}
|
32 |
64 |
|
33 |
65 |
/**
|
34 |
66 |
* Moves the minimap viewport to a new position based on current viewport position and minimap scale.
|
|
67 |
* @public
|
35 |
68 |
* @param {Coordinates} coords Coordinates of the viewport.
|
36 |
69 |
*/
|
37 |
|
this.setViewportPosition = function(coords) {
|
38 |
|
viewportElement.setAttribute('x', coords.x * -1 * scale);
|
39 |
|
viewportElement.setAttribute('y', coords.y * -1 * scale);
|
40 |
|
};
|
|
70 |
set viewportPosition(coords) {
|
|
71 |
this._viewportElement.setAttribute('x', coords.x * -1 * this._scale);
|
|
72 |
this._viewportElement.setAttribute('y', coords.y * -1 * this._scale);
|
|
73 |
}
|
41 |
74 |
|
42 |
75 |
/**
|
43 |
|
* Creates a new DOM element representing the minimap in memory.
|
44 |
|
* @returns {Element} HTML DOM element.
|
|
76 |
* @private
|
45 |
77 |
*/
|
46 |
|
this.render = function() {
|
47 |
|
rootElement = DOM.createSvgElement('svg', {
|
48 |
|
'class': 'minimap',
|
49 |
|
'id': 'minimapComponent',
|
50 |
|
'viewBox': `-100 -50 ${width} ${height}`,
|
51 |
|
});
|
52 |
|
rootElement.addEventListener('mousedown', onRootMouseDown.bind(this));
|
53 |
|
|
54 |
|
var graphElement = DOM.createSvgElement('use', {
|
55 |
|
'transform': `scale(${scale})`,
|
56 |
|
});
|
57 |
|
graphElement.setAttributeNS('http://www.w3.org/1999/xlink', 'href', useElement);
|
58 |
|
rootElement.appendChild(graphElement);
|
59 |
|
|
60 |
|
viewportElement = DOM.createSvgElement('rect', {
|
61 |
|
'class': 'minimap-viewport',
|
62 |
|
});
|
63 |
|
viewportElement.addEventListener('mousedown', onViewportMouseDown.bind(this));
|
64 |
|
rootElement.appendChild(viewportElement);
|
|
78 |
_onRootMouseDown(e) {
|
|
79 |
let start = new Coordinates(e.clientX, e.clientY);
|
65 |
80 |
|
66 |
|
return rootElement;
|
67 |
|
};
|
|
81 |
let viewBox = this._rootElement.getAttribute('viewBox').split(' ');
|
|
82 |
let minimapRootPosition = new Coordinates(parseInt(viewBox[0]), parseInt(viewBox[1]));
|
68 |
83 |
|
69 |
|
function onRootMouseDown(e) {
|
70 |
|
var start = new Coordinates(e.clientX, e.clientY);
|
71 |
|
|
72 |
|
var viewBox = rootElement.getAttribute('viewBox').split(' ');
|
73 |
|
var minimapRootPosition = new Coordinates(parseInt(viewBox[0]), parseInt(viewBox[1]));
|
|
84 |
let that = this;
|
74 |
85 |
|
75 |
86 |
document.body.addEventListener('mousemove', mouseMove);
|
76 |
87 |
document.body.addEventListener('mouseup', mouseUp);
|
... | ... | |
78 |
89 |
function mouseMove(e) {
|
79 |
90 |
e.preventDefault();
|
80 |
91 |
|
81 |
|
var offset = new Coordinates(start.x - e.clientX, start.y - e.clientY);
|
|
92 |
let offset = new Coordinates(start.x - e.clientX, start.y - e.clientY);
|
82 |
93 |
|
83 |
|
rootElement.setAttribute('viewBox', `${minimapRootPosition.x + offset.x} ${minimapRootPosition.y + offset.y} ${width} ${height}`);
|
|
94 |
that._rootElement.setAttribute('viewBox', `${minimapRootPosition.x + offset.x} ${minimapRootPosition.y + offset.y} ${that._width} ${that._height}`);
|
84 |
95 |
}
|
85 |
96 |
|
86 |
|
function mouseUp(e) {
|
87 |
|
start = null;
|
88 |
|
|
|
97 |
function mouseUp() {
|
89 |
98 |
document.body.removeEventListener('mousemove', mouseMove);
|
90 |
99 |
document.body.removeEventListener('mouseup', mouseUp);
|
91 |
100 |
}
|
92 |
101 |
}
|
93 |
102 |
|
94 |
|
function onViewportMouseDown(e) {
|
|
103 |
/**
|
|
104 |
* @private
|
|
105 |
*/
|
|
106 |
_onViewportMouseDown(e) {
|
95 |
107 |
e.stopPropagation();
|
96 |
108 |
|
97 |
|
var start = new Coordinates(e.clientX, e.clientY);
|
98 |
|
var minimapViewportPosition = this.getViewportPosition();
|
99 |
|
var viewportPosition = app.viewportComponent.getPosition();
|
|
109 |
let start = new Coordinates(e.clientX, e.clientY);
|
|
110 |
let minimapViewportPosition = this.viewportPosition;
|
|
111 |
let viewportPosition = app.viewportComponent.getPosition();
|
|
112 |
|
|
113 |
let that = this;
|
100 |
114 |
|
101 |
115 |
document.body.addEventListener('mousemove', mouseMove);
|
102 |
116 |
document.body.addEventListener('mouseup', mouseUp);
|
... | ... | |
104 |
118 |
function mouseMove(e) {
|
105 |
119 |
e.preventDefault();
|
106 |
120 |
|
107 |
|
var offset = new Coordinates(start.x - e.clientX, start.y - e.clientY);
|
|
121 |
let offset = new Coordinates(start.x - e.clientX, start.y - e.clientY);
|
108 |
122 |
|
109 |
|
viewportElement.setAttribute('x', minimapViewportPosition.x - offset.x);
|
110 |
|
viewportElement.setAttribute('y', minimapViewportPosition.y - offset.y);
|
|
123 |
that._viewportElement.setAttribute('x', minimapViewportPosition.x - offset.x);
|
|
124 |
that._viewportElement.setAttribute('y', minimapViewportPosition.y - offset.y);
|
111 |
125 |
|
112 |
|
app.viewportComponent.setPosition(new Coordinates(viewportPosition.x + (1 / scale) * offset.x, viewportPosition.y + (1 / scale) * offset.y));
|
|
126 |
app.viewportComponent.setPosition(new Coordinates(viewportPosition.x + (1 / that._scale) * offset.x, viewportPosition.y + (1 / that._scale) * offset.y));
|
113 |
127 |
}
|
114 |
128 |
|
115 |
|
function mouseUp(e) {
|
116 |
|
start = null;
|
117 |
|
minimapViewportPosition = null;
|
118 |
|
viewportPosition = null;
|
119 |
|
|
|
129 |
function mouseUp() {
|
120 |
130 |
document.body.removeEventListener('mousemove', mouseMove);
|
121 |
131 |
document.body.removeEventListener('mouseup', mouseUp);
|
122 |
132 |
|
reworked Minimap to ES6 class