mirror of
https://github.com/ecomfe/vue-echarts.git
synced 2025-08-15 03:33:19 +08:00
feat: support .once modifier
This commit is contained in:
@ -19,6 +19,7 @@ import {
|
|||||||
import { init as initChart } from "echarts/core";
|
import { init as initChart } from "echarts/core";
|
||||||
import {
|
import {
|
||||||
EChartsType,
|
EChartsType,
|
||||||
|
EventTarget,
|
||||||
Option,
|
Option,
|
||||||
Theme,
|
Theme,
|
||||||
ThemeInjection,
|
ThemeInjection,
|
||||||
@ -116,23 +117,52 @@ export default defineComponent({
|
|||||||
.forEach(key => {
|
.forEach(key => {
|
||||||
// onClick -> c + lick
|
// onClick -> c + lick
|
||||||
// onZr:click -> z + r:click
|
// onZr:click -> z + r:click
|
||||||
const event = key.charAt(2).toLowerCase() + key.slice(3);
|
let event = key.charAt(2).toLowerCase() + key.slice(3);
|
||||||
|
|
||||||
|
// clickOnce -> ~click
|
||||||
|
// zr:clickOnce -> ~zr:click
|
||||||
|
if (event.substring(event.length - 4) === "Once") {
|
||||||
|
event = `~${event.substring(0, event.length - 4)}`;
|
||||||
|
}
|
||||||
|
|
||||||
realListeners[event] = attrs[key];
|
realListeners[event] = attrs[key];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.keys(realListeners).forEach(key => {
|
Object.keys(realListeners).forEach(key => {
|
||||||
const handler = realListeners[key] as any;
|
let handler = realListeners[key];
|
||||||
|
|
||||||
if (!handler) {
|
if (!handler) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key.indexOf("zr:") === 0) {
|
let event = key.toLowerCase();
|
||||||
instance.getZr().on(key.slice(3).toLowerCase(), handler);
|
if (event.charAt(0) === "~") {
|
||||||
} else {
|
event = event.substring(1);
|
||||||
instance.on(key.toLowerCase(), handler);
|
handler.__once__ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let target: EventTarget = instance;
|
||||||
|
if (event.indexOf("zr:") === 0) {
|
||||||
|
target = instance.getZr();
|
||||||
|
event = event.substring(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (handler.__once__) {
|
||||||
|
delete handler.__once__;
|
||||||
|
|
||||||
|
const raw = handler;
|
||||||
|
|
||||||
|
handler = (...args: any[]) => {
|
||||||
|
raw(...args);
|
||||||
|
target.off(event, handler);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore EChartsType["on"] is not compatible with ZRenderType["on"]
|
||||||
|
// but it's okay here
|
||||||
|
target.on(event, handler);
|
||||||
});
|
});
|
||||||
|
|
||||||
function resize() {
|
function resize() {
|
||||||
|
@ -9,6 +9,8 @@ export type InitOptions = NonNullable<InitParameters[2]>;
|
|||||||
export type InitOptionsInjection = InitOptions | null | Ref<InitOptions | null>;
|
export type InitOptionsInjection = InitOptions | null | Ref<InitOptions | null>;
|
||||||
|
|
||||||
export type EChartsType = ReturnType<InitType>;
|
export type EChartsType = ReturnType<InitType>;
|
||||||
|
type ZRenderType = ReturnType<EChartsType["getZr"]>;
|
||||||
|
export type EventTarget = EChartsType | ZRenderType;
|
||||||
type SetOptionType = EChartsType["setOption"];
|
type SetOptionType = EChartsType["setOption"];
|
||||||
export type Option = Parameters<SetOptionType>[0];
|
export type Option = Parameters<SetOptionType>[0];
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user