From 9840b9fe934461b5bc9018db57baae80cd505fa2 Mon Sep 17 00:00:00 2001 From: "yilin.qyl" Date: Wed, 19 Dec 2018 15:45:49 +0800 Subject: [PATCH 1/3] chore: sort out base class --- src/base.js | 39 --------------------------------------- src/graph/graph.js | 4 ++-- test/unit/base-spec.js | 28 ---------------------------- 3 files changed, 2 insertions(+), 69 deletions(-) delete mode 100644 src/base.js delete mode 100644 test/unit/base-spec.js diff --git a/src/base.js b/src/base.js deleted file mode 100644 index d9c963aa69..0000000000 --- a/src/base.js +++ /dev/null @@ -1,39 +0,0 @@ -/** - * @fileOverview - * The base class for complex class - * @author huangtonger@aliyun.com - */ - -const Util = require('./util/'); -const EventEmitter = require('@antv/g/lib/').EventEmitter; - -class Base extends EventEmitter { - - getDefaultCfg() { - return {}; - } - - constructor(cfg) { - super(); - const self = this; - Util.mix(self, self.getDefaultCfg()); // 对象私有属性 - self.model = Util.mix({}, cfg); // 用户设置的 - this._cfg = {}; // 状态,绘图属性等暂存 - } - - get(name) { - return this.model[name]; - } - - set(name, value) { - this.model[name] = value; - } - - destroy() { - this.model = {}; - this.removeEvent(); - this.destroyed = true; - } -} - -module.exports = Base; diff --git a/src/graph/graph.js b/src/graph/graph.js index 4da7437113..3e388d298c 100755 --- a/src/graph/graph.js +++ b/src/graph/graph.js @@ -3,10 +3,10 @@ * @author huangtonger@aliyun.com */ -const Base = require('../base'); +const EventEmitter = require('@antv/g/lib/').EventEmitter; // const Util = require('./util/'); -class Graph extends Base { +class Graph extends EventEmitter { /** * Access to the default configuration properties * @return {object} default configuration diff --git a/test/unit/base-spec.js b/test/unit/base-spec.js deleted file mode 100644 index a47fc2af79..0000000000 --- a/test/unit/base-spec.js +++ /dev/null @@ -1,28 +0,0 @@ -const expect = require('chai').expect; -const Base = require('../../src/base'); - -describe('base test', () => { - const base = new Base(); - it('new base', () => { - const baseInst = new Base({ test: 'aaaa' }); - expect(baseInst).not.to.be.undefined; - expect(baseInst.model).not.to.be.undefined; - expect(baseInst.model.test).to.equal('aaaa'); - expect(baseInst.get('test')).to.equal('aaaa'); - }); - it('base getter & setter', ()=> { - base.set('a', 'a'); - expect(base.get('a')).to.equal('a'); - }); - it('base events', () => { - let triggered = false; - base.on('event', () => { triggered = true; }); - base.emit('event'); - expect(triggered).to.be.true; - - triggered = false; - base.removeEvent('event'); - base.emit('event'); - expect(triggered).to.be.false; - }); -}); From 08b5ad488e945da3479042fc6ebac107289c1389 Mon Sep 17 00:00:00 2001 From: "yilin.qyl" Date: Wed, 19 Dec 2018 16:21:03 +0800 Subject: [PATCH 2/3] chore: sort out graph base functions --- src/graph/graph.js | 166 ++++++++++++++++++++++++--------------------- src/item/item.js | 15 ++-- 2 files changed, 96 insertions(+), 85 deletions(-) diff --git a/src/graph/graph.js b/src/graph/graph.js index 3e388d298c..5adfd310b9 100755 --- a/src/graph/graph.js +++ b/src/graph/graph.js @@ -4,7 +4,7 @@ */ const EventEmitter = require('@antv/g/lib/').EventEmitter; -// const Util = require('./util/'); +const Util = require('../util'); class Graph extends EventEmitter { /** @@ -43,51 +43,50 @@ class Graph extends EventEmitter { */ mode: [], /** - * all the node instances - * @type Array - */ - nodes: [], - /** - * all the eadge instances - * @type Array - */ - edges: [], - /** - * nodes instances indexed by id + * source data * @type object */ - nodesById: {}, + data: null, /** - * ed instances indexed by id - * @type object + * capture events + * @type boolean */ - edgesById: {} + event: true }; } constructor(inputCfg) { - super(inputCfg); + super(); + this._cfg = Util.mix({}, this.getDefaultCfg(), inputCfg); // merge graph configs + this.nodes = []; // all the node instances + this.edges = []; // all the edge instances + this.itemById = {}; // all the item indexed by id this._init(); } _init() { - // all the node instances - this.nodes = []; - // all the edge instances - this.edges = []; - // node instances indexed by id - this._nodesById = {}; - // edge instances indexed by id - this._edgesById = {}; + // todo init controllers & G.Canvas etc.. } - - /** - * @return {domobject} graphcontainer - */ - getGraphContainer() { - return this; + get(key) { + return this._cfg[key]; } + set(key, val) { + this._cfg[key] = val; + } + draw() {} + render() {} _drawInner() {} _clearInner() {} + addNode(type, cfgs) { + return { type, cfgs }; + } + addEdge(type, cfgs) { + return { type, cfgs }; + } + focus() {} + fitView() {} + // move(dx, dy) {} + // translate(x, y) {} + // zoom(scale, center) {} /** * @param {string} type item type * @param {object} model data model @@ -96,19 +95,6 @@ class Graph extends EventEmitter { getShapeObj(type, model) { return { type, model }; } - /** - * @return {object} source data - */ - getSource() { - return this.get('_sourceData'); - } - /** - * @param {object} data source data - * @return {object} plain data - */ - parseSource(data) { - return data; - } /** * @return {G.Canvas} canvas */ @@ -134,12 +120,6 @@ class Graph extends EventEmitter { source(data) { return data; } - /** - * @return {Graph} this - */ - render() { - return this; - } /** * @return {Graph} - this */ @@ -148,14 +128,6 @@ class Graph extends EventEmitter { this.read(data); return this; } - /** - * set canvas captrue - * @param {boolean} bool boolean - */ - setCapture(bool) { - const rootGroup = this.get('_rootGroup'); - rootGroup.set('capture', bool); - } /** * @return {Graph} - this */ @@ -168,29 +140,9 @@ class Graph extends EventEmitter { save() { return this; } - /** - * @param {string} type item type - * @param {object} model data model - * @return {Graph} this - */ - add(type, model) { - return { type, model }; - } - /** - * @param {string|Item} item - target item - * @return {Graph} this - */ - remove(item) { + update(item) { return item; } - /** - * @param {string|Item|undefined} item target item - * @param {object} model data model - * @return {Graph} this - */ - update(item, model) { - return { item, model }; - } /** * change data * @param {object} data - source data @@ -226,6 +178,62 @@ class Graph extends EventEmitter { changeSize(width, height) { return { width, height }; } + findById(id) { + return this.itemById[id]; + } + findNode(fn) { + return this.find('nodes', fn); + } + findEdge(fn) { + return this.find('edges', fn); + } + findAllNodes(fn) { + this.findAll('nodes', fn); + } + findAllEdges(fn) { + return this.findAll('edges', fn); + } + find(type, fn) { + let result; + const items = this[type]; + Util.each(items, (item, i) => { + if (fn(item, i)) { + result = item; + return false; + } + }); + return result; + } + findAll(type, fn) { + const result = []; + Util.each(this[type], (item, i) => { + if (fn(item, i)) { + result.push(item); + } + }); + return result; + } + removeNode(node) { + this.remove('nodes', node); + return this; + } + removeEdge(edge) { + this.remove('edges', edge); + return this; + } + remove(type, item) { + if (Util.isString(item)) { + item = this.itemById[item]; + } + if (!item) { + return; + } + const items = this[type]; + const index = items.indexOf(item); + items.splice(index, 1); + delete this.itemById[item.get('id')]; + item.remove(); + } } module.exports = Graph; diff --git a/src/item/item.js b/src/item/item.js index cf935d04b0..4498e1ba1a 100644 --- a/src/item/item.js +++ b/src/item/item.js @@ -71,17 +71,21 @@ class Item { */ visible: true }; - Util.mix(this, defaultCfg, cfg); + Util.mix(this, defaultCfg); + this.model = cfg; + this.id = cfg.id; this._init(); } _init() { this._initGroup(); this.draw(); } - _mapping() { - const mapper = this.mapper; - const model = this.model; - mapper.mapping(model); + get(key) { + return this.model[key]; + } + set(key, val) { + this.model[key] = val; + return this; } _initGroup() { const group = this.group; @@ -174,7 +178,6 @@ class Item { const animate = this.animate; const group = this.group; group.clear(!animate); - this._mapping(); this._setShapeObj(); const shapeObj = this.shapeObj; const keyShape = shapeObj.draw(this); From 145ee923511eba7ecb8a278385894163946dc1f2 Mon Sep 17 00:00:00 2001 From: "yilin.qyl" Date: Wed, 19 Dec 2018 17:08:22 +0800 Subject: [PATCH 3/3] chore: remove unnecessary utils --- src/util/base.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/util/base.js b/src/util/base.js index 777b364dde..435fa05af0 100644 --- a/src/util/base.js +++ b/src/util/base.js @@ -1,11 +1,3 @@ - -Math.sign = function(x) { - x = +x; - if (x === 0 || isNaN(x)) { - return x; - } - return x > 0 ? 1 : -1; -}; const BaseUtil = { deepMix: require('@antv/util/lib/deep-mix'), mix: require('@antv/util/lib/mix'),