Skip to content

Commit 09808a4

Browse files
kingyue737Justineo
authored andcommitted
fix: move events collecting from init to setup
1 parent 898195c commit 09808a4

File tree

1 file changed

+52
-54
lines changed

1 file changed

+52
-54
lines changed

src/ECharts.ts

+52-54
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,58 @@ export default defineComponent({
101101

102102
// @ts-expect-error listeners for Vue 2 compatibility
103103
const listeners = getCurrentInstance().proxy.$listeners;
104+
const realListeners: Record<string, any> = {};
105+
106+
if (!listeners) {
107+
// This is for Vue 3.
108+
// We are converting all `on<Event>` props to event listeners compatible with Vue 2
109+
// and collect them into `realListeners` so that we can bind them to the chart instance
110+
// later in the same way.
111+
// For `onNative:<event>` props, we just strip the `Native:` part and collect them into
112+
// `nativeListeners` so that we can bind them to the root element directly.
113+
Object.keys(attrs)
114+
.filter(key => isOn(key))
115+
.forEach(key => {
116+
// onClick -> c + lick
117+
// onZr:click -> z + r:click
118+
let event = key.charAt(2).toLowerCase() + key.slice(3);
119+
120+
// Collect native DOM events
121+
if (event.indexOf("native:") === 0) {
122+
// native:click -> onClick
123+
const nativeKey = `on${event.charAt(7).toUpperCase()}${event.slice(
124+
8
125+
)}`;
126+
127+
nativeListeners[nativeKey] = attrs[key];
128+
return;
129+
}
130+
131+
// clickOnce -> ~click
132+
// zr:clickOnce -> ~zr:click
133+
if (event.substring(event.length - 4) === "Once") {
134+
event = `~${event.substring(0, event.length - 4)}`;
135+
}
136+
137+
realListeners[event] = attrs[key];
138+
});
139+
} else {
140+
// This is for Vue 2.
141+
// We just need to distinguish normal events and `native:<event>` events and
142+
// collect them into `realListeners` and `nativeListeners` respectively.
143+
// For `native:<event>` events, we just strip the `native:` part and collect them
144+
// into `nativeListeners` so that we can bind them to the root element directly.
145+
// native:click -> click
146+
// ~native:click -> ~click
147+
// &~!native:click -> &~!click
148+
Object.keys(listeners).forEach(key => {
149+
if (NATIVE_EVENT_RE.test(key)) {
150+
nativeListeners[key.replace(NATIVE_EVENT_RE, "$1")] = listeners[key];
151+
} else {
152+
realListeners[key] = listeners[key];
153+
}
154+
});
155+
}
104156

105157
function init(option?: Option) {
106158
if (!inner.value) {
@@ -117,60 +169,6 @@ export default defineComponent({
117169
instance.group = props.group;
118170
}
119171

120-
const realListeners: Record<string, any> = {};
121-
122-
if (!listeners) {
123-
// This is for Vue 3.
124-
// We are converting all `on<Event>` props to event listeners compatible with Vue 2
125-
// and collect them into `realListeners` so that we can bind them to the chart instance
126-
// later in the same way.
127-
// For `onNative:<event>` props, we just strip the `Native:` part and collect them into
128-
// `nativeListeners` so that we can bind them to the root element directly.
129-
Object.keys(attrs)
130-
.filter(key => isOn(key))
131-
.forEach(key => {
132-
// onClick -> c + lick
133-
// onZr:click -> z + r:click
134-
let event = key.charAt(2).toLowerCase() + key.slice(3);
135-
136-
// Collect native DOM events
137-
if (event.indexOf("native:") === 0) {
138-
// native:click -> onClick
139-
const nativeKey = `on${event
140-
.charAt(7)
141-
.toUpperCase()}${event.slice(8)}`;
142-
143-
nativeListeners[nativeKey] = attrs[key];
144-
return;
145-
}
146-
147-
// clickOnce -> ~click
148-
// zr:clickOnce -> ~zr:click
149-
if (event.substring(event.length - 4) === "Once") {
150-
event = `~${event.substring(0, event.length - 4)}`;
151-
}
152-
153-
realListeners[event] = attrs[key];
154-
});
155-
} else {
156-
// This is for Vue 2.
157-
// We just need to distinguish normal events and `native:<event>` events and
158-
// collect them into `realListeners` and `nativeListeners` respectively.
159-
// For `native:<event>` events, we just strip the `native:` part and collect them
160-
// into `nativeListeners` so that we can bind them to the root element directly.
161-
// native:click -> click
162-
// ~native:click -> ~click
163-
// &~!native:click -> &~!click
164-
Object.keys(listeners).forEach(key => {
165-
if (NATIVE_EVENT_RE.test(key)) {
166-
nativeListeners[key.replace(NATIVE_EVENT_RE, "$1")] =
167-
listeners[key];
168-
} else {
169-
realListeners[key] = listeners[key];
170-
}
171-
});
172-
}
173-
174172
Object.keys(realListeners).forEach(key => {
175173
let handler = realListeners[key];
176174

0 commit comments

Comments
 (0)