Skip to content

Commit

Permalink
save label positions instead of calculating center points for widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
gojko committed Nov 20, 2017
1 parent 3fb91fa commit a07e01f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
6 changes: 2 additions & 4 deletions specs/browser/update-connector-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,10 @@ describe('updateConnector', function () {
expect(underTest.data('theme')).toEqual(customTheme);

});
it('appends the labelCenterPoint', function () {
it('appends the connection position as data', function () {
themePath.theme = customTheme;
underTest.updateConnector({connectorBuilder: builder});
expect(underTest.data('theme')).toEqual(customTheme);
expect(underTest.data('labelCenterPoint').x).toEqual(20);
expect(underTest.data('labelCenterPoint').y).toEqual(20);
expect(underTest.data('position')).toEqual({ left: 101, top: 102, width: 50, height: 60 });

});
it('paints the text label according to the default theme if no theme supplied', function () {
Expand Down
5 changes: 2 additions & 3 deletions specs/browser/update-link-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,9 @@ describe('updateLink', function () {
underTest.updateLink({linkBuilder: builder});
expect(underTest.data('theme')).toEqual(linkPath.theme);
});
it('appends the labelCenterPoint', function () {
it('appends the connector position so it can be used by widgets', function () {
underTest.updateLink({linkBuilder: builder});
expect(underTest.data('labelCenterPoint').x).toEqual(20);
expect(underTest.data('labelCenterPoint').y).toEqual(20);
expect(underTest.data('position')).toEqual({left: 101, top: 102, width: 50, height: 60 });
});
it('paints the rect element 2 pixels above the text element', function () {
linkPath.theme.label.backgroundColor = 'rgb(1, 2, 3)';
Expand Down
9 changes: 5 additions & 4 deletions src/browser/update-connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ jQuery.fn.updateConnector = function (optional) {
}
},
applyLabel = function () {
const labelTheme = (connection.theme && connection.theme.label) || defaultTheme.connector.default.label,
labelCenterPoint = calcLabelCenterPont(connection, toBox, pathElement[0], labelTheme);
element.data('labelCenterPoint', labelCenterPoint);
const labelText = (connectorAttr && connectorAttr.label) || '',
labelTheme = (connection.theme && connection.theme.label) || defaultTheme.connector.default.label,
labelCenterPoint = labelText && calcLabelCenterPont(connection.position, toBox, pathElement[0], labelTheme);
updateConnectorText(
element,
labelCenterPoint,
(connectorAttr && connectorAttr.label) || '',
labelText,
labelTheme
);
};
Expand Down Expand Up @@ -77,6 +77,7 @@ jQuery.fn.updateConnector = function (optional) {

connection = _.extend(connectorBuilder(fromBox, toBox, theme), connectorAttr);
element.data('theme', connection.theme);
element.data('position', Object.assign({}, connection.position));
pathElement = element.find('path.mapjs-connector');
hitElement = element.find('path.mapjs-link-hit');
element.css(_.extend(convertPositionToTransform(connection.position), {stroke: connection.color}));
Expand Down
9 changes: 5 additions & 4 deletions src/browser/update-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ jQuery.fn.updateLink = function (optional) {
shapeTo = element.data('nodeTo'),
attrs = element.data('attr') || {},
applyLabel = function (connection, toBox, pathElement) {
const labelTheme = connection.theme.label,
labelCenterPoint = calcLabelCenterPont(connection, toBox, pathElement[0], labelTheme);
element.data('labelCenterPoint', labelCenterPoint);
const labelText = attrs.label || '',
labelTheme = connection.theme.label,
labelCenterPoint = labelText && calcLabelCenterPont(connection.position, toBox, pathElement[0], labelTheme);
updateConnectorText(
element,
labelCenterPoint,
attrs.label || '',
labelText,
labelTheme
);
};
Expand All @@ -74,6 +74,7 @@ jQuery.fn.updateLink = function (optional) {

connection = linkBuilder(fromBox, toBox, attrs, theme);
element.data('theme', connection.theme);
element.data('position', Object.assign({}, connection.position));
element.css(_.extend(convertPositionToTransform(connection.position), {stroke: connection.lineProps.color}));

if (pathElement.length === 0) {
Expand Down
11 changes: 7 additions & 4 deletions src/core/util/calc-label-center-point.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/*global module */
module.exports = function calcLabelCenterPoint(connection, toBox, pathDOM, labelTheme) {
/*global module, require */
const defaultTheme = require('../theme/default-theme');
module.exports = function calcLabelCenterPoint(connectionPosition, toBox, pathDOM, labelTheme) {
'use strict';
labelTheme = labelTheme || defaultTheme.connector.default.label;

if (labelTheme.position.ratio) {
return pathDOM.getPointAtLength(pathDOM.getTotalLength() * labelTheme.position.ratio);
}
return {
x: toBox.left + (toBox.width / 2) - connection.position.left,
y: toBox.top - connection.position.top - labelTheme.position.aboveEnd
x: toBox.left + (toBox.width / 2) - connectionPosition.left,
y: toBox.top - connectionPosition.top - labelTheme.position.aboveEnd
};
};

0 comments on commit a07e01f

Please sign in to comment.