@antv/g6
Version:
graph visualization frame work
115 lines (96 loc) • 3.55 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 layout controller
* @author huangtonger@aliyun.com
*/
var Base = require('./base');
var Controller = function (_Base) {
_inherits(Controller, _Base);
Controller.prototype.getDefaultCfg = function getDefaultCfg() {
return {
/**
* graph object
* @type {object}
*/
graph: null,
/**
* if auto layout afterchange
* @type {boolean|string}
* could be true false 'once'
*/
auto: true,
/**
* layout processer
* @type {object}
*/
processer: null
};
};
function Controller(cfg) {
_classCallCheck(this, Controller);
var _this = _possibleConstructorReturn(this, _Base.call(this, cfg));
_this._init();
return _this;
}
Controller.prototype._init = function _init() {
var _this2 = this;
var graph = this.graph;
graph.on('afteritemdraw', function (ev) {
var item = ev.item;
var keyShape = item.getKeyShape();
var model = item.getModel();
if (item.isEdge) {
model.lineWidth = keyShape.attr('lineWidth');
} else if (item.isNode || item.isGroup) {
var bbox = item.getBBox();
model.width = bbox.width;
model.height = bbox.height;
}
});
graph.on('afterchange', function (_ref) {
var action = _ref.action;
var auto = _this2.auto;
if (auto === 'once') {
if (action === 'changeData') {
!graph.destroyed && _this2.layout();
}
} else {
_this2.auto && !graph.destroyed && _this2.layout();
}
});
};
Controller.prototype.changeLayout = function changeLayout(processer) {
this.processer = processer;
this.layout();
};
Controller.prototype.layout = function layout() {
var graph = this.graph;
var processer = this.processer;
graph.emit('beforelayout');
var nodes = graph.getNodes().filter(function (node) {
return node.isVisible();
}).map(function (edge) {
return edge.getModel();
});
var edges = graph.getEdges().filter(function (edge) {
return edge.isVisible();
}).map(function (edge) {
return edge.getModel();
});
var groups = graph.getGroups().filter(function (group) {
return group.isVisible();
}).map(function (group) {
return group.getModel();
});
graph._executeLayout(processer, nodes, edges, groups);
graph.updateNodePosition();
graph.emit('afterlayout');
};
Controller.prototype.getLayoutProcesser = function getLayoutProcesser() {
return this.processer;
};
return Controller;
}(Base);
module.exports = Controller;