diff --git a/package.json b/package.json index 12ed11cff5..dd29374c5d 100644 --- a/package.json +++ b/package.json @@ -99,7 +99,7 @@ "screenshot": "node ./bin/screenshot.js", "start": "npm run dev", "test": "torch --compile --renderer --recursive ./test/unit", - "test-live": "torch --compile --interactive --watch --recursive ./test/unit", + "test-live": "torch --compile --interactive --watch --recursive ./test/unit/graph/graph-spec.js", "watch": "webpack --config webpack-dev.config.js", "win-dev": "node ./bin/win-dev.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/controller/index.js b/src/graph/controller/index.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/graph/graph.js b/src/graph/graph.js index b195327303..d76e3c24c4 100755 --- a/src/graph/graph.js +++ b/src/graph/graph.js @@ -3,10 +3,10 @@ * @author huangtonger@aliyun.com */ -const Base = require('../base'); -// const Util = require('./util/'); +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 @@ -43,51 +43,50 @@ class Graph extends Base { */ 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 Base { 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 */ @@ -137,12 +123,6 @@ class Graph extends Base { source(data) { return data; } - /** - * @return {Graph} this - */ - render() { - return this; - } /** * @return {Graph} - this */ @@ -151,14 +131,6 @@ class Graph extends Base { 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 */ @@ -171,29 +143,9 @@ class Graph extends Base { 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 @@ -229,6 +181,62 @@ class Graph extends Base { 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 98f288de58..fc987597e6 100644 --- a/src/item/item.js +++ b/src/item/item.js @@ -73,6 +73,7 @@ class Item { }; Util.mix(this, defaultCfg); this.model = cfg; + this.id = cfg.id; this._init(); } _init() { 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; - }); -}); diff --git a/test/unit/graph/graph-spec.js b/test/unit/graph/graph-spec.js new file mode 100644 index 0000000000..e69de29bb2