Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix detecting changed events #9050

Merged
merged 3 commits into from May 10, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
106 changes: 68 additions & 38 deletions src/core/core.controller.js
Expand Up @@ -116,8 +116,9 @@ class Chart {
this.chartArea = undefined;
this._active = [];
this._lastEvent = undefined;
/** @type {{attach?: function, detach?: function, resize?: function}} */
this._listeners = {};
/** @type {?{attach?: function, detach?: function, resize?: function}} */
this._responsiveListeners = undefined;
this._sortedMetasets = [];
this.scales = {};
this.scale = undefined;
Expand Down Expand Up @@ -483,8 +484,8 @@ class Chart {
const existingEvents = new Set(Object.keys(me._listeners));
const newEvents = new Set(me.options.events);

if (!setsEqual(existingEvents, newEvents)) {
// The events array has changed. Rebind it
if (!setsEqual(existingEvents, newEvents) || !!this._responsiveListeners !== me.options.responsive) {
// The configured events have changed. Rebind.
me.unbindEvents();
me.bindEvents();
}
Expand Down Expand Up @@ -894,6 +895,18 @@ class Chart {
* @private
*/
bindEvents() {
this.bindUserEvents();
if (this.options.responsive) {
this.bindResponsiveEvents();
} else {
this.attached = true;
etimberg marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* @private
*/
bindUserEvents() {
const me = this;
const listeners = me._listeners;
const platform = me.platform;
Expand All @@ -902,53 +915,66 @@ class Chart {
platform.addEventListener(me, type, listener);
listeners[type] = listener;
};
const _remove = (type, listener) => {
if (listeners[type]) {
platform.removeEventListener(me, type, listener);
delete listeners[type];
}
};

let listener = function(e, x, y) {
const listener = function(e, x, y) {
e.offsetX = x;
e.offsetY = y;
me._eventHandler(e);
};

each(me.options.events, (type) => _add(type, listener));
}

if (me.options.responsive) {
listener = (width, height) => {
if (me.canvas) {
me.resize(width, height);
}
};
/**
* @private
*/
bindResponsiveEvents() {
const me = this;
if (!me._responsiveListeners) {
me._responsiveListeners = {};
}
const listeners = me._responsiveListeners;
const platform = me.platform;

const _add = (type, listener) => {
platform.addEventListener(me, type, listener);
listeners[type] = listener;
};
const _remove = (type, listener) => {
if (listeners[type]) {
platform.removeEventListener(me, type, listener);
delete listeners[type];
}
};

let detached; // eslint-disable-line prefer-const
const attached = () => {
_remove('attach', attached);
const listener = (width, height) => {
if (me.canvas) {
me.resize(width, height);
}
};

me.attached = true;
me.resize();
let detached; // eslint-disable-line prefer-const
const attached = () => {
_remove('attach', attached);

_add('resize', listener);
_add('detach', detached);
};
me.attached = true;
me.resize();

detached = () => {
me.attached = false;
_add('resize', listener);
_add('detach', detached);
};

_remove('resize', listener);
_add('attach', attached);
};
detached = () => {
me.attached = false;

if (platform.isAttached(me.canvas)) {
attached();
} else {
detached();
}
_remove('resize', listener);
_add('attach', attached);
};

if (platform.isAttached(me.canvas)) {
attached();
} else {
me.attached = true;
detached();
}
}

Expand All @@ -958,14 +984,18 @@ class Chart {
unbindEvents() {
const me = this;
const listeners = me._listeners;
if (!listeners) {
return;
}

me._listeners = {};
each(listeners, (listener, type) => {
joshkel marked this conversation as resolved.
Show resolved Hide resolved
me.platform.removeEventListener(me, type, listener);
});

if (me._responsiveListeners) {
joshkel marked this conversation as resolved.
Show resolved Hide resolved
each(me._responsiveListeners, (listener, type) => {
me.platform.removeEventListener(me, type, listener);
});
me._responsiveListeners = undefined;
}
}

updateHoverStyle(items, mode, enabled) {
Expand Down