@antv/g6
Version:
graph visualization frame work
198 lines (172 loc) • 6.26 kB
JavaScript
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/**
* @fileOverview node item
* @author huangtonger@aliyun.com
*/
var Util = require('../util/');
var Item = require('./item');
var Node = function (_Item) {
_inherits(Node, _Item);
function Node(cfg) {
_classCallCheck(this, Node);
var defaultCfg = {
type: 'node',
linkable: true,
isNode: true,
zIndex: 3
};
Util.mix(defaultCfg, cfg);
return _possibleConstructorReturn(this, _Item.call(this, defaultCfg));
}
Node.prototype.updatePosition = function updatePosition() {
var group = this.group;
var model = this.model;
group.setMatrix([1, 0, 0, 0, 1, 0, model.x ? model.x : 0, model.y ? model.y : 0, 1]);
this.bbox = this._calculateBBox();
};
Node.prototype._shouldDraw = function _shouldDraw() {
var diff = this._getDiff();
var superBool = _Item.prototype._shouldDraw.call(this);
return diff && !(diff.length === 2 && diff.indexOf('x') !== -1 && diff.indexOf('y') !== -1) && !(diff.length === 1 && (diff[0] === 'x' || diff[0] === 'y')) && superBool;
};
Node.prototype._afterDraw = function _afterDraw() {
this.updatePosition();
_Item.prototype._afterDraw.call(this);
};
Node.prototype.layoutUpdate = function layoutUpdate() {
this._beforeDraw();
this._afterDraw();
};
Node.prototype.getEdges = function getEdges(callback) {
var id = this.id;
var itemMap = this.itemMap;
var edges = itemMap._edges;
var rst = [];
Util.each(edges, function (edge) {
if (callback) {
if (callback(edge)) {
rst.push(edge);
}
return;
}
var edgeModel = edge.getModel();
if (id === edgeModel.target || id === edgeModel.source) {
rst.push(edge);
}
});
return rst;
};
Node.prototype.getInEdges = function getInEdges() {
var _this2 = this;
return this.getEdges(function (edge) {
return edge.target === _this2;
});
};
Node.prototype.getOutEdges = function getOutEdges() {
var _this3 = this;
return this.getEdges(function (edge) {
return edge.source === _this3;
});
};
/**
* get anchor points, if there is anchors return the points sorted by arc , others return the link point
* @param {Object | Number} point - start point
* @return {array} - all anchor points sorted by angle, ASC
*/
Node.prototype.getLinkPoints = function getLinkPoints(point) {
var anchorPoints = this.getAnchorPoints();
if (Util.isNumber(point) && anchorPoints[point]) {
return [anchorPoints[point]];
}
var x = point.x,
y = point.y;
var bbox = this.getBBox();
var centerX = bbox.centerX,
centerY = bbox.centerY;
var x1 = x - centerX;
var y1 = y - centerY;
var anchor = this.shapeObj.anchor || {};
var defaultIntersectBox = this.defaultIntersectBox;
var points = [];
if (Util.isEmpty(anchorPoints)) {
// get link point if there are no default anchors
var intersectBox = anchor.intersectBox || anchor.type || defaultIntersectBox;
switch (intersectBox) {
case 'rect':
points = [Util.getIntersectPointRect(bbox, point)];
break;
case 'path':
if (this.keyShape && this.keyShape.get('type') === 'path') {
var linePath = Util.parsePathArray(['M', x, y, 'L', centerX, centerY]);
points = [Util.intersection(linePath, this.keyShape.get('path'))];
}
break;
default:
// default circle
points = [Util.getIntersectPointCircle(x, y, bbox.centerX, bbox.centerY, Math.max(bbox.width, bbox.height) / 2)];
break;
}
// if no link points return center
if (Util.isEmpty(points[0])) {
points = [{
x: centerX,
y: centerY
}];
}
} else {
// get sorted points by arc if there are default points
points = anchorPoints.map(function (p) {
var x2 = p.x - centerX;
var y2 = p.y - centerY;
var arc = Util.getArcOfVectors({ x: x1, y: y1 }, { x: x2, y: y2 });
return Util.mix({}, p, {
arc: arc
});
}).sort(function (a, b) {
return a.arc - b.arc;
});
}
return points;
};
/**
* get position of anchor points
* @param {number} index the index of points
* @return {array} anchorPoints
*/
Node.prototype.getAnchorPoints = function getAnchorPoints(index) {
var shapeObj = this.shapeObj;
var bbox = this.getBBox();
var anchorPoints = [];
var anchor = shapeObj.anchor || {};
var points = void 0;
if (Util.isArray(anchor)) {
points = anchor;
} else if (Util.isFunction(anchor)) {
points = anchor(this);
} else {
if (Util.isFunction(anchor.points)) {
points = anchor.points(this);
} else {
points = anchor.points;
}
}
Util.each(points, function (pointArr, index) {
var anchorPoint = Util.mix({
x: bbox.minX + pointArr[0] * bbox.width,
y: bbox.minY + pointArr[1] * bbox.height
}, pointArr[2], {
index: index
});
anchorPoints.push(anchorPoint);
});
this._anchorPoints = anchorPoints;
if (Util.isNumber(index)) {
return this._anchorPoints[index];
}
return this._anchorPoints;
};
return Node;
}(Item);
module.exports = Node;