feat:新增当泄漏区出现节点时的事件

This commit is contained in:
黎智洲 2021-05-29 11:32:35 +08:00
parent 66a084ba12
commit 32f403b5aa
8 changed files with 117 additions and 29 deletions

2
dist/sv.js vendored

File diff suppressed because one or more lines are too long

38
src/Common/eventBus.ts Normal file
View File

@ -0,0 +1,38 @@
export interface EventBusInterface {
events: { [key: string]: Function[] };
on(eventName: string, callback: Function): void;
emit(eventName: string, payload?: any): void;
}
export const EventBus: EventBusInterface = {
events: { },
/**
*
* @param eventName
* @param callback
*/
on(eventName: string, callback: Function) {
if(EventBus.events[eventName] === undefined) {
EventBus.events[eventName] = [];
}
EventBus.events[eventName].push(callback);
},
/**
*
* @param eventName
* @param payload
*/
emit(eventName: string, payload?: any) {
if(EventBus.events[eventName] === undefined) {
return;
}
EventBus.events[eventName].map(item => item(payload));
}
};

View File

@ -2,6 +2,13 @@ import { Model } from "../Model/modelData";
import { SV } from "../StructV"; import { SV } from "../StructV";
export type animationConfig = {
duration: number;
timingFunction: string;
payload?: any;
callback?: Function;
}
/** /**
* *
@ -11,19 +18,17 @@ export const Animations = {
/** /**
* / * /
* @param model * @param model
* @param duration * @param animationConfig
* @param timingFunction
* @param callback
*/ */
animate_append(model: Model, duration: number, timingFunction: string, callback: Function = null) { animate_append(model: Model, animationConfig: animationConfig) {
const G6Item = model.G6Item, const G6Item = model.G6Item,
type = G6Item.getType(), type = G6Item.getType(),
group = G6Item.getContainer(), group = G6Item.getContainer(),
Mat3 = SV.Mat3, Mat3 = SV.Mat3,
animateCfg = { animateCfg = {
duration: duration, duration: animationConfig.duration,
easing: timingFunction, easing: animationConfig.timingFunction,
callback callback: animationConfig.callback
}; };
if(type === 'node') { if(type === 'node') {
@ -49,19 +54,17 @@ export const Animations = {
/** /**
* / * /
* @param model * @param model
* @param duration * @param animationConfig
* @param timingFunction
* @param callback
*/ */
animate_remove(model: Model, duration: number, timingFunction: string, callback: Function = null) { animate_remove(model: Model, animationConfig: animationConfig) {
const G6Item = model.G6Item, const G6Item = model.G6Item,
type = G6Item.getType(), type = G6Item.getType(),
group = G6Item.getContainer(), group = G6Item.getContainer(),
Mat3 = SV.Mat3, Mat3 = SV.Mat3,
animateCfg = { animateCfg = {
duration: duration, duration: animationConfig.duration,
easing: timingFunction, easing: animationConfig.timingFunction,
callback callback: animationConfig.callback
}; };
if(type === 'node') { if(type === 'node') {
@ -77,6 +80,28 @@ export const Animations = {
line.animate({ lineDash: [0, length], opacity: 0 }, animateCfg); line.animate({ lineDash: [0, length], opacity: 0 }, animateCfg);
} }
},
/**
* /
* @param model
* @param animationConfig
*/
animate_moveTo(model: Model, animationConfig: animationConfig) {
const G6Item = model.G6Item,
group = G6Item.getContainer(),
Mat3 = SV.Mat3,
target = animationConfig.payload,
animateCfg = {
duration: animationConfig.duration,
easing: animationConfig.timingFunction,
callback: animationConfig.callback
};
let matrix = Mat3.clone(group.getMatrix());
Mat3.translate(matrix, matrix, [target.x, target.y]);
group.animate({ opacity: 0, matrix }, animateCfg);
} }
}; };

View File

@ -112,12 +112,16 @@ export class Container {
let counter = 0; let counter = 0;
appendModels.forEach(item => { appendModels.forEach(item => {
Animations.animate_append(item, this.animationsOptions.duration, this.animationsOptions.timingFunction, () => { Animations.animate_append(item, {
duration: this.animationsOptions.duration,
timingFunction: this.animationsOptions.timingFunction,
callback: () => {
counter++; counter++;
if(counter === appendModels.length) { if(counter === appendModels.length) {
this.afterAppendModelsCallbacks.map(item => item(appendModels)); this.afterAppendModelsCallbacks.map(item => item(appendModels));
} }
}
}); });
}); });
} }
@ -130,7 +134,10 @@ export class Container {
let counter = 0; let counter = 0;
removeModels.forEach(item => { removeModels.forEach(item => {
Animations.animate_remove(item, this.animationsOptions.duration, this.animationsOptions.timingFunction, () => { Animations.animate_remove(item, {
duration: this.animationsOptions.duration,
timingFunction: this.animationsOptions.timingFunction,
callback: () => {
this.renderer.removeModel(item); this.renderer.removeModel(item);
item.renderG6Item = item.G6Item = null; item.renderG6Item = item.G6Item = null;
@ -139,6 +146,7 @@ export class Container {
if(counter === removeModels.length) { if(counter === removeModels.length) {
this.afterRemoveModelsCallbacks.map(item => item(removeModels)); this.afterRemoveModelsCallbacks.map(item => item(removeModels));
} }
}
}); });
}); });
} }

View File

@ -3,4 +3,6 @@ import { Container } from "./container";
/** /**
* *
*/ */
export class LeakContainer extends Container { }; export class LeakContainer extends Container {
};

View File

@ -152,6 +152,8 @@ import { Container } from "./container";
}); });
} }
protected
protected handleChangeModels(models: Model[]) { protected handleChangeModels(models: Model[]) {
const changeHighlightColor: string = this.interactionOptions.changeHighlight; const changeHighlightColor: string = this.interactionOptions.changeHighlight;

View File

@ -9,6 +9,7 @@ import { LeakContainer } from "./container/leak";
import { Layouter } from "./layouter"; import { Layouter } from "./layouter";
import { LayoutGroup, LayoutGroupTable } from "../Model/modelConstructor"; import { LayoutGroup, LayoutGroupTable } from "../Model/modelConstructor";
import { Util } from "../Common/util"; import { Util } from "../Common/util";
import { EventBus } from "../Common/eventBus";
export class ViewManager { export class ViewManager {
@ -226,6 +227,7 @@ export class ViewManager {
} }
if(this.freedContainer) { if(this.freedContainer) {
EventBus.emit('onFreed', freedList);
this.freedContainer.render(freedList); this.freedContainer.render(freedList);
} }
@ -237,6 +239,7 @@ export class ViewManager {
if(this.leakContainer && this.prevLayoutGroupTable) { if(this.leakContainer && this.prevLayoutGroupTable) {
this.mainContainer.afterRemoveModels(() => { this.mainContainer.afterRemoveModels(() => {
EventBus.emit('onLeak', leakLayoutGroupTable);
this.leakContainer.render(Util.convertGroupTable2ModelList(leakLayoutGroupTable)); this.leakContainer.render(Util.convertGroupTable2ModelList(leakLayoutGroupTable));
}); });
} }

View File

@ -4,6 +4,7 @@ import { ModelConstructor } from "./Model/modelConstructor";
import { AnimationOptions, EngineOptions, InteractionOptions, LayoutGroupOptions, ViewOptions } from "./options"; import { AnimationOptions, EngineOptions, InteractionOptions, LayoutGroupOptions, ViewOptions } from "./options";
import { ViewManager } from "./View/viewManager"; import { ViewManager } from "./View/viewManager";
import { SV } from "./StructV"; import { SV } from "./StructV";
import { EventBus } from "./Common/eventBus";
export class Engine { export class Engine {
@ -190,6 +191,15 @@ export class Engine {
* @param callback * @param callback
*/ */
public on(eventName: string, callback: Function) { public on(eventName: string, callback: Function) {
if(typeof callback !== 'function') {
return;
}
if(eventName === 'onFreed' || eventName === 'onLeak') {
EventBus.on(eventName, callback);
return;
}
this.viewManager.getG6Instance().on(eventName, callback); this.viewManager.getG6Instance().on(eventName, callback);
} }