feat: update the getConstructor()

This commit is contained in:
shiwu 2018-06-29 15:37:50 +08:00 committed by Yanyan-Wang
parent 39db43e941
commit 42c06c60b6
4 changed files with 16 additions and 22 deletions

View File

@ -32,13 +32,6 @@ class Base extends EventEmitter {
this.removeAllListeners();
this.destroyed = true;
}
getConstructor(CanvasCons, SvgCons, render) {
if (render === 'svg') {
return SvgCons;
}
return CanvasCons;
}
}
module.exports = Base;

View File

@ -189,7 +189,6 @@ class Graph extends Base {
const width = this.get('width');
const height = this.get('height');
const fontFamily = this.get('fontFamily');
const render = this.get('render');
const canvasCfg = {
width,
height,
@ -198,20 +197,10 @@ class Graph extends Base {
containerDOM: graphContainer
};
const Constructor = this.getConstructor(Canvas, SVG, render);
const Constructor = this.getConstructor(Canvas, SVG);
const canvas = new Constructor(canvasCfg);
const frontCanvas = new Constructor(canvasCfg);
// let canvas;
// let frontCanvas;
// if (render === 'svg') {
// canvas = new SVG(canvasCfg);
// frontCanvas = new SVG(canvasCfg);
// } else {
// canvas = new Canvas(canvasCfg);
// frontCanvas = new Canvas(canvasCfg);
// }
const frontEl = frontCanvas.get('el');
const htmlElementContaniner = graphContainer.appendChild(Util.createDOM('<div class="graph-container-html-Elements"></div>'));
canvas.on('beforedraw', () => {
@ -236,7 +225,7 @@ class Graph extends Base {
mouseEventWarrper.setAttribute('tabindex', TAB_INDEX);
canvas.set('htmlElementContaniner', htmlElementContaniner);
const RootGroup = this.getConstructor(CanvasRootGroup, SvgRootGroup, render);
const RootGroup = this.getConstructor(CanvasRootGroup, SvgRootGroup);
const rootGroup = canvas.addGroup(RootGroup);
const frontRootGroup = frontCanvas.addGroup(RootGroup);
@ -363,6 +352,18 @@ class Graph extends Base {
item && !item.destroyed && item.destroy();
});
}
/**
* @param {function} CanvasCons option 1
* @param {function} SvgCons option 2
* @return {function} function
*/
getConstructor(CanvasCons, SvgCons) {
const render = this.get('render');
if (render === 'svg') {
return SvgCons;
}
return CanvasCons;
}
/**
* @param {string} type item type
* @param {object} model data model

View File

@ -59,7 +59,7 @@ module.exports = {
const graphPixelRatio = graphCanvas.get('pixelRatio');
tranScale = pixelRatio / graphPixelRatio;
graphCanvas.scale(tranScale, tranScale);
const Constructor = graph.getConstructor(Canvas, SVG, graph.get('render'));
const Constructor = graph.getConstructor(Canvas, SVG);
canvas = new Constructor({
containerDOM,
width: width * tranScale,

View File

@ -1,6 +1,6 @@
const G6 = require('../../../src/index');
const Global = require('../../../src/global');
const Graph = G6.Graph;
const Graph = G6.Graph;
const expect = require('chai').expect;
const Util = require('../../../src/util/');
const data = require('../../fixtures/sample-graph-data.json');